utkuarslan5 commited on
Commit
36046af
·
1 Parent(s): b70be97
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +125 -0
  3. requitements.txt +3 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import find_dotenv, load_dotenv
2
+ from langchain.llms import OpenAI
3
+ from langchain.callbacks import get_openai_callback
4
+ from langchain.memory import ConversationBufferMemory
5
+ from langchain.chains import (SimpleSequentialChain,
6
+ SequentialChain,
7
+ PALChain,
8
+ LLMMathChain,
9
+ ConversationChain)
10
+ from langchain.prompts.prompt import PromptTemplate
11
+ from langchain.prompts.chat import (
12
+ ChatPromptTemplate,
13
+ SystemMessagePromptTemplate,
14
+ HumanMessagePromptTemplate,
15
+ )
16
+ import streamlit as st
17
+ import warnings
18
+ warnings.filterwarnings("ignore")
19
+
20
+ # Load environment variables
21
+ load_dotenv(find_dotenv())
22
+
23
+ q = """Let the triangle ABC with the vertices A, B and C be given on the Bessele ellipsoid. The mean latitude is 48° 30'. The constants of the Bessele ellipsoid are given by c = 6398786.849 m and e'² = 0.00671922. Let the length (of the geodesic line) between the vertices B and C be 58461.234 m. Furthermore the angles (= 62° 26' 54'.21) and (= 52° 12' 06'.99) are known.
24
+
25
+ Determine (in this order and independently)
26
+ a) the side AC of the triangle ABC using Legendre's theorem,
27
+ b) the side AB of the triangle ABC according to Soldner's additament method as well as
28
+ c) the angle using Ehlert's formulas.
29
+ d) control the side BC using the spherical cosine theorem.
30
+ """
31
+
32
+
33
+ # --------------------------------------------------------------
34
+ # LLM: Tutors you in your homeworks
35
+ # --------------------------------------------------------------
36
+ temperature = 0.3
37
+ # Define llm
38
+ llm = OpenAI(model_name="gpt-3.5-turbo",
39
+ temperature=temperature,
40
+ max_retries=15,
41
+ max_tokens=2048)
42
+
43
+ # Template to use for the system message prompt
44
+ SYSTEM_PROMPT_TEMPLATE = """You are a Tutor for human students. Humans will ask you questions and you will provide which calculations to make. If you are unsure, output None. First, you will be given a context from the user, then user will ask questions about the context. Format your output readible Markdown.
45
+
46
+ Output:
47
+ Summary: <A summary of the answer>
48
+ Steps: <step-by-step list of answer>
49
+ Calculate: <Calculation to be made>
50
+ """
51
+ system_message_prompt = SystemMessagePromptTemplate.from_template(SYSTEM_PROMPT_TEMPLATE)
52
+
53
+ # Human question prompt
54
+ USER_PROMPT_TEMPLATE = """The following is a educational conversation with a Student and a Tutor. Tutor outputs step-by-step which calculations to make to answer the question based on the context. If the Tutor is unsure of the steps, it truthfully says it does not know.
55
+
56
+ Current conversation:
57
+ {history}
58
+ Student: {input}
59
+ Tutor:"""
60
+ human_message_prompt = HumanMessagePromptTemplate.from_template(USER_PROMPT_TEMPLATE)
61
+
62
+ _CHAT_PROMPT = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
63
+
64
+ conversation_chain = ConversationChain(llm=llm,
65
+ prompt=_CHAT_PROMPT,
66
+ memory=ConversationBufferMemory(ai_prefix="Tutor", human_prefix="Student"),
67
+ verbose=True
68
+ )
69
+
70
+ # Math prompt template
71
+ MATH_PROMPT_TEMPLATE = """You are GPT-3, and you can't do math.
72
+
73
+ You can do basic math, and your memorization abilities are impressive, but you can't do any complex calculations that a human could not do in their head. You also have an annoying tendency to just make up highly specific, but wrong, answers.
74
+
75
+ So we hooked you up to a Python 3 kernel, and now you can execute code. If you execute code, you must print out the final answer using the print function. You MUST use the python package numpy or sympy to answer your question. You must import numpy as np. Output the question, your code, output of your code, and your answer.
76
+
77
+
78
+ Question: ${{Question with hard calculation.}}
79
+ ```python
80
+ ${{Code that prints what you need to know}}
81
+ print(${{code}})
82
+ ```
83
+ ```output
84
+ ${{Output of your code}}
85
+ ```
86
+ Answer: ${{Answer}}
87
+
88
+ Begin.
89
+
90
+ Question: What is 37593 * 67?
91
+
92
+ ```python
93
+ import numpy as np
94
+ print(np.multiply(37593, 67))
95
+ ```
96
+ ```output
97
+ 2518731
98
+ ```
99
+ Answer: 2518731
100
+
101
+ Question: {question}"""
102
+
103
+ PROMPT = PromptTemplate(input_variables=["question"], template=MATH_PROMPT_TEMPLATE)
104
+
105
+ math_chain = PALChain.from_math_prompt(llm=llm,
106
+ verbose=True,)
107
+
108
+ # --------------------------------------------------------------
109
+ # Setup UI components
110
+ # --------------------------------------------------------------
111
+ tutor, math = st.tabs(["Tutor", "Math"])
112
+
113
+ with tutor:
114
+ chat = st.text_area(label='Chat')
115
+ ask = st.button(label='Ask!')
116
+ if ask:
117
+ response = conversation_chain.predict(input=chat)
118
+ st.write(response)
119
+
120
+ with math:
121
+ calculation = st.text_input(label='Calculate')
122
+ ask2 = st.button(label='Calculate!')
123
+ if ask2:
124
+ response = math_chain.run(calculation)
125
+ st.write(response)
requitements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ openai
3
+ langchain