Create init
Browse files
init
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Initialize Agents
|
| 2 |
+
def initialize_agents(config_list, docs_path=None):
|
| 3 |
+
...
|
| 4 |
+
return assistant, ragproxyagent
|
| 5 |
+
|
| 6 |
+
# Initialize Chat
|
| 7 |
+
def initiate_chat(config_list, problem, queue, n_results=3):
|
| 8 |
+
...
|
| 9 |
+
assistant.reset()
|
| 10 |
+
try:
|
| 11 |
+
ragproxyagent.a_initiate_chat(
|
| 12 |
+
assistant, problem=problem, silent=False, n_results=n_results
|
| 13 |
+
)
|
| 14 |
+
messages = ragproxyagent.chat_messages
|
| 15 |
+
messages = [messages[k] for k in messages.keys()][0]
|
| 16 |
+
messages = [m["content"] for m in messages if m["role"] == "user"]
|
| 17 |
+
print("messages: ", messages)
|
| 18 |
+
except Exception as e:
|
| 19 |
+
messages = [str(e)]
|
| 20 |
+
queue.put(messages)
|
| 21 |
+
|
| 22 |
+
# Wrap AutoGen part into a function
|
| 23 |
+
def chatbot_reply(input_text):
|
| 24 |
+
"""Chat with the agent through terminal."""
|
| 25 |
+
queue = mp.Queue()
|
| 26 |
+
process = mp.Process(
|
| 27 |
+
target=initiate_chat,
|
| 28 |
+
args=(config_list, input_text, queue),
|
| 29 |
+
)
|
| 30 |
+
process.start()
|
| 31 |
+
try:
|
| 32 |
+
messages = queue.get(timeout=TIMEOUT)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
messages = [str(e) if len(str(e)) > 0 else "Invalid Request to OpenAI, please check your API keys."]
|
| 35 |
+
finally:
|
| 36 |
+
try:
|
| 37 |
+
process.terminate()
|
| 38 |
+
except:
|
| 39 |
+
pass
|
| 40 |
+
return messages
|
| 41 |
+
|
| 42 |
+
...
|
| 43 |
+
|
| 44 |
+
# Set up UI with Gradio
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
...
|
| 47 |
+
assistant, ragproxyagent = initialize_agents(config_list)
|
| 48 |
+
|
| 49 |
+
chatbot = gr.Chatbot(
|
| 50 |
+
[],
|
| 51 |
+
elem_id="chatbot",
|
| 52 |
+
bubble_full_width=False,
|
| 53 |
+
avatar_images=(None, (os.path.join(os.path.dirname(__file__), "autogen.png"))),
|
| 54 |
+
# height=600,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
txt_input = gr.Textbox(
|
| 58 |
+
scale=4,
|
| 59 |
+
show_label=False,
|
| 60 |
+
placeholder="Enter text and press enter",
|
| 61 |
+
container=False,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
txt_model = gr.Dropdown(
|
| 66 |
+
label="Model",
|
| 67 |
+
choices=[
|
| 68 |
+
"gpt-4",
|
| 69 |
+
"gpt-35-turbo",
|
| 70 |
+
"gpt-3.5-turbo",
|
| 71 |
+
],
|
| 72 |
+
allow_custom_value=True,
|
| 73 |
+
value="gpt-35-turbo",
|
| 74 |
+
container=True,
|
| 75 |
+
)
|
| 76 |
+
txt_oai_key = gr.Textbox(
|
| 77 |
+
label="OpenAI API Key",
|
| 78 |
+
placeholder="Enter key and press enter",
|
| 79 |
+
max_lines=1,
|
| 80 |
+
show_label=True,
|
| 81 |
+
value=os.environ.get("OPENAI_API_KEY", ""),
|
| 82 |
+
container=True,
|
| 83 |
+
type="password",
|
| 84 |
+
)
|
| 85 |
+
...
|
| 86 |
+
|
| 87 |
+
clear = gr.ClearButton([txt_input, chatbot])
|
| 88 |
+
|
| 89 |
+
...
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
demo.launch(share=True)
|