import { ChatCompletionCreateParams, ChatCompletionChunk, ChatCompletion, CreateEmbeddingResponse, EmbeddingCreateParams, ChatCompletionMessageParam } from "openai/resources"; import { OpenAILikeInterface } from "../interfaces/OpenAILike"; import { ChatCompletionStream } from "../utils/stream"; import { AzureOpenAI } from 'openai' export class AzureOpenAIService implements OpenAILikeInterface { apiKey: string apiVersion: string endpoint: string client: AzureOpenAI constructor(apiKey: string, deployment: string, endpoint?: string) { this.apiKey = apiKey this.endpoint = endpoint || process.env.AZURE_OPENAI_ENDPOINT || "" this.apiVersion = process.env.AZURE_OPENAI_API_VERSION || "2024-05-01-preview" if (!this.endpoint) { throw new Error("Azure OpenAI endpoint is not set") } const client = new AzureOpenAI({ endpoint: this.endpoint, apiKey: this.apiKey, apiVersion: this.apiVersion, deployment: deployment }) this.client = client } async chatComplete(params: ChatCompletionCreateParams): Promise | ChatCompletion> { if (params.stream) { const stream = new ChatCompletionStream() this.client.chat.completions.create(params).then(async (response) => { for await (const iterator of response) { stream.write(iterator) } setTimeout(() => { stream.end() }, 500) }) return stream } else { const response = await this.client.chat.completions.create(params) return response } } async embeddings(params: EmbeddingCreateParams): Promise { // const inputs: string[] = typeof params.input === 'string' ? [params.input] : params.input as string[] // const response = await this.client.getEmbeddings(params.model, inputs, { // user: params.user, // dimensions: params.dimensions, // model: params.model // }) // const result: CreateEmbeddingResponse = { // object: "list", // model: params.model, // data: response.data.map(item => ({ // object: "embedding", // embedding: item.embedding, // index: item.index // })), // usage: { // prompt_tokens: response.usage.promptTokens, // total_tokens: response.usage.totalTokens // } // } // return result const response = await this.client.embeddings.create(params) return response } // private toolCallsFormat(toolCalls: ChatCompletionsToolCallUnion[]): ChatCompletionChunk.Choice.Delta.ToolCall[] { // const result: ChatCompletionChunk.Choice.Delta.ToolCall[] = [] // toolCalls.forEach(toolCall => { // const item: ChatCompletionChunk.Choice.Delta.ToolCall = { // index: toolCall.index || 0, // id: toolCall.id, // type: toolCall.type as "function", // function: { // name: (toolCall as ChatCompletionsFunctionToolCall).function.name, // arguments: (toolCall as ChatCompletionsFunctionToolCall).function.arguments // } // } // result.push(item) // }) // return result // } // private messageFormat(messages: ChatCompletionMessageParam[]): ChatRequestMessageUnion[] { // const result: ChatRequestMessageUnion[] = [] // messages.forEach(message => { // if (message.role === "system") { // result.push({ // role: "system", // content: message.content, // name: message.name // }) // } else if (message.role === "user") { // let msg: ChatRequestUserMessage = { // role: "user", // name: message.name, // content: "" // } // if (message.content instanceof Array) { // const contents: ChatMessageContentItemUnion[] = [] // message.content.forEach(part => { // if (part.type === "text") { // contents.push({ // type: "text", // text: part.text // } as ChatMessageTextContentItem) // } else if (part.type === "image_url") { // contents.push({ // type: "image_url", // imageUrl: { // url: part.image_url.url // } // } as ChatMessageImageContentItem) // } // }) // msg.content = contents // } else { // msg.content = message.content // } // result.push(msg) // } else if (message.role === "assistant") { // let msg: ChatRequestAssistantMessage = { // role: "assistant", // name: message.name, // content: "" // } // msg.content = message.content || null // if (message.function_call) { // msg.functionCall = { // name: message.function_call.name, // arguments: message.function_call.arguments // } // } // if (message.tool_calls) { // const toolCalls: ChatCompletionsToolCallUnion[] = [] // let index = 0 // message.tool_calls.forEach(call => { // let toolCall: ChatCompletionsToolCallUnion = { // type: call.type, // id: call.id, // index: index // } // if (call.function) { // (toolCall as ChatCompletionsFunctionToolCall).function = { // name: call.function.name, // arguments: call.function.arguments // } // } // toolCalls.push(toolCall) // index++ // }) // msg.toolCalls = toolCalls // } // result.push(msg) // } else if (message.role === "function") { // let msg: ChatRequestFunctionMessage = { // role: "function", // name: message.name, // content: message.content // } // result.push(msg) // } else if (message.role === "tool") { // let msg: ChatRequestToolMessage = { // role: "tool", // toolCallId: message.tool_call_id, // content: message.content // } // result.push(msg) // } // }) // return result // } }