Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
snakeying authored Oct 9, 2024
1 parent 5fde343 commit 560732e
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dist/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This folder contains the built output assets for the worker "gpt-telegram-worker" generated at 2024-10-09T17:40:34.953Z.
This folder contains the built output assets for the worker "gpt-telegram-worker" generated at 2024-10-09T20:27:42.863Z.
67 changes: 62 additions & 5 deletions dist/index.js

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

6 changes: 3 additions & 3 deletions dist/index.js.map

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions src/api/azure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Env, getConfig } from '../env';
import { ModelAPIInterface } from './model_api_interface';
import { Message } from './openai_api';

interface AzureChatCompletionResponse {
id: string;
object: string;
created: number;
choices: Array<{
index: number;
message: {
role: string;
content: string;
};
finish_reason: string;
}>;
}

export class AzureAPI implements ModelAPIInterface {
private apiKey: string;
private baseUrl: string;
private models: string[];
private defaultModel: string;

constructor(env: Env) {
const config = getConfig(env);
this.apiKey = config.azureApiKey;
this.baseUrl = config.azureEndpoint;
this.models = config.azureModels;
this.defaultModel = this.models[0];
}

async generateResponse(messages: Message[], model?: string): Promise<string> {
const useModel = model || this.defaultModel;
const url = `${this.baseUrl}/openai/deployments/${useModel}/chat/completions?api-version=2024-02-01`;

const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'api-key': this.apiKey,
},
body: JSON.stringify({
messages: messages,
}),
});

if (!response.ok) {
const errorText = await response.text();
console.error(`Azure API error: ${response.statusText}`, errorText);
throw new Error(`Azure API error: ${response.statusText}\n${errorText}`);
}

const data: AzureChatCompletionResponse = await response.json();
if (!data.choices || data.choices.length === 0) {
throw new Error('Azure API 未生成任何响应');
}
return data.choices[0].message.content.trim();
}

isValidModel(model: string): boolean {
return this.models.includes(model);
}

getDefaultModel(): string {
return this.defaultModel;
}

getAvailableModels(): string[] {
return this.models;
}
}

export default AzureAPI;
3 changes: 3 additions & 0 deletions src/api/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ModelAPIInterface } from './model_api_interface';
import GeminiAPI from './gemini';
import GroqAPI from './groq';
import ClaudeAPI from './claude';
import AzureAPI from './azure'; // 新增 AzureAPI 导入

export class TelegramBot {
private token: string;
Expand Down Expand Up @@ -46,6 +47,8 @@ export class TelegramBot {
return new GroqAPI(this.env);
} else if (config.claudeModels.includes(currentModel)) {
return new ClaudeAPI(this.env);
} else if (config.azureModels.includes(currentModel)) { // 新增 Azure 模型检查
return new AzureAPI(this.env);
}

// 如果没有匹配的模型,使用默认的 OpenAI API
Expand Down
5 changes: 3 additions & 2 deletions src/config/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const commands: Command[] = [
const language = await bot.getUserLanguage(userId);
const currentModel = await bot.getCurrentModel(userId);
const welcomeMessage = translate('welcome', language) + '\n' +
translate('current_model', language) + currentModel;
translate('current_model', language) + currentModel;
await bot.sendMessageWithFallback(chatId, welcomeMessage);
},
},
Expand Down Expand Up @@ -56,7 +56,8 @@ export const commands: Command[] = [
...config.openaiModels,
...config.googleModels,
...config.groqModels,
...config.claudeModels // 添加 Claude 模型
...config.claudeModels,
...config.azureModels, // 新增 Azure 模型
];
const keyboard = {
inline_keyboard: availableModels.map(model => [{text: model, callback_data: `model_${model}`}])
Expand Down
11 changes: 9 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export interface Env {
CLAUDE_API_KEY: string;
CLAUDE_MODELS: string;
CLAUDE_ENDPOINT?: string;
AZURE_API_KEY: string; // Azure API 密钥
AZURE_MODELS: string; // 逗号分隔的 Azure 模型列表
AZURE_ENDPOINT: string; // Azure API 端点
}

const getEnvOrDefault = (env: Env, key: keyof Env, defaultValue: string): string => {
Expand All @@ -37,9 +40,10 @@ export const getConfig = (env: Env) => {
const hasGoogle = !!env.GOOGLE_MODEL_KEY;
const hasGroq = !!env.GROQ_API_KEY;
const hasClaude = !!env.CLAUDE_API_KEY;
const hasAzure = !!env.AZURE_API_KEY;

if (!hasOpenAI && !hasGoogle && !hasGroq && !hasClaude) {
throw new Error('At least one model API key must be set (OpenAI, Google, Groq, or Claude)');
if (!hasOpenAI && !hasGoogle && !hasGroq && !hasClaude && !hasAzure) {
throw new Error('At least one model API key must be set (OpenAI, Google, Groq, Claude, or Azure)');
}

return {
Expand Down Expand Up @@ -71,5 +75,8 @@ export const getConfig = (env: Env) => {
claudeApiKey: env.CLAUDE_API_KEY,
claudeModels: env.CLAUDE_MODELS ? env.CLAUDE_MODELS.split(',').map(model => model.trim()) : [],
claudeEndpoint: getEnvOrDefault(env, 'CLAUDE_ENDPOINT', 'https://api.anthropic.com/v1'),
azureApiKey: env.AZURE_API_KEY,
azureModels: env.AZURE_MODELS ? env.AZURE_MODELS.split(',').map(model => model.trim()) : [],
azureEndpoint: env.AZURE_ENDPOINT,
};
};

0 comments on commit 560732e

Please sign in to comment.