@lobehub/chat 0.162.16 → 0.162.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +4 -0
- package/CHANGELOG.md +50 -0
- package/Dockerfile +3 -0
- package/README.md +1 -0
- package/README.zh-CN.md +1 -0
- package/docs/self-hosting/advanced/settings-url-share.mdx +89 -57
- package/docs/self-hosting/advanced/settings-url-share.zh-CN.mdx +87 -56
- package/docs/self-hosting/advanced/upstream-sync.mdx +1 -1
- package/docs/self-hosting/environment-variables/model-provider.mdx +10 -1
- package/docs/self-hosting/environment-variables/model-provider.zh-CN.mdx +9 -0
- package/docs/usage/features/multi-ai-providers.mdx +1 -0
- package/docs/usage/features/multi-ai-providers.zh-CN.mdx +1 -0
- package/locales/ar/error.json +7 -37
- package/locales/ar/modelProvider.json +15 -171
- package/locales/ar/setting.json +5 -8
- package/locales/bg-BG/error.json +6 -36
- package/locales/bg-BG/modelProvider.json +15 -171
- package/locales/bg-BG/setting.json +5 -8
- package/locales/de-DE/error.json +6 -36
- package/locales/de-DE/modelProvider.json +15 -171
- package/locales/de-DE/setting.json +5 -8
- package/locales/en-US/error.json +6 -36
- package/locales/en-US/modelProvider.json +15 -171
- package/locales/en-US/setting.json +6 -9
- package/locales/es-ES/error.json +6 -36
- package/locales/es-ES/modelProvider.json +15 -171
- package/locales/es-ES/setting.json +5 -8
- package/locales/fr-FR/error.json +6 -36
- package/locales/fr-FR/modelProvider.json +15 -171
- package/locales/fr-FR/setting.json +9 -12
- package/locales/it-IT/error.json +6 -36
- package/locales/it-IT/modelProvider.json +15 -171
- package/locales/it-IT/setting.json +5 -8
- package/locales/ja-JP/error.json +6 -36
- package/locales/ja-JP/modelProvider.json +15 -171
- package/locales/ja-JP/setting.json +5 -8
- package/locales/ko-KR/error.json +6 -36
- package/locales/ko-KR/modelProvider.json +15 -171
- package/locales/ko-KR/setting.json +5 -8
- package/locales/nl-NL/error.json +6 -36
- package/locales/nl-NL/modelProvider.json +15 -171
- package/locales/nl-NL/setting.json +5 -8
- package/locales/pl-PL/error.json +6 -36
- package/locales/pl-PL/modelProvider.json +15 -171
- package/locales/pl-PL/setting.json +6 -9
- package/locales/pt-BR/error.json +6 -36
- package/locales/pt-BR/modelProvider.json +15 -171
- package/locales/pt-BR/setting.json +5 -8
- package/locales/ru-RU/error.json +6 -36
- package/locales/ru-RU/modelProvider.json +15 -171
- package/locales/ru-RU/setting.json +5 -8
- package/locales/tr-TR/error.json +6 -36
- package/locales/tr-TR/modelProvider.json +15 -171
- package/locales/tr-TR/setting.json +5 -8
- package/locales/vi-VN/error.json +6 -36
- package/locales/vi-VN/modelProvider.json +15 -171
- package/locales/vi-VN/setting.json +5 -8
- package/locales/zh-CN/error.json +7 -37
- package/locales/zh-CN/modelProvider.json +15 -171
- package/locales/zh-CN/setting.json +5 -8
- package/locales/zh-TW/error.json +7 -37
- package/locales/zh-TW/modelProvider.json +15 -171
- package/locales/zh-TW/setting.json +5 -8
- package/package.json +1 -2
- package/src/app/(main)/settings/llm/ProviderList/Azure/index.tsx +107 -0
- package/src/app/(main)/settings/llm/ProviderList/Bedrock/index.tsx +68 -0
- package/src/app/(main)/settings/llm/ProviderList/Ollama/index.tsx +29 -0
- package/src/app/(main)/settings/llm/ProviderList/OpenAI/index.tsx +20 -0
- package/src/app/(main)/settings/llm/ProviderList/providers.tsx +141 -0
- package/src/app/(main)/settings/llm/components/ProviderConfig/index.tsx +18 -27
- package/src/app/(main)/settings/llm/index.tsx +7 -34
- package/src/app/(main)/settings/llm/type.ts +5 -0
- package/src/app/api/chat/[provider]/route.ts +8 -3
- package/src/app/api/chat/agentRuntime.test.ts +17 -0
- package/src/app/api/chat/agentRuntime.ts +7 -0
- package/src/app/api/errorResponse.test.ts +7 -89
- package/src/app/api/errorResponse.ts +6 -43
- package/src/app/api/middleware/auth/index.ts +7 -3
- package/src/components/ModelProviderIcon/index.tsx +5 -0
- package/src/components/ModelSelect/index.tsx +7 -10
- package/src/config/llm.ts +6 -0
- package/src/config/modelProviders/anthropic.ts +5 -0
- package/src/config/modelProviders/azure.ts +2 -0
- package/src/config/modelProviders/bedrock.ts +2 -0
- package/src/config/modelProviders/deepseek.ts +4 -1
- package/src/config/modelProviders/google.ts +5 -0
- package/src/config/modelProviders/groq.ts +5 -0
- package/src/config/modelProviders/index.ts +4 -0
- package/src/config/modelProviders/minimax.ts +2 -0
- package/src/config/modelProviders/mistral.ts +2 -0
- package/src/config/modelProviders/moonshot.ts +2 -0
- package/src/config/modelProviders/ollama.ts +4 -0
- package/src/config/modelProviders/openai.ts +3 -0
- package/src/config/modelProviders/openrouter.ts +3 -0
- package/src/config/modelProviders/perplexity.ts +5 -0
- package/src/config/modelProviders/qwen.ts +35 -0
- package/src/config/modelProviders/togetherai.ts +3 -0
- package/src/config/modelProviders/zeroone.ts +4 -2
- package/src/config/modelProviders/zhipu.ts +2 -0
- package/src/const/settings/llm.ts +5 -0
- package/src/features/Conversation/Error/APIKeyForm/ProviderApiKeyForm.tsx +6 -3
- package/src/features/Conversation/Error/APIKeyForm/ProviderAvatar.tsx +5 -0
- package/src/features/Conversation/Error/APIKeyForm/index.tsx +4 -0
- package/src/features/Conversation/Error/OllamaBizError/InvalidOllamaModel/index.tsx +11 -14
- package/src/features/Conversation/Error/index.tsx +29 -19
- package/src/features/Conversation/components/ChatItem/index.tsx +4 -13
- package/src/features/ModelSelect/index.tsx +1 -1
- package/src/features/ModelSwitchPanel/index.tsx +1 -1
- package/src/hooks/useProviderName.ts +8 -0
- package/src/libs/agent-runtime/AgentRuntime.ts +8 -1
- package/src/libs/agent-runtime/anthropic/index.test.ts +8 -5
- package/src/libs/agent-runtime/anthropic/index.ts +3 -3
- package/src/libs/agent-runtime/azureOpenai/index.test.ts +5 -2
- package/src/libs/agent-runtime/azureOpenai/index.ts +2 -2
- package/src/libs/agent-runtime/bedrock/index.test.ts +2 -2
- package/src/libs/agent-runtime/bedrock/index.ts +2 -2
- package/src/libs/agent-runtime/deepseek/index.test.ts +3 -2
- package/src/libs/agent-runtime/deepseek/index.ts +0 -5
- package/src/libs/agent-runtime/error.ts +11 -43
- package/src/libs/agent-runtime/google/index.test.ts +3 -4
- package/src/libs/agent-runtime/google/index.ts +4 -4
- package/src/libs/agent-runtime/groq/index.test.ts +3 -2
- package/src/libs/agent-runtime/groq/index.ts +0 -4
- package/src/libs/agent-runtime/index.ts +1 -0
- package/src/libs/agent-runtime/minimax/index.test.ts +3 -2
- package/src/libs/agent-runtime/minimax/index.ts +5 -5
- package/src/libs/agent-runtime/mistral/index.test.ts +2 -2
- package/src/libs/agent-runtime/mistral/index.ts +0 -5
- package/src/libs/agent-runtime/moonshot/index.test.ts +3 -2
- package/src/libs/agent-runtime/moonshot/index.ts +0 -5
- package/src/libs/agent-runtime/openrouter/index.test.ts +3 -2
- package/src/libs/agent-runtime/openrouter/index.ts +0 -6
- package/src/libs/agent-runtime/perplexity/index.test.ts +2 -2
- package/src/libs/agent-runtime/perplexity/index.ts +0 -5
- package/src/libs/agent-runtime/qwen/index.test.ts +251 -0
- package/src/libs/agent-runtime/qwen/index.ts +28 -0
- package/src/libs/agent-runtime/togetherai/index.test.ts +3 -2
- package/src/libs/agent-runtime/togetherai/index.ts +0 -5
- package/src/libs/agent-runtime/types/type.ts +1 -1
- package/src/libs/agent-runtime/utils/openaiCompatibleFactory/index.test.ts +123 -6
- package/src/libs/agent-runtime/utils/openaiCompatibleFactory/index.ts +12 -6
- package/src/libs/agent-runtime/utils/streams/openai.ts +1 -1
- package/src/libs/agent-runtime/zeroone/index.test.ts +3 -2
- package/src/libs/agent-runtime/zeroone/index.ts +1 -5
- package/src/libs/agent-runtime/zhipu/index.test.ts +8 -5
- package/src/libs/agent-runtime/zhipu/index.ts +2 -2
- package/src/locales/default/error.ts +10 -51
- package/src/locales/default/modelProvider.ts +12 -169
- package/src/locales/default/setting.ts +5 -8
- package/src/server/globalConfig/index.ts +2 -0
- package/src/services/__tests__/chat.test.ts +16 -0
- package/src/services/chat.ts +3 -0
- package/src/services/message/client.test.ts +4 -1
- package/src/types/fetch.ts +3 -1
- package/src/types/llm.ts +22 -0
- package/src/types/user/settings/keyVaults.ts +1 -0
- package/src/app/(main)/settings/llm/Anthropic/index.tsx +0 -25
- package/src/app/(main)/settings/llm/Azure/index.tsx +0 -110
- package/src/app/(main)/settings/llm/Bedrock/index.tsx +0 -74
- package/src/app/(main)/settings/llm/DeepSeek/index.tsx +0 -21
- package/src/app/(main)/settings/llm/Google/index.tsx +0 -31
- package/src/app/(main)/settings/llm/Groq/index.tsx +0 -26
- package/src/app/(main)/settings/llm/Minimax/index.tsx +0 -20
- package/src/app/(main)/settings/llm/Mistral/index.tsx +0 -20
- package/src/app/(main)/settings/llm/Moonshot/index.tsx +0 -28
- package/src/app/(main)/settings/llm/Ollama/index.tsx +0 -37
- package/src/app/(main)/settings/llm/OpenAI/index.tsx +0 -28
- package/src/app/(main)/settings/llm/OpenRouter/index.tsx +0 -21
- package/src/app/(main)/settings/llm/Perplexity/index.tsx +0 -23
- package/src/app/(main)/settings/llm/TogetherAI/index.tsx +0 -19
- package/src/app/(main)/settings/llm/ZeroOne/index.tsx +0 -20
- package/src/app/(main)/settings/llm/Zhipu/index.tsx +0 -18
- /package/src/app/(main)/settings/llm/{Ollama → ProviderList/Ollama}/Checker.tsx +0 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import OpenAI from 'openai';
|
|
3
|
+
import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import { LobeOpenAICompatibleRuntime } from '@/libs/agent-runtime';
|
|
6
|
+
import { ModelProvider } from '@/libs/agent-runtime';
|
|
7
|
+
import { AgentRuntimeErrorType } from '@/libs/agent-runtime';
|
|
8
|
+
|
|
9
|
+
import * as debugStreamModule from '../utils/debugStream';
|
|
10
|
+
import { LobeQwenAI } from './index';
|
|
11
|
+
|
|
12
|
+
const provider = ModelProvider.Qwen;
|
|
13
|
+
const defaultBaseURL = 'https://dashscope.aliyuncs.com/compatible-mode/v1';
|
|
14
|
+
const bizErrorType = AgentRuntimeErrorType.ProviderBizError;
|
|
15
|
+
const invalidErrorType = AgentRuntimeErrorType.InvalidProviderAPIKey;
|
|
16
|
+
|
|
17
|
+
// Mock the console.error to avoid polluting test output
|
|
18
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
19
|
+
|
|
20
|
+
let instance: LobeOpenAICompatibleRuntime;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
instance = new LobeQwenAI({ apiKey: 'test' });
|
|
24
|
+
|
|
25
|
+
// 使用 vi.spyOn 来模拟 chat.completions.create 方法
|
|
26
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(
|
|
27
|
+
new ReadableStream() as any,
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
vi.clearAllMocks();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('LobeQwenAI', () => {
|
|
36
|
+
describe('init', () => {
|
|
37
|
+
it('should correctly initialize with an API key', async () => {
|
|
38
|
+
const instance = new LobeQwenAI({ apiKey: 'test_api_key' });
|
|
39
|
+
expect(instance).toBeInstanceOf(LobeQwenAI);
|
|
40
|
+
expect(instance.baseURL).toEqual(defaultBaseURL);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('chat', () => {
|
|
45
|
+
describe('Error', () => {
|
|
46
|
+
it('should return QwenBizError with an openai error response when OpenAI.APIError is thrown', async () => {
|
|
47
|
+
// Arrange
|
|
48
|
+
const apiError = new OpenAI.APIError(
|
|
49
|
+
400,
|
|
50
|
+
{
|
|
51
|
+
status: 400,
|
|
52
|
+
error: {
|
|
53
|
+
message: 'Bad Request',
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
'Error message',
|
|
57
|
+
{},
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
|
61
|
+
|
|
62
|
+
// Act
|
|
63
|
+
try {
|
|
64
|
+
await instance.chat({
|
|
65
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
66
|
+
model: 'qwen-turbo',
|
|
67
|
+
temperature: 0.999,
|
|
68
|
+
});
|
|
69
|
+
} catch (e) {
|
|
70
|
+
expect(e).toEqual({
|
|
71
|
+
endpoint: defaultBaseURL,
|
|
72
|
+
error: {
|
|
73
|
+
error: { message: 'Bad Request' },
|
|
74
|
+
status: 400,
|
|
75
|
+
},
|
|
76
|
+
errorType: bizErrorType,
|
|
77
|
+
provider,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should throw AgentRuntimeError with InvalidQwenAPIKey if no apiKey is provided', async () => {
|
|
83
|
+
try {
|
|
84
|
+
new LobeQwenAI({});
|
|
85
|
+
} catch (e) {
|
|
86
|
+
expect(e).toEqual({ errorType: invalidErrorType });
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should return QwenBizError with the cause when OpenAI.APIError is thrown with cause', async () => {
|
|
91
|
+
// Arrange
|
|
92
|
+
const errorInfo = {
|
|
93
|
+
stack: 'abc',
|
|
94
|
+
cause: {
|
|
95
|
+
message: 'api is undefined',
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
|
99
|
+
|
|
100
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
|
101
|
+
|
|
102
|
+
// Act
|
|
103
|
+
try {
|
|
104
|
+
await instance.chat({
|
|
105
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
106
|
+
model: 'qwen-turbo',
|
|
107
|
+
temperature: 0.999,
|
|
108
|
+
});
|
|
109
|
+
} catch (e) {
|
|
110
|
+
expect(e).toEqual({
|
|
111
|
+
endpoint: defaultBaseURL,
|
|
112
|
+
error: {
|
|
113
|
+
cause: { message: 'api is undefined' },
|
|
114
|
+
stack: 'abc',
|
|
115
|
+
},
|
|
116
|
+
errorType: bizErrorType,
|
|
117
|
+
provider,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should return QwenBizError with an cause response with desensitize Url', async () => {
|
|
123
|
+
// Arrange
|
|
124
|
+
const errorInfo = {
|
|
125
|
+
stack: 'abc',
|
|
126
|
+
cause: { message: 'api is undefined' },
|
|
127
|
+
};
|
|
128
|
+
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
|
129
|
+
|
|
130
|
+
instance = new LobeQwenAI({
|
|
131
|
+
apiKey: 'test',
|
|
132
|
+
|
|
133
|
+
baseURL: 'https://api.abc.com/v1',
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
|
137
|
+
|
|
138
|
+
// Act
|
|
139
|
+
try {
|
|
140
|
+
await instance.chat({
|
|
141
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
142
|
+
model: 'qwen-turbo',
|
|
143
|
+
temperature: 0.999,
|
|
144
|
+
});
|
|
145
|
+
} catch (e) {
|
|
146
|
+
expect(e).toEqual({
|
|
147
|
+
endpoint: 'https://api.***.com/v1',
|
|
148
|
+
error: {
|
|
149
|
+
cause: { message: 'api is undefined' },
|
|
150
|
+
stack: 'abc',
|
|
151
|
+
},
|
|
152
|
+
errorType: bizErrorType,
|
|
153
|
+
provider,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should throw an InvalidQwenAPIKey error type on 401 status code', async () => {
|
|
159
|
+
// Mock the API call to simulate a 401 error
|
|
160
|
+
const error = new Error('InvalidApiKey') as any;
|
|
161
|
+
error.status = 401;
|
|
162
|
+
vi.mocked(instance['client'].chat.completions.create).mockRejectedValue(error);
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
await instance.chat({
|
|
166
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
167
|
+
model: 'qwen-turbo',
|
|
168
|
+
temperature: 0.999,
|
|
169
|
+
});
|
|
170
|
+
} catch (e) {
|
|
171
|
+
expect(e).toEqual({
|
|
172
|
+
endpoint: defaultBaseURL,
|
|
173
|
+
error: new Error('InvalidApiKey'),
|
|
174
|
+
errorType: invalidErrorType,
|
|
175
|
+
provider,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('should return AgentRuntimeError for non-OpenAI errors', async () => {
|
|
181
|
+
// Arrange
|
|
182
|
+
const genericError = new Error('Generic Error');
|
|
183
|
+
|
|
184
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(genericError);
|
|
185
|
+
|
|
186
|
+
// Act
|
|
187
|
+
try {
|
|
188
|
+
await instance.chat({
|
|
189
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
190
|
+
model: 'qwen-turbo',
|
|
191
|
+
temperature: 0.999,
|
|
192
|
+
});
|
|
193
|
+
} catch (e) {
|
|
194
|
+
expect(e).toEqual({
|
|
195
|
+
endpoint: defaultBaseURL,
|
|
196
|
+
errorType: 'AgentRuntimeError',
|
|
197
|
+
provider,
|
|
198
|
+
error: {
|
|
199
|
+
name: genericError.name,
|
|
200
|
+
cause: genericError.cause,
|
|
201
|
+
message: genericError.message,
|
|
202
|
+
stack: genericError.stack,
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
describe('DEBUG', () => {
|
|
210
|
+
it('should call debugStream and return StreamingTextResponse when DEBUG_QWEN_CHAT_COMPLETION is 1', async () => {
|
|
211
|
+
// Arrange
|
|
212
|
+
const mockProdStream = new ReadableStream() as any; // 模拟的 prod 流
|
|
213
|
+
const mockDebugStream = new ReadableStream({
|
|
214
|
+
start(controller) {
|
|
215
|
+
controller.enqueue('Debug stream content');
|
|
216
|
+
controller.close();
|
|
217
|
+
},
|
|
218
|
+
}) as any;
|
|
219
|
+
mockDebugStream.toReadableStream = () => mockDebugStream; // 添加 toReadableStream 方法
|
|
220
|
+
|
|
221
|
+
// 模拟 chat.completions.create 返回值,包括模拟的 tee 方法
|
|
222
|
+
(instance['client'].chat.completions.create as Mock).mockResolvedValue({
|
|
223
|
+
tee: () => [mockProdStream, { toReadableStream: () => mockDebugStream }],
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// 保存原始环境变量值
|
|
227
|
+
const originalDebugValue = process.env.DEBUG_QWEN_CHAT_COMPLETION;
|
|
228
|
+
|
|
229
|
+
// 模拟环境变量
|
|
230
|
+
process.env.DEBUG_QWEN_CHAT_COMPLETION = '1';
|
|
231
|
+
vi.spyOn(debugStreamModule, 'debugStream').mockImplementation(() => Promise.resolve());
|
|
232
|
+
|
|
233
|
+
// 执行测试
|
|
234
|
+
// 运行你的测试函数,确保它会在条件满足时调用 debugStream
|
|
235
|
+
// 假设的测试函数调用,你可能需要根据实际情况调整
|
|
236
|
+
await instance.chat({
|
|
237
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
238
|
+
model: 'qwen-turbo',
|
|
239
|
+
stream: true,
|
|
240
|
+
temperature: 0.999,
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// 验证 debugStream 被调用
|
|
244
|
+
expect(debugStreamModule.debugStream).toHaveBeenCalled();
|
|
245
|
+
|
|
246
|
+
// 恢复原始环境变量值
|
|
247
|
+
process.env.DEBUG_QWEN_CHAT_COMPLETION = originalDebugValue;
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
|
|
3
|
+
import { ModelProvider } from '../types';
|
|
4
|
+
import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory';
|
|
5
|
+
|
|
6
|
+
export const LobeQwenAI = LobeOpenAICompatibleFactory({
|
|
7
|
+
baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
|
8
|
+
chatCompletion: {
|
|
9
|
+
handlePayload: (payload) => {
|
|
10
|
+
const top_p = payload.top_p;
|
|
11
|
+
return {
|
|
12
|
+
...payload,
|
|
13
|
+
stream: payload.stream ?? true,
|
|
14
|
+
top_p: top_p && top_p >= 1 ? 0.9999 : top_p,
|
|
15
|
+
} as OpenAI.ChatCompletionCreateParamsStreaming;
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
constructorOptions: {
|
|
19
|
+
defaultHeaders: {
|
|
20
|
+
'Content-Type': 'application/json',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
debug: {
|
|
24
|
+
chatCompletion: () => process.env.DEBUG_QWEN_CHAT_COMPLETION === '1',
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
provider: ModelProvider.Qwen,
|
|
28
|
+
});
|
|
@@ -10,8 +10,9 @@ import { LobeTogetherAI } from './index';
|
|
|
10
10
|
|
|
11
11
|
const provider = 'togetherai';
|
|
12
12
|
const defaultBaseURL = 'https://api.together.xyz/v1';
|
|
13
|
-
|
|
14
|
-
const
|
|
13
|
+
|
|
14
|
+
const bizErrorType = 'ProviderBizError';
|
|
15
|
+
const invalidErrorType = 'InvalidProviderAPIKey';
|
|
15
16
|
|
|
16
17
|
// Mock the console.error to avoid polluting test output
|
|
17
18
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { LOBE_DEFAULT_MODEL_LIST } from '@/config/modelProviders';
|
|
2
2
|
|
|
3
|
-
import { AgentRuntimeErrorType } from '../error';
|
|
4
3
|
import { ModelProvider } from '../types';
|
|
5
4
|
import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory';
|
|
6
5
|
import { TogetherAIModel } from './type';
|
|
@@ -17,10 +16,6 @@ export const LobeTogetherAI = LobeOpenAICompatibleFactory({
|
|
|
17
16
|
debug: {
|
|
18
17
|
chatCompletion: () => process.env.DEBUG_TOGETHERAI_CHAT_COMPLETION === '1',
|
|
19
18
|
},
|
|
20
|
-
errorType: {
|
|
21
|
-
bizError: AgentRuntimeErrorType.TogetherAIBizError,
|
|
22
|
-
invalidAPIKey: AgentRuntimeErrorType.InvalidTogetherAIAPIKey,
|
|
23
|
-
},
|
|
24
19
|
models: async ({ apiKey }) => {
|
|
25
20
|
const data = await fetch(`${baseURL}/api/models`, {
|
|
26
21
|
headers: {
|
|
@@ -14,8 +14,8 @@ import { LobeOpenAICompatibleFactory } from './index';
|
|
|
14
14
|
|
|
15
15
|
const provider = 'groq';
|
|
16
16
|
const defaultBaseURL = 'https://api.groq.com/openai/v1';
|
|
17
|
-
const bizErrorType = '
|
|
18
|
-
const invalidErrorType = '
|
|
17
|
+
const bizErrorType = 'ProviderBizError';
|
|
18
|
+
const invalidErrorType = 'InvalidProviderAPIKey';
|
|
19
19
|
|
|
20
20
|
// Mock the console.error to avoid polluting test output
|
|
21
21
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
@@ -34,10 +34,6 @@ const LobeMockProvider = LobeOpenAICompatibleFactory({
|
|
|
34
34
|
debug: {
|
|
35
35
|
chatCompletion: () => process.env.DEBUG_MOCKPROVIDER_CHAT_COMPLETION === '1',
|
|
36
36
|
},
|
|
37
|
-
errorType: {
|
|
38
|
-
bizError: AgentRuntimeErrorType.GroqBizError,
|
|
39
|
-
invalidAPIKey: AgentRuntimeErrorType.InvalidGroqAPIKey,
|
|
40
|
-
},
|
|
41
37
|
provider: ModelProvider.Groq,
|
|
42
38
|
});
|
|
43
39
|
|
|
@@ -148,6 +144,127 @@ describe('LobeOpenAICompatibleFactory', () => {
|
|
|
148
144
|
expect((await reader.read()).done).toBe(true);
|
|
149
145
|
});
|
|
150
146
|
|
|
147
|
+
// https://github.com/lobehub/lobe-chat/issues/2752
|
|
148
|
+
it('should handle burn hair data chunks correctly', async () => {
|
|
149
|
+
const chunks = [
|
|
150
|
+
{
|
|
151
|
+
choices: [],
|
|
152
|
+
created: 0,
|
|
153
|
+
id: '',
|
|
154
|
+
model: '',
|
|
155
|
+
object: '',
|
|
156
|
+
prompt_filter_results: [
|
|
157
|
+
{
|
|
158
|
+
prompt_index: 0,
|
|
159
|
+
content_filter_results: {
|
|
160
|
+
hate: { filtered: false, severity: 'safe' },
|
|
161
|
+
self_harm: { filtered: false, severity: 'safe' },
|
|
162
|
+
sexual: { filtered: false, severity: 'safe' },
|
|
163
|
+
violence: { filtered: false, severity: 'safe' },
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
choices: [
|
|
170
|
+
{
|
|
171
|
+
delta: { content: '', role: 'assistant' },
|
|
172
|
+
finish_reason: null,
|
|
173
|
+
index: 0,
|
|
174
|
+
logprobs: null,
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
created: 1717249403,
|
|
178
|
+
id: 'chatcmpl-9VJIxA3qNM2C2YdAnNYA2KgDYfFnX',
|
|
179
|
+
model: 'gpt-4o-2024-05-13',
|
|
180
|
+
object: 'chat.completion.chunk',
|
|
181
|
+
system_fingerprint: 'fp_5f4bad809a',
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
choices: [{ delta: { content: '1' }, finish_reason: null, index: 0, logprobs: null }],
|
|
185
|
+
created: 1717249403,
|
|
186
|
+
id: 'chatcmpl-9VJIxA3qNM2C2YdAnNYA2KgDYfFnX',
|
|
187
|
+
model: 'gpt-4o-2024-05-13',
|
|
188
|
+
object: 'chat.completion.chunk',
|
|
189
|
+
system_fingerprint: 'fp_5f4bad809a',
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
choices: [{ delta: {}, finish_reason: 'stop', index: 0, logprobs: null }],
|
|
193
|
+
created: 1717249403,
|
|
194
|
+
id: 'chatcmpl-9VJIxA3qNM2C2YdAnNYA2KgDYfFnX',
|
|
195
|
+
model: 'gpt-4o-2024-05-13',
|
|
196
|
+
object: 'chat.completion.chunk',
|
|
197
|
+
system_fingerprint: 'fp_5f4bad809a',
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
choices: [
|
|
201
|
+
{
|
|
202
|
+
content_filter_offsets: { check_offset: 35, start_offset: 35, end_offset: 36 },
|
|
203
|
+
content_filter_results: {
|
|
204
|
+
hate: { filtered: false, severity: 'safe' },
|
|
205
|
+
self_harm: { filtered: false, severity: 'safe' },
|
|
206
|
+
sexual: { filtered: false, severity: 'safe' },
|
|
207
|
+
violence: { filtered: false, severity: 'safe' },
|
|
208
|
+
},
|
|
209
|
+
finish_reason: null,
|
|
210
|
+
index: 0,
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
created: 0,
|
|
214
|
+
id: '',
|
|
215
|
+
model: '',
|
|
216
|
+
object: '',
|
|
217
|
+
},
|
|
218
|
+
];
|
|
219
|
+
const mockStream = new ReadableStream({
|
|
220
|
+
start(controller) {
|
|
221
|
+
chunks.forEach((item) => {
|
|
222
|
+
controller.enqueue(item);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
controller.close();
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(
|
|
229
|
+
mockStream as any,
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const stream: string[] = [];
|
|
233
|
+
const result = await instance.chat({
|
|
234
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
235
|
+
model: 'gpt-3.5-turbo',
|
|
236
|
+
temperature: 0,
|
|
237
|
+
});
|
|
238
|
+
const decoder = new TextDecoder();
|
|
239
|
+
const reader = result.body!.getReader();
|
|
240
|
+
|
|
241
|
+
while (true) {
|
|
242
|
+
const { value, done } = await reader.read();
|
|
243
|
+
if (done) break;
|
|
244
|
+
stream.push(decoder.decode(value));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
expect(stream).toEqual(
|
|
248
|
+
[
|
|
249
|
+
'id: ',
|
|
250
|
+
'event: data',
|
|
251
|
+
'data: {"choices":[],"created":0,"id":"","model":"","object":"","prompt_filter_results":[{"prompt_index":0,"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}}}]}\n',
|
|
252
|
+
'id: chatcmpl-9VJIxA3qNM2C2YdAnNYA2KgDYfFnX',
|
|
253
|
+
'event: text',
|
|
254
|
+
'data: ""\n',
|
|
255
|
+
'id: chatcmpl-9VJIxA3qNM2C2YdAnNYA2KgDYfFnX',
|
|
256
|
+
'event: text',
|
|
257
|
+
'data: "1"\n',
|
|
258
|
+
'id: chatcmpl-9VJIxA3qNM2C2YdAnNYA2KgDYfFnX',
|
|
259
|
+
'event: stop',
|
|
260
|
+
'data: "stop"\n',
|
|
261
|
+
'id: ',
|
|
262
|
+
'event: data',
|
|
263
|
+
'data: {"id":"","index":0}\n',
|
|
264
|
+
].map((item) => `${item}\n`),
|
|
265
|
+
);
|
|
266
|
+
});
|
|
267
|
+
|
|
151
268
|
it('should transform non-streaming response to stream correctly', async () => {
|
|
152
269
|
const mockResponse: OpenAI.ChatCompletion = {
|
|
153
270
|
id: 'a',
|
|
@@ -5,7 +5,7 @@ import { TextToImagePayload } from '@/libs/agent-runtime/types/textToImage';
|
|
|
5
5
|
import { ChatModelCard } from '@/types/llm';
|
|
6
6
|
|
|
7
7
|
import { LobeRuntimeAI } from '../../BaseAI';
|
|
8
|
-
import { ILobeAgentRuntimeErrorType } from '../../error';
|
|
8
|
+
import { AgentRuntimeErrorType, ILobeAgentRuntimeErrorType } from '../../error';
|
|
9
9
|
import { ChatCompetitionOptions, ChatCompletionErrorPayload, ChatStreamPayload } from '../../types';
|
|
10
10
|
import { AgentRuntimeError } from '../createError';
|
|
11
11
|
import { debugStream } from '../debugStream';
|
|
@@ -37,7 +37,7 @@ interface OpenAICompatibleFactoryOptions {
|
|
|
37
37
|
debug?: {
|
|
38
38
|
chatCompletion: () => boolean;
|
|
39
39
|
};
|
|
40
|
-
errorType
|
|
40
|
+
errorType?: {
|
|
41
41
|
bizError: ILobeAgentRuntimeErrorType;
|
|
42
42
|
invalidAPIKey: ILobeAgentRuntimeErrorType;
|
|
43
43
|
};
|
|
@@ -52,19 +52,24 @@ interface OpenAICompatibleFactoryOptions {
|
|
|
52
52
|
export const LobeOpenAICompatibleFactory = ({
|
|
53
53
|
provider,
|
|
54
54
|
baseURL: DEFAULT_BASE_URL,
|
|
55
|
-
errorType
|
|
55
|
+
errorType,
|
|
56
56
|
debug,
|
|
57
57
|
constructorOptions,
|
|
58
58
|
chatCompletion,
|
|
59
59
|
models,
|
|
60
|
-
}: OpenAICompatibleFactoryOptions) =>
|
|
61
|
-
|
|
60
|
+
}: OpenAICompatibleFactoryOptions) => {
|
|
61
|
+
const ErrorType = {
|
|
62
|
+
bizError: errorType?.bizError || AgentRuntimeErrorType.ProviderBizError,
|
|
63
|
+
invalidAPIKey: errorType?.invalidAPIKey || AgentRuntimeErrorType.InvalidProviderAPIKey,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return class LobeOpenAICompatibleAI implements LobeRuntimeAI {
|
|
62
67
|
client: OpenAI;
|
|
63
68
|
|
|
64
69
|
baseURL: string;
|
|
65
70
|
|
|
66
71
|
constructor({ apiKey, baseURL = DEFAULT_BASE_URL, ...res }: ClientOptions) {
|
|
67
|
-
if (!apiKey) throw AgentRuntimeError.createError(ErrorType
|
|
72
|
+
if (!apiKey) throw AgentRuntimeError.createError(ErrorType?.invalidAPIKey);
|
|
68
73
|
|
|
69
74
|
this.client = new OpenAI({ apiKey, baseURL, ...constructorOptions, ...res });
|
|
70
75
|
this.baseURL = this.client.baseURL;
|
|
@@ -286,3 +291,4 @@ export const LobeOpenAICompatibleFactory = ({
|
|
|
286
291
|
});
|
|
287
292
|
}
|
|
288
293
|
};
|
|
294
|
+
};
|
|
@@ -52,7 +52,7 @@ export const transformOpenAIStream = (chunk: OpenAI.ChatCompletionChunk): Stream
|
|
|
52
52
|
return { data: item.finish_reason, id: chunk.id, type: 'stop' };
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
if (item.delta
|
|
55
|
+
if (item.delta?.content === null) {
|
|
56
56
|
return { data: item.delta, id: chunk.id, type: 'data' };
|
|
57
57
|
}
|
|
58
58
|
|
|
@@ -9,8 +9,9 @@ import { LobeZeroOneAI } from './index';
|
|
|
9
9
|
|
|
10
10
|
const provider = 'zeroone';
|
|
11
11
|
const defaultBaseURL = 'https://api.lingyiwanwu.com/v1';
|
|
12
|
-
|
|
13
|
-
const
|
|
12
|
+
|
|
13
|
+
const bizErrorType = 'ProviderBizError';
|
|
14
|
+
const invalidErrorType = 'InvalidProviderAPIKey';
|
|
14
15
|
|
|
15
16
|
// Mock the console.error to avoid polluting test output
|
|
16
17
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { AgentRuntimeErrorType } from '../error';
|
|
2
1
|
import { ModelProvider } from '../types';
|
|
3
2
|
import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory';
|
|
4
3
|
|
|
@@ -7,9 +6,6 @@ export const LobeZeroOneAI = LobeOpenAICompatibleFactory({
|
|
|
7
6
|
debug: {
|
|
8
7
|
chatCompletion: () => process.env.DEBUG_ZEROONE_CHAT_COMPLETION === '1',
|
|
9
8
|
},
|
|
10
|
-
|
|
11
|
-
bizError: AgentRuntimeErrorType.ZeroOneBizError,
|
|
12
|
-
invalidAPIKey: AgentRuntimeErrorType.InvalidZeroOneAPIKey,
|
|
13
|
-
},
|
|
9
|
+
|
|
14
10
|
provider: ModelProvider.ZeroOne,
|
|
15
11
|
});
|
|
@@ -8,6 +8,9 @@ import * as debugStreamModule from '@/libs/agent-runtime/utils/debugStream';
|
|
|
8
8
|
import * as authTokenModule from './authToken';
|
|
9
9
|
import { LobeZhipuAI } from './index';
|
|
10
10
|
|
|
11
|
+
const bizErrorType = 'ProviderBizError';
|
|
12
|
+
const invalidErrorType = 'InvalidProviderAPIKey';
|
|
13
|
+
|
|
11
14
|
// Mock相关依赖
|
|
12
15
|
vi.mock('./authToken');
|
|
13
16
|
|
|
@@ -33,7 +36,7 @@ describe('LobeZhipuAI', () => {
|
|
|
33
36
|
try {
|
|
34
37
|
await LobeZhipuAI.fromAPIKey({ apiKey: 'asd' });
|
|
35
38
|
} catch (e) {
|
|
36
|
-
expect(e).toEqual({ errorType:
|
|
39
|
+
expect(e).toEqual({ errorType: invalidErrorType });
|
|
37
40
|
}
|
|
38
41
|
});
|
|
39
42
|
});
|
|
@@ -164,7 +167,7 @@ describe('LobeZhipuAI', () => {
|
|
|
164
167
|
error: { message: 'Bad Request' },
|
|
165
168
|
status: 400,
|
|
166
169
|
},
|
|
167
|
-
errorType:
|
|
170
|
+
errorType: bizErrorType,
|
|
168
171
|
provider: 'zhipu',
|
|
169
172
|
});
|
|
170
173
|
}
|
|
@@ -174,7 +177,7 @@ describe('LobeZhipuAI', () => {
|
|
|
174
177
|
try {
|
|
175
178
|
await LobeZhipuAI.fromAPIKey({ apiKey: '' });
|
|
176
179
|
} catch (e) {
|
|
177
|
-
expect(e).toEqual({ errorType:
|
|
180
|
+
expect(e).toEqual({ errorType: invalidErrorType });
|
|
178
181
|
}
|
|
179
182
|
});
|
|
180
183
|
|
|
@@ -204,7 +207,7 @@ describe('LobeZhipuAI', () => {
|
|
|
204
207
|
cause: { message: 'api is undefined' },
|
|
205
208
|
stack: 'abc',
|
|
206
209
|
},
|
|
207
|
-
errorType:
|
|
210
|
+
errorType: bizErrorType,
|
|
208
211
|
provider: 'zhipu',
|
|
209
212
|
});
|
|
210
213
|
}
|
|
@@ -240,7 +243,7 @@ describe('LobeZhipuAI', () => {
|
|
|
240
243
|
cause: { message: 'api is undefined' },
|
|
241
244
|
stack: 'abc',
|
|
242
245
|
},
|
|
243
|
-
errorType:
|
|
246
|
+
errorType: bizErrorType,
|
|
244
247
|
provider: 'zhipu',
|
|
245
248
|
});
|
|
246
249
|
}
|
|
@@ -31,7 +31,7 @@ export class LobeZhipuAI implements LobeRuntimeAI {
|
|
|
31
31
|
|
|
32
32
|
static async fromAPIKey({ apiKey, baseURL = DEFAULT_BASE_URL, ...res }: ClientOptions) {
|
|
33
33
|
const invalidZhipuAPIKey = AgentRuntimeError.createError(
|
|
34
|
-
AgentRuntimeErrorType.
|
|
34
|
+
AgentRuntimeErrorType.InvalidProviderAPIKey,
|
|
35
35
|
);
|
|
36
36
|
|
|
37
37
|
if (!apiKey) throw invalidZhipuAPIKey;
|
|
@@ -70,7 +70,7 @@ export class LobeZhipuAI implements LobeRuntimeAI {
|
|
|
70
70
|
} catch (error) {
|
|
71
71
|
const { errorResult, RuntimeError } = handleOpenAIError(error);
|
|
72
72
|
|
|
73
|
-
const errorType = RuntimeError || AgentRuntimeErrorType.
|
|
73
|
+
const errorType = RuntimeError || AgentRuntimeErrorType.ProviderBizError;
|
|
74
74
|
let desensitizedEndpoint = this.baseURL;
|
|
75
75
|
|
|
76
76
|
if (this.baseURL !== DEFAULT_BASE_URL) {
|