From ee660d64a6ba2c1bd835ff6db6ce19b59f0c12e2 Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Tue, 3 Jun 2025 00:22:14 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20rename=20the?= =?UTF-8?q?=20createOpenAICompatibleRuntime=20(#8049)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor model runtime createOpenAICompatibleRuntime * upgrade version and fix tests * fix tests * fix tests --- package.json | 86 +++++++++---------- .../Render/utils/iframeOnReady.test.ts | 2 +- .../Render/utils/pluginSettings.test.ts | 2 +- .../Render/utils/pluginState.test.ts | 2 +- src/libs/model-runtime/BaseAI.ts | 6 +- src/libs/model-runtime/ModelRuntime.ts | 4 +- .../model-runtime/UniformRuntime/index.ts | 4 +- src/libs/model-runtime/ai21/index.ts | 4 +- src/libs/model-runtime/ai360/index.ts | 4 +- src/libs/model-runtime/anthropic/index.ts | 26 +++--- src/libs/model-runtime/azureOpenai/index.ts | 4 +- src/libs/model-runtime/azureai/index.ts | 8 +- src/libs/model-runtime/baichuan/index.ts | 4 +- src/libs/model-runtime/bedrock/index.ts | 8 +- src/libs/model-runtime/cloudflare/index.ts | 4 +- src/libs/model-runtime/cohere/index.ts | 4 +- src/libs/model-runtime/deepseek/index.ts | 4 +- src/libs/model-runtime/fireworksai/index.ts | 4 +- src/libs/model-runtime/giteeai/index.ts | 4 +- src/libs/model-runtime/github/index.ts | 4 +- src/libs/model-runtime/google/index.ts | 12 +-- src/libs/model-runtime/groq/index.ts | 4 +- src/libs/model-runtime/higress/index.ts | 4 +- src/libs/model-runtime/huggingface/index.ts | 4 +- src/libs/model-runtime/hunyuan/index.ts | 4 +- src/libs/model-runtime/index.ts | 2 +- src/libs/model-runtime/infiniai/index.ts | 4 +- src/libs/model-runtime/internlm/index.ts | 16 ++-- src/libs/model-runtime/jina/index.ts | 4 +- src/libs/model-runtime/lmstudio/index.ts | 4 +- src/libs/model-runtime/minimax/index.ts | 4 +- src/libs/model-runtime/mistral/index.ts | 4 +- src/libs/model-runtime/modelscope/index.ts | 5 +- src/libs/model-runtime/moonshot/index.ts | 4 +- src/libs/model-runtime/novita/index.ts | 4 +- src/libs/model-runtime/nvidia/index.ts | 4 +- src/libs/model-runtime/ollama/index.ts | 4 +- src/libs/model-runtime/openai/index.ts | 6 +- src/libs/model-runtime/openrouter/index.ts | 4 +- src/libs/model-runtime/perplexity/index.ts | 4 +- src/libs/model-runtime/ppio/index.ts | 4 +- src/libs/model-runtime/qiniu/index.ts | 4 +- src/libs/model-runtime/qwen/index.ts | 4 +- src/libs/model-runtime/sambanova/index.ts | 4 +- src/libs/model-runtime/search1api/index.ts | 4 +- src/libs/model-runtime/sensenova/index.ts | 4 +- src/libs/model-runtime/siliconcloud/index.ts | 4 +- src/libs/model-runtime/spark/index.ts | 28 +++--- src/libs/model-runtime/stepfun/index.ts | 4 +- src/libs/model-runtime/taichu/index.ts | 4 +- src/libs/model-runtime/tencentcloud/index.ts | 4 +- src/libs/model-runtime/togetherai/index.ts | 4 +- src/libs/model-runtime/types/chat.ts | 2 +- src/libs/model-runtime/upstage/index.ts | 4 +- .../openaiCompatibleFactory/index.test.ts | 14 +-- .../utils/openaiCompatibleFactory/index.ts | 6 +- src/libs/model-runtime/vllm/index.ts | 4 +- src/libs/model-runtime/volcengine/index.ts | 4 +- src/libs/model-runtime/wenxin/index.ts | 4 +- src/libs/model-runtime/xai/index.ts | 9 +- src/libs/model-runtime/xinference/index.ts | 4 +- src/libs/model-runtime/zeroone/index.ts | 4 +- src/libs/model-runtime/zhipu/index.ts | 4 +- src/server/routers/tools/search.test.ts | 6 +- .../actions/__tests__/generateAIChat.test.ts | 6 +- src/store/chat/slices/message/action.test.ts | 3 +- src/store/chat/slices/topic/action.test.ts | 5 +- 67 files changed, 225 insertions(+), 215 deletions(-) diff --git a/package.json b/package.json index d8373b69dd..91841e9472 100644 --- a/package.json +++ b/package.json @@ -123,15 +123,15 @@ "@ant-design/pro-components": "^2.8.7", "@anthropic-ai/sdk": "^0.51.0", "@auth/core": "^0.38.0", - "@aws-sdk/client-bedrock-runtime": "^3.812.0", - "@aws-sdk/client-s3": "^3.812.0", - "@aws-sdk/s3-request-presigner": "^3.812.0", + "@aws-sdk/client-bedrock-runtime": "^3.821.0", + "@aws-sdk/client-s3": "^3.821.0", + "@aws-sdk/s3-request-presigner": "^3.821.0", "@azure-rest/ai-inference": "1.0.0-beta.5", "@azure/core-auth": "^1.9.0", "@cfworker/json-schema": "^4.1.1", - "@clerk/localizations": "^3.16.0", - "@clerk/nextjs": "^6.19.4", - "@clerk/themes": "^2.2.45", + "@clerk/localizations": "^3.16.3", + "@clerk/nextjs": "^6.20.2", + "@clerk/themes": "^2.2.48", "@codesandbox/sandpack-react": "^2.20.0", "@cyntler/react-doc-viewer": "^1.17.0", "@electric-sql/pglite": "0.2.17", @@ -140,7 +140,7 @@ "@huggingface/inference": "^2.8.1", "@icons-pack/react-simple-icons": "9.6.0", "@khmyznikov/pwa-install": "0.3.9", - "@langchain/community": "^0.3.43", + "@langchain/community": "^0.3.45", "@lobechat/electron-client-ipc": "workspace:*", "@lobechat/electron-server-ipc": "workspace:*", "@lobechat/file-loaders": "workspace:*", @@ -148,21 +148,21 @@ "@lobehub/charts": "^2.0.0", "@lobehub/chat-plugin-sdk": "^1.32.4", "@lobehub/chat-plugins-gateway": "^1.9.0", - "@lobehub/icons": "^2.0.0", + "@lobehub/icons": "^2.2.0", "@lobehub/tts": "^2.0.1", - "@lobehub/ui": "^2.1.7", - "@modelcontextprotocol/sdk": "^1.11.4", + "@lobehub/ui": "^2.1.15", + "@modelcontextprotocol/sdk": "^1.12.1", "@neondatabase/serverless": "^1.0.0", - "@next/third-parties": "^15.3.2", + "@next/third-parties": "^15.3.3", "@react-spring/web": "^9.7.5", "@sentry/nextjs": "^7.120.3", "@serwist/next": "^9.0.14", "@t3-oss/env-nextjs": "^0.12.0", - "@tanstack/react-query": "^5.76.1", - "@trpc/client": "^11.1.2", - "@trpc/next": "^11.1.2", - "@trpc/react-query": "^11.1.2", - "@trpc/server": "^11.1.2", + "@tanstack/react-query": "^5.79.0", + "@trpc/client": "^11.2.0", + "@trpc/next": "^11.1.4", + "@trpc/react-query": "^11.2.0", + "@trpc/server": "^11.2.0", "@vercel/analytics": "^1.5.0", "@vercel/edge-config": "^1.4.0", "@vercel/functions": "^2.1.0", @@ -170,7 +170,7 @@ "@xterm/xterm": "^5.5.0", "ahooks": "^3.8.5", "ai": "^3.4.33", - "antd": "^5.25.2", + "antd": "^5.25.4", "antd-style": "^3.7.1", "brotli-wasm": "^3.0.1", "chroma-js": "^3.1.2", @@ -184,7 +184,7 @@ "epub2": "^3.0.2", "fast-deep-equal": "^3.1.3", "file-type": "^20.5.0", - "framer-motion": "^12.12.1", + "framer-motion": "^12.15.0", "gpt-tokenizer": "^2.9.0", "html-to-text": "^9.0.5", "i18next": "^24.2.3", @@ -193,19 +193,19 @@ "idb-keyval": "^6.2.2", "immer": "^10.1.1", "jose": "^5.10.0", - "js-sha256": "^0.11.0", + "js-sha256": "^0.11.1", "jsonl-parse-stringify": "^1.0.3", "keyv": "^4.5.4", - "langchain": "^0.3.26", - "langfuse": "^3.37.2", - "langfuse-core": "^3.37.2", + "langchain": "^0.3.27", + "langfuse": "^3.37.3", + "langfuse-core": "^3.37.3", "lodash-es": "^4.17.21", "lucide-react": "^0.509.0", - "mammoth": "^1.9.0", + "mammoth": "^1.9.1", "mdast-util-to-markdown": "^2.1.2", "modern-screenshot": "^4.6.0", "nanoid": "^5.1.5", - "next": "^15.3.2", + "next": "^15.3.3", "next-auth": "5.0.0-beta.25", "next-mdx-remote": "^5.0.0", "nextjs-toploader": "^3.8.16", @@ -213,8 +213,8 @@ "nuqs": "^2.4.3", "officeparser": "^5.1.1", "oidc-provider": "^8.8.1", - "ollama": "^0.5.15", - "openai": "^4.100.0", + "ollama": "^0.5.16", + "openai": "^4.104.0", "openapi-fetch": "^0.9.8", "partial-json": "^0.1.7", "path-browserify-esm": "^1.0.6", @@ -224,22 +224,22 @@ "pino": "^9.7.0", "plaiceholder": "^3.0.0", "polished": "^4.3.1", - "posthog-js": "^1.245.0", + "posthog-js": "^1.249.0", "pwa-install-handler": "^2.6.2", - "query-string": "^9.1.2", + "query-string": "^9.2.0", "random-words": "^2.0.1", "react": "^19.1.0", "react-confetti": "^6.4.0", "react-dom": "^19.1.0", "react-fast-marquee": "^1.6.5", "react-hotkeys-hook": "^4.6.2", - "react-i18next": "^15.5.1", + "react-i18next": "^15.5.2", "react-layout-kit": "^1.9.1", "react-lazy-load": "^4.0.1", "react-pdf": "^9.2.1", "react-rnd": "^10.5.2", "react-scan": "^0.3.4", - "react-virtuoso": "^4.12.7", + "react-virtuoso": "^4.12.8", "react-wrap-balancer": "^1.1.1", "remark": "^15.0.1", "remark-gfm": "^4.0.1", @@ -252,7 +252,7 @@ "shiki": "^3.4.2", "stripe": "^16.12.0", "superjson": "^2.2.2", - "svix": "^1.65.0", + "svix": "^1.66.0", "swr": "^2.3.3", "systemjs": "^6.15.1", "tokenx": "^0.4.1", @@ -268,7 +268,7 @@ "y-webrtc": "^10.3.0", "yaml": "^2.8.0", "yjs": "^13.6.27", - "zod": "^3.25.7", + "zod": "^3.25.48", "zustand": "5.0.4", "zustand-utils": "^2.1.0" }, @@ -276,11 +276,11 @@ "@commitlint/cli": "^19.8.1", "@edge-runtime/vm": "^5.0.0", "@huggingface/tasks": "^0.15.9", - "@lobehub/i18n-cli": "^1.20.3", - "@lobehub/lint": "^1.26.1", - "@lobehub/seo-cli": "^1.4.3", - "@next/bundle-analyzer": "^15.3.2", - "@next/eslint-plugin-next": "^15.3.2", + "@lobehub/i18n-cli": "^1.22.0", + "@lobehub/lint": "^1.26.2", + "@lobehub/seo-cli": "^1.6.0", + "@next/bundle-analyzer": "^15.3.3", + "@next/eslint-plugin-next": "^15.3.3", "@peculiar/webcrypto": "^1.5.0", "@semantic-release/exec": "^6.0.3", "@testing-library/jest-dom": "^6.6.3", @@ -293,13 +293,13 @@ "@types/fs-extra": "^11.0.4", "@types/ip": "^1.1.3", "@types/json-schema": "^7.0.15", - "@types/lodash": "^4.17.16", + "@types/lodash": "^4.17.17", "@types/lodash-es": "^4.17.12", - "@types/node": "^22.15.19", + "@types/node": "^22.15.29", "@types/numeral": "^2.0.5", "@types/oidc-provider": "^8.8.1", - "@types/pg": "^8.15.2", - "@types/react": "^19.1.4", + "@types/pg": "^8.15.4", + "@types/react": "^19.1.6", "@types/react-dom": "^19.1.5", "@types/rtl-detect": "^1.0.3", "@types/semver": "^7.7.0", @@ -325,7 +325,7 @@ "fs-extra": "^11.3.0", "glob": "^11.0.2", "gray-matter": "^4.0.3", - "happy-dom": "^17.4.7", + "happy-dom": "^17.5.6", "husky": "^9.1.7", "just-diff": "^6.0.2", "lint-staged": "^15.5.2", @@ -351,7 +351,7 @@ "unified": "^11.0.5", "unist-util-visit": "^5.0.0", "vite": "^5.4.19", - "vitest": "^3.1.4", + "vitest": "^3.2.0", "vitest-canvas-mock": "^0.3.3" }, "packageManager": "pnpm@10.10.0", diff --git a/src/features/PluginsUI/Render/utils/iframeOnReady.test.ts b/src/features/PluginsUI/Render/utils/iframeOnReady.test.ts index b294e9be3a..0f34bc840b 100644 --- a/src/features/PluginsUI/Render/utils/iframeOnReady.test.ts +++ b/src/features/PluginsUI/Render/utils/iframeOnReady.test.ts @@ -9,7 +9,7 @@ describe('useOnPluginReadyForInteraction', () => { afterEach(() => { mockOnReady.mockReset(); - window.removeEventListener('message', expect.any(Function)); + window.removeEventListener('message', () => {}); }); it('sets readyForRender to true when a PluginChannel.pluginReadyForRender message is received', async () => { diff --git a/src/features/PluginsUI/Render/utils/pluginSettings.test.ts b/src/features/PluginsUI/Render/utils/pluginSettings.test.ts index 18f761e13c..2385dbded0 100644 --- a/src/features/PluginsUI/Render/utils/pluginSettings.test.ts +++ b/src/features/PluginsUI/Render/utils/pluginSettings.test.ts @@ -9,7 +9,7 @@ describe('useOnPluginSettingsUpdate', () => { afterEach(() => { mockCallback.mockReset(); - window.removeEventListener('message', expect.any(Function)); + window.removeEventListener('message', () => {}); }); it('calls the callback when a PluginChannel updatePluginSettings message is received', () => { diff --git a/src/features/PluginsUI/Render/utils/pluginState.test.ts b/src/features/PluginsUI/Render/utils/pluginState.test.ts index 8fc9cdae05..a8e645c8b2 100644 --- a/src/features/PluginsUI/Render/utils/pluginState.test.ts +++ b/src/features/PluginsUI/Render/utils/pluginState.test.ts @@ -12,7 +12,7 @@ describe('useOnPluginStateUpdate', () => { // Reset the mock callback after each test mockCallback.mockReset(); // Ensure no event listeners are left hanging after each test - window.removeEventListener('message', expect.any(Function)); + window.removeEventListener('message', () => {}); }); it('calls the callback when a PluginChannel update message is received', () => { diff --git a/src/libs/model-runtime/BaseAI.ts b/src/libs/model-runtime/BaseAI.ts index b3fc18e3c2..d821be6632 100644 --- a/src/libs/model-runtime/BaseAI.ts +++ b/src/libs/model-runtime/BaseAI.ts @@ -3,7 +3,7 @@ import OpenAI from 'openai'; import { ChatModelCard } from '@/types/llm'; import { - ChatCompetitionOptions, + ChatMethodOptions, ChatStreamPayload, Embeddings, EmbeddingsOptions, @@ -18,7 +18,7 @@ import { /* eslint-disable sort-keys-fix/sort-keys-fix , typescript-sort-keys/interface */ export interface LobeRuntimeAI { baseURL?: string; - chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions): Promise; + chat(payload: ChatStreamPayload, options?: ChatMethodOptions): Promise; embeddings?(payload: EmbeddingsPayload, options?: EmbeddingsOptions): Promise; @@ -40,7 +40,7 @@ export abstract class LobeOpenAICompatibleRuntime { abstract baseURL: string; abstract client: OpenAI; - abstract chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions): Promise; + abstract chat(payload: ChatStreamPayload, options?: ChatMethodOptions): Promise; abstract models(): Promise; diff --git a/src/libs/model-runtime/ModelRuntime.ts b/src/libs/model-runtime/ModelRuntime.ts index 601ed045fc..388e5c72ea 100644 --- a/src/libs/model-runtime/ModelRuntime.ts +++ b/src/libs/model-runtime/ModelRuntime.ts @@ -8,7 +8,7 @@ import { LobeCloudflareParams } from './cloudflare'; import { LobeOpenAI } from './openai'; import { providerRuntimeMap } from './runtimeMap'; import { - ChatCompetitionOptions, + ChatMethodOptions, ChatStreamPayload, EmbeddingsOptions, EmbeddingsPayload, @@ -60,7 +60,7 @@ class ModelRuntime { * })); * ``` */ - async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions) { + async chat(payload: ChatStreamPayload, options?: ChatMethodOptions) { return this._runtime.chat(payload, options); } diff --git a/src/libs/model-runtime/UniformRuntime/index.ts b/src/libs/model-runtime/UniformRuntime/index.ts index 0698df6b58..558dd0fe2b 100644 --- a/src/libs/model-runtime/UniformRuntime/index.ts +++ b/src/libs/model-runtime/UniformRuntime/index.ts @@ -2,8 +2,8 @@ import { LobeRuntimeAI } from '../BaseAI'; import { LobeOpenAI } from '../openai'; import { providerRuntimeMap } from '../runtimeMap'; import { - ChatCompetitionOptions, type ChatCompletionErrorPayload, + ChatMethodOptions, ChatStreamPayload, EmbeddingsOptions, EmbeddingsPayload, @@ -70,7 +70,7 @@ class UniformRuntime { return runtimeItem.runtime; } - async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions) { + async chat(payload: ChatStreamPayload, options?: ChatMethodOptions) { try { const runtime = this.getRuntimeByModel(payload.model); diff --git a/src/libs/model-runtime/ai21/index.ts b/src/libs/model-runtime/ai21/index.ts index 67089341b7..842dd8fb45 100644 --- a/src/libs/model-runtime/ai21/index.ts +++ b/src/libs/model-runtime/ai21/index.ts @@ -1,7 +1,7 @@ import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; -export const LobeAi21AI = LobeOpenAICompatibleFactory({ +export const LobeAi21AI = createOpenAICompatibleRuntime({ baseURL: 'https://api.ai21.com/studio/v1', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/ai360/index.ts b/src/libs/model-runtime/ai360/index.ts index 8ddcf0e634..f1341d562b 100644 --- a/src/libs/model-runtime/ai360/index.ts +++ b/src/libs/model-runtime/ai360/index.ts @@ -1,7 +1,7 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface Ai360ModelCard { id: string; @@ -9,7 +9,7 @@ export interface Ai360ModelCard { total_tokens: number; } -export const LobeAi360AI = LobeOpenAICompatibleFactory({ +export const LobeAi360AI = createOpenAICompatibleRuntime({ baseURL: 'https://api.360.cn/v1', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/anthropic/index.ts b/src/libs/model-runtime/anthropic/index.ts index 95567b2eeb..28aa43b920 100644 --- a/src/libs/model-runtime/anthropic/index.ts +++ b/src/libs/model-runtime/anthropic/index.ts @@ -5,8 +5,8 @@ import type { ChatModelCard } from '@/types/llm'; import { LobeRuntimeAI } from '../BaseAI'; import { AgentRuntimeErrorType } from '../error'; import { - ChatCompetitionOptions, type ChatCompletionErrorPayload, + ChatMethodOptions, ChatStreamPayload, ModelProvider, } from '../types'; @@ -52,15 +52,15 @@ export class LobeAnthropicAI implements LobeRuntimeAI { this.client = new Anthropic({ apiKey, baseURL, - ...(betaHeaders ? { defaultHeaders: { "anthropic-beta": betaHeaders } } : {}), - ...res + ...(betaHeaders ? { defaultHeaders: { 'anthropic-beta': betaHeaders } } : {}), + ...res, }); this.baseURL = this.client.baseURL; this.apiKey = apiKey; this.id = id || ModelProvider.Anthropic; } - async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions) { + async chat(payload: ChatStreamPayload, options?: ChatMethodOptions) { try { const anthropicPayload = await this.buildAnthropicPayload(payload); const inputStartAt = Date.now(); @@ -112,7 +112,7 @@ export class LobeAnthropicAI implements LobeRuntimeAI { } = payload; const { default: anthropicModels } = await import('@/config/aiModels/anthropic'); - const modelConfig = anthropicModels.find(m => m.id === model); + const modelConfig = anthropicModels.find((m) => m.id === model); const defaultMaxOutput = modelConfig?.maxOutput; // 配置优先级:用户设置 > 模型配置 > 硬编码默认值 @@ -137,7 +137,9 @@ export class LobeAnthropicAI implements LobeRuntimeAI { const postMessages = await buildAnthropicMessages(user_messages, { enabledContextCaching }); - let postTools: anthropicTools[] | undefined = buildAnthropicTools(tools, { enabledContextCaching }); + let postTools: anthropicTools[] | undefined = buildAnthropicTools(tools, { + enabledContextCaching, + }); if (enabledSearch) { // Limit the number of searches per request @@ -146,9 +148,11 @@ export class LobeAnthropicAI implements LobeRuntimeAI { const webSearchTool: Anthropic.WebSearchTool20250305 = { name: 'web_search', type: 'web_search_20250305', - ...(maxUses && Number.isInteger(Number(maxUses)) && Number(maxUses) > 0 && { - max_uses: Number(maxUses) - }), + ...(maxUses && + Number.isInteger(Number(maxUses)) && + Number(maxUses) > 0 && { + max_uses: Number(maxUses), + }), }; // 如果已有工具,则添加到现有工具列表中;否则创建新的工具列表 @@ -171,8 +175,8 @@ export class LobeAnthropicAI implements LobeRuntimeAI { system: systemPrompts, thinking: { ...thinking, - budget_tokens: thinking?.budget_tokens - ? Math.min(thinking.budget_tokens, maxTokens - 1) // `max_tokens` must be greater than `thinking.budget_tokens`. + budget_tokens: thinking?.budget_tokens + ? Math.min(thinking.budget_tokens, maxTokens - 1) // `max_tokens` must be greater than `thinking.budget_tokens`. : 1024, }, tools: postTools, diff --git a/src/libs/model-runtime/azureOpenai/index.ts b/src/libs/model-runtime/azureOpenai/index.ts index 149302fd55..38a6af278d 100644 --- a/src/libs/model-runtime/azureOpenai/index.ts +++ b/src/libs/model-runtime/azureOpenai/index.ts @@ -5,7 +5,7 @@ import { systemToUserModels } from '@/const/models'; import { LobeRuntimeAI } from '../BaseAI'; import { AgentRuntimeErrorType } from '../error'; -import { ChatCompetitionOptions, ChatStreamPayload, ModelProvider } from '../types'; +import { ChatMethodOptions, ChatStreamPayload, ModelProvider } from '../types'; import { AgentRuntimeError } from '../utils/createError'; import { debugStream } from '../utils/debugStream'; import { transformResponseToStream } from '../utils/openaiCompatibleFactory'; @@ -32,7 +32,7 @@ export class LobeAzureOpenAI implements LobeRuntimeAI { baseURL: string; - async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions) { + async chat(payload: ChatStreamPayload, options?: ChatMethodOptions) { const { messages, model, ...params } = payload; // o1 series models on Azure OpenAI does not support streaming currently const enableStreaming = model.includes('o1') ? false : (params.stream ?? true); diff --git a/src/libs/model-runtime/azureai/index.ts b/src/libs/model-runtime/azureai/index.ts index b7c4accf22..ca6c44fff5 100644 --- a/src/libs/model-runtime/azureai/index.ts +++ b/src/libs/model-runtime/azureai/index.ts @@ -6,7 +6,7 @@ import { systemToUserModels } from '@/const/models'; import { LobeRuntimeAI } from '../BaseAI'; import { AgentRuntimeErrorType } from '../error'; -import { ChatCompetitionOptions, ChatStreamPayload, ModelProvider } from '../types'; +import { ChatMethodOptions, ChatStreamPayload, ModelProvider } from '../types'; import { AgentRuntimeError } from '../utils/createError'; import { debugStream } from '../utils/debugStream'; import { transformResponseToStream } from '../utils/openaiCompatibleFactory'; @@ -33,7 +33,7 @@ export class LobeAzureAI implements LobeRuntimeAI { baseURL: string; - async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions) { + async chat(payload: ChatStreamPayload, options?: ChatMethodOptions) { const { messages, model, temperature, top_p, ...params } = payload; // o1 series models on Azure OpenAI does not support streaming currently const enableStreaming = model.includes('o1') ? false : (params.stream ?? true); @@ -56,9 +56,9 @@ export class LobeAzureAI implements LobeRuntimeAI { model, ...params, stream: enableStreaming, - temperature: (model.includes('o3') || model.includes('o4')) ? undefined : temperature, + temperature: model.includes('o3') || model.includes('o4') ? undefined : temperature, tool_choice: params.tools ? 'auto' : undefined, - top_p: (model.includes('o3') || model.includes('o4')) ? undefined : top_p, + top_p: model.includes('o3') || model.includes('o4') ? undefined : top_p, }, }); diff --git a/src/libs/model-runtime/baichuan/index.ts b/src/libs/model-runtime/baichuan/index.ts index 9ae5e40e28..71eb70f34e 100644 --- a/src/libs/model-runtime/baichuan/index.ts +++ b/src/libs/model-runtime/baichuan/index.ts @@ -1,7 +1,7 @@ import type { ChatModelCard } from '@/types/llm'; import { ChatStreamPayload, ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface BaichuanModelCard { function_call: boolean; @@ -11,7 +11,7 @@ export interface BaichuanModelCard { model_show_name: string; } -export const LobeBaichuanAI = LobeOpenAICompatibleFactory({ +export const LobeBaichuanAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.baichuan-ai.com/v1', chatCompletion: { handlePayload: (payload: ChatStreamPayload) => { diff --git a/src/libs/model-runtime/bedrock/index.ts b/src/libs/model-runtime/bedrock/index.ts index 2e8ddeec02..b72c217271 100644 --- a/src/libs/model-runtime/bedrock/index.ts +++ b/src/libs/model-runtime/bedrock/index.ts @@ -8,7 +8,7 @@ import { experimental_buildLlama2Prompt } from 'ai/prompts'; import { LobeRuntimeAI } from '../BaseAI'; import { AgentRuntimeErrorType } from '../error'; import { - ChatCompetitionOptions, + ChatMethodOptions, ChatStreamPayload, Embeddings, EmbeddingsOptions, @@ -51,7 +51,7 @@ export class LobeBedrockAI implements LobeRuntimeAI { }); } - async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions) { + async chat(payload: ChatStreamPayload, options?: ChatMethodOptions) { if (payload.model.startsWith('meta')) return this.invokeLlamaModel(payload, options); return this.invokeClaudeModel(payload, options); @@ -114,7 +114,7 @@ export class LobeBedrockAI implements LobeRuntimeAI { private invokeClaudeModel = async ( payload: ChatStreamPayload, - options?: ChatCompetitionOptions, + options?: ChatMethodOptions, ): Promise => { const { max_tokens, messages, model, temperature, top_p, tools } = payload; const system_message = messages.find((m) => m.role === 'system'); @@ -169,7 +169,7 @@ export class LobeBedrockAI implements LobeRuntimeAI { private invokeLlamaModel = async ( payload: ChatStreamPayload, - options?: ChatCompetitionOptions, + options?: ChatMethodOptions, ): Promise => { const { max_tokens, messages, model } = payload; const command = new InvokeModelWithResponseStreamCommand({ diff --git a/src/libs/model-runtime/cloudflare/index.ts b/src/libs/model-runtime/cloudflare/index.ts index 3ce04fe685..c8ae4f96ee 100644 --- a/src/libs/model-runtime/cloudflare/index.ts +++ b/src/libs/model-runtime/cloudflare/index.ts @@ -2,7 +2,7 @@ import { ChatModelCard } from '@/types/llm'; import { LobeRuntimeAI } from '../BaseAI'; import { AgentRuntimeErrorType } from '../error'; -import { ChatCompetitionOptions, ChatStreamPayload, ModelProvider } from '../types'; +import { ChatMethodOptions, ChatStreamPayload, ModelProvider } from '../types'; import { CloudflareStreamTransformer, DEFAULT_BASE_URL_PREFIX, @@ -54,7 +54,7 @@ export class LobeCloudflareAI implements LobeRuntimeAI { this.apiKey = apiKey; } - async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions): Promise { + async chat(payload: ChatStreamPayload, options?: ChatMethodOptions): Promise { try { const { model, tools, ...restPayload } = payload; const functions = tools?.map((tool) => tool.function); diff --git a/src/libs/model-runtime/cohere/index.ts b/src/libs/model-runtime/cohere/index.ts index d96c4e002d..409df5dc53 100644 --- a/src/libs/model-runtime/cohere/index.ts +++ b/src/libs/model-runtime/cohere/index.ts @@ -1,7 +1,7 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface CohereModelCard { context_length: number; @@ -10,7 +10,7 @@ export interface CohereModelCard { supports_vision: boolean; } -export const LobeCohereAI = LobeOpenAICompatibleFactory({ +export const LobeCohereAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.cohere.ai/compatibility/v1', chatCompletion: { // https://docs.cohere.com/v2/docs/compatibility-api#unsupported-parameters diff --git a/src/libs/model-runtime/deepseek/index.ts b/src/libs/model-runtime/deepseek/index.ts index cb68e8af01..b87281df37 100644 --- a/src/libs/model-runtime/deepseek/index.ts +++ b/src/libs/model-runtime/deepseek/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface DeepSeekModelCard { id: string; } -export const LobeDeepSeekAI = LobeOpenAICompatibleFactory({ +export const LobeDeepSeekAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.deepseek.com/v1', debug: { chatCompletion: () => process.env.DEBUG_DEEPSEEK_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/fireworksai/index.ts b/src/libs/model-runtime/fireworksai/index.ts index bdb764c3e7..02041ef2f3 100644 --- a/src/libs/model-runtime/fireworksai/index.ts +++ b/src/libs/model-runtime/fireworksai/index.ts @@ -1,7 +1,7 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface FireworksAIModelCard { context_length: number; @@ -10,7 +10,7 @@ export interface FireworksAIModelCard { supports_tools: boolean; } -export const LobeFireworksAI = LobeOpenAICompatibleFactory({ +export const LobeFireworksAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.fireworks.ai/inference/v1', debug: { chatCompletion: () => process.env.DEBUG_FIREWORKSAI_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/giteeai/index.ts b/src/libs/model-runtime/giteeai/index.ts index 19cffdea03..b0b6162e8c 100644 --- a/src/libs/model-runtime/giteeai/index.ts +++ b/src/libs/model-runtime/giteeai/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface GiteeAIModelCard { id: string; } -export const LobeGiteeAI = LobeOpenAICompatibleFactory({ +export const LobeGiteeAI = createOpenAICompatibleRuntime({ baseURL: 'https://ai.gitee.com/v1', debug: { chatCompletion: () => process.env.DEBUG_GITEE_AI_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/github/index.ts b/src/libs/model-runtime/github/index.ts index 3ee49f5af0..6719a6259b 100644 --- a/src/libs/model-runtime/github/index.ts +++ b/src/libs/model-runtime/github/index.ts @@ -2,7 +2,7 @@ import type { ChatModelCard } from '@/types/llm'; import { AgentRuntimeErrorType } from '../error'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; import { pruneReasoningPayload } from '../utils/openaiHelpers'; export interface GithubModelCard { @@ -16,7 +16,7 @@ export interface GithubModelCard { /* eslint-enable typescript-sort-keys/interface */ -export const LobeGithubAI = LobeOpenAICompatibleFactory({ +export const LobeGithubAI = createOpenAICompatibleRuntime({ baseURL: 'https://models.github.ai/inference', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/google/index.ts b/src/libs/model-runtime/google/index.ts index 817e31bea4..aae1b60b92 100644 --- a/src/libs/model-runtime/google/index.ts +++ b/src/libs/model-runtime/google/index.ts @@ -17,8 +17,8 @@ import { safeParseJSON } from '@/utils/safeParseJSON'; import { LobeRuntimeAI } from '../BaseAI'; import { AgentRuntimeErrorType, ILobeAgentRuntimeErrorType } from '../error'; import { - ChatCompetitionOptions, ChatCompletionTool, + ChatMethodOptions, ChatStreamPayload, OpenAIChatMessage, UserMessageContentPart, @@ -111,15 +111,15 @@ export class LobeGoogleAI implements LobeRuntimeAI { this.provider = id || (isVertexAi ? 'vertexai' : 'google'); } - async chat(rawPayload: ChatStreamPayload, options?: ChatCompetitionOptions) { + async chat(rawPayload: ChatStreamPayload, options?: ChatMethodOptions) { try { const payload = this.buildPayload(rawPayload); const { model, thinking } = payload; const thinkingConfig: GoogleAIThinkingConfig = { includeThoughts: - (thinking?.type === 'enabled') || - (!thinking && model && (model.includes('-2.5-') || model.includes('thinking'))) + thinking?.type === 'enabled' || + (!thinking && model && (model.includes('-2.5-') || model.includes('thinking'))) ? true : undefined, thinkingBudget: @@ -142,7 +142,9 @@ export class LobeGoogleAI implements LobeRuntimeAI { response_modalities: modelsWithModalities.has(model) ? ['Text', 'Image'] : undefined, temperature: payload.temperature, topP: payload.top_p, - ...(modelsDisableInstuction.has(model) || model.toLowerCase().includes('learnlm') ? {} : { thinkingConfig }), + ...(modelsDisableInstuction.has(model) || model.toLowerCase().includes('learnlm') + ? {} + : { thinkingConfig }), }, model, // avoid wide sensitive words diff --git a/src/libs/model-runtime/groq/index.ts b/src/libs/model-runtime/groq/index.ts index 883519f009..3f21b6d3e9 100644 --- a/src/libs/model-runtime/groq/index.ts +++ b/src/libs/model-runtime/groq/index.ts @@ -2,14 +2,14 @@ import type { ChatModelCard } from '@/types/llm'; import { AgentRuntimeErrorType } from '../error'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface GroqModelCard { context_window: number; id: string; } -export const LobeGroq = LobeOpenAICompatibleFactory({ +export const LobeGroq = createOpenAICompatibleRuntime({ baseURL: 'https://api.groq.com/openai/v1', chatCompletion: { handleError: (error) => { diff --git a/src/libs/model-runtime/higress/index.ts b/src/libs/model-runtime/higress/index.ts index 53678be304..af2dc73f5f 100644 --- a/src/libs/model-runtime/higress/index.ts +++ b/src/libs/model-runtime/higress/index.ts @@ -3,7 +3,7 @@ import { uniqueId } from 'lodash-es'; import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface HigressModelCard { context_length: number; @@ -15,7 +15,7 @@ export interface HigressModelCard { }; } -export const LobeHigressAI = LobeOpenAICompatibleFactory({ +export const LobeHigressAI = createOpenAICompatibleRuntime({ constructorOptions: { defaultHeaders: { 'HTTP-Referer': 'https://chat-preview.lobehub.com', diff --git a/src/libs/model-runtime/huggingface/index.ts b/src/libs/model-runtime/huggingface/index.ts index 51e1226227..dc0254d3f3 100644 --- a/src/libs/model-runtime/huggingface/index.ts +++ b/src/libs/model-runtime/huggingface/index.ts @@ -5,7 +5,7 @@ import type { ChatModelCard } from '@/types/llm'; import { AgentRuntimeErrorType } from '../error'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; import { convertIterableToStream } from '../utils/streams'; export interface HuggingFaceModelCard { @@ -13,7 +13,7 @@ export interface HuggingFaceModelCard { tags: string[]; } -export const LobeHuggingFaceAI = LobeOpenAICompatibleFactory({ +export const LobeHuggingFaceAI = createOpenAICompatibleRuntime({ chatCompletion: { handleStreamBizErrorType: (error) => { // e.g.: Server meta-llama/Meta-Llama-3.1-8B-Instruct does not seem to support chat completion. Error: Model requires a Pro subscription; check out hf.co/pricing to learn more. Make sure to include your HF token in your query. diff --git a/src/libs/model-runtime/hunyuan/index.ts b/src/libs/model-runtime/hunyuan/index.ts index 168272fd4c..7d14b44d8b 100644 --- a/src/libs/model-runtime/hunyuan/index.ts +++ b/src/libs/model-runtime/hunyuan/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface HunyuanModelCard { id: string; } -export const LobeHunyuanAI = LobeOpenAICompatibleFactory({ +export const LobeHunyuanAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.hunyuan.cloud.tencent.com/v1', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/index.ts b/src/libs/model-runtime/index.ts index e167d9bbcd..3955af53ba 100644 --- a/src/libs/model-runtime/index.ts +++ b/src/libs/model-runtime/index.ts @@ -20,7 +20,7 @@ export { LobeQwenAI } from './qwen'; export { LobeTogetherAI } from './togetherai'; export * from './types'; export { AgentRuntimeError } from './utils/createError'; -export { LobeOpenAICompatibleFactory } from './utils/openaiCompatibleFactory'; +export { createOpenAICompatibleRuntime } from './utils/openaiCompatibleFactory'; export { pruneReasoningPayload } from './utils/openaiHelpers'; export { LobeVolcengineAI } from './volcengine'; export { LobeZeroOneAI } from './zeroone'; diff --git a/src/libs/model-runtime/infiniai/index.ts b/src/libs/model-runtime/infiniai/index.ts index 11ad73749d..90ece2a97d 100644 --- a/src/libs/model-runtime/infiniai/index.ts +++ b/src/libs/model-runtime/infiniai/index.ts @@ -2,13 +2,13 @@ import type { ChatModelCard } from '@/types/llm'; import { AgentRuntimeErrorType } from '../error'; import { ChatCompletionErrorPayload, ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface InfiniAIModelCard { id: string; } -export const LobeInfiniAI = LobeOpenAICompatibleFactory({ +export const LobeInfiniAI = createOpenAICompatibleRuntime({ baseURL: 'https://cloud.infini-ai.com/maas/v1', chatCompletion: { handleError(error): Omit | undefined { diff --git a/src/libs/model-runtime/internlm/index.ts b/src/libs/model-runtime/internlm/index.ts index 67f6f8bc12..7a3894a8f6 100644 --- a/src/libs/model-runtime/internlm/index.ts +++ b/src/libs/model-runtime/internlm/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface InternLMModelCard { id: string; } -export const LobeInternLMAI = LobeOpenAICompatibleFactory({ +export const LobeInternLMAI = createOpenAICompatibleRuntime({ baseURL: 'https://internlm-chat.intern-ai.org.cn/puyu/api/v1', chatCompletion: { handlePayload: (payload) => { @@ -23,9 +23,9 @@ export const LobeInternLMAI = LobeOpenAICompatibleFactory({ models: async ({ client }) => { const { LOBE_DEFAULT_MODEL_LIST } = await import('@/config/aiModels'); - const functionCallKeywords = ['internlm'] + const functionCallKeywords = ['internlm']; - const visionKeywords = ['internvl'] + const visionKeywords = ['internvl']; const modelsPage = (await client.models.list()) as any; const modelList: InternLMModelCard[] = modelsPage.data; @@ -41,15 +41,13 @@ export const LobeInternLMAI = LobeOpenAICompatibleFactory({ displayName: knownModel?.displayName ?? undefined, enabled: knownModel?.enabled || false, functionCall: - functionCallKeywords.some(keyword => model.id.toLowerCase().includes(keyword)) || + functionCallKeywords.some((keyword) => model.id.toLowerCase().includes(keyword)) || knownModel?.abilities?.functionCall || false, id: model.id, - reasoning: - knownModel?.abilities?.reasoning || - false, + reasoning: knownModel?.abilities?.reasoning || false, vision: - visionKeywords.some(keyword => model.id.toLowerCase().includes(keyword)) || + visionKeywords.some((keyword) => model.id.toLowerCase().includes(keyword)) || knownModel?.abilities?.vision || false, }; diff --git a/src/libs/model-runtime/jina/index.ts b/src/libs/model-runtime/jina/index.ts index 5530bcbd25..1b3162e412 100644 --- a/src/libs/model-runtime/jina/index.ts +++ b/src/libs/model-runtime/jina/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface JinaModelCard { id: string; } -export const LobeJinaAI = LobeOpenAICompatibleFactory({ +export const LobeJinaAI = createOpenAICompatibleRuntime({ baseURL: 'https://deepsearch.jina.ai/v1', debug: { chatCompletion: () => process.env.DEBUG_JINA_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/lmstudio/index.ts b/src/libs/model-runtime/lmstudio/index.ts index eceb1a16b7..4a3f7994a3 100644 --- a/src/libs/model-runtime/lmstudio/index.ts +++ b/src/libs/model-runtime/lmstudio/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface LMStudioModelCard { id: string; } -export const LobeLMStudioAI = LobeOpenAICompatibleFactory({ +export const LobeLMStudioAI = createOpenAICompatibleRuntime({ apiKey: 'placeholder-to-avoid-error', baseURL: 'http://127.0.0.1:1234/v1', debug: { diff --git a/src/libs/model-runtime/minimax/index.ts b/src/libs/model-runtime/minimax/index.ts index 2f2ffbb3bb..eb60b5bbec 100644 --- a/src/libs/model-runtime/minimax/index.ts +++ b/src/libs/model-runtime/minimax/index.ts @@ -1,14 +1,14 @@ import minimaxChatModels from '@/config/aiModels/minimax'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export const getMinimaxMaxOutputs = (modelId: string): number | undefined => { const model = minimaxChatModels.find((model) => model.id === modelId); return model ? model.maxOutput : undefined; }; -export const LobeMinimaxAI = LobeOpenAICompatibleFactory({ +export const LobeMinimaxAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.minimax.chat/v1', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/mistral/index.ts b/src/libs/model-runtime/mistral/index.ts index 46d30c6415..99b6491807 100644 --- a/src/libs/model-runtime/mistral/index.ts +++ b/src/libs/model-runtime/mistral/index.ts @@ -1,7 +1,7 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface MistralModelCard { capabilities: { @@ -13,7 +13,7 @@ export interface MistralModelCard { max_context_length: number; } -export const LobeMistralAI = LobeOpenAICompatibleFactory({ +export const LobeMistralAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.mistral.ai/v1', chatCompletion: { // Mistral API does not support stream_options: { include_usage: true } diff --git a/src/libs/model-runtime/modelscope/index.ts b/src/libs/model-runtime/modelscope/index.ts index 0cda978843..bf2f507bc6 100644 --- a/src/libs/model-runtime/modelscope/index.ts +++ b/src/libs/model-runtime/modelscope/index.ts @@ -1,8 +1,7 @@ - import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface ModelScopeModelCard { created: number; @@ -11,7 +10,7 @@ export interface ModelScopeModelCard { owned_by: string; } -export const LobeModelScopeAI = LobeOpenAICompatibleFactory({ +export const LobeModelScopeAI = createOpenAICompatibleRuntime({ baseURL: 'https://api-inference.modelscope.cn/v1', debug: { chatCompletion: () => process.env.DEBUG_MODELSCOPE_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/moonshot/index.ts b/src/libs/model-runtime/moonshot/index.ts index c44e9058fa..0c3b1709cb 100644 --- a/src/libs/model-runtime/moonshot/index.ts +++ b/src/libs/model-runtime/moonshot/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ChatStreamPayload, ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface MoonshotModelCard { id: string; } -export const LobeMoonshotAI = LobeOpenAICompatibleFactory({ +export const LobeMoonshotAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.moonshot.cn/v1', chatCompletion: { handlePayload: (payload: ChatStreamPayload) => { diff --git a/src/libs/model-runtime/novita/index.ts b/src/libs/model-runtime/novita/index.ts index 7ceff161f3..f5d84a50a8 100644 --- a/src/libs/model-runtime/novita/index.ts +++ b/src/libs/model-runtime/novita/index.ts @@ -1,10 +1,10 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; import { NovitaModelCard } from './type'; -export const LobeNovitaAI = LobeOpenAICompatibleFactory({ +export const LobeNovitaAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.novita.ai/v3/openai', constructorOptions: { defaultHeaders: { diff --git a/src/libs/model-runtime/nvidia/index.ts b/src/libs/model-runtime/nvidia/index.ts index 37a1a4851a..35817c36f0 100644 --- a/src/libs/model-runtime/nvidia/index.ts +++ b/src/libs/model-runtime/nvidia/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface NvidiaModelCard { id: string; } -export const LobeNvidiaAI = LobeOpenAICompatibleFactory({ +export const LobeNvidiaAI = createOpenAICompatibleRuntime({ baseURL: 'https://integrate.api.nvidia.com/v1', debug: { chatCompletion: () => process.env.DEBUG_NVIDIA_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/ollama/index.ts b/src/libs/model-runtime/ollama/index.ts index c128902b92..d7fd5eacdf 100644 --- a/src/libs/model-runtime/ollama/index.ts +++ b/src/libs/model-runtime/ollama/index.ts @@ -8,7 +8,7 @@ import { createErrorResponse } from '@/utils/errorResponse'; import { LobeRuntimeAI } from '../BaseAI'; import { AgentRuntimeErrorType } from '../error'; import { - ChatCompetitionOptions, + ChatMethodOptions, ChatStreamPayload, Embeddings, EmbeddingsPayload, @@ -43,7 +43,7 @@ export class LobeOllamaAI implements LobeRuntimeAI { if (baseURL) this.baseURL = baseURL; } - async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions) { + async chat(payload: ChatStreamPayload, options?: ChatMethodOptions) { try { const abort = () => { this.client.abort(); diff --git a/src/libs/model-runtime/openai/index.ts b/src/libs/model-runtime/openai/index.ts index 6b420e00cb..2fc73da4ab 100644 --- a/src/libs/model-runtime/openai/index.ts +++ b/src/libs/model-runtime/openai/index.ts @@ -1,7 +1,7 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; import { pruneReasoningPayload } from '../utils/openaiHelpers'; export interface OpenAIModelCard { @@ -10,13 +10,13 @@ export interface OpenAIModelCard { const prunePrefixes = ['o1', 'o3', 'o4']; -export const LobeOpenAI = LobeOpenAICompatibleFactory({ +export const LobeOpenAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.openai.com/v1', chatCompletion: { handlePayload: (payload) => { const { model } = payload; - if (prunePrefixes.some(prefix => model.startsWith(prefix))) { + if (prunePrefixes.some((prefix) => model.startsWith(prefix))) { return pruneReasoningPayload(payload) as any; } diff --git a/src/libs/model-runtime/openrouter/index.ts b/src/libs/model-runtime/openrouter/index.ts index 38fc078f20..e80676e6b2 100644 --- a/src/libs/model-runtime/openrouter/index.ts +++ b/src/libs/model-runtime/openrouter/index.ts @@ -1,7 +1,7 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; import { OpenRouterModelCard, OpenRouterModelExtraInfo, OpenRouterReasoning } from './type'; const formatPrice = (price: string) => { @@ -9,7 +9,7 @@ const formatPrice = (price: string) => { return Number((Number(price) * 1e6).toPrecision(5)); }; -export const LobeOpenRouterAI = LobeOpenAICompatibleFactory({ +export const LobeOpenRouterAI = createOpenAICompatibleRuntime({ baseURL: 'https://openrouter.ai/api/v1', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/perplexity/index.ts b/src/libs/model-runtime/perplexity/index.ts index ac904ca0eb..5e75f9b5db 100644 --- a/src/libs/model-runtime/perplexity/index.ts +++ b/src/libs/model-runtime/perplexity/index.ts @@ -1,9 +1,9 @@ import OpenAI from 'openai'; import { ChatStreamPayload, ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; -export const LobePerplexityAI = LobeOpenAICompatibleFactory({ +export const LobePerplexityAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.perplexity.ai', chatCompletion: { handlePayload: (payload: ChatStreamPayload) => { diff --git a/src/libs/model-runtime/ppio/index.ts b/src/libs/model-runtime/ppio/index.ts index 82ce6b77b3..c7c35b624c 100644 --- a/src/libs/model-runtime/ppio/index.ts +++ b/src/libs/model-runtime/ppio/index.ts @@ -1,10 +1,10 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; import { PPIOModelCard } from './type'; -export const LobePPIOAI = LobeOpenAICompatibleFactory({ +export const LobePPIOAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.ppinfra.com/v3/openai', constructorOptions: { defaultHeaders: { diff --git a/src/libs/model-runtime/qiniu/index.ts b/src/libs/model-runtime/qiniu/index.ts index 70466e79f9..3c2fd8d3c3 100644 --- a/src/libs/model-runtime/qiniu/index.ts +++ b/src/libs/model-runtime/qiniu/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface QiniuModelCard { id: string; } -export const LobeQiniuAI = LobeOpenAICompatibleFactory({ +export const LobeQiniuAI = createOpenAICompatibleRuntime({ apiKey: 'placeholder-to-avoid-error', baseURL: 'https://api.qnaigc.com/v1', debug: { diff --git a/src/libs/model-runtime/qwen/index.ts b/src/libs/model-runtime/qwen/index.ts index 1787b9b2fb..e87c04271c 100644 --- a/src/libs/model-runtime/qwen/index.ts +++ b/src/libs/model-runtime/qwen/index.ts @@ -1,7 +1,7 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; import { QwenAIStream } from '../utils/streams'; export interface QwenModelCard { @@ -20,7 +20,7 @@ export const QwenLegacyModels = new Set([ 'qwen-1.8b-longcontext-chat', ]); -export const LobeQwenAI = LobeOpenAICompatibleFactory({ +export const LobeQwenAI = createOpenAICompatibleRuntime({ baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/sambanova/index.ts b/src/libs/model-runtime/sambanova/index.ts index 2839251fe0..70987c928b 100644 --- a/src/libs/model-runtime/sambanova/index.ts +++ b/src/libs/model-runtime/sambanova/index.ts @@ -1,7 +1,7 @@ import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; -export const LobeSambaNovaAI = LobeOpenAICompatibleFactory({ +export const LobeSambaNovaAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.sambanova.ai/v1', debug: { chatCompletion: () => process.env.DEBUG_SAMBANOVA_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/search1api/index.ts b/src/libs/model-runtime/search1api/index.ts index d0b92aa8ae..d61ae97a25 100644 --- a/src/libs/model-runtime/search1api/index.ts +++ b/src/libs/model-runtime/search1api/index.ts @@ -3,13 +3,13 @@ import OpenAI from 'openai'; import type { ChatModelCard } from '@/types/llm'; import { ChatStreamPayload, ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface Search1APIModelCard { id: string; } -export const LobeSearch1API = LobeOpenAICompatibleFactory({ +export const LobeSearch1API = createOpenAICompatibleRuntime({ baseURL: 'https://api.search1api.com/v1', chatCompletion: { handlePayload: (payload: ChatStreamPayload) => { diff --git a/src/libs/model-runtime/sensenova/index.ts b/src/libs/model-runtime/sensenova/index.ts index 81283ad144..9fe2cffd04 100644 --- a/src/libs/model-runtime/sensenova/index.ts +++ b/src/libs/model-runtime/sensenova/index.ts @@ -1,14 +1,14 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; import { convertSenseNovaMessage } from '../utils/sensenovaHelpers'; export interface SenseNovaModelCard { id: string; } -export const LobeSenseNovaAI = LobeOpenAICompatibleFactory({ +export const LobeSenseNovaAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.sensenova.cn/compatible-mode/v1', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/siliconcloud/index.ts b/src/libs/model-runtime/siliconcloud/index.ts index 2e2f68aff0..8ff40e821d 100644 --- a/src/libs/model-runtime/siliconcloud/index.ts +++ b/src/libs/model-runtime/siliconcloud/index.ts @@ -2,13 +2,13 @@ import type { ChatModelCard } from '@/types/llm'; import { AgentRuntimeErrorType } from '../error'; import { ChatCompletionErrorPayload, ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface SiliconCloudModelCard { id: string; } -export const LobeSiliconCloudAI = LobeOpenAICompatibleFactory({ +export const LobeSiliconCloudAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.siliconflow.cn/v1', chatCompletion: { handleError: (error: any): Omit | undefined => { diff --git a/src/libs/model-runtime/spark/index.ts b/src/libs/model-runtime/spark/index.ts index e410b52e65..db357e8aeb 100644 --- a/src/libs/model-runtime/spark/index.ts +++ b/src/libs/model-runtime/spark/index.ts @@ -1,26 +1,28 @@ import { ChatStreamPayload, ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; import { SparkAIStream, transformSparkResponseToStream } from '../utils/streams'; -export const LobeSparkAI = LobeOpenAICompatibleFactory({ +export const LobeSparkAI = createOpenAICompatibleRuntime({ baseURL: 'https://spark-api-open.xf-yun.com/v1', chatCompletion: { handlePayload: (payload: ChatStreamPayload) => { const { enabledSearch, tools, ...rest } = payload; - const sparkTools = enabledSearch ? [ - ...(tools || []), - { - type: "web_search", - web_search: { - enable: true, - search_mode: process.env.SPARK_SEARCH_MODE || "normal", // normal or deep - /* + const sparkTools = enabledSearch + ? [ + ...(tools || []), + { + type: 'web_search', + web_search: { + enable: true, + search_mode: process.env.SPARK_SEARCH_MODE || 'normal', // normal or deep + /* show_ref_label: true, */ - }, - } - ] : tools; + }, + }, + ] + : tools; return { ...rest, diff --git a/src/libs/model-runtime/stepfun/index.ts b/src/libs/model-runtime/stepfun/index.ts index 11b033e84f..50065c8803 100644 --- a/src/libs/model-runtime/stepfun/index.ts +++ b/src/libs/model-runtime/stepfun/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface StepfunModelCard { id: string; } -export const LobeStepfunAI = LobeOpenAICompatibleFactory({ +export const LobeStepfunAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.stepfun.com/v1', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/taichu/index.ts b/src/libs/model-runtime/taichu/index.ts index 23beae0d1f..b810ee2056 100644 --- a/src/libs/model-runtime/taichu/index.ts +++ b/src/libs/model-runtime/taichu/index.ts @@ -1,9 +1,9 @@ import OpenAI from 'openai'; import { ChatStreamPayload, ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; -export const LobeTaichuAI = LobeOpenAICompatibleFactory({ +export const LobeTaichuAI = createOpenAICompatibleRuntime({ baseURL: 'https://ai-maas.wair.ac.cn/maas/v1', chatCompletion: { handlePayload: (payload: ChatStreamPayload) => { diff --git a/src/libs/model-runtime/tencentcloud/index.ts b/src/libs/model-runtime/tencentcloud/index.ts index 64ce56b84d..6838a4bcb2 100644 --- a/src/libs/model-runtime/tencentcloud/index.ts +++ b/src/libs/model-runtime/tencentcloud/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface TencentCloudModelCard { id: string; } -export const LobeTencentCloudAI = LobeOpenAICompatibleFactory({ +export const LobeTencentCloudAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.lkeap.cloud.tencent.com/v1', debug: { chatCompletion: () => process.env.DEBUG_TENCENT_CLOUD_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/togetherai/index.ts b/src/libs/model-runtime/togetherai/index.ts index 7f8d13a35a..a9f7a49b5d 100644 --- a/src/libs/model-runtime/togetherai/index.ts +++ b/src/libs/model-runtime/togetherai/index.ts @@ -1,10 +1,10 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; import { TogetherAIModel } from './type'; -export const LobeTogetherAI = LobeOpenAICompatibleFactory({ +export const LobeTogetherAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.together.xyz/v1', constructorOptions: { defaultHeaders: { diff --git a/src/libs/model-runtime/types/chat.ts b/src/libs/model-runtime/types/chat.ts index 655aa0748f..eff5b61f37 100644 --- a/src/libs/model-runtime/types/chat.ts +++ b/src/libs/model-runtime/types/chat.ts @@ -137,7 +137,7 @@ export interface ChatStreamPayload { top_p?: number; } -export interface ChatCompetitionOptions { +export interface ChatMethodOptions { callback?: ChatStreamCallbacks; /** * response headers diff --git a/src/libs/model-runtime/upstage/index.ts b/src/libs/model-runtime/upstage/index.ts index b3576292b2..6129e54715 100644 --- a/src/libs/model-runtime/upstage/index.ts +++ b/src/libs/model-runtime/upstage/index.ts @@ -1,7 +1,7 @@ import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; -export const LobeUpstageAI = LobeOpenAICompatibleFactory({ +export const LobeUpstageAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.upstage.ai/v1/solar', debug: { chatCompletion: () => process.env.DEBUG_UPSTAGE_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/utils/openaiCompatibleFactory/index.test.ts b/src/libs/model-runtime/utils/openaiCompatibleFactory/index.test.ts index 149a2f21fe..b12426ec42 100644 --- a/src/libs/model-runtime/utils/openaiCompatibleFactory/index.test.ts +++ b/src/libs/model-runtime/utils/openaiCompatibleFactory/index.test.ts @@ -14,7 +14,7 @@ import officalOpenAIModels from '@/libs/model-runtime/openai/fixtures/openai-mod import { sleep } from '@/utils/sleep'; import * as debugStreamModule from '../debugStream'; -import { LobeOpenAICompatibleFactory } from './index'; +import { createOpenAICompatibleRuntime } from './index'; const provider = 'groq'; const defaultBaseURL = 'https://api.groq.com/openai/v1'; @@ -26,7 +26,7 @@ vi.spyOn(console, 'error').mockImplementation(() => {}); let instance: LobeOpenAICompatibleRuntime; -const LobeMockProvider = LobeOpenAICompatibleFactory({ +const LobeMockProvider = createOpenAICompatibleRuntime({ baseURL: defaultBaseURL, chatCompletion: { handleError: (error) => { @@ -426,7 +426,7 @@ describe('LobeOpenAICompatibleFactory', () => { describe('noUserId option', () => { it('should not add user to payload when noUserId is true', async () => { - const LobeMockProvider = LobeOpenAICompatibleFactory({ + const LobeMockProvider = createOpenAICompatibleRuntime({ baseURL: 'https://api.mistral.ai/v1', chatCompletion: { noUserId: true, @@ -457,7 +457,7 @@ describe('LobeOpenAICompatibleFactory', () => { }); it('should add user to payload when noUserId is false', async () => { - const LobeMockProvider = LobeOpenAICompatibleFactory({ + const LobeMockProvider = createOpenAICompatibleRuntime({ baseURL: 'https://api.mistral.ai/v1', chatCompletion: { noUserId: false, @@ -488,7 +488,7 @@ describe('LobeOpenAICompatibleFactory', () => { }); it('should add user to payload when noUserId is not set in chatCompletion', async () => { - const LobeMockProvider = LobeOpenAICompatibleFactory({ + const LobeMockProvider = createOpenAICompatibleRuntime({ baseURL: 'https://api.mistral.ai/v1', provider: ModelProvider.Mistral, }); @@ -831,7 +831,7 @@ describe('LobeOpenAICompatibleFactory', () => { }, ); - const LobeMockProvider = LobeOpenAICompatibleFactory({ + const LobeMockProvider = createOpenAICompatibleRuntime({ baseURL: 'https://api.test.com/v1', chatCompletion: { handleStream: customStreamHandler, @@ -890,7 +890,7 @@ describe('LobeOpenAICompatibleFactory', () => { }); }); - const LobeMockProvider = LobeOpenAICompatibleFactory({ + const LobeMockProvider = createOpenAICompatibleRuntime({ baseURL: 'https://api.test.com/v1', chatCompletion: { handleTransformResponseToStream: customTransformHandler, diff --git a/src/libs/model-runtime/utils/openaiCompatibleFactory/index.ts b/src/libs/model-runtime/utils/openaiCompatibleFactory/index.ts index a759d5e935..84847d9a60 100644 --- a/src/libs/model-runtime/utils/openaiCompatibleFactory/index.ts +++ b/src/libs/model-runtime/utils/openaiCompatibleFactory/index.ts @@ -9,8 +9,8 @@ import type { ChatModelCard } from '@/types/llm'; import { LobeRuntimeAI } from '../../BaseAI'; import { AgentRuntimeErrorType, ILobeAgentRuntimeErrorType } from '../../error'; import type { - ChatCompetitionOptions, ChatCompletionErrorPayload, + ChatMethodOptions, ChatStreamPayload, Embeddings, EmbeddingsOptions, @@ -150,7 +150,7 @@ export function transformResponseToStream(data: OpenAI.ChatCompletion) { }); } -export const LobeOpenAICompatibleFactory = = any>({ +export const createOpenAICompatibleRuntime = = any>({ provider, baseURL: DEFAULT_BASE_URL, apiKey: DEFAULT_API_LEY, @@ -199,7 +199,7 @@ export const LobeOpenAICompatibleFactory = = any> this.id = options.id || provider; } - async chat({ responseMode, ...payload }: ChatStreamPayload, options?: ChatCompetitionOptions) { + async chat({ responseMode, ...payload }: ChatStreamPayload, options?: ChatMethodOptions) { try { const inputStartAt = Date.now(); const postPayload = chatCompletion?.handlePayload diff --git a/src/libs/model-runtime/vllm/index.ts b/src/libs/model-runtime/vllm/index.ts index 66b01ba2f3..6819fbd504 100644 --- a/src/libs/model-runtime/vllm/index.ts +++ b/src/libs/model-runtime/vllm/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface VLLMModelCard { id: string; } -export const LobeVLLMAI = LobeOpenAICompatibleFactory({ +export const LobeVLLMAI = createOpenAICompatibleRuntime({ baseURL: 'http://localhost:8000/v1', debug: { chatCompletion: () => process.env.DEBUG_VLLM_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/volcengine/index.ts b/src/libs/model-runtime/volcengine/index.ts index 2dda04330c..1f922c573a 100644 --- a/src/libs/model-runtime/volcengine/index.ts +++ b/src/libs/model-runtime/volcengine/index.ts @@ -1,7 +1,7 @@ import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; -export const LobeVolcengineAI = LobeOpenAICompatibleFactory({ +export const LobeVolcengineAI = createOpenAICompatibleRuntime({ baseURL: 'https://ark.cn-beijing.volces.com/api/v3', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/wenxin/index.ts b/src/libs/model-runtime/wenxin/index.ts index 64a2caa6be..ca335ee7e7 100644 --- a/src/libs/model-runtime/wenxin/index.ts +++ b/src/libs/model-runtime/wenxin/index.ts @@ -1,7 +1,7 @@ import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; -export const LobeWenxinAI = LobeOpenAICompatibleFactory({ +export const LobeWenxinAI = createOpenAICompatibleRuntime({ baseURL: 'https://qianfan.baidubce.com/v2', chatCompletion: { handlePayload: (payload) => { diff --git a/src/libs/model-runtime/xai/index.ts b/src/libs/model-runtime/xai/index.ts index 6242d70c04..f8ce8023e9 100644 --- a/src/libs/model-runtime/xai/index.ts +++ b/src/libs/model-runtime/xai/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface XAIModelCard { id: string; } -export const LobeXAI = LobeOpenAICompatibleFactory({ +export const LobeXAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.x.ai/v1', chatCompletion: { handlePayload: (payload) => { @@ -21,7 +21,10 @@ export const LobeXAI = LobeOpenAICompatibleFactory({ stream: true, ...(enabledSearch && { search_parameters: { - max_search_results: Math.min(Math.max(parseInt(process.env.XAI_MAX_SEARCH_RESULTS ?? '15', 10), 1), 30), + max_search_results: Math.min( + Math.max(parseInt(process.env.XAI_MAX_SEARCH_RESULTS ?? '15', 10), 1), + 30, + ), mode: 'auto', return_citations: true, sources: [ diff --git a/src/libs/model-runtime/xinference/index.ts b/src/libs/model-runtime/xinference/index.ts index d3582c18e6..0b34a3458f 100644 --- a/src/libs/model-runtime/xinference/index.ts +++ b/src/libs/model-runtime/xinference/index.ts @@ -1,7 +1,7 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface XinferenceModelCard { context_length: number; @@ -12,7 +12,7 @@ export interface XinferenceModelCard { name: string; } -export const LobeXinferenceAI = LobeOpenAICompatibleFactory({ +export const LobeXinferenceAI = createOpenAICompatibleRuntime({ baseURL: 'http://localhost:9997/v1', debug: { chatCompletion: () => process.env.DEBUG_XINFERENCE_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/zeroone/index.ts b/src/libs/model-runtime/zeroone/index.ts index 41f6ddb9db..3a10359e2a 100644 --- a/src/libs/model-runtime/zeroone/index.ts +++ b/src/libs/model-runtime/zeroone/index.ts @@ -1,13 +1,13 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface ZeroOneModelCard { id: string; } -export const LobeZeroOneAI = LobeOpenAICompatibleFactory({ +export const LobeZeroOneAI = createOpenAICompatibleRuntime({ baseURL: 'https://api.lingyiwanwu.com/v1', debug: { chatCompletion: () => process.env.DEBUG_ZEROONE_CHAT_COMPLETION === '1', diff --git a/src/libs/model-runtime/zhipu/index.ts b/src/libs/model-runtime/zhipu/index.ts index d1daa9b601..3595ca071d 100644 --- a/src/libs/model-runtime/zhipu/index.ts +++ b/src/libs/model-runtime/zhipu/index.ts @@ -1,7 +1,7 @@ import type { ChatModelCard } from '@/types/llm'; import { ModelProvider } from '../types'; -import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; +import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory'; export interface ZhipuModelCard { description: string; @@ -9,7 +9,7 @@ export interface ZhipuModelCard { modelName: string; } -export const LobeZhipuAI = LobeOpenAICompatibleFactory({ +export const LobeZhipuAI = createOpenAICompatibleRuntime({ baseURL: 'https://open.bigmodel.cn/api/paas/v4', chatCompletion: { handlePayload: (payload) => { diff --git a/src/server/routers/tools/search.test.ts b/src/server/routers/tools/search.test.ts index 047a082a0f..f0a37d77f8 100644 --- a/src/server/routers/tools/search.test.ts +++ b/src/server/routers/tools/search.test.ts @@ -104,8 +104,7 @@ describe('searchRouter', () => { query: 'test query', }); - expect(result).toEqual({ - costTime: 0, + expect(result).toMatchObject({ query: 'test query', results: [ { @@ -139,8 +138,7 @@ describe('searchRouter', () => { query: 'test query', }); - expect(result).toEqual({ - costTime: 0, + expect(result).toMatchObject({ query: 'test query', results: [ { diff --git a/src/store/chat/slices/aiChat/actions/__tests__/generateAIChat.test.ts b/src/store/chat/slices/aiChat/actions/__tests__/generateAIChat.test.ts index 4f9d61b793..c5bec665ef 100644 --- a/src/store/chat/slices/aiChat/actions/__tests__/generateAIChat.test.ts +++ b/src/store/chat/slices/aiChat/actions/__tests__/generateAIChat.test.ts @@ -671,14 +671,16 @@ describe('chatMessage actions', () => { (chatService.createAssistantMessage as Mock).mockResolvedValue(aiResponse); const spy = vi.spyOn(chatService, 'createAssistantMessageStream'); // 模拟消息创建 - (messageService.createMessage as Mock).mockResolvedValue('assistant-message-id'); + const createMessageSpyOn = vi + .spyOn(messageService, 'createMessage') + .mockResolvedValue('assistant-message-id'); await act(async () => { await result.current.internal_coreProcessMessage(messages, userMessage.id); }); // 验证是否创建了代表 AI 响应的消息 - expect(messageService.createMessage).toHaveBeenCalledWith( + expect(createMessageSpyOn).toHaveBeenCalledWith( expect.objectContaining({ role: 'assistant', content: LOADING_FLAT, diff --git a/src/store/chat/slices/message/action.test.ts b/src/store/chat/slices/message/action.test.ts index a4f2bc780b..487067e38b 100644 --- a/src/store/chat/slices/message/action.test.ts +++ b/src/store/chat/slices/message/action.test.ts @@ -369,11 +369,12 @@ describe('chatMessage actions', () => { const messageId = 'message-id'; const newContent = 'Updated content'; + const spy = vi.spyOn(messageService, 'updateMessage'); await act(async () => { await result.current.internal_updateMessageContent(messageId, newContent); }); - expect(messageService.updateMessage).toHaveBeenCalledWith(messageId, { content: newContent }); + expect(spy).toHaveBeenCalledWith(messageId, { content: newContent }); }); it('should dispatch message update action', async () => { diff --git a/src/store/chat/slices/topic/action.test.ts b/src/store/chat/slices/topic/action.test.ts index c07a8dbb68..a5842c72ac 100644 --- a/src/store/chat/slices/topic/action.test.ts +++ b/src/store/chat/slices/topic/action.test.ts @@ -264,7 +264,8 @@ describe('topic action', () => { const topicId = 'topic-id'; const newTitle = 'Updated Topic Title'; // Mock the topicService.updateTitle to resolve immediately - (topicService.updateTopic as Mock).mockResolvedValue(undefined); + + const spyOn = vi.spyOn(topicService, 'updateTopic'); const { result } = renderHook(() => useChatStore()); @@ -276,7 +277,7 @@ describe('topic action', () => { }); // Verify that the topicService.updateTitle was called with correct parameters - expect(topicService.updateTopic).toHaveBeenCalledWith(topicId, { + expect(spyOn).toHaveBeenCalledWith(topicId, { title: 'Updated Topic Title', });