refactor: simplifica o adapter DeepAgents
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
"""
|
||||
Deep Agents implementation of the AgentVoice Agent port.
|
||||
|
||||
@@ -24,6 +23,7 @@ from contextlib import aclosing
|
||||
|
||||
from deepagents import create_deep_agent
|
||||
from langchain_core.messages import HumanMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
from langchain_oci import ChatOCIGenAI
|
||||
from langgraph.checkpoint.memory import MemorySaver
|
||||
|
||||
@@ -95,12 +95,11 @@ class DeepAgentsAdapter(StreamingAgentAdapter):
|
||||
|
||||
self._stream_event_timeout = float(timeout_seconds)
|
||||
|
||||
self.model = ChatOCIGenAI(
|
||||
self._model = ChatOCIGenAI(
|
||||
model_id=model_id,
|
||||
compartment_id=compartment_id,
|
||||
service_endpoint=(
|
||||
f"https://inference.generativeai."
|
||||
f"{region}.oci.oraclecloud.com"
|
||||
f"https://inference.generativeai.{region}.oci.oraclecloud.com"
|
||||
),
|
||||
auth_type=auth_type,
|
||||
auth_profile=auth_profile,
|
||||
@@ -112,18 +111,19 @@ class DeepAgentsAdapter(StreamingAgentAdapter):
|
||||
},
|
||||
)
|
||||
|
||||
self.checkpointer = MemorySaver()
|
||||
self._checkpointer = MemorySaver()
|
||||
|
||||
self.agent = create_deep_agent(
|
||||
model=self.model,
|
||||
self._graph = create_deep_agent(
|
||||
model=self._model,
|
||||
tools=[],
|
||||
system_prompt=system_prompt,
|
||||
checkpointer=self.checkpointer,
|
||||
checkpointer=self._checkpointer,
|
||||
)
|
||||
|
||||
self._closed = False
|
||||
|
||||
def _config(self, session_id: str) -> dict:
|
||||
@staticmethod
|
||||
def _session_config(session_id: str) -> RunnableConfig:
|
||||
if not session_id:
|
||||
raise ValueError("session_id is required.")
|
||||
|
||||
@@ -133,34 +133,6 @@ class DeepAgentsAdapter(StreamingAgentAdapter):
|
||||
}
|
||||
}
|
||||
|
||||
async def process(
|
||||
self,
|
||||
session_id: str,
|
||||
turn_id: int,
|
||||
text: str,
|
||||
) -> str:
|
||||
parts: list[str] = []
|
||||
|
||||
async with aclosing(
|
||||
self.stream(
|
||||
session_id=session_id,
|
||||
turn_id=turn_id,
|
||||
text=text,
|
||||
)
|
||||
) as chunks:
|
||||
async for chunk in chunks:
|
||||
parts.append(chunk)
|
||||
|
||||
response = "".join(parts).strip()
|
||||
|
||||
if not response:
|
||||
raise RuntimeError(
|
||||
"Deep Agent returned an empty response."
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
async def stream(
|
||||
self,
|
||||
session_id: str,
|
||||
@@ -173,9 +145,9 @@ class DeepAgentsAdapter(StreamingAgentAdapter):
|
||||
if not text.strip():
|
||||
raise ValueError("Agent input text cannot be empty.")
|
||||
|
||||
config = self._config(session_id)
|
||||
config = self._session_config(session_id)
|
||||
|
||||
events = self.agent.astream(
|
||||
events = self._graph.astream(
|
||||
{"messages": [HumanMessage(content=text)]},
|
||||
config=config,
|
||||
stream_mode="messages",
|
||||
@@ -200,10 +172,9 @@ class DeepAgentsAdapter(StreamingAgentAdapter):
|
||||
for fragment in _text_fragments(message.content):
|
||||
yield fragment
|
||||
|
||||
|
||||
async def aclose(self) -> None:
|
||||
if self._closed:
|
||||
return
|
||||
|
||||
await self.model.aclose()
|
||||
await self._model.aclose()
|
||||
self._closed = True
|
||||
|
||||
Reference in New Issue
Block a user