Skip to content

Commit

Permalink
refactor: 提取 OpenAI API 密钥和请求头生成逻辑以提高代码复用性
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Dec 23, 2024
1 parent ba81a00 commit e1e1e77
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 40 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 17 additions & 18 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions packages/lib/core/src/agent/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import type { ChatAgent, HistoryItem, HistoryModifier, LLMChatParams, UserMessag
import { ENV } from '#/config';
import { extractTextContent } from './utils';

/**
* @returns {(function(string): number)}
*/
function tokensCounter(): (text: string) => number {
return (text) => {
return text.length;
Expand Down
32 changes: 16 additions & 16 deletions packages/lib/core/src/agent/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,20 @@ export async function renderOpenAIMessages(prompt: string | undefined, items: Hi
return messages;
}

class OpenAIBase {
readonly name = 'openai';
apikey = (context: AgentUserConfig): string => {
const length = context.OPENAI_API_KEY.length;
return context.OPENAI_API_KEY[Math.floor(Math.random() * length)];
function openAIApiKey(context: AgentUserConfig): string {
const length = context.OPENAI_API_KEY.length;
return context.OPENAI_API_KEY[Math.floor(Math.random() * length)];
}

function openAIHeaders(context: AgentUserConfig): Record<string, string> {
return {
'Content-Type': 'application/json',
'Authorization': `Bearer ${openAIApiKey(context)}`
};
}

export class OpenAI extends OpenAIBase implements ChatAgent {
export class OpenAI implements ChatAgent {
readonly name = 'openai';
readonly modelKey = 'OPENAI_CHAT_MODEL';

readonly enable: AgentEnable = (context: AgentUserConfig): boolean => {
Expand All @@ -93,10 +98,7 @@ export class OpenAI extends OpenAIBase implements ChatAgent {
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 = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apikey(context)}`,
};
const header = openAIHeaders(context);
const body = {
model: context.OPENAI_CHAT_MODEL,
...context.OPENAI_API_EXTRA_PARAMS,
Expand All @@ -113,14 +115,15 @@ export class OpenAI extends OpenAIBase implements ChatAgent {
}
return loadModelsList(context.OPENAI_CHAT_MODELS_LIST, async (url): Promise<string[]> => {
const data = await fetch(url, {
headers: { Authorization: `Bearer ${this.apikey(context)}` },
headers: openAIHeaders(context),
}).then(res => res.json()) as any;
return data.data?.map((model: any) => model.id) || [];
});
};
}

export class Dalle extends OpenAIBase implements ImageAgent {
export class Dalle implements ImageAgent {
readonly name = 'openai';
readonly modelKey = 'OPENAI_DALLE_API';

readonly enable: AgentEnable = (context: AgentUserConfig): boolean => {
Expand All @@ -133,10 +136,7 @@ export class Dalle extends OpenAIBase implements ImageAgent {

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',
'Authorization': `Bearer ${this.apikey(context)}`,
};
const header = openAIHeaders(context);
const body: any = {
prompt,
n: 1,
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/core/src/config/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const BUILD_TIMESTAMP = 1734938027;
export const BUILD_VERSION = 'ce28808';
export const BUILD_TIMESTAMP = 1734938667;
export const BUILD_VERSION = 'ba81a00';

0 comments on commit e1e1e77

Please sign in to comment.