| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- // dashboard-v4/dashboard/src/services/modelAdapters/base.ts
- import type { IAiModel } from "../../api/ai";
- import type {
- ModelAdapter,
- OpenAIMessage,
- SendOptions,
- ParsedChunk,
- ToolCall,
- } from "../../types/chat";
- import {
- getArgs,
- type GetTermDefinitionArgs,
- type SearchByPageRefArgs,
- type SearchByQueryArgs,
- type SearchPaliArgs,
- type SearchResponse,
- } from "../../types/search";
- import {
- getTermDefinition,
- searchByPageRef,
- searchByQuery,
- searchPali,
- tools,
- } from "../agentApi";
- export abstract class BaseModelAdapter implements ModelAdapter {
- abstract name: string;
- abstract supportsFunctionCall: boolean;
- abstract model: IAiModel | undefined;
- abstract sendMessage(
- messages: OpenAIMessage[],
- options: SendOptions
- ): Promise<AsyncIterable<string>>;
- abstract parseStreamChunk(chunk: string): ParsedChunk | null;
- public setModel(model: IAiModel) {
- this.model = model;
- }
- protected createStreamController() {
- return {
- addToken: (_token: string) => {
- // 流式输出控制逻辑
- },
- complete: () => {
- // 完成处理逻辑
- },
- };
- }
- protected buildRequestPayload(
- messages: OpenAIMessage[],
- options: SendOptions
- ): SendOptions {
- return {
- model: this.model?.name,
- messages,
- stream: true,
- temperature: options.temperature || 0.7,
- max_tokens: options.max_tokens || 2048,
- top_p: options.top_p || 1,
- tools: tools,
- tool_choice: "auto",
- };
- }
- // ---------------------------------------------------------------- //
- // 核心 Function Calling 处理函数 //
- // ---------------------------------------------------------------- //
- /**
- * 核心函数:根据 AI 助手返回的函数调用对象,执行相应的操作。
- *
- * @param functionCall AI 助手返回的函数调用对象。
- * @returns 返回一个 Promise,包含搜索结果。
- */
- async handleFunctionCall(functionCall: ToolCall): Promise<SearchResponse> {
- console.info("ai chat function call", functionCall);
- switch (functionCall.function.name) {
- case "search_by_query":
- return searchByQuery(
- getArgs<SearchByQueryArgs>(functionCall.function.arguments)
- );
- case "search_by_page_ref":
- return searchByPageRef(
- getArgs<SearchByPageRefArgs>(functionCall.function.arguments)
- );
- case "get_term_definition":
- return getTermDefinition(
- getArgs<GetTermDefinitionArgs>(functionCall.function.arguments)
- );
- case "search_pali":
- return searchPali(
- getArgs<SearchPaliArgs>(functionCall.function.arguments)
- );
- default:
- throw new Error(`Unknown function call: ${functionCall.function.name}`);
- }
- }
- }
|