初始化

This commit is contained in:
gouhanke
2026-03-05 18:29:51 +08:00
parent 5073036034
commit 9375dc29ee
5 changed files with 4 additions and 124 deletions

View File

@@ -1,5 +1,3 @@
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.messages import MultiModalMessage, TextMessage
from autogen_core import Image
@@ -7,12 +5,11 @@ from autogen_core.models import ModelFamily
from autogen_ext.models.ollama import OllamaChatCompletionClient
from . import config
from .mcp_tools import load_mcp_tools
class AvatarAgentService:
def __init__(self) -> None:
self._model_client = OllamaChatCompletionClient(
model_client = OllamaChatCompletionClient(
model=config.OLLAMA_MODEL,
model_info={
"vision": True,
@@ -22,34 +19,18 @@ class AvatarAgentService:
"structured_output": True,
},
)
self._agent: AssistantAgent | None = None
self._agent_lock = asyncio.Lock()
async def _create_agent(self) -> AssistantAgent:
tools = await load_mcp_tools()
return AssistantAgent(
self._agent = AssistantAgent(
name="avatar",
model_client=self._model_client,
model_client=model_client,
system_message=config.SYSTEM_MESSAGE,
tools=tools or None,
reflect_on_tool_use=bool(tools),
)
async def _get_agent(self) -> AssistantAgent:
if self._agent is not None:
return self._agent
async with self._agent_lock:
if self._agent is None:
self._agent = await self._create_agent()
return self._agent
async def reply(self, user_text: str, image_b64: str) -> str:
agent = await self._get_agent()
user_image = Image.from_base64(image_b64)
multimodal_task = MultiModalMessage(source="user", content=[user_text, user_image])
ai_response = ""
async for message in agent.run_stream(task=multimodal_task):
async for message in self._agent.run_stream(task=multimodal_task):
if isinstance(message, TextMessage) and message.source == "avatar":
ai_response = message.content