124 lines
3.5 KiB
Python
124 lines
3.5 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
智能体模块
|
||
"""
|
||
|
||
# 列举导入模块
|
||
from pathlib import Path
|
||
from typing import List, Optional
|
||
from typing_extensions import is_protocol
|
||
from uuid import uuid4
|
||
|
||
from pydantic_ai import Agent as BaseAgent, AgentRunResult
|
||
from pydantic_ai.builtin_tools import AbstractBuiltinTool
|
||
from pydantic_ai.capabilities import AgentCapability
|
||
from pydantic_ai.models.openai import OpenAIChatModel
|
||
from pydantic_ai.output import OutputSpec
|
||
from pydantic_ai.providers.openai import OpenAIProvider
|
||
from pydantic_core.core_schema import is_subclass_schema
|
||
from starlette.applications import Starlette
|
||
from starlette.routing import Mount, Route
|
||
|
||
from sys import path
|
||
|
||
path.append(Path(__file__).resolve().parent.as_posix())
|
||
from memory import Memory
|
||
|
||
|
||
class Agent:
|
||
"""
|
||
智能体,支持:
|
||
1)实例智能体
|
||
2)异步运行
|
||
"""
|
||
|
||
def __init__(
|
||
self,
|
||
instructions: str,
|
||
output_type: OutputSpec = str,
|
||
capabilities: Optional[List[AgentCapability]] = None,
|
||
):
|
||
"""
|
||
初始化智能体
|
||
:param instructions: 指令
|
||
:param skills: 智能体技能列表,默认为不使用技能
|
||
:param output_type: 输出类型
|
||
:return: 智能体实例
|
||
"""
|
||
# 生成会话唯一标识
|
||
self.session_id = uuid4().hex.lower()
|
||
|
||
# 创建智能体
|
||
self.agent = self._create_agent(
|
||
instructions=instructions,
|
||
capabilities=capabilities,
|
||
output_type=output_type,
|
||
)
|
||
a = self.agent.to_web()
|
||
|
||
# 实例记忆体
|
||
self.memory = Memory()
|
||
|
||
def _create_agent(
|
||
self,
|
||
instructions: str,
|
||
capabilities: Optional[List[AgentCapability]],
|
||
output_type: OutputSpec,
|
||
) -> BaseAgent:
|
||
"""
|
||
创建智能体
|
||
:param instructions: 指令
|
||
:param capabilities: 智能体能力列表
|
||
:param output_type: 输出类型
|
||
:return: Agent 实例
|
||
"""
|
||
agent = BaseAgent(
|
||
model=OpenAIChatModel(
|
||
model_name="deepseek-v4-flash",
|
||
provider=OpenAIProvider(
|
||
base_url="https://tokenhub.tencentmaas.com/v1",
|
||
api_key="sk-D9Y1mCe8VlvNqLuSC4mAjqEwxJ2nW4C0h8a7EPn8kg9RLsHq",
|
||
),
|
||
),
|
||
instructions=instructions,
|
||
capabilities=capabilities,
|
||
output_type=output_type,
|
||
retries=1,
|
||
)
|
||
return agent
|
||
|
||
async def run(self, user_prompt: str | List[str]) -> AgentRunResult:
|
||
"""
|
||
异步运行
|
||
:param user_prompt: 用户提示词
|
||
:return: 智能体回复
|
||
"""
|
||
# 查询会话历史消息
|
||
message_history = self.memory.read(session_id=self.session_id)
|
||
result = await self.agent.run(
|
||
user_prompt=user_prompt, message_history=message_history
|
||
)
|
||
# 记录会话历史消息
|
||
self.memory.create(
|
||
session_id=self.session_id,
|
||
dialogue_message=result.new_messages(),
|
||
)
|
||
return result
|
||
|
||
def create_starlette_application(
|
||
self,
|
||
builtin_tools: Optional[List[AbstractBuiltinTool]] = None,
|
||
) -> Starlette:
|
||
"""
|
||
创建 Starlette 应用,为智能体提供交互式对话界面
|
||
:param models: ModelsParam = None,
|
||
:param builtin_tools: Sequence[AbstractBuiltinTool] | None = None,
|
||
:return: Starlette 实例
|
||
"""
|
||
|
||
|
||
exit()
|
||
|
||
exit()
|
||
return application
|