56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
任务模块
|
||
"""
|
||
from typing import Any, AsyncGenerator, Callable, Dict, Optional
|
||
from typing import AsyncGenerator, List
|
||
|
||
from pydantic import BaseModel
|
||
from pydantic_ai import Agent, ModelMessage
|
||
from pydantic_ai.models.openai import OpenAIChatModel
|
||
from pydantic_ai.providers.openai import OpenAIProvider
|
||
|
||
from application.domain_models import Dialog, TaskType
|
||
from models import DEEPSEEK_V4_FLASH_MODEL
|
||
|
||
instruction = """
|
||
# 角色
|
||
专业友好AI助手,结构化解答各类问题。
|
||
|
||
# 输出硬性规则
|
||
1. 全文强制标准Markdown,禁止纯文本;不要额外说明排版格式,直接输出内容;
|
||
2. 层级使用 `#/##/###`,列表用 `-` 无序列表或数字有序列表;
|
||
3. 代码块用 ```语言名``` 包裹;
|
||
4. 重点内容标注 **粗体**/*斜体*;
|
||
5. 思考、工具日志仅输出文本,适配前端折叠面板,禁止输出HTML标签;
|
||
6. 内容分点拆分,排版整洁适配前端Markdown渲染。
|
||
|
||
# 行文要求
|
||
语言通俗,逻辑完整简洁,无多余废话。
|
||
"""
|
||
|
||
|
||
async def run_stream_events(
|
||
task_type: TaskType,
|
||
user_prompt: str,
|
||
message_history: List[ModelMessage],
|
||
) -> AsyncGenerator:
|
||
"""
|
||
以流式事件模式运行
|
||
"""
|
||
match task_type:
|
||
case TaskType.CHAT:
|
||
agent = Agent(
|
||
model=DEEPSEEK_V4_FLASH_MODEL,
|
||
instructions=instruction,
|
||
)
|
||
async with agent.run_stream_events(
|
||
user_prompt=user_prompt,
|
||
message_history=message_history,
|
||
) as events:
|
||
async for event in events:
|
||
yield event
|
||
|
||
case "flight":
|
||
yield "未知任务类型"
|