Skip to content

Commit aefe7d4

Browse files
Add streaming support (#111)
* feat: add streaming response for agent retriever with event handling * fix: fix typo * fix: update endpoint path for agent retriever streaming response --------- Signed-off-by: Palaniappan R <palaniappan.r.mail@gmail.com>
1 parent e2af48f commit aefe7d4

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

backend/src/agents/retriever_graph.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(
7373
llm_model: Union[ChatGoogleGenerativeAI, ChatVertexAI, ChatOllama],
7474
embeddings_config: dict[str, str],
7575
reranking_model_name: str,
76-
inbuit_tool_calling: bool,
76+
inbuilt_tool_calling: bool,
7777
use_cuda: bool = False,
7878
):
7979
self.llm = llm_model
@@ -100,7 +100,7 @@ def __init__(
100100
"retrieve_errinfo",
101101
"retrieve_yosys_rtdocs",
102102
]
103-
self.inbuit_tool_calling = inbuit_tool_calling
103+
self.inbuilt_tool_calling = inbuilt_tool_calling
104104

105105
self.tool_descriptions = ""
106106
for tool in self.tools:
@@ -120,7 +120,7 @@ def agent(self, state: AgentState) -> dict[str, list[str]]:
120120
if self.llm is None:
121121
return {"tools": []}
122122

123-
if self.inbuit_tool_calling:
123+
if self.inbuilt_tool_calling:
124124
model = self.llm.bind_tools(self.tools, tool_choice="any")
125125

126126
tool_choice_chain = (
@@ -195,7 +195,7 @@ def route(self, state: AgentState) -> list[str]:
195195
if tools == []:
196196
return ["retrieve_general"]
197197

198-
if self.inbuit_tool_calling:
198+
if self.inbuilt_tool_calling:
199199
tool_names = [tool["name"] for tool in tools if "name" in tool] # type: ignore
200200
return tool_names
201201
else:

backend/src/api/routers/graphs.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from langchain_google_vertexai import ChatVertexAI
99
from langchain_google_genai import ChatGoogleGenerativeAI
1010
from langchain_ollama import ChatOllama
11+
from langchain_core.messages import AIMessageChunk
12+
13+
from starlette.responses import StreamingResponse
1114

1215
from ...agents.retriever_graph import RetrieverGraph
1316
from ..models.response_model import ChatResponse, UserInput
@@ -76,7 +79,7 @@
7679
embeddings_config=embeddings_config,
7780
reranking_model_name=hf_reranker,
7881
use_cuda=use_cuda,
79-
inbuit_tool_calling=False,
82+
inbuilt_tool_calling=False,
8083
)
8184
rg.initialize()
8285

@@ -142,3 +145,47 @@ async def get_agent_response(user_input: UserInput) -> ChatResponse:
142145
response = {"response": llm_response, "tool": tools}
143146

144147
return ChatResponse(**response)
148+
149+
150+
async def get_response_stream(user_input: UserInput):
151+
user_question = user_input.query
152+
153+
inputs = {
154+
"messages": [
155+
("user", user_question),
156+
],
157+
"chat_history": get_history_str(user_input.chat_history),
158+
}
159+
160+
urls: list[str] = []
161+
current_llm_call_count = 1
162+
163+
if rg.graph is not None:
164+
async for event in rg.graph.astream_events(inputs, version="v2"):
165+
chunk = event["event"]
166+
167+
if chunk == "on_chat_model_end":
168+
current_llm_call_count += 1
169+
170+
if chunk == "on_retriever_start" or chunk == "on_retriever_end":
171+
for document in event.get("data", {}).get("output", {}):
172+
urls.append(document.metadata["url"])
173+
174+
if chunk == "on_chat_model_stream" and current_llm_call_count == 2:
175+
message_content = event.get("data", {}).get("chunk", {})
176+
if isinstance(message_content, AIMessageChunk):
177+
msg = message_content.content
178+
else:
179+
msg = None
180+
181+
yield str(msg) + "\n\n"
182+
183+
urls = list(set(urls))
184+
yield f"Sources: {', '.join(urls)}\n\n"
185+
186+
187+
@router.post("/agent-retriever/stream", response_class=StreamingResponse)
188+
async def get_agent_response_streaming(user_input: UserInput):
189+
return StreamingResponse(
190+
get_response_stream(user_input), media_type="text/event-stream"
191+
)

0 commit comments

Comments
 (0)