Spaces:
Runtime error
Runtime error
Init RetrieveChat Demo
Browse files- app.py +191 -0
- autogen.png +0 -0
app.py
ADDED
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
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 RetrieveUserProxyAgent, PROMPT_DEFAULT
|
11 |
+
|
12 |
+
|
13 |
+
def setup_configurations(config_file="OAI_CONFIG_LIST"):
|
14 |
+
config_list = autogen.config_list_from_json(
|
15 |
+
env_or_file=config_file,
|
16 |
+
file_location=".",
|
17 |
+
filter_dict={
|
18 |
+
"model": {
|
19 |
+
"gpt-4",
|
20 |
+
"gpt4",
|
21 |
+
"gpt-4-32k",
|
22 |
+
"gpt-4-32k-0314",
|
23 |
+
"gpt-35-turbo",
|
24 |
+
"gpt-3.5-turbo",
|
25 |
+
}
|
26 |
+
},
|
27 |
+
)
|
28 |
+
assert len(config_list) > 0
|
29 |
+
print("models to use: ", [config_list[i]["model"] for i in range(len(config_list))])
|
30 |
+
|
31 |
+
return config_list
|
32 |
+
|
33 |
+
|
34 |
+
def initialize_agents(config_list, docs_path=None):
|
35 |
+
if docs_path is None:
|
36 |
+
docs_path = "https://raw.githubusercontent.com/microsoft/autogen/main/README.md"
|
37 |
+
autogen.ChatCompletion.start_logging()
|
38 |
+
|
39 |
+
assistant = RetrieveAssistantAgent(
|
40 |
+
name="assistant",
|
41 |
+
system_message="You are a helpful assistant.",
|
42 |
+
llm_config={
|
43 |
+
"request_timeout": 600,
|
44 |
+
"seed": 42,
|
45 |
+
"config_list": config_list,
|
46 |
+
},
|
47 |
+
)
|
48 |
+
|
49 |
+
ragproxyagent = RetrieveUserProxyAgent(
|
50 |
+
name="ragproxyagent",
|
51 |
+
human_input_mode="NEVER",
|
52 |
+
max_consecutive_auto_reply=5,
|
53 |
+
retrieve_config={
|
54 |
+
# "task": "qa",
|
55 |
+
"docs_path": docs_path,
|
56 |
+
"chunk_token_size": 2000,
|
57 |
+
"model": config_list[0]["model"],
|
58 |
+
"client": chromadb.PersistentClient(path="/tmp/chromadb1"),
|
59 |
+
"embedding_model": "all-mpnet-base-v2",
|
60 |
+
"customized_prompt": PROMPT_DEFAULT,
|
61 |
+
},
|
62 |
+
)
|
63 |
+
|
64 |
+
return assistant, ragproxyagent
|
65 |
+
|
66 |
+
|
67 |
+
def initiate_chat(problem, queue, n_results=3):
|
68 |
+
global assistant, ragproxyagent
|
69 |
+
if assistant is None:
|
70 |
+
queue.put(["Please upload the LLM config file first"])
|
71 |
+
return
|
72 |
+
assistant.reset()
|
73 |
+
ragproxyagent.initiate_chat(assistant, problem=problem, silent=False, n_results=n_results)
|
74 |
+
# queue.put(ragproxyagent.last_message()["content"])
|
75 |
+
messages = ragproxyagent.chat_messages
|
76 |
+
messages = [messages[k] for k in messages.keys()][0]
|
77 |
+
messages = [m["content"] for m in messages if m["role"] == "user"]
|
78 |
+
print("messages: ", messages)
|
79 |
+
queue.put(messages)
|
80 |
+
|
81 |
+
|
82 |
+
def chatbot_reply(input_text):
|
83 |
+
"""Chat with the agent through terminal."""
|
84 |
+
queue = mp.Queue()
|
85 |
+
process = mp.Process(
|
86 |
+
target=initiate_chat,
|
87 |
+
args=(input_text, queue),
|
88 |
+
)
|
89 |
+
process.start()
|
90 |
+
process.join()
|
91 |
+
messages = queue.get()
|
92 |
+
return messages
|
93 |
+
|
94 |
+
def get_description_text():
|
95 |
+
return """
|
96 |
+
# Microsoft AutoGen: Retrieve Chat Demo
|
97 |
+
|
98 |
+
This demo shows how to use the RetrieveUserProxyAgent and RetrieveAssistantAgent to build a chatbot.
|
99 |
+
|
100 |
+
#### [GitHub](https://github.com/microsoft/autogen) [Discord](https://discord.gg/pAbnFJrkgZ) [Docs](https://microsoft.github.io/autogen/) [Paper](https://arxiv.org/abs/2308.08155)
|
101 |
+
|
102 |
+
LLM configure file should contain OpenAI, Azure OpenAI or other openai compatible models, for example:
|
103 |
+
```
|
104 |
+
[
|
105 |
+
{
|
106 |
+
"engine": "gpt-35-turbo",
|
107 |
+
"model": "gpt-3.5-turbo",
|
108 |
+
"api_base": "https://xxx.openai.azure.com",
|
109 |
+
"api_type": "azure",
|
110 |
+
"api_version": "2023-05-15",
|
111 |
+
"api_key": "xxx",
|
112 |
+
}
|
113 |
+
]
|
114 |
+
```
|
115 |
+
"""
|
116 |
+
|
117 |
+
global config_list, assistant, ragproxyagent
|
118 |
+
assistant = None
|
119 |
+
with gr.Blocks() as demo:
|
120 |
+
gr.Markdown(get_description_text())
|
121 |
+
chatbot = gr.Chatbot(
|
122 |
+
[],
|
123 |
+
elem_id="chatbot",
|
124 |
+
bubble_full_width=False,
|
125 |
+
avatar_images=(None, (os.path.join(os.path.dirname(__file__), "autogen.png"))),
|
126 |
+
height=600,
|
127 |
+
)
|
128 |
+
with gr.Row():
|
129 |
+
txt_input = gr.Textbox(
|
130 |
+
scale=4,
|
131 |
+
show_label=False,
|
132 |
+
placeholder="Enter text and press enter",
|
133 |
+
container=False,
|
134 |
+
)
|
135 |
+
|
136 |
+
def upload_file(file):
|
137 |
+
global config_list, assistant, ragproxyagent
|
138 |
+
config_list = setup_configurations(config_file=file.name)
|
139 |
+
assistant, ragproxyagent = initialize_agents(config_list)
|
140 |
+
|
141 |
+
upload_button = gr.UploadButton("Click to Upload LLM Config File", file_types=["file"], file_count="single")
|
142 |
+
upload_button.upload(upload_file, upload_button)
|
143 |
+
|
144 |
+
clear = gr.ClearButton([txt_input, chatbot])
|
145 |
+
|
146 |
+
txt_context_url = gr.Textbox(
|
147 |
+
label="Enter the url to your context file and chat on the context",
|
148 |
+
info=f"File must be in the format of [{', '.join(TEXT_FORMATS)}]",
|
149 |
+
max_lines=1,
|
150 |
+
show_label=True,
|
151 |
+
value="https://arxiv.org/pdf/2308.08155.pdf",
|
152 |
+
container=True,
|
153 |
+
)
|
154 |
+
|
155 |
+
txt_prompt = gr.Textbox(
|
156 |
+
label="Enter your prompt for Retrieve Agent and press enter to replace the default prompt",
|
157 |
+
max_lines=40,
|
158 |
+
show_label=True,
|
159 |
+
value=PROMPT_DEFAULT,
|
160 |
+
container=True,
|
161 |
+
show_copy_button=True,
|
162 |
+
layout={"height": 20},
|
163 |
+
)
|
164 |
+
|
165 |
+
|
166 |
+
def respond(message, chat_history):
|
167 |
+
messages = chatbot_reply(message)
|
168 |
+
chat_history.append((message, messages[-1] if messages[-1] != "TERMINATE" else messages[-2]))
|
169 |
+
return "", chat_history
|
170 |
+
|
171 |
+
def update_prompt(prompt):
|
172 |
+
ragproxyagent.customized_prompt = prompt
|
173 |
+
return prompt
|
174 |
+
|
175 |
+
def update_context_url(context_url):
|
176 |
+
global assistant, ragproxyagent
|
177 |
+
try:
|
178 |
+
shutil.rmtree("/tmp/chromadb1/")
|
179 |
+
except:
|
180 |
+
pass
|
181 |
+
assistant, ragproxyagent = initialize_agents(config_list, docs_path=context_url)
|
182 |
+
return context_url
|
183 |
+
|
184 |
+
txt_input.submit(respond, [txt_input, chatbot], [txt_input, chatbot])
|
185 |
+
txt_prompt.submit(update_prompt, [txt_prompt], [txt_prompt])
|
186 |
+
txt_context_url.submit(update_context_url, [txt_context_url], [txt_context_url])
|
187 |
+
|
188 |
+
|
189 |
+
if __name__ == "__main__":
|
190 |
+
demo.launch(share=True)
|
191 |
+
|
autogen.png
ADDED
![]() |