|
| 1 | +import { Message } from 'src/shared/types' |
| 2 | +import { ApiError, ChatboxAIAPIError } from './errors' |
| 3 | +import Base, { onResultChange } from './base' |
| 4 | + |
| 5 | +interface Options { |
| 6 | + arkApiKey: string |
| 7 | + arkBaseURL: string |
| 8 | + arkModel: ArkModel | 'custom-model' |
| 9 | + arkEndpointId: string |
| 10 | + temperature: number |
| 11 | + topP: number |
| 12 | +} |
| 13 | + |
| 14 | +export default class VolcArk extends Base { |
| 15 | + public name = 'VolcengineArk' |
| 16 | + |
| 17 | + public options: Options |
| 18 | + constructor(options: Options) { |
| 19 | + super() |
| 20 | + this.options = options |
| 21 | + this.options.arkBaseURL = this.options.arkBaseURL || 'https://ark.cn-beijing.volces.com/api/v3' |
| 22 | + } |
| 23 | + |
| 24 | + async callChatCompletion( |
| 25 | + rawMessages: Message[], |
| 26 | + signal?: AbortSignal, |
| 27 | + onResultChange?: onResultChange |
| 28 | + ): Promise<string> { |
| 29 | + try { |
| 30 | + return await this._callChatCompletion(rawMessages, signal, onResultChange) |
| 31 | + } catch (e) { |
| 32 | + if ( |
| 33 | + e instanceof ApiError && |
| 34 | + e.message.includes('Invalid content type. image_url is only supported by certain models.') |
| 35 | + ) { |
| 36 | + throw ChatboxAIAPIError.fromCodeName('model_not_support_image', 'model_not_support_image') |
| 37 | + } |
| 38 | + throw e |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + async _callChatCompletion( |
| 43 | + rawMessages: Message[], |
| 44 | + signal?: AbortSignal, |
| 45 | + onResultChange?: onResultChange |
| 46 | + ): Promise<string> { |
| 47 | + const model = this.options.arkEndpointId |
| 48 | + |
| 49 | + rawMessages = injectModelSystemPrompt(this.options.arkModel, rawMessages) |
| 50 | + |
| 51 | + const messages = await populateGPTMessage(rawMessages) |
| 52 | + return this.requestChatCompletionsStream( |
| 53 | + { |
| 54 | + messages, |
| 55 | + model, |
| 56 | + max_tokens: undefined, |
| 57 | + temperature: this.options.temperature, |
| 58 | + top_p: this.options.topP, |
| 59 | + stream: true, |
| 60 | + }, |
| 61 | + signal, |
| 62 | + onResultChange |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + async requestChatCompletionsStream( |
| 67 | + requestBody: Record<string, any>, |
| 68 | + signal?: AbortSignal, |
| 69 | + onResultChange?: onResultChange |
| 70 | + ): Promise<string> { |
| 71 | + const apiPath = '/chat/completions' |
| 72 | + const response = await this.post(`${this.options.arkBaseURL}${apiPath}`, this.getHeaders(), requestBody, signal) |
| 73 | + let result = '' |
| 74 | + await this.handleSSE(response, (message) => { |
| 75 | + if (message === '[DONE]') { |
| 76 | + return |
| 77 | + } |
| 78 | + const data = JSON.parse(message) |
| 79 | + if (data.error) { |
| 80 | + throw new ApiError(`Error from OpenAI: ${JSON.stringify(data)}`) |
| 81 | + } |
| 82 | + const text = data.choices[0]?.delta?.content |
| 83 | + if (text !== undefined) { |
| 84 | + result += text |
| 85 | + if (onResultChange) { |
| 86 | + onResultChange(result) |
| 87 | + } |
| 88 | + } |
| 89 | + }) |
| 90 | + return result |
| 91 | + } |
| 92 | + |
| 93 | + async requestChatCompletionsNotStream( |
| 94 | + requestBody: Record<string, any>, |
| 95 | + signal?: AbortSignal, |
| 96 | + onResultChange?: onResultChange |
| 97 | + ): Promise<string> { |
| 98 | + const apiPath = '/chat/completions' |
| 99 | + const response = await this.post(`${this.options.arkBaseURL}${apiPath}`, this.getHeaders(), requestBody, signal) |
| 100 | + const json = await response.json() |
| 101 | + if (json.error) { |
| 102 | + throw new ApiError(`Error from OpenAI: ${JSON.stringify(json)}`) |
| 103 | + } |
| 104 | + if (onResultChange) { |
| 105 | + onResultChange(json.choices[0].message.content) |
| 106 | + } |
| 107 | + return json.choices[0].message.content |
| 108 | + } |
| 109 | + |
| 110 | + getHeaders() { |
| 111 | + const headers: Record<string, string> = { |
| 112 | + Authorization: `Bearer ${this.options.arkApiKey}`, |
| 113 | + 'Content-Type': 'application/json', |
| 114 | + } |
| 115 | + return headers |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +export const arkModelConfigs = { |
| 120 | + 'doubao-1.5-pro-256k': { |
| 121 | + maxTokens: 12_288, |
| 122 | + maxContextTokens: 131_072, |
| 123 | + }, |
| 124 | + 'doubao-1.5-pro-32k': { |
| 125 | + maxTokens: 12_288, |
| 126 | + maxContextTokens: 32_768, |
| 127 | + }, |
| 128 | + 'doubao-1.5-vision-pro-32k': { |
| 129 | + maxTokens: 12_288, |
| 130 | + maxContextTokens: 32_768, |
| 131 | + }, |
| 132 | + 'deepseek-r1': { |
| 133 | + maxTokens: 8192, |
| 134 | + maxContextTokens: 64_000, |
| 135 | + }, |
| 136 | + 'deepseek-v3': { |
| 137 | + maxTokens: 8192, |
| 138 | + maxContextTokens: 64_000, |
| 139 | + }, |
| 140 | +} |
| 141 | +export type ArkModel = keyof typeof arkModelConfigs |
| 142 | +export const models = Array.from(Object.keys(arkModelConfigs)).sort() as ArkModel[] |
| 143 | + |
| 144 | +export async function populateGPTMessage(rawMessages: Message[]): Promise<OpenAIMessage[]> { |
| 145 | + const messages: OpenAIMessage[] = rawMessages.map((m) => ({ |
| 146 | + role: m.role, |
| 147 | + content: m.content, |
| 148 | + })) |
| 149 | + return messages |
| 150 | +} |
| 151 | + |
| 152 | +export function injectModelSystemPrompt(model: string, messages: Message[]) { |
| 153 | + const metadataPrompt = ` |
| 154 | +Current model: ${model} |
| 155 | +Current date: ${new Date().toISOString()} |
| 156 | +
|
| 157 | +` |
| 158 | + let hasInjected = false |
| 159 | + return messages.map((m) => { |
| 160 | + if (m.role === 'system' && !hasInjected) { |
| 161 | + m = { ...m } |
| 162 | + m.content = metadataPrompt + m.content |
| 163 | + hasInjected = true |
| 164 | + } |
| 165 | + return m |
| 166 | + }) |
| 167 | +} |
| 168 | + |
| 169 | +export interface OpenAIMessage { |
| 170 | + role: 'system' | 'user' | 'assistant' |
| 171 | + content: string |
| 172 | + name?: string |
| 173 | +} |
0 commit comments