|
| 1 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 2 | +import axios from "axios" |
| 3 | +import OpenAI from "openai" |
| 4 | +import { ApiHandler } from "../" |
| 5 | +import { ApiHandlerOptions, ModelInfo, glamaDefaultModelId, glamaDefaultModelInfo } from "../../shared/api" |
| 6 | +import { convertToOpenAiMessages } from "../transform/openai-format" |
| 7 | +import { ApiStream } from "../transform/stream" |
| 8 | +import delay from "delay" |
| 9 | + |
| 10 | +export class GlamaHandler implements ApiHandler { |
| 11 | + private options: ApiHandlerOptions |
| 12 | + private client: OpenAI |
| 13 | + |
| 14 | + constructor(options: ApiHandlerOptions) { |
| 15 | + this.options = options |
| 16 | + this.client = new OpenAI({ |
| 17 | + baseURL: "https://glama.ai/api/gateway/openai/v1", |
| 18 | + apiKey: this.options.glamaApiKey, |
| 19 | + }) |
| 20 | + } |
| 21 | + |
| 22 | + async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream { |
| 23 | + // Convert Anthropic messages to OpenAI format |
| 24 | + const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ |
| 25 | + { role: "system", content: systemPrompt }, |
| 26 | + ...convertToOpenAiMessages(messages), |
| 27 | + ] |
| 28 | + |
| 29 | + // this is specifically for claude models (some models may 'support prompt caching' automatically without this) |
| 30 | + if (this.getModel().id.startsWith("anthropic/claude-3")) { |
| 31 | + openAiMessages[0] = { |
| 32 | + role: "system", |
| 33 | + content: [ |
| 34 | + { |
| 35 | + type: "text", |
| 36 | + text: systemPrompt, |
| 37 | + // @ts-ignore-next-line |
| 38 | + cache_control: { type: "ephemeral" }, |
| 39 | + }, |
| 40 | + ], |
| 41 | + } |
| 42 | + |
| 43 | + // Add cache_control to the last two user messages |
| 44 | + // (note: this works because we only ever add one user message at a time, |
| 45 | + // but if we added multiple we'd need to mark the user message before the last assistant message) |
| 46 | + const lastTwoUserMessages = openAiMessages.filter((msg) => msg.role === "user").slice(-2) |
| 47 | + lastTwoUserMessages.forEach((msg) => { |
| 48 | + if (typeof msg.content === "string") { |
| 49 | + msg.content = [{ type: "text", text: msg.content }] |
| 50 | + } |
| 51 | + if (Array.isArray(msg.content)) { |
| 52 | + // NOTE: this is fine since env details will always be added at the end. |
| 53 | + // but if it weren't there, and the user added a image_url type message, |
| 54 | + // it would pop a text part before it and then move it after to the end. |
| 55 | + let lastTextPart = msg.content.filter((part) => part.type === "text").pop() |
| 56 | + |
| 57 | + if (!lastTextPart) { |
| 58 | + lastTextPart = { type: "text", text: "..." } |
| 59 | + msg.content.push(lastTextPart) |
| 60 | + } |
| 61 | + // @ts-ignore-next-line |
| 62 | + lastTextPart["cache_control"] = { type: "ephemeral" } |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + // Required by Anthropic |
| 68 | + // Other providers default to max tokens allowed. |
| 69 | + let maxTokens: number | undefined |
| 70 | + |
| 71 | + if (this.getModel().id.startsWith("anthropic/")) { |
| 72 | + maxTokens = 8_192 |
| 73 | + } |
| 74 | + |
| 75 | + const { data: completion, response } = await this.client.chat.completions.create({ |
| 76 | + model: this.getModel().id, |
| 77 | + max_tokens: maxTokens, |
| 78 | + temperature: 0, |
| 79 | + messages: openAiMessages, |
| 80 | + stream: true, |
| 81 | + }).withResponse(); |
| 82 | + |
| 83 | + const completionRequestId = response.headers.get( |
| 84 | + 'x-completion-request-id', |
| 85 | + ); |
| 86 | + |
| 87 | + for await (const chunk of completion) { |
| 88 | + const delta = chunk.choices[0]?.delta |
| 89 | + |
| 90 | + if (delta?.content) { |
| 91 | + yield { |
| 92 | + type: "text", |
| 93 | + text: delta.content, |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + const response = await axios.get(`https://glama.ai/api/gateway/v1/completion-requests/${completionRequestId}`, { |
| 100 | + headers: { |
| 101 | + Authorization: `Bearer ${this.options.glamaApiKey}`, |
| 102 | + }, |
| 103 | + }) |
| 104 | + |
| 105 | + const completionRequest = response.data; |
| 106 | + |
| 107 | + if (completionRequest.tokenUsage) { |
| 108 | + yield { |
| 109 | + type: "usage", |
| 110 | + inputTokens: completionRequest.tokenUsage.promptTokens, |
| 111 | + outputTokens: completionRequest.tokenUsage.completionTokens, |
| 112 | + totalCost: completionRequest.totalCostUsd, |
| 113 | + } |
| 114 | + } |
| 115 | + } catch (error) { |
| 116 | + // ignore if fails |
| 117 | + console.error("Error fetching Glama generation details:", error) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + getModel(): { id: string; info: ModelInfo } { |
| 122 | + const modelId = this.options.glamaModelId |
| 123 | + const modelInfo = this.options.glamaModelInfo |
| 124 | + |
| 125 | + if (modelId && modelInfo) { |
| 126 | + return { id: modelId, info: modelInfo } |
| 127 | + } |
| 128 | + |
| 129 | + return { id: glamaDefaultModelId, info: glamaDefaultModelInfo } |
| 130 | + } |
| 131 | +} |
0 commit comments