Skip to content

Commit

Permalink
chore: 更新Agent类型以增强可读性和一致性
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Dec 23, 2024
1 parent e07a3ca commit d47f2e0
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 35 deletions.
9 changes: 6 additions & 3 deletions packages/lib/core/src/agent/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import type { AgentUserConfig } from '#/config';
import type { SseChatCompatibleOptions } from './request';
import type { SSEMessage, SSEParserResult } from './stream';
import type {
AgentEnable,
AgentModel,
ChatAgent,
ChatAgentRequest,
ChatAgentResponse,
ChatStreamTextHandler,
HistoryItem,
Expand All @@ -18,7 +21,7 @@ export class Anthropic implements ChatAgent {
readonly name = 'anthropic';
readonly modelKey = 'ANTHROPIC_CHAT_MODEL';

readonly enable = (context: AgentUserConfig): boolean => {
readonly enable: AgentEnable = (context: AgentUserConfig): boolean => {
return !!(context.ANTHROPIC_API_KEY);
};

Expand Down Expand Up @@ -57,7 +60,7 @@ export class Anthropic implements ChatAgent {
return res;
};

readonly model = (ctx: AgentUserConfig): string | null => {
readonly model: AgentModel = (ctx: AgentUserConfig): string | null => {
return ctx.ANTHROPIC_CHAT_MODEL;
};

Expand Down Expand Up @@ -86,7 +89,7 @@ export class Anthropic implements ChatAgent {
}
}

readonly request = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
readonly request: ChatAgentRequest = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
const { prompt, messages } = params;
const url = `${context.ANTHROPIC_API_BASE}/messages`;
const header = {
Expand Down
16 changes: 10 additions & 6 deletions packages/lib/core/src/agent/azure.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { AgentUserConfig } from '#/config';
import type {
AgentEnable,
AgentModel,
ChatAgent,
ChatAgentRequest,
ChatAgentResponse,
ChatStreamTextHandler,
ImageAgent,
ImageAgentRequest,
LLMChatParams,
} from './types';
import { ImageSupportFormat, renderOpenAIMessages } from './openai';
Expand All @@ -14,15 +18,15 @@ export class AzureChatAI implements ChatAgent {
readonly name = 'azure';
readonly modelKey = 'AZURE_CHAT_MODEL';

readonly enable = (context: AgentUserConfig): boolean => {
readonly enable: AgentEnable = (context: AgentUserConfig): boolean => {
return !!(context.AZURE_API_KEY && context.AZURE_RESOURCE_NAME);
};

readonly model = (ctx: AgentUserConfig): string | null => {
readonly model: AgentModel = (ctx: AgentUserConfig): string | null => {
return ctx.AZURE_CHAT_MODEL;
};

readonly request = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
readonly request: ChatAgentRequest = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
const { prompt, messages } = params;
const url = `https://${context.AZURE_RESOURCE_NAME}.openai.azure.com/openai/deployments/${context.AZURE_CHAT_MODEL}/chat/completions?api-version=${context.AZURE_API_VERSION}`;
const header = {
Expand All @@ -46,15 +50,15 @@ export class AzureImageAI implements ImageAgent {
readonly name = 'azure';
readonly modelKey = 'AZURE_DALLE_API';

readonly enable = (context: AgentUserConfig): boolean => {
readonly enable: AgentEnable = (context: AgentUserConfig): boolean => {
return !!(context.AZURE_API_KEY && context.AZURE_DALLE_API);
};

readonly model = (ctx: AgentUserConfig) => {
readonly model: AgentModel = (ctx: AgentUserConfig) => {
return ctx.AZURE_IMAGE_MODEL;
};

readonly request = async (prompt: string, context: AgentUserConfig): Promise<string | Blob> => {
readonly request: ImageAgentRequest = async (prompt: string, context: AgentUserConfig): Promise<string | Blob> => {
const url = `https://${context.AZURE_RESOURCE_NAME}.openai.azure.com/openai/deployments/${context.AZURE_IMAGE_MODEL}/images/generations?api-version=${context.AZURE_API_VERSION}`;
const header = {
'Content-Type': 'application/json',
Expand Down
16 changes: 12 additions & 4 deletions packages/lib/core/src/agent/cohere.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import type { AgentUserConfig } from '#/config';
import type { SseChatCompatibleOptions } from './request';
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, LLMChatParams } from './types';
import type {
AgentEnable,
AgentModel,
ChatAgent,
ChatAgentRequest,
ChatAgentResponse,
ChatStreamTextHandler,
LLMChatParams,
} from './types';
import { renderOpenAIMessages } from './openai';
import { requestChatCompletions } from './request';
import { convertStringToResponseMessages, loadModelsList } from './utils';
Expand All @@ -9,15 +17,15 @@ export class Cohere implements ChatAgent {
readonly name = 'cohere';
readonly modelKey = 'COHERE_CHAT_MODEL';

readonly enable = (context: AgentUserConfig): boolean => {
readonly enable: AgentEnable = (context: AgentUserConfig): boolean => {
return !!(context.COHERE_API_KEY);
};

readonly model = (ctx: AgentUserConfig): string | null => {
readonly model: AgentModel = (ctx: AgentUserConfig): string | null => {
return ctx.COHERE_CHAT_MODEL;
};

readonly request = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
readonly request: ChatAgentRequest = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
const { prompt, messages } = params;
const url = `${context.COHERE_API_BASE}/chat`;
const header = {
Expand Down
16 changes: 12 additions & 4 deletions packages/lib/core/src/agent/gemini.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import type { AgentUserConfig } from '#/config';
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, LLMChatParams } from './types';
import type {
AgentEnable,
AgentModel,
ChatAgent,
ChatAgentRequest,
ChatAgentResponse,
ChatStreamTextHandler,
LLMChatParams,
} from './types';
import { ImageSupportFormat, renderOpenAIMessages } from './openai';
import { requestChatCompletions } from './request';
import { convertStringToResponseMessages, loadModelsList } from './utils';
Expand All @@ -8,15 +16,15 @@ export class Gemini implements ChatAgent {
readonly name = 'gemini';
readonly modelKey = 'GOOGLE_COMPLETIONS_MODEL';

readonly enable = (context: AgentUserConfig): boolean => {
readonly enable: AgentEnable = (context: AgentUserConfig): boolean => {
return !!(context.GOOGLE_API_KEY);
};

readonly model = (ctx: AgentUserConfig): string => {
readonly model: AgentModel = (ctx: AgentUserConfig): string => {
return ctx.GOOGLE_COMPLETIONS_MODEL;
};

readonly request = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
readonly request: ChatAgentRequest = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
const { prompt, messages } = params;
const url = `${context.GOOGLE_API_BASE}/openai/chat/completions`;
const header = {
Expand Down
16 changes: 12 additions & 4 deletions packages/lib/core/src/agent/mistralai.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import type { AgentUserConfig } from '#/config';
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, LLMChatParams } from './types';
import type {
AgentEnable,
AgentModel,
ChatAgent,
ChatAgentRequest,
ChatAgentResponse,
ChatStreamTextHandler,
LLMChatParams,
} from './types';
import { ImageSupportFormat, renderOpenAIMessages } from './openai';
import { requestChatCompletions } from './request';
import { convertStringToResponseMessages, loadModelsList } from './utils';
Expand All @@ -8,15 +16,15 @@ export class Mistral implements ChatAgent {
readonly name = 'mistral';
readonly modelKey = 'MISTRAL_CHAT_MODEL';

readonly enable = (context: AgentUserConfig): boolean => {
readonly enable: AgentEnable = (context: AgentUserConfig): boolean => {
return !!(context.MISTRAL_API_KEY);
};

readonly model = (ctx: AgentUserConfig): string | null => {
readonly model: AgentModel = (ctx: AgentUserConfig): string | null => {
return ctx.MISTRAL_CHAT_MODEL;
};

readonly request = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
readonly request: ChatAgentRequest = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
const { prompt, messages } = params;
const url = `${context.MISTRAL_API_BASE}/chat/completions`;
const header = {
Expand Down
16 changes: 10 additions & 6 deletions packages/lib/core/src/agent/openai.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { AgentUserConfig } from '#/config';
import type {
AgentEnable,
AgentModel,
ChatAgent,
ChatAgentRequest,
ChatAgentResponse,
ChatStreamTextHandler,
HistoryItem,
ImageAgent,
ImageAgentRequest,
LLMChatParams,
} from './types';
import { ENV } from '#/config';
Expand Down Expand Up @@ -78,15 +82,15 @@ class OpenAIBase {
export class OpenAI extends OpenAIBase implements ChatAgent {
readonly modelKey = 'OPENAI_CHAT_MODEL';

readonly enable = (context: AgentUserConfig): boolean => {
readonly enable: AgentEnable = (context: AgentUserConfig): boolean => {
return context.OPENAI_API_KEY.length > 0;
};

readonly model = (ctx: AgentUserConfig): string | null => {
readonly model: AgentModel = (ctx: AgentUserConfig): string | null => {
return ctx.OPENAI_CHAT_MODEL;
};

readonly request = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
readonly request: ChatAgentRequest = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
const { prompt, messages } = params;
const url = `${context.OPENAI_API_BASE}/chat/completions`;
const header = {
Expand Down Expand Up @@ -119,15 +123,15 @@ export class OpenAI extends OpenAIBase implements ChatAgent {
export class Dalle extends OpenAIBase implements ImageAgent {
readonly modelKey = 'OPENAI_DALLE_API';

readonly enable = (context: AgentUserConfig): boolean => {
readonly enable: AgentEnable = (context: AgentUserConfig): boolean => {
return context.OPENAI_API_KEY.length > 0;
};

readonly model = (ctx: AgentUserConfig): string => {
readonly model: AgentModel = (ctx: AgentUserConfig): string => {
return ctx.DALL_E_MODEL;
};

readonly request = async (prompt: string, context: AgentUserConfig): Promise<string | Blob> => {
readonly request: ImageAgentRequest = async (prompt: string, context: AgentUserConfig): Promise<string | Blob> => {
const url = `${context.OPENAI_API_BASE}/images/generations`;
const header = {
'Content-Type': 'application/json',
Expand Down
6 changes: 4 additions & 2 deletions packages/lib/core/src/agent/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ export interface ChatAgentResponse {
export type ChatStreamTextHandler = (text: string) => Promise<any>;
export type HistoryModifier = (history: HistoryItem[], message: UserMessageItem | null) => HistoryModifierResult;

export type AgentEnable = (context: AgentUserConfig) => boolean;
export type AgentModel = (ctx: AgentUserConfig) => string | null;
export type ChatAgentRequest = (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null) => Promise<ChatAgentResponse>;
export type ImageAgentRequest = (prompt: string, context: AgentUserConfig) => Promise<string | Blob>;

export interface Agent<AgentRequest> {
name: string;
modelKey: string;
enable: (ctx: AgentUserConfig) => boolean;
enable: AgentEnable;
model: AgentModel;
request: AgentRequest;
model: (ctx: AgentUserConfig) => string | null;
}

export interface ChatAgent extends Agent<ChatAgentRequest> {
Expand Down
16 changes: 10 additions & 6 deletions packages/lib/core/src/agent/workersai.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { SseChatCompatibleOptions } from './request';
import type {
AgentEnable,
AgentModel,
ChatAgent,
ChatAgentRequest,
ChatAgentResponse,
ChatStreamTextHandler,
ImageAgent,
ImageAgentRequest,
LLMChatParams,
} from './types';
import { type AgentUserConfig, ENV } from '#/config';
Expand Down Expand Up @@ -32,13 +36,13 @@ function isWorkerAIEnable(context: AgentUserConfig): boolean {
export class WorkersChat implements ChatAgent {
readonly name = 'workers';
readonly modelKey = 'WORKERS_CHAT_MODEL';
readonly enable = isWorkerAIEnable;
readonly enable: AgentEnable = isWorkerAIEnable;

readonly model = (ctx: AgentUserConfig): string | null => {
readonly model: AgentModel = (ctx: AgentUserConfig): string | null => {
return ctx.WORKERS_CHAT_MODEL;
};

readonly request = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
readonly request: ChatAgentRequest = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> => {
const { prompt, messages } = params;
const model = context.WORKERS_CHAT_MODEL;
const body = {
Expand Down Expand Up @@ -89,13 +93,13 @@ export class WorkersChat implements ChatAgent {
export class WorkersImage implements ImageAgent {
readonly name = 'workers';
readonly modelKey = 'WORKERS_IMAGE_MODEL';
readonly enable = isWorkerAIEnable;
readonly enable: AgentEnable = isWorkerAIEnable;

readonly model = (ctx: AgentUserConfig): string => {
readonly model: AgentModel = (ctx: AgentUserConfig): string => {
return ctx.WORKERS_IMAGE_MODEL;
};

readonly request = async (prompt: string, context: AgentUserConfig): Promise<string | Blob> => {
readonly request: ImageAgentRequest = async (prompt: string, context: AgentUserConfig): Promise<string | Blob> => {
const id = context.CLOUDFLARE_ACCOUNT_ID;
const token = context.CLOUDFLARE_TOKEN;
let raw: Response | null = null;
Expand Down

0 comments on commit d47f2e0

Please sign in to comment.