110 lines
2.9 KiB
Python
110 lines
2.9 KiB
Python
|
|
"""
|
|
Deep Agents contextual memory smoke test.
|
|
|
|
Uses OCI Generative AI through LangChain.
|
|
Does not modify the AgentVoice application.
|
|
"""
|
|
|
|
import asyncio
|
|
import os
|
|
|
|
from deepagents import create_deep_agent
|
|
from langchain_oci import ChatOCIGenAI
|
|
from langchain_core.messages import HumanMessage
|
|
from langgraph.checkpoint.memory import MemorySaver
|
|
|
|
|
|
async def main():
|
|
compartment_id = os.environ.get("OCI_COMPARTMENT_ID")
|
|
|
|
if not compartment_id:
|
|
raise RuntimeError("OCI_COMPARTMENT_ID is required.")
|
|
|
|
model = ChatOCIGenAI(
|
|
model_id="openai.gpt-4o-mini",
|
|
compartment_id=compartment_id,
|
|
service_endpoint=(
|
|
"https://inference.generativeai."
|
|
"sa-vinhedo-1.oci.oraclecloud.com"
|
|
),
|
|
auth_type="INSTANCE_PRINCIPAL",
|
|
is_stream=True,
|
|
model_kwargs={
|
|
"temperature": 0.3,
|
|
"max_tokens": 256,
|
|
},
|
|
)
|
|
|
|
try:
|
|
agent = create_deep_agent(
|
|
model=model,
|
|
tools=[],
|
|
system_prompt=(
|
|
"Você é o AgentVoice, um assistente por voz. "
|
|
"Responda em português brasileiro, de forma "
|
|
"natural, breve e contextual. "
|
|
"Considere as mensagens anteriores da conversa. "
|
|
"Quando o usuário corrigir uma pergunta, "
|
|
"interprete a correção usando o histórico."
|
|
),
|
|
checkpointer=MemorySaver(),
|
|
)
|
|
|
|
config = {
|
|
"configurable": {
|
|
"thread_id": "agentvoice-context-test",
|
|
}
|
|
}
|
|
|
|
questions = [
|
|
(
|
|
"Estou planejando visitar o Japão. "
|
|
"Guarde esse destino no contexto da conversa."
|
|
),
|
|
(
|
|
"Qual foi o país que mencionei anteriormente? "
|
|
"Responda somente com o nome do país."
|
|
),
|
|
]
|
|
|
|
for number, question in enumerate(questions, 1):
|
|
print(f"\n--- Turn {number} ---")
|
|
print("User:", question)
|
|
|
|
result = await agent.ainvoke(
|
|
{
|
|
"messages": [
|
|
HumanMessage(content=question)
|
|
]
|
|
},
|
|
config=config,
|
|
)
|
|
|
|
messages = result.get("messages", [])
|
|
|
|
response = next(
|
|
(
|
|
message.content
|
|
for message in reversed(messages)
|
|
if message.type == "ai"
|
|
and isinstance(message.content, str)
|
|
and message.content.strip()
|
|
),
|
|
None,
|
|
)
|
|
|
|
if not response:
|
|
raise RuntimeError(
|
|
f"Turn {number} returned no AI response."
|
|
)
|
|
|
|
print("Agent:", response)
|
|
|
|
finally:
|
|
await model.aclose()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|