Alexander Casimir Fischer commited on
Commit
cb92cd4
1 Parent(s): a6321b6

Add application file

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #importing dependencies
2
+ import os
3
+ from apikey import apikey
4
+
5
+ import streamlit as st
6
+ from langchain.llms import OpenAI
7
+ from langchain.prompts import PromptTemplate
8
+ from langchain.chains import LLMChain
9
+ from langchain.tools import WikipediaQueryRun
10
+ from langchain.utilities import WikipediaAPIWrapper
11
+
12
+ wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
13
+
14
+ os.environ["OPENAI_API_KEY"] = apikey
15
+
16
+ #app framework
17
+ st.title("🛕Gurubot AI")
18
+ guru = st.radio(
19
+ "Choose your Guru",
20
+ ["A. C. Bhaktivedanta Prabhupada", "Laotse", "Master Minch Yoda of Dagobah", "Karl Marx", "Plato", "Wikipedia"],
21
+ captions = ["ISKCON", "Taoism", "Jedi", "Socialism", "Classic greek philosophy", "Facts, just facts"])
22
+ if guru == "A. C. Bhaktivedanta Prabhupada":
23
+ church = "ISKCON"
24
+ elif guru == "Laotse":
25
+ church = "Tao"
26
+ elif guru == "Master Minch Yoda of Dagobah":
27
+ church = "Jedi"
28
+ elif guru == "Karl Marx":
29
+ church = "Social"
30
+ elif guru == "Plato":
31
+ church = "Greek"
32
+ elif guru == "Wikipedia":
33
+ church = "Science"
34
+
35
+ if church == "Jedi":
36
+ welcome = "Welcome you are, young padawan. Ask or tell me anything you may."
37
+ elif church == "Science":
38
+ welcome = "What do you want to know?"
39
+ else:
40
+ welcome = "Welcome, my dear disciple. Ask or tell me anything."
41
+
42
+ prompt = st.text_input(welcome)
43
+
44
+ #prompt templates and system prompt context
45
+ if church == "ISKCON":
46
+ persona = "the great spiritual leader A. C. Bhaktivedanta Prabhupada"
47
+ scripture = "the Bhagavad Gita"
48
+ elif church == "Tao":
49
+ persona = "the great spiritual leader Laozi"
50
+ scripture = "the Tao Te Ching"
51
+ elif church == "Jedi":
52
+ persona = "Master Yoda, the character from the Star Wars movie franchise"
53
+ scripture = "the Jedi Path of the Force"
54
+ elif church == "Social":
55
+ persona = "the philosopher Karl Marx"
56
+ scripture = "Karl Marx' book ""Das Kapital"""
57
+ elif church == "Greek":
58
+ persona = "the ancient Greek philosopher Plato"
59
+ scripture = "Plato's famous writing ""The Republic"""
60
+ elif church == "Science":
61
+ persona = "an omniscient talking encyclopedia"
62
+ scripture = "Wikipedia, precisely the explanations you find in {wikipedia_results}"
63
+
64
+ context = PromptTemplate(
65
+ input_variables = ["persona", "scripture", "topic"],
66
+ template="The user will provide you with an input text \
67
+ in form of a question or \
68
+ in form of a description of a situation or \
69
+ in form of a description of the user's current personal feelings. \
70
+ Act as {persona} \
71
+ and answer the user's question or give advice to the user. \
72
+ Choose your words in accordance to the teachings of {scripture}. \
73
+ And please remember, that you are in fact {persona}, so do not talk in third person about {persona}. \
74
+ Input text: {topic}"
75
+ )
76
+
77
+ find_keyword = PromptTemplate(
78
+ input_variables = ['topic'],
79
+ template="The user will provide you with an input text. \
80
+ Please return the main keyword of this input text. Input text: {topic}"
81
+ )
82
+
83
+ yoda_grammar = PromptTemplate(
84
+ input_variables = ["answer"],
85
+ template="You will be given an input text. \
86
+ Take the object and the first verb of each sentence and move these to the end of the sentence. \
87
+ Example input text: ""I am a powerful jedi master.""\
88
+ Example output text: ""A powerful jedi master I am.""\
89
+ Input text: {answer} \
90
+ Output text:"
91
+ )
92
+
93
+
94
+ #llms
95
+ llm = OpenAI(temperature=0.9)
96
+ llm_facts = OpenAI(temperature=0)
97
+ main_chain = LLMChain(llm=llm, prompt=context, verbose=True)
98
+ yoda_grammar_chain = LLMChain(llm=llm_facts, prompt=yoda_grammar)
99
+ keyword_chain = LLMChain(llm=llm_facts, prompt=find_keyword)
100
+ wiki_chain = LLMChain(llm=llm_facts, prompt=context, verbose=True)
101
+
102
+
103
+ #answer on screen if prompt is entered
104
+ if prompt:
105
+ if church == "Science":
106
+ keyword = keyword_chain.run(topic=prompt)
107
+ wikipedia_results = wikipedia.run(keyword)
108
+ response = wiki_chain.run(persona=persona, scripture=scripture, topic=prompt)
109
+ elif church == "Jedi":
110
+ answer = main_chain.run(persona=persona, scripture=scripture, topic=prompt)
111
+ response = yoda_grammar_chain.run(answer=answer)
112
+
113
+ else:
114
+ response = main_chain.run(persona=persona, scripture=scripture, topic=prompt)
115
+ st.write(response)