feat: Agently Gradio Demo with Search and Browse Ability
Browse files- app.py +129 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import json
|
| 3 |
+
import Agently
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
model_dict = {
|
| 7 |
+
"GPT 3.5": {
|
| 8 |
+
"model_serial": "OAIClient",
|
| 9 |
+
"options": { "model": "gpt-3.5-turbo" },
|
| 10 |
+
},
|
| 11 |
+
"GPT 4": {
|
| 12 |
+
"model_serial": "OpenAI",
|
| 13 |
+
"options": { "model": "gpt-4" },
|
| 14 |
+
},
|
| 15 |
+
"Claude 3": {
|
| 16 |
+
"model_serial": "Claude",
|
| 17 |
+
"options": { "model": "claude-3-opus-20240229" },
|
| 18 |
+
},
|
| 19 |
+
"Gemini Pro": {
|
| 20 |
+
"model_serial": "Google",
|
| 21 |
+
"options": {},
|
| 22 |
+
},
|
| 23 |
+
"Baidu Ernie 4.0": {
|
| 24 |
+
"model_serial": "ERNIE",
|
| 25 |
+
"options": { "model": "ernie-4.0" },
|
| 26 |
+
},
|
| 27 |
+
"Zhipu GLM 4": {
|
| 28 |
+
"model_serial": "ZhipuAI",
|
| 29 |
+
"options": { "model": "glm-4" },
|
| 30 |
+
},
|
| 31 |
+
"Kimi": {
|
| 32 |
+
"model_serial": "OAIClient",
|
| 33 |
+
"url": "https://api.moonshot.cn/v1",
|
| 34 |
+
"options": { "model": "moonshot-v1-8k" },
|
| 35 |
+
},
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
async def chat(message, history, model, base_url, auth, agent_id, session_id):
|
| 39 |
+
if not auth or not model:
|
| 40 |
+
yield "Welcome! First time open please add your model settings in \"Additional Inputs\" down below."
|
| 41 |
+
return
|
| 42 |
+
if not session_id:
|
| 43 |
+
session_id = "$AGENTLY_GRADIO_SESSION"
|
| 44 |
+
model_info = model_dict[model].copy()
|
| 45 |
+
model_serial = model_info["model_serial"]
|
| 46 |
+
del model_info["model_serial"]
|
| 47 |
+
agent = (
|
| 48 |
+
Agently.create_agent(agent_id)
|
| 49 |
+
.set_settings("current_model", model_serial)
|
| 50 |
+
.set_settings("is_debug", True)
|
| 51 |
+
)
|
| 52 |
+
for setting_name, setting_value in model_info.items():
|
| 53 |
+
agent.set_settings(f"model.{ model_serial }.{setting_name}", setting_value)
|
| 54 |
+
if model_serial == "ERNIE":
|
| 55 |
+
agent.set_settings(f"model.{ model_serial }.auth", { "aistudio": auth })
|
| 56 |
+
else:
|
| 57 |
+
agent.set_settings(f"model.{ model_serial }.auth", { "api_key": auth })
|
| 58 |
+
if base_url and base_url != "":
|
| 59 |
+
agent.set_settings(f"model.{ model_serial }.url", base_url)
|
| 60 |
+
reply_queue = asyncio.Queue()
|
| 61 |
+
async def wait_to_yield():
|
| 62 |
+
buffer = ""
|
| 63 |
+
yield buffer
|
| 64 |
+
while True:
|
| 65 |
+
reply = await reply_queue.get()
|
| 66 |
+
buffer += reply
|
| 67 |
+
if reply == "$STOP":
|
| 68 |
+
break
|
| 69 |
+
yield buffer
|
| 70 |
+
yield_gen = wait_to_yield()
|
| 71 |
+
asyncio.ensure_future(yield_gen.__anext__())
|
| 72 |
+
async def start_agent():
|
| 73 |
+
nonlocal session_id, message, history
|
| 74 |
+
agent.active_session(session_id)
|
| 75 |
+
if message == "#erase":
|
| 76 |
+
agent.rewrite_chat_history([])
|
| 77 |
+
agent.stop_session()
|
| 78 |
+
await reply_queue.put("已经重置对话")
|
| 79 |
+
await reply_queue.put("$STOP")
|
| 80 |
+
else:
|
| 81 |
+
print(len(history))
|
| 82 |
+
print(session_id)
|
| 83 |
+
if len(history) == 0 and session_id == "$AGENTLY_GRADIO_SESSION":
|
| 84 |
+
agent.rewrite_chat_history([])
|
| 85 |
+
agent.save_session()
|
| 86 |
+
@agent.on_event("delta")
|
| 87 |
+
async def delta_handler(data):
|
| 88 |
+
await reply_queue.put(data)
|
| 89 |
+
try:
|
| 90 |
+
(
|
| 91 |
+
await agent
|
| 92 |
+
.use_public_tools(["search", "browse"])
|
| 93 |
+
.set_tool_proxy("http://127.0.0.1:7890")
|
| 94 |
+
.input(message)
|
| 95 |
+
.start_async()
|
| 96 |
+
)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
await reply_queue.put(f"Error: { str(e) }")
|
| 99 |
+
await reply_queue.put("$STOP")
|
| 100 |
+
agent.stop_session()
|
| 101 |
+
await reply_queue.put("$STOP")
|
| 102 |
+
start_agent_task = asyncio.create_task(start_agent())
|
| 103 |
+
while True:
|
| 104 |
+
try:
|
| 105 |
+
wait_to_yield_task = asyncio.ensure_future(yield_gen.__anext__())
|
| 106 |
+
value = await wait_to_yield_task
|
| 107 |
+
if value == "$STOP":
|
| 108 |
+
return
|
| 109 |
+
else:
|
| 110 |
+
yield value
|
| 111 |
+
except StopAsyncIteration:
|
| 112 |
+
return
|
| 113 |
+
|
| 114 |
+
iface = gr.ChatInterface(
|
| 115 |
+
chat,
|
| 116 |
+
title="Agently Gradio Chat Interface",
|
| 117 |
+
retry_btn=None,
|
| 118 |
+
undo_btn=None,
|
| 119 |
+
clear_btn=None,
|
| 120 |
+
additional_inputs=[
|
| 121 |
+
gr.Radio(model_dict.keys(), value="GPT 3.5", label="Choose Your Model"),
|
| 122 |
+
gr.Textbox(placeholder="Input API Base URL (use default URL leave it empty)", label="Base URL"),
|
| 123 |
+
gr.Textbox(placeholder="Input API-KEY or Access-Token", label="API-KEY"),
|
| 124 |
+
gr.Textbox(value="demo_agent", label="Agent ID"),
|
| 125 |
+
gr.Textbox(placeholder="Input any identity string to save the chat history", label="Session ID"),
|
| 126 |
+
]
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Agently==3.2.2.6
|
| 2 |
+
gradio
|