Skip to content

Commit

Permalink
βœ… test: add test for agent runtime providers (#1417)
Browse files Browse the repository at this point in the history
* βœ… test: add test for agent runtime providers

* βœ… test: fix test

* βœ… test: fix crypto relative test
  • Loading branch information
arvinxx authored Feb 28, 2024
1 parent 87bf1fb commit 6aa1689
Show file tree
Hide file tree
Showing 19 changed files with 1,688 additions and 206 deletions.
15 changes: 8 additions & 7 deletions src/app/api/chat/[provider]/agentRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getServerConfig } from '@/config/server';
import { JWTPayload } from '@/const/auth';
import {
ChatCompetitionOptions,
ChatStreamPayload,
LobeAzureOpenAI,
LobeBedrockAI,
Expand Down Expand Up @@ -29,8 +30,8 @@ class AgentRuntime {
this._runtime = runtime;
}

async chat(payload: ChatStreamPayload) {
return this._runtime.chat(payload);
async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions) {
return this._runtime.chat(payload, options);
}

static async initializeWithUserPayload(
Expand Down Expand Up @@ -123,14 +124,14 @@ class AgentRuntime {
const { ZHIPU_API_KEY } = getServerConfig();
const apiKey = apiKeyManager.pick(payload?.apiKey || ZHIPU_API_KEY);

return LobeZhipuAI.fromAPIKey(apiKey);
return LobeZhipuAI.fromAPIKey({ apiKey });
}

private static initMoonshot(payload: JWTPayload) {
const { MOONSHOT_API_KEY, MOONSHOT_PROXY_URL } = getServerConfig();
const apiKey = apiKeyManager.pick(payload?.apiKey || MOONSHOT_API_KEY);

return new LobeMoonshotAI(apiKey, MOONSHOT_PROXY_URL);
return new LobeMoonshotAI({ apiKey, baseURL: MOONSHOT_PROXY_URL });
}

private static initGoogle(payload: JWTPayload) {
Expand Down Expand Up @@ -158,16 +159,16 @@ class AgentRuntime {

private static initOllama(payload: JWTPayload) {
const { OLLAMA_PROXY_URL } = getServerConfig();
const baseUrl = payload?.endpoint || OLLAMA_PROXY_URL;
const baseURL = payload?.endpoint || OLLAMA_PROXY_URL;

return new LobeOllamaAI(baseUrl);
return new LobeOllamaAI({ baseURL });
}

private static initPerplexity(payload: JWTPayload) {
const { PERPLEXITY_API_KEY } = getServerConfig();
const apiKey = apiKeyManager.pick(payload?.apiKey || PERPLEXITY_API_KEY);

return new LobePerplexityAI(apiKey);
return new LobePerplexityAI({ apiKey });
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/app/api/chat/[provider]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const preferredRegion = getPreferredRegion();

export const POST = async (req: Request, { params }: { params: { provider: string } }) => {
let agentRuntime: AgentRuntime;
const { provider } = params;

// ============ 1. init chat model ============ //

Expand All @@ -34,7 +35,7 @@ export const POST = async (req: Request, { params }: { params: { provider: strin
checkAuthMethod(payload.accessCode, payload.apiKey, oauthAuthorized);

const body = await req.clone().json();
agentRuntime = await AgentRuntime.initializeWithUserPayload(params.provider, payload, {
agentRuntime = await AgentRuntime.initializeWithUserPayload(provider, payload, {
apiVersion: payload.azureApiVersion,
model: body.model,
useAzure: payload.useAzure,
Expand All @@ -44,10 +45,7 @@ export const POST = async (req: Request, { params }: { params: { provider: strin
const err = e as AgentInitErrorPayload;
return createErrorResponse(
(err.errorType || ChatErrorType.InternalServerError) as ILobeAgentRuntimeErrorType,
{
error: err.error || e,
provider: params.provider,
},
{ error: err.error || e, provider },
);
}

Expand Down
7 changes: 5 additions & 2 deletions src/libs/agent-runtime/BaseAI.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { StreamingTextResponse } from 'ai';

import { ChatStreamPayload } from '@/types/openai/chat';
import { ChatCompetitionOptions, ChatStreamPayload } from './types';

export interface LobeRuntimeAI {
baseURL?: string;

chat(payload: ChatStreamPayload): Promise<StreamingTextResponse>;
chat(
payload: ChatStreamPayload,
options?: ChatCompetitionOptions,
): Promise<StreamingTextResponse>;
}
Loading

0 comments on commit 6aa1689

Please sign in to comment.