7ef266e4a0
实现AI问答的function call功能,支持调用系统接口进行查询。 改动内容: 1. 数据库扩展: - 在ai_chat_messages表增加tool_calls和tool_call_id字段 - 创建数据库迁移文件 2. 模型和Schema更新: - AiChatMessage模型增加tool_calls(JSON)和tool_call_id字段 - AiChatMessageSummary schema增加对应字段 3. Function Call实现: - 定义4个可调用函数:query_tower_models、query_lines、query_users、query_system_params - 实现_execute_function处理函数调用并返回格式化结果 - 更新_call_openai_api支持tools参数 4. 消息流程更新: - 重构send_message支持完整的function call流程 - 流程:用户消息 -> AI请求function call -> 执行函数 -> AI基于结果回复 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai>
62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, Integer, JSON, String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from ..core.database import Base
|
|
from .base import utcnow
|
|
|
|
if TYPE_CHECKING:
|
|
from .user import User
|
|
|
|
|
|
class AiChatConversation(Base):
|
|
__tablename__ = "ai_chat_conversations"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
title: Mapped[str] = mapped_column(String(256), default="新对话")
|
|
user_id: Mapped[str] = mapped_column(
|
|
String(36),
|
|
ForeignKey("users.user_id", ondelete="CASCADE"),
|
|
index=True,
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
default=utcnow,
|
|
onupdate=utcnow,
|
|
)
|
|
|
|
user: Mapped[User] = relationship("User", foreign_keys=[user_id], lazy="selectin")
|
|
messages: Mapped[list[AiChatMessage]] = relationship(
|
|
"AiChatMessage",
|
|
back_populates="conversation",
|
|
cascade="all, delete-orphan",
|
|
lazy="select",
|
|
)
|
|
|
|
|
|
class AiChatMessage(Base):
|
|
__tablename__ = "ai_chat_messages"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
conversation_id: Mapped[int] = mapped_column(
|
|
Integer,
|
|
ForeignKey("ai_chat_conversations.id", ondelete="CASCADE"),
|
|
index=True,
|
|
)
|
|
role: Mapped[str] = mapped_column(String(16), index=True)
|
|
content: Mapped[str] = mapped_column(Text())
|
|
tool_calls: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
|
tool_call_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
|
|
|
|
conversation: Mapped[AiChatConversation] = relationship(
|
|
"AiChatConversation",
|
|
back_populates="messages",
|
|
lazy="selectin",
|
|
)
|