primeira versão customizada de VAD- Voice Activity Detection
This commit is contained in:
64
tools/deepagents_adapter_smoke_test.py
Normal file
64
tools/deepagents_adapter_smoke_test.py
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
"""
|
||||
Smoke test for DeepAgentsAdapter streaming and context.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
from contextlib import aclosing
|
||||
|
||||
from voice_assistant.agent.deep_agent import DeepAgentsAdapter
|
||||
|
||||
|
||||
async def main():
|
||||
compartment_id = os.environ.get("OCI_COMPARTMENT_ID")
|
||||
|
||||
if not compartment_id:
|
||||
raise RuntimeError("OCI_COMPARTMENT_ID is required.")
|
||||
|
||||
adapter = DeepAgentsAdapter(
|
||||
region="sa-vinhedo-1",
|
||||
compartment_id=compartment_id,
|
||||
model_id="openai.gpt-4o-mini",
|
||||
temperature=0.3,
|
||||
max_completion_tokens=256,
|
||||
)
|
||||
|
||||
try:
|
||||
questions = [
|
||||
"Estou planejando visitar o Japão. Lembre desse destino.",
|
||||
"Qual país mencionei? Responda somente com o nome.",
|
||||
]
|
||||
|
||||
for turn_id, question in enumerate(questions, 1):
|
||||
print(f"\n--- Turn {turn_id} ---")
|
||||
print("User:", question)
|
||||
|
||||
chunks = []
|
||||
|
||||
async with aclosing(
|
||||
adapter.stream(
|
||||
session_id="agentvoice-adapter-smoke",
|
||||
turn_id=turn_id,
|
||||
text=question,
|
||||
)
|
||||
) as stream:
|
||||
async for chunk in stream:
|
||||
chunks.append(chunk)
|
||||
print(chunk, end="", flush=True)
|
||||
|
||||
response = "".join(chunks).strip()
|
||||
|
||||
if not response:
|
||||
raise RuntimeError(
|
||||
f"Turn {turn_id} returned empty streaming text."
|
||||
)
|
||||
|
||||
print(f"\nChunks: {len(chunks)}")
|
||||
|
||||
finally:
|
||||
await adapter.aclose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
109
tools/deepagents_context_smoke_test.py
Normal file
109
tools/deepagents_context_smoke_test.py
Normal file
@@ -0,0 +1,109 @@
|
||||
|
||||
"""
|
||||
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())
|
||||
109
tools/deepagents_oci_smoke_test.py
Normal file
109
tools/deepagents_oci_smoke_test.py
Normal file
@@ -0,0 +1,109 @@
|
||||
|
||||
"""
|
||||
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())
|
||||
Reference in New Issue
Block a user