Spaces:
Runtime error
Runtime error
Add State
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import shutil
|
|
4 |
import autogen
|
5 |
import chromadb
|
6 |
import multiprocessing as mp
|
7 |
-
from autogen.oai.openai_utils import config_list_from_json
|
8 |
from autogen.retrieve_utils import TEXT_FORMATS
|
9 |
from autogen.agentchat.contrib.retrieve_assistant_agent import RetrieveAssistantAgent
|
10 |
from autogen.agentchat.contrib.retrieve_user_proxy_agent import (
|
@@ -13,17 +12,12 @@ from autogen.agentchat.contrib.retrieve_user_proxy_agent import (
|
|
13 |
)
|
14 |
|
15 |
|
16 |
-
def
|
17 |
-
config_list
|
18 |
-
|
19 |
-
|
20 |
-
if len(config_list) > 0:
|
21 |
-
return [config_list[0]]
|
22 |
else:
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
def initialize_agents(config_list, docs_path=None):
|
27 |
if docs_path is None:
|
28 |
docs_path = "https://raw.githubusercontent.com/microsoft/autogen/main/README.md"
|
29 |
autogen.ChatCompletion.start_logging()
|
@@ -31,11 +25,6 @@ def initialize_agents(config_list, docs_path=None):
|
|
31 |
assistant = RetrieveAssistantAgent(
|
32 |
name="assistant",
|
33 |
system_message="You are a helpful assistant.",
|
34 |
-
llm_config={
|
35 |
-
"request_timeout": 600,
|
36 |
-
"seed": 42,
|
37 |
-
"config_list": config_list,
|
38 |
-
},
|
39 |
)
|
40 |
|
41 |
ragproxyagent = RetrieveUserProxyAgent(
|
@@ -46,7 +35,7 @@ def initialize_agents(config_list, docs_path=None):
|
|
46 |
# "task": "qa",
|
47 |
"docs_path": docs_path,
|
48 |
"chunk_token_size": 2000,
|
49 |
-
"model":
|
50 |
"client": chromadb.PersistentClient(path="/tmp/chromadb"),
|
51 |
"embedding_model": "all-mpnet-base-v2",
|
52 |
"customized_prompt": PROMPT_DEFAULT,
|
@@ -57,15 +46,27 @@ def initialize_agents(config_list, docs_path=None):
|
|
57 |
|
58 |
|
59 |
def initiate_chat(problem, queue, n_results=3):
|
60 |
-
global assistant, ragproxyagent
|
61 |
-
if
|
|
|
|
|
|
|
|
|
62 |
queue.put(["Please set the LLM config first"])
|
63 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
assistant.reset()
|
65 |
ragproxyagent.initiate_chat(
|
66 |
assistant, problem=problem, silent=False, n_results=n_results
|
67 |
)
|
68 |
-
# queue.put(ragproxyagent.last_message()["content"])
|
69 |
messages = ragproxyagent.chat_messages
|
70 |
messages = [messages[k] for k in messages.keys()][0]
|
71 |
messages = [m["content"] for m in messages if m["role"] == "user"]
|
@@ -97,12 +98,24 @@ def get_description_text():
|
|
97 |
|
98 |
|
99 |
global config_list, assistant, ragproxyagent
|
100 |
-
config_list = setup_configurations()
|
101 |
-
assistant, ragproxyagent = (
|
102 |
-
initialize_agents(config_list) if config_list else (None, None)
|
103 |
-
)
|
104 |
|
105 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
gr.Markdown(get_description_text())
|
107 |
chatbot = gr.Chatbot(
|
108 |
[],
|
@@ -126,34 +139,43 @@ with gr.Blocks() as demo:
|
|
126 |
update_context_url(file.name)
|
127 |
|
128 |
upload_button = gr.UploadButton(
|
129 |
-
"
|
130 |
file_types=[f".{i}" for i in TEXT_FORMATS],
|
131 |
file_count="single",
|
132 |
)
|
133 |
upload_button.upload(upload_file, upload_button)
|
134 |
|
135 |
-
def update_config():
|
136 |
-
global
|
137 |
-
config_list =
|
|
|
|
|
|
|
138 |
assistant, ragproxyagent = (
|
139 |
-
initialize_agents(
|
140 |
)
|
|
|
141 |
|
142 |
-
def
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
157 |
txt_oai_key = gr.Textbox(
|
158 |
label="OpenAI API Key",
|
159 |
placeholder="Enter key and press enter",
|
@@ -163,7 +185,6 @@ with gr.Blocks() as demo:
|
|
163 |
container=True,
|
164 |
type="password",
|
165 |
)
|
166 |
-
txt_oai_key.submit(set_oai_key, [txt_oai_key], [txt_oai_key])
|
167 |
txt_aoai_key = gr.Textbox(
|
168 |
label="Azure OpenAI API Key",
|
169 |
placeholder="Enter key and press enter",
|
@@ -173,7 +194,6 @@ with gr.Blocks() as demo:
|
|
173 |
container=True,
|
174 |
type="password",
|
175 |
)
|
176 |
-
txt_aoai_key.submit(set_aoai_key, [txt_aoai_key], [txt_aoai_key])
|
177 |
txt_aoai_base_url = gr.Textbox(
|
178 |
label="Azure OpenAI API Base",
|
179 |
placeholder="Enter base url and press enter",
|
@@ -183,8 +203,11 @@ with gr.Blocks() as demo:
|
|
183 |
container=True,
|
184 |
type="password",
|
185 |
)
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
188 |
)
|
189 |
|
190 |
clear = gr.ClearButton([txt_input, chatbot])
|
@@ -225,7 +248,7 @@ with gr.Blocks() as demo:
|
|
225 |
shutil.rmtree("/tmp/chromadb/")
|
226 |
except:
|
227 |
pass
|
228 |
-
assistant, ragproxyagent = initialize_agents(
|
229 |
return context_url
|
230 |
|
231 |
txt_input.submit(respond, [txt_input, chatbot], [txt_input, chatbot])
|
|
|
4 |
import autogen
|
5 |
import chromadb
|
6 |
import multiprocessing as mp
|
|
|
7 |
from autogen.retrieve_utils import TEXT_FORMATS
|
8 |
from autogen.agentchat.contrib.retrieve_assistant_agent import RetrieveAssistantAgent
|
9 |
from autogen.agentchat.contrib.retrieve_user_proxy_agent import (
|
|
|
12 |
)
|
13 |
|
14 |
|
15 |
+
def initialize_agents(docs_path=None):
|
16 |
+
global config_list
|
17 |
+
if isinstance(config_list, gr.State):
|
18 |
+
_config_list = config_list.value
|
|
|
|
|
19 |
else:
|
20 |
+
_config_list = config_list
|
|
|
|
|
|
|
21 |
if docs_path is None:
|
22 |
docs_path = "https://raw.githubusercontent.com/microsoft/autogen/main/README.md"
|
23 |
autogen.ChatCompletion.start_logging()
|
|
|
25 |
assistant = RetrieveAssistantAgent(
|
26 |
name="assistant",
|
27 |
system_message="You are a helpful assistant.",
|
|
|
|
|
|
|
|
|
|
|
28 |
)
|
29 |
|
30 |
ragproxyagent = RetrieveUserProxyAgent(
|
|
|
35 |
# "task": "qa",
|
36 |
"docs_path": docs_path,
|
37 |
"chunk_token_size": 2000,
|
38 |
+
"model": _config_list[0]["model"],
|
39 |
"client": chromadb.PersistentClient(path="/tmp/chromadb"),
|
40 |
"embedding_model": "all-mpnet-base-v2",
|
41 |
"customized_prompt": PROMPT_DEFAULT,
|
|
|
46 |
|
47 |
|
48 |
def initiate_chat(problem, queue, n_results=3):
|
49 |
+
global assistant, ragproxyagent, config_list
|
50 |
+
if isinstance(config_list, gr.State):
|
51 |
+
_config_list = config_list.value
|
52 |
+
else:
|
53 |
+
_config_list = config_list
|
54 |
+
if len(_config_list[0].get("api_key", "")) < 2:
|
55 |
queue.put(["Please set the LLM config first"])
|
56 |
return
|
57 |
+
else:
|
58 |
+
llm_config ={
|
59 |
+
"request_timeout": 600,
|
60 |
+
"seed": 42,
|
61 |
+
"config_list": _config_list,
|
62 |
+
},
|
63 |
+
print(llm_config, type(llm_config))
|
64 |
+
print(assistant.llm_config, type(assistant.llm_config))
|
65 |
+
assistant.llm_config.update(llm_config[0])
|
66 |
assistant.reset()
|
67 |
ragproxyagent.initiate_chat(
|
68 |
assistant, problem=problem, silent=False, n_results=n_results
|
69 |
)
|
|
|
70 |
messages = ragproxyagent.chat_messages
|
71 |
messages = [messages[k] for k in messages.keys()][0]
|
72 |
messages = [m["content"] for m in messages if m["role"] == "user"]
|
|
|
98 |
|
99 |
|
100 |
global config_list, assistant, ragproxyagent
|
|
|
|
|
|
|
|
|
101 |
|
102 |
with gr.Blocks() as demo:
|
103 |
+
config_list, assistant, ragproxyagent = (
|
104 |
+
gr.State(
|
105 |
+
[
|
106 |
+
{
|
107 |
+
"api_key": "",
|
108 |
+
"api_base": "",
|
109 |
+
"api_type": "azure",
|
110 |
+
"api_version": "2023-07-01-preview",
|
111 |
+
"model": "gpt-35-turbo",
|
112 |
+
}
|
113 |
+
]
|
114 |
+
),
|
115 |
+
None,
|
116 |
+
None,
|
117 |
+
)
|
118 |
+
|
119 |
gr.Markdown(get_description_text())
|
120 |
chatbot = gr.Chatbot(
|
121 |
[],
|
|
|
139 |
update_context_url(file.name)
|
140 |
|
141 |
upload_button = gr.UploadButton(
|
142 |
+
"Upload Document",
|
143 |
file_types=[f".{i}" for i in TEXT_FORMATS],
|
144 |
file_count="single",
|
145 |
)
|
146 |
upload_button.upload(upload_file, upload_button)
|
147 |
|
148 |
+
def update_config(config_list):
|
149 |
+
global assistant, ragproxyagent
|
150 |
+
config_list = autogen.config_list_from_models(
|
151 |
+
model_list=[os.environ["MODEL"]]
|
152 |
+
)
|
153 |
+
print(config_list, type(config_list))
|
154 |
assistant, ragproxyagent = (
|
155 |
+
initialize_agents() if config_list else (None, None)
|
156 |
)
|
157 |
+
return config_list
|
158 |
|
159 |
+
def set_params(model, oai_key, aoai_key, aoai_base):
|
160 |
+
global config_list, assistant, ragproxyagent
|
161 |
+
os.environ["MODEL"] = model
|
162 |
+
os.environ["OPENAI_API_KEY"] = oai_key
|
163 |
+
os.environ["AZURE_OPENAI_API_KEY"] = aoai_key
|
164 |
+
os.environ["AZURE_OPENAI_API_BASE"] = aoai_base
|
165 |
+
config_list = update_config(config_list)
|
166 |
+
return model, oai_key, aoai_key, aoai_base
|
167 |
+
|
168 |
+
txt_model = gr.Dropdown(
|
169 |
+
label="Model",
|
170 |
+
choices=[
|
171 |
+
"gpt-4",
|
172 |
+
"gpt-35-turbo",
|
173 |
+
"gpt-3.5-turbo",
|
174 |
+
],
|
175 |
+
allow_custom_value=True,
|
176 |
+
default_value="gpt-35-turbo",
|
177 |
+
container=True,
|
178 |
+
)
|
179 |
txt_oai_key = gr.Textbox(
|
180 |
label="OpenAI API Key",
|
181 |
placeholder="Enter key and press enter",
|
|
|
185 |
container=True,
|
186 |
type="password",
|
187 |
)
|
|
|
188 |
txt_aoai_key = gr.Textbox(
|
189 |
label="Azure OpenAI API Key",
|
190 |
placeholder="Enter key and press enter",
|
|
|
194 |
container=True,
|
195 |
type="password",
|
196 |
)
|
|
|
197 |
txt_aoai_base_url = gr.Textbox(
|
198 |
label="Azure OpenAI API Base",
|
199 |
placeholder="Enter base url and press enter",
|
|
|
203 |
container=True,
|
204 |
type="password",
|
205 |
)
|
206 |
+
set_params_button = gr.Button(value="Set Params", type="button")
|
207 |
+
set_params_button.click(
|
208 |
+
set_params,
|
209 |
+
[txt_model, txt_oai_key, txt_aoai_key, txt_aoai_base_url],
|
210 |
+
[txt_model, txt_oai_key, txt_aoai_key, txt_aoai_base_url],
|
211 |
)
|
212 |
|
213 |
clear = gr.ClearButton([txt_input, chatbot])
|
|
|
248 |
shutil.rmtree("/tmp/chromadb/")
|
249 |
except:
|
250 |
pass
|
251 |
+
assistant, ragproxyagent = initialize_agents(docs_path=context_url)
|
252 |
return context_url
|
253 |
|
254 |
txt_input.submit(respond, [txt_input, chatbot], [txt_input, chatbot])
|