base.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // dashboard-v4/dashboard/src/services/modelAdapters/base.ts
  2. import type { IAiModel } from "../../api/ai";
  3. import type {
  4. ModelAdapter,
  5. OpenAIMessage,
  6. SendOptions,
  7. ParsedChunk,
  8. ToolCall,
  9. } from "../../types/chat";
  10. import {
  11. getArgs,
  12. type GetTermDefinitionArgs,
  13. type SearchByPageRefArgs,
  14. type SearchByQueryArgs,
  15. type SearchPaliArgs,
  16. type SearchResponse,
  17. } from "../../types/search";
  18. import {
  19. getTermDefinition,
  20. searchByPageRef,
  21. searchByQuery,
  22. searchPali,
  23. tools,
  24. } from "../agentApi";
  25. export abstract class BaseModelAdapter implements ModelAdapter {
  26. abstract name: string;
  27. abstract supportsFunctionCall: boolean;
  28. abstract model: IAiModel | undefined;
  29. abstract sendMessage(
  30. messages: OpenAIMessage[],
  31. options: SendOptions
  32. ): Promise<AsyncIterable<string>>;
  33. abstract parseStreamChunk(chunk: string): ParsedChunk | null;
  34. public setModel(model: IAiModel) {
  35. this.model = model;
  36. }
  37. protected createStreamController() {
  38. return {
  39. addToken: (_token: string) => {
  40. // 流式输出控制逻辑
  41. },
  42. complete: () => {
  43. // 完成处理逻辑
  44. },
  45. };
  46. }
  47. protected buildRequestPayload(
  48. messages: OpenAIMessage[],
  49. options: SendOptions
  50. ): SendOptions {
  51. return {
  52. model: this.model?.name,
  53. messages,
  54. stream: true,
  55. temperature: options.temperature || 0.7,
  56. max_tokens: options.max_tokens || 2048,
  57. top_p: options.top_p || 1,
  58. tools: tools,
  59. tool_choice: "auto",
  60. };
  61. }
  62. // ---------------------------------------------------------------- //
  63. // 核心 Function Calling 处理函数 //
  64. // ---------------------------------------------------------------- //
  65. /**
  66. * 核心函数:根据 AI 助手返回的函数调用对象,执行相应的操作。
  67. *
  68. * @param functionCall AI 助手返回的函数调用对象。
  69. * @returns 返回一个 Promise,包含搜索结果。
  70. */
  71. async handleFunctionCall(functionCall: ToolCall): Promise<SearchResponse> {
  72. console.info("ai chat function call", functionCall);
  73. switch (functionCall.function.name) {
  74. case "search_by_query":
  75. return searchByQuery(
  76. getArgs<SearchByQueryArgs>(functionCall.function.arguments)
  77. );
  78. case "search_by_page_ref":
  79. return searchByPageRef(
  80. getArgs<SearchByPageRefArgs>(functionCall.function.arguments)
  81. );
  82. case "get_term_definition":
  83. return getTermDefinition(
  84. getArgs<GetTermDefinitionArgs>(functionCall.function.arguments)
  85. );
  86. case "search_pali":
  87. return searchPali(
  88. getArgs<SearchPaliArgs>(functionCall.function.arguments)
  89. );
  90. default:
  91. throw new Error(`Unknown function call: ${functionCall.function.name}`);
  92. }
  93. }
  94. }