refactor: formaliza contratos de streaming e ciclo de vida dos provedores
This commit is contained in:
@@ -7,16 +7,17 @@ chat inference using the GenericChatRequest format.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
import logging
|
||||
from collections.abc import AsyncIterator
|
||||
from collections.abc import AsyncGenerator
|
||||
from contextlib import aclosing
|
||||
|
||||
import oci
|
||||
|
||||
from voice_assistant.agent.adapter import AgentAdapter
|
||||
from voice_assistant.agent.adapter import StreamingAgentAdapter
|
||||
from voice_assistant.agent.oci_stream import stream_sse_events
|
||||
from voice_assistant.agent.oci_connection import open_oci_stream
|
||||
|
||||
@@ -91,7 +92,7 @@ def extract_text_delta(raw_data: str) -> str | None:
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OCIAgentAdapter(AgentAdapter):
|
||||
class OCIAgentAdapter(StreamingAgentAdapter):
|
||||
"""
|
||||
OCI Generative AI streaming Agent.
|
||||
|
||||
@@ -135,6 +136,7 @@ class OCIAgentAdapter(AgentAdapter):
|
||||
self.temperature = temperature
|
||||
self.max_completion_tokens = max_completion_tokens
|
||||
self.timeout_seconds = timeout_seconds
|
||||
self._closed = False
|
||||
|
||||
signer = (
|
||||
oci.auth.signers
|
||||
@@ -184,7 +186,7 @@ class OCIAgentAdapter(AgentAdapter):
|
||||
session_id: str,
|
||||
turn_id: int,
|
||||
text: str,
|
||||
) -> AsyncIterator[str]:
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""
|
||||
Stream OCI Generative AI text deltas.
|
||||
|
||||
@@ -192,6 +194,9 @@ class OCIAgentAdapter(AgentAdapter):
|
||||
event loop. SSE events are consumed through the
|
||||
asynchronous bridge.
|
||||
"""
|
||||
if self._closed:
|
||||
raise RuntimeError("OCI Agent is closed.")
|
||||
|
||||
if not text.strip():
|
||||
raise ValueError("Agent input text cannot be empty.")
|
||||
|
||||
@@ -255,3 +260,11 @@ class OCIAgentAdapter(AgentAdapter):
|
||||
)
|
||||
|
||||
return response_text
|
||||
|
||||
|
||||
async def aclose(self) -> None:
|
||||
"""Release the OCI HTTP session after turn execution has stopped."""
|
||||
if self._closed:
|
||||
return
|
||||
await asyncio.to_thread(self.client.base_client.session.close)
|
||||
self._closed = True
|
||||
|
||||
Reference in New Issue
Block a user