Spaces:
Runtime error
Runtime error
xuyingliKepler
commited on
Commit
•
5e30d89
1
Parent(s):
d718356
Update app.py
Browse files
app.py
CHANGED
@@ -16,6 +16,19 @@ import openai
|
|
16 |
import multiprocessing
|
17 |
import autogen.agentchat.user_proxy_agent as upa
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
class OutputCapture:
|
20 |
def __init__(self):
|
21 |
self.contents = []
|
@@ -43,17 +56,6 @@ class ExtendedUserProxyAgent(upa.UserProxyAgent):
|
|
43 |
self.log_interaction(f"Human input: {human_input}")
|
44 |
return human_input
|
45 |
|
46 |
-
# Example usage:
|
47 |
-
config_list = [
|
48 |
-
{
|
49 |
-
"model": "gpt-4",
|
50 |
-
"api_key": st.secrets["OPENAI_API_KEY"]
|
51 |
-
}
|
52 |
-
]
|
53 |
-
|
54 |
-
gpt4_api_key = config_list[0]["api_key"]
|
55 |
-
os.environ['OPENAI_API_KEY'] = st.secrets["OPENAI_API_KEY"]
|
56 |
-
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
57 |
|
58 |
def build_vector_store(pdf_path, chunk_size=1000):
|
59 |
loaders = [PyPDFLoader(pdf_path)]
|
@@ -85,53 +87,57 @@ def answer_question(question, qa_chain):
|
|
85 |
response = qa_chain({"question": question})
|
86 |
return response["answer"]
|
87 |
|
88 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
llm_config={
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
},
|
106 |
-
"required": ["question"],
|
107 |
},
|
108 |
-
|
109 |
-
|
|
|
|
|
110 |
}
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
name="user_proxy",
|
114 |
human_input_mode="NEVER",
|
115 |
max_consecutive_auto_reply=10,
|
116 |
code_execution_config={"work_dir": "."},
|
117 |
llm_config=llm_config,
|
118 |
system_message="""Reply TERMINATE if the task has been solved at full satisfaction.
|
119 |
-
Otherwise, reply CONTINUE, or the reason why the task is not solved yet.""",
|
120 |
function_map={"answer_question": answer_question}
|
121 |
)
|
122 |
-
return assistant, user_proxy
|
123 |
-
|
124 |
-
def initiate_task(user_proxy, assistant, user_question):
|
125 |
-
user_proxy.initiate_chat(
|
126 |
-
assistant,
|
127 |
-
message= user_question
|
128 |
-
)
|
129 |
-
|
130 |
-
def initiate_task_process(queue, tmp_path, user_question):
|
131 |
-
loaders = [PyPDFLoader(tmp_path)]
|
132 |
-
vectorstore = build_vector_store(tmp_path)
|
133 |
-
qa_chain = setup_qa_chain(vectorstore)
|
134 |
-
assistant, user_proxy = setup_agents(config_list, lambda q: answer_uniswap_question(q, qa_chain))
|
135 |
|
136 |
output_capture = OutputCapture()
|
137 |
sys.stdout = output_capture
|
|
|
16 |
import multiprocessing
|
17 |
import autogen.agentchat.user_proxy_agent as upa
|
18 |
|
19 |
+
|
20 |
+
config_list = [
|
21 |
+
{
|
22 |
+
"model": "gpt-4",
|
23 |
+
"api_key": st.secrets["OPENAI_API_KEY"]
|
24 |
+
}
|
25 |
+
]
|
26 |
+
|
27 |
+
gpt4_api_key = config_list[0]["api_key"]
|
28 |
+
os.environ['OPENAI_API_KEY'] = st.secrets["OPENAI_API_KEY"]
|
29 |
+
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
30 |
+
|
31 |
+
|
32 |
class OutputCapture:
|
33 |
def __init__(self):
|
34 |
self.contents = []
|
|
|
56 |
self.log_interaction(f"Human input: {human_input}")
|
57 |
return human_input
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
def build_vector_store(pdf_path, chunk_size=1000):
|
61 |
loaders = [PyPDFLoader(pdf_path)]
|
|
|
87 |
response = qa_chain({"question": question})
|
88 |
return response["answer"]
|
89 |
|
90 |
+
def initiate_task(user_proxy, assistant, user_question):
|
91 |
+
user_proxy.initiate_chat(
|
92 |
+
assistant,
|
93 |
+
message= user_question
|
94 |
+
)
|
95 |
+
|
96 |
+
def initiate_task_process(queue, tmp_path, user_question):
|
97 |
+
vectorstore = build_vector_store(tmp_path)
|
98 |
+
qa = setup_qa_chain(vectorstore)
|
99 |
+
def answer_question(question):
|
100 |
+
response = qa({"question": question})
|
101 |
+
return response["answer"]
|
102 |
llm_config={
|
103 |
+
"request_timeout": 600,
|
104 |
+
"seed": 42,
|
105 |
+
"config_list": config_list,
|
106 |
+
"temperature": 0,
|
107 |
+
"functions": [
|
108 |
+
{
|
109 |
+
"name": "answer_question",
|
110 |
+
"description": "Answer any questions in relation to the paper",
|
111 |
+
"parameters": {
|
112 |
+
"type": "object",
|
113 |
+
"properties": {
|
114 |
+
"question": {
|
115 |
+
"type": "string",
|
116 |
+
"description": "The question to ask in relation to the paper",
|
117 |
+
}
|
|
|
|
|
118 |
},
|
119 |
+
"required": ["question"],
|
120 |
+
},
|
121 |
+
}
|
122 |
+
],
|
123 |
}
|
124 |
+
|
125 |
+
# create an AssistantAgent instance named "assistant"
|
126 |
+
assistant = autogen.AssistantAgent(
|
127 |
+
name="assistant",
|
128 |
+
llm_config=llm_config,
|
129 |
+
)
|
130 |
+
# create a UserProxyAgent instance named "user_proxy"
|
131 |
+
user_proxy = autogen.UserProxyAgent(
|
132 |
name="user_proxy",
|
133 |
human_input_mode="NEVER",
|
134 |
max_consecutive_auto_reply=10,
|
135 |
code_execution_config={"work_dir": "."},
|
136 |
llm_config=llm_config,
|
137 |
system_message="""Reply TERMINATE if the task has been solved at full satisfaction.
|
138 |
+
Otherwise, reply CONTINUE, or the reason why the task is not solved yet.""",
|
139 |
function_map={"answer_question": answer_question}
|
140 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
output_capture = OutputCapture()
|
143 |
sys.stdout = output_capture
|