|
|
|
def initialize_agents(config_list, docs_path=None): |
|
... |
|
return assistant, ragproxyagent |
|
|
|
|
|
def initiate_chat(config_list, problem, queue, n_results=3): |
|
... |
|
assistant.reset() |
|
try: |
|
ragproxyagent.a_initiate_chat( |
|
assistant, problem=problem, silent=False, n_results=n_results |
|
) |
|
messages = ragproxyagent.chat_messages |
|
messages = [messages[k] for k in messages.keys()][0] |
|
messages = [m["content"] for m in messages if m["role"] == "user"] |
|
print("messages: ", messages) |
|
except Exception as e: |
|
messages = [str(e)] |
|
queue.put(messages) |
|
|
|
|
|
def chatbot_reply(input_text): |
|
"""Chat with the agent through terminal.""" |
|
queue = mp.Queue() |
|
process = mp.Process( |
|
target=initiate_chat, |
|
args=(config_list, input_text, queue), |
|
) |
|
process.start() |
|
try: |
|
messages = queue.get(timeout=TIMEOUT) |
|
except Exception as e: |
|
messages = [str(e) if len(str(e)) > 0 else "Invalid Request to OpenAI, please check your API keys."] |
|
finally: |
|
try: |
|
process.terminate() |
|
except: |
|
pass |
|
return messages |
|
|
|
... |
|
|
|
|
|
with gr.Blocks() as demo: |
|
... |
|
assistant, ragproxyagent = initialize_agents(config_list) |
|
|
|
chatbot = gr.Chatbot( |
|
[], |
|
elem_id="chatbot", |
|
bubble_full_width=False, |
|
avatar_images=(None, (os.path.join(os.path.dirname(__file__), "autogen.png"))), |
|
# height=600, |
|
) |
|
|
|
txt_input = gr.Textbox( |
|
scale=4, |
|
show_label=False, |
|
placeholder="Enter text and press enter", |
|
container=False, |
|
) |
|
|
|
with gr.Row(): |
|
txt_model = gr.Dropdown( |
|
label="Model", |
|
choices=[ |
|
"gpt-4", |
|
"gpt-35-turbo", |
|
"gpt-3.5-turbo", |
|
], |
|
allow_custom_value=True, |
|
value="gpt-35-turbo", |
|
container=True, |
|
) |
|
txt_oai_key = gr.Textbox( |
|
label="OpenAI API Key", |
|
placeholder="Enter key and press enter", |
|
max_lines=1, |
|
show_label=True, |
|
value=os.environ.get("OPENAI_API_KEY", ""), |
|
container=True, |
|
type="password", |
|
) |
|
... |
|
|
|
clear = gr.ClearButton([txt_input, chatbot]) |
|
|
|
... |
|
|
|
if __name__ == "__main__": |
|
demo.launch(share=True) |