🐛 fix: fix fallback behavior of default mode in AgentRuntime (#4813)

* 🐛 fix: fix fallback behavior of default mode in AgentRuntime

* ♻️ refactor: optimize fallback behavior

* 🔨 chore: add unit test
This commit is contained in:
Zhijie He
2024-11-28 01:47:05 +08:00
committed by GitHub
parent 0a7203b5cf
commit e7cb62ea0f
2 changed files with 23 additions and 1 deletions
@@ -370,6 +370,28 @@ describe('initAgentRuntimeWithUserPayload method', () => {
expect(runtime['_runtime']).toBeInstanceOf(LobeWenxinAI);
});
it('OpenAI provider: without apikey with OPENAI_PROXY_URL', async () => {
process.env.OPENAI_PROXY_URL = 'https://proxy.example.com/v1';
const jwtPayload: JWTPayload = {};
const runtime = await initAgentRuntimeWithUserPayload(ModelProvider.OpenAI, jwtPayload);
expect(runtime['_runtime']).toBeInstanceOf(LobeOpenAI);
// 应返回 OPENAI_PROXY_URL
expect(runtime['_runtime'].baseURL).toBe('https://proxy.example.com/v1');
});
it('Qwen AI provider: without apiKey and endpoint with OPENAI_PROXY_URL', async () => {
process.env.OPENAI_PROXY_URL = 'https://proxy.example.com/v1';
const jwtPayload: JWTPayload = {};
const runtime = await initAgentRuntimeWithUserPayload(ModelProvider.Qwen, jwtPayload);
// 假设 LobeQwenAI 是 Qwen 提供者的实现类
expect(runtime['_runtime']).toBeInstanceOf(LobeQwenAI);
// endpoint 不存在,应返回 DEFAULT_BASE_URL
expect(runtime['_runtime'].baseURL).toBe('https://dashscope.aliyuncs.com/compatible-mode/v1');
});
it('Unknown Provider', async () => {
const jwtPayload = {};
const runtime = await initAgentRuntimeWithUserPayload('unknown', jwtPayload);
+1 -1
View File
@@ -33,7 +33,7 @@ const getLlmOptionsFromPayload = (provider: string, payload: JWTPayload) => {
default: {
let upperProvider = provider.toUpperCase();
if (!llmConfig[`${upperProvider}_API_KEY`]) {
if (!( `${upperProvider}_API_KEY` in llmConfig)) {
upperProvider = ModelProvider.OpenAI.toUpperCase(); // Use OpenAI options as default
}