Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +108 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
|
5 |
+
openai.api_key = st.secrets['OpenAI_API_Key']
|
6 |
+
|
7 |
+
def main():
|
8 |
+
# Creating a sidebar for user inputs
|
9 |
+
st.sidebar.title("Chat with SmartGPT-4")
|
10 |
+
|
11 |
+
# Creating a text input field for user question context
|
12 |
+
context = st.sidebar.text_input("Enter the question context:")
|
13 |
+
|
14 |
+
# Creating a text input field for user questions
|
15 |
+
question = st.sidebar.text_input("Type your question:")
|
16 |
+
|
17 |
+
# Creating a button for the user to send their message
|
18 |
+
send_button = st.sidebar.button("Send")
|
19 |
+
|
20 |
+
# Initialize chat history in the first run
|
21 |
+
if 'chat_history' not in st.session_state:
|
22 |
+
st.session_state['chat_history'] = []
|
23 |
+
|
24 |
+
# Handle the send button click
|
25 |
+
if send_button:
|
26 |
+
# Set the system prompt
|
27 |
+
system_prompt = "ChatGPT, please act as a highly experienced, intelligent assistant who has access to vast amounts of knowledge across a wide range of disciplines. Your responses should demonstrate a deep understanding of any given topic, presenting nuanced, comprehensive information in a detailed and understandable manner. When asked a question or presented with a task, your approach should be to provide not only the answer or solution but also to offer relevant context, potential implications, comparisons and contrasts, and any other pertinent information that could deepen the user's understanding of the topic or task at hand. For complex or multifaceted inquiries, structure your responses to first offer a succinct summary, followed by a deeper dive into the matter. Consider the needs and expertise of the person asking the question and tailor your explanations accordingly, being aware that while some may require simplified explanations, others may prefer more technical details. And above all, prioritize the usefulness and accuracy of the information provided. If any additional details or follow-up actions are needed, proactively suggest them in your responses."
|
28 |
+
|
29 |
+
# Prepare 'prompt_1'
|
30 |
+
txt_1 = 'Question. '
|
31 |
+
txt_2 = "Answer: Let's work this out in a step by step way to be sure we have the right answer."
|
32 |
+
prompt_1 = context + '\n\n' + txt_1 + question + '\n' + txt_2
|
33 |
+
prompt_1 = prompt_1.strip()
|
34 |
+
|
35 |
+
# Save 'prompt_1' to the chat history
|
36 |
+
st.session_state['chat_history'].append(f"You: {prompt_1}")
|
37 |
+
|
38 |
+
# Hit the OpenAI API with 'prompt_1'
|
39 |
+
response = openai.ChatCompletion.create(
|
40 |
+
model='gpt-4',
|
41 |
+
messages=[
|
42 |
+
{'role': 'system', 'content': system_prompt},
|
43 |
+
{'role': 'user', 'content': prompt_1}
|
44 |
+
],
|
45 |
+
temperature=1, # Default value
|
46 |
+
n=3 # Number of chat completions to generate
|
47 |
+
)
|
48 |
+
|
49 |
+
messages = response['choices']
|
50 |
+
|
51 |
+
out_list = []
|
52 |
+
for idx, message in enumerate(messages):
|
53 |
+
idx = idx + 1
|
54 |
+
output = 'Answer Option ' + str(idx) + '.\n' + message['message']['content']
|
55 |
+
out_list.append(output)
|
56 |
+
|
57 |
+
answers_1 = '\n\n'.join(out_list)
|
58 |
+
|
59 |
+
# Save GPT's response to the chat history
|
60 |
+
st.session_state['chat_history'].append(f"SmartGPT-4: {answers_1}")
|
61 |
+
|
62 |
+
# Prepare 'prompt_2'
|
63 |
+
prompt_2 = context + '\n\n' + txt_1 + question + '\n\n' + answers_1 + '\n\n' + "You are a researcher tasked with investigating the 3 answer options provided. List the flaws and faulty logic of each answer option. Let's work this out in a step by step way to be sure we have all the errors:"
|
64 |
+
prompt_2 = prompt_2.strip()
|
65 |
+
|
66 |
+
# Save 'prompt_2' to the chat history
|
67 |
+
st.session_state['chat_history'].append(f"You: {prompt_2}")
|
68 |
+
|
69 |
+
# Hit the OpenAI API with 'prompt_2'
|
70 |
+
response = openai.ChatCompletion.create(
|
71 |
+
model='gpt-4',
|
72 |
+
messages=[
|
73 |
+
{'role': 'system', 'content': system_prompt},
|
74 |
+
{'role': 'user', 'content': prompt_2}
|
75 |
+
],
|
76 |
+
temperature=1 # Default value
|
77 |
+
)
|
78 |
+
|
79 |
+
answers_2 = response['choices'][0]['message']['content']
|
80 |
+
|
81 |
+
# Save GPT's response to the chat history
|
82 |
+
st.session_state['chat_history'].append(f"SmartGPT-4: {answers_2}")
|
83 |
+
|
84 |
+
# Prepare 'prompt_3'
|
85 |
+
prompt_3 = answers_1 + '\n\n' + answers_2 + '\n\n' + "You are a resolver tasked with 1) finding which of the 3 answer options the researcher thought was best 2) improving that answer, and 3) printing the improved answer in full. Let's work this out in a step by step way to be sure we have the right answer:"
|
86 |
+
|
87 |
+
# Hit the OpenAI API with 'prompt_3'
|
88 |
+
response = openai.ChatCompletion.create(
|
89 |
+
model='gpt-4',
|
90 |
+
messages=[
|
91 |
+
{'role': 'system', 'content': system_prompt},
|
92 |
+
{'role': 'user', 'content': prompt_3}
|
93 |
+
],
|
94 |
+
temperature=1 # Default value
|
95 |
+
)
|
96 |
+
|
97 |
+
answers_3 = response['choices'][0]['message']['content']
|
98 |
+
|
99 |
+
# Save GPT's response to the chat history
|
100 |
+
st.session_state['chat_history'].append(f"SmartGPT-4: {answers_3}")
|
101 |
+
|
102 |
+
# Creating an area to display the chat history
|
103 |
+
for message in st.session_state['chat_history']:
|
104 |
+
st.markdown(message)
|
105 |
+
|
106 |
+
|
107 |
+
if __name__ == "__main__":
|
108 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
openai
|