-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
155 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters