Spaces:
Sleeping
Sleeping
Commit
·
82c7f91
1
Parent(s):
f3da3e8
Refactor respond function to use structured history format and enhance chatbot UI properties
Browse files- src/main.py +20 -14
src/main.py
CHANGED
|
@@ -26,12 +26,12 @@ def _build_clarification_block(questions: list[str], answers: str) -> str:
|
|
| 26 |
|
| 27 |
|
| 28 |
async def respond(
|
| 29 |
-
message: str, history: list[
|
| 30 |
):
|
| 31 |
if not state:
|
| 32 |
state = {"stage": "awaiting_query", "query": "", "questions": []}
|
| 33 |
|
| 34 |
-
history.append(
|
| 35 |
|
| 36 |
if state["stage"] == "awaiting_query":
|
| 37 |
state["query"] = message
|
|
@@ -42,17 +42,17 @@ async def respond(
|
|
| 42 |
state["stage"] = "awaiting_answers"
|
| 43 |
q_text = _format_clarifications(questions)
|
| 44 |
history.append(
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
f"Please answer the following questions, one per line:\n{q_text}",
|
| 48 |
-
|
| 49 |
)
|
| 50 |
yield history, state
|
| 51 |
else:
|
| 52 |
state["stage"] = "running"
|
| 53 |
-
history.append(
|
| 54 |
async for chunk in manager.run(message, ""):
|
| 55 |
-
history[-1] = (
|
| 56 |
yield history, state
|
| 57 |
state["stage"] = "awaiting_query"
|
| 58 |
yield history, state
|
|
@@ -60,27 +60,33 @@ async def respond(
|
|
| 60 |
answers_block = _build_clarification_block(state["questions"], message)
|
| 61 |
manager = ResearchManager()
|
| 62 |
state["stage"] = "running"
|
| 63 |
-
history.append(
|
| 64 |
async for chunk in manager.run(state["query"], answers_block):
|
| 65 |
-
history[-1] = (
|
| 66 |
yield history, state
|
| 67 |
state["stage"] = "awaiting_query"
|
| 68 |
yield history, state
|
| 69 |
else:
|
| 70 |
-
history.append(
|
| 71 |
yield history, state
|
| 72 |
|
| 73 |
|
| 74 |
def reset():
|
| 75 |
-
return [
|
| 76 |
"stage": "awaiting_query",
|
| 77 |
"query": "",
|
| 78 |
"questions": [],
|
| 79 |
-
}
|
| 80 |
|
| 81 |
|
| 82 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="yellow")) as ui:
|
| 83 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
state = gr.State({})
|
| 85 |
msg = gr.Textbox(placeholder="Type your message and press Enter")
|
| 86 |
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
async def respond(
|
| 29 |
+
message: str, history: list[dict], state: dict
|
| 30 |
):
|
| 31 |
if not state:
|
| 32 |
state = {"stage": "awaiting_query", "query": "", "questions": []}
|
| 33 |
|
| 34 |
+
history.append({"role": "user", "content": message})
|
| 35 |
|
| 36 |
if state["stage"] == "awaiting_query":
|
| 37 |
state["query"] = message
|
|
|
|
| 42 |
state["stage"] = "awaiting_answers"
|
| 43 |
q_text = _format_clarifications(questions)
|
| 44 |
history.append(
|
| 45 |
+
{
|
| 46 |
+
"role": "assistant",
|
| 47 |
+
"content": f"Please answer the following questions, one per line:\n{q_text}",
|
| 48 |
+
}
|
| 49 |
)
|
| 50 |
yield history, state
|
| 51 |
else:
|
| 52 |
state["stage"] = "running"
|
| 53 |
+
history.append({"role": "assistant", "content": ""})
|
| 54 |
async for chunk in manager.run(message, ""):
|
| 55 |
+
history[-1]["content"] = (history[-1]["content"] or "") + chunk
|
| 56 |
yield history, state
|
| 57 |
state["stage"] = "awaiting_query"
|
| 58 |
yield history, state
|
|
|
|
| 60 |
answers_block = _build_clarification_block(state["questions"], message)
|
| 61 |
manager = ResearchManager()
|
| 62 |
state["stage"] = "running"
|
| 63 |
+
history.append({"role": "assistant", "content": ""})
|
| 64 |
async for chunk in manager.run(state["query"], answers_block):
|
| 65 |
+
history[-1]["content"] = (history[-1]["content"] or "") + chunk
|
| 66 |
yield history, state
|
| 67 |
state["stage"] = "awaiting_query"
|
| 68 |
yield history, state
|
| 69 |
else:
|
| 70 |
+
history.append({"role": "assistant", "content": "Please wait for the current task to finish."})
|
| 71 |
yield history, state
|
| 72 |
|
| 73 |
|
| 74 |
def reset():
|
| 75 |
+
return ([{"role": "assistant", "content": WELCOME_MESSAGE}], {
|
| 76 |
"stage": "awaiting_query",
|
| 77 |
"query": "",
|
| 78 |
"questions": [],
|
| 79 |
+
})
|
| 80 |
|
| 81 |
|
| 82 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="yellow")) as ui:
|
| 83 |
+
chatbot = gr.Chatbot(
|
| 84 |
+
label="Deep Research",
|
| 85 |
+
height=500,
|
| 86 |
+
resizable=True,
|
| 87 |
+
show_copy_button=True,
|
| 88 |
+
type="messages",
|
| 89 |
+
)
|
| 90 |
state = gr.State({})
|
| 91 |
msg = gr.Textbox(placeholder="Type your message and press Enter")
|
| 92 |
|