mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-20 20:39:55 +00:00
style: 统一代码风格和格式 docs: 完善函数和方法的文档字符串 refactor(base_model): 移除冗余的表名和表参数生成方法 refactor(constant): 更新返回码注释格式 refactor(router_class): 添加路由处理器的详细文档 refactor(database): 完善数据库连接函数的文档 refactor(security): 添加认证类和方法的详细文档 refactor(validator): 更新验证器函数的文档格式 refactor(serialize): 优化序列化工具类的文档 refactor(response): 完善响应类的文档字符串 refactor(dependencies): 添加依赖函数的详细文档 refactor(initialize): 完善初始化脚本的文档 refactor(plugin): 添加生命周期和中间件注册的文档 refactor(service): 完善服务层方法的文档 refactor(controller): 添加控制器方法的详细文档 refactor(crud): 完善CRUD操作的文档字符串 refactor(schema): 简化模型类并移除冗余字段 refactor(param): 更新查询参数类的注释格式 refactor(template): 优化代码生成模板的格式 refactor(console): 添加控制台输出功能的实现 refactor(util): 完善工具函数的文档字符串
71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Any, AsyncGenerator
|
|
from openai import AsyncOpenAI, OpenAI
|
|
from openai.types.chat.chat_completion import ChatCompletion
|
|
import httpx
|
|
|
|
from app.config.setting import settings
|
|
from app.core.logger import logger
|
|
|
|
|
|
class AIClient:
|
|
"""
|
|
AI客户端类,用于与OpenAI API交互。
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.model = settings.OPENAI_MODEL
|
|
# 创建一个不带冲突参数的httpx客户端
|
|
self.http_client = httpx.AsyncClient(
|
|
timeout=30.0,
|
|
follow_redirects=True
|
|
)
|
|
|
|
# 使用自定义的http客户端
|
|
self.client = AsyncOpenAI(
|
|
api_key=settings.OPENAI_API_KEY,
|
|
base_url=settings.OPENAI_BASE_URL,
|
|
http_client=self.http_client
|
|
)
|
|
|
|
async def process(self, query: str) -> AsyncGenerator[str, None]:
|
|
"""
|
|
处理查询并返回流式响应
|
|
|
|
参数:
|
|
- query (str): 用户查询。
|
|
|
|
返回:
|
|
- AsyncGenerator[str, None]: 流式响应内容。
|
|
"""
|
|
system_prompt = """你是一个有用的AI助手,可以帮助用户回答问题和提供帮助。请用中文回答用户的问题。"""
|
|
|
|
try:
|
|
# 使用 await 调用异步客户端
|
|
response = await self.client.chat.completions.create(
|
|
model=self.model,
|
|
messages=[
|
|
{"role": "system", "content": system_prompt},
|
|
{"role": "user", "content": query}
|
|
],
|
|
stream=True
|
|
)
|
|
|
|
# 流式返回响应
|
|
async for chunk in response:
|
|
if chunk.choices and chunk.choices[0].delta.content:
|
|
yield chunk.choices[0].delta.content
|
|
|
|
except Exception as e:
|
|
logger.error(f"AI处理查询失败: {str(e)}")
|
|
yield f"抱歉,处理您的请求时出现了错误: {str(e)}"
|
|
|
|
async def close(self) -> None:
|
|
"""
|
|
关闭客户端连接
|
|
"""
|
|
if hasattr(self, 'client'):
|
|
await self.client.close()
|
|
if hasattr(self, 'http_client'):
|
|
await self.http_client.aclose() |