20260601更新
This commit is contained in:
parent
11cbe59cce
commit
49d3a589ee
|
|
@ -5,24 +5,24 @@
|
|||
|
||||
# 列举导入模块
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
from typing_extensions import is_protocol
|
||||
from uuid import uuid4
|
||||
|
||||
from typing import List, Optional, Union, cast
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from pydantic_ai import Agent as BaseAgent, AgentRunResult
|
||||
from pydantic_ai.models import Model
|
||||
from pydantic_ai.models.openai import OpenAIChatModel
|
||||
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_ai.ui._web.api import ConfigureFrontend, ModelInfo, BuiltinToolInfo
|
||||
from pydantic_ai_skills import SkillsCapability
|
||||
|
||||
from starlette.routing import Route
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
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:
|
||||
|
|
@ -45,9 +45,8 @@ class Agent:
|
|||
:param output_type: 输出类型
|
||||
:return: 智能体实例
|
||||
"""
|
||||
# 引入相应模块
|
||||
from uuid import uuid4
|
||||
from .memory import Memory
|
||||
# 生成会话唯一标识
|
||||
self.session_id = uuid4().hex.lower()
|
||||
|
||||
# 创建智能体
|
||||
self.agent = self._create_agent(
|
||||
|
|
@ -55,9 +54,7 @@ class Agent:
|
|||
capabilities=capabilities,
|
||||
output_type=output_type,
|
||||
)
|
||||
|
||||
# 生成会话唯一标识
|
||||
self.session_id = uuid4().hex.lower()
|
||||
a = self.agent.to_web()
|
||||
|
||||
# 实例记忆体
|
||||
self.memory = Memory()
|
||||
|
|
@ -73,7 +70,7 @@ class Agent:
|
|||
:param instructions: 指令
|
||||
:param capabilities: 智能体能力列表
|
||||
:param output_type: 输出类型
|
||||
:return: 智能体实例
|
||||
:return: Agent 实例
|
||||
"""
|
||||
agent = BaseAgent(
|
||||
model=OpenAIChatModel(
|
||||
|
|
@ -108,45 +105,19 @@ class Agent:
|
|||
)
|
||||
return result
|
||||
|
||||
def start_starlette_app(self) -> None:
|
||||
def create_starlette_application(
|
||||
self,
|
||||
builtin_tools: Optional[List[AbstractBuiltinTool]] = None,
|
||||
) -> Starlette:
|
||||
"""
|
||||
启动 Starlette 应用,为智能体提供网页交互式对话界面
|
||||
:return: None
|
||||
创建 Starlette 应用,为智能体提供交互式对话界面
|
||||
:param models: ModelsParam = None,
|
||||
:param builtin_tools: Sequence[AbstractBuiltinTool] | None = None,
|
||||
:return: Starlette 实例
|
||||
"""
|
||||
self.agent.to_web()
|
||||
|
||||
model = cast(Model, self.agent.model)
|
||||
|
||||
model_infos = [ModelInfo(id=model.model_id, name=model.label, builtin_tools=[])]
|
||||
|
||||
async def options_chat(request: Request) -> Response:
|
||||
"""处理 OPTIONS 请求"""
|
||||
return Response()
|
||||
|
||||
async def configure_frontend(request: Request) -> Response:
|
||||
"""向前端提供模型和技能"""
|
||||
config = ConfigureFrontend(
|
||||
models=[
|
||||
ModelInfo(
|
||||
id=model.model_id,
|
||||
name=model.label,
|
||||
builtin_tools=model.profile.supported_builtin_tools,
|
||||
)
|
||||
],
|
||||
builtin_tools=[],
|
||||
)
|
||||
return JSONResponse(config.model_dump(by_alias=True))
|
||||
|
||||
Starlette(
|
||||
routes=[
|
||||
Route("/chat", options_chat, methods=["OPTIONS"]),
|
||||
Route("/chat", post_chat, methods=["POST"]),
|
||||
Route("/configure", configure_frontend, methods=["GET"]),
|
||||
Route("/health", health, methods=["GET"]),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
a = Agent(instructions="你是一个专业的翻译")
|
||||
exit()
|
||||
|
||||
print(a)
|
||||
exit()
|
||||
return application
|
||||
|
|
|
|||
|
|
@ -9,12 +9,12 @@ import sys
|
|||
from pathlib import Path
|
||||
|
||||
sys.path.append(Path(__file__).parent.parent.as_posix())
|
||||
from utils.agent import BaseAgent
|
||||
from utils.agent import Agent
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 实例智能体
|
||||
agent = BaseAgent(
|
||||
agent = Agent(
|
||||
instructions=(
|
||||
"你是耐烧蚀高分子领域 Wiki 助手。\n\n"
|
||||
"### 【工具规范】\n"
|
||||
|
|
@ -29,6 +29,6 @@ if __name__ == "__main__":
|
|||
"### 【原则】\n"
|
||||
"严禁幻觉,保持专业严谨。"
|
||||
),
|
||||
skill_names=["wiki-helper"],
|
||||
)
|
||||
uvicorn.run(app=agent.start_web_service(), host="127.0.0.1", port=7932)
|
||||
a = agent.create_starlette_application()
|
||||
uvicorn.run(app=a, host="127.0.0.1", port=7932)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue