Skip to content

Commit

Permalink
🐛 fix: fix fallback behavior of default mode in AgentRuntime (#4813)
Browse files Browse the repository at this point in the history
* 🐛 fix: fix fallback behavior of default mode in AgentRuntime

* ♻️ refactor: optimize fallback behavior

* 🔨 chore: add unit test
  • Loading branch information
hezhijie0327 authored Nov 27, 2024
1 parent 0a7203b commit e7cb62e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/server/modules/AgentRuntime/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/server/modules/AgentRuntime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit e7cb62e

Please sign in to comment.