( systemPrompt: string, messages: Anthropic.Messages.MessageParam[], metadata?: ApiHandlerCreateMessageMetadata, )
| 37 | } |
| 38 | |
| 39 | override async *createMessage( |
| 40 | systemPrompt: string, |
| 41 | messages: Anthropic.Messages.MessageParam[], |
| 42 | metadata?: ApiHandlerCreateMessageMetadata, |
| 43 | ): ApiStream { |
| 44 | const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ |
| 45 | { role: "system", content: systemPrompt }, |
| 46 | ...convertToOpenAiMessages(messages), |
| 47 | ] |
| 48 | |
| 49 | // ------------------------- |
| 50 | // Track token usage |
| 51 | // ------------------------- |
| 52 | const toContentBlocks = ( |
| 53 | blocks: Anthropic.Messages.MessageParam[] | string, |
| 54 | ): Anthropic.Messages.ContentBlockParam[] => { |
| 55 | if (typeof blocks === "string") { |
| 56 | return [{ type: "text", text: blocks }] |
| 57 | } |
| 58 | |
| 59 | const result: Anthropic.Messages.ContentBlockParam[] = [] |
| 60 | for (const msg of blocks) { |
| 61 | if (typeof msg.content === "string") { |
| 62 | result.push({ type: "text", text: msg.content }) |
| 63 | } else if (Array.isArray(msg.content)) { |
| 64 | for (const part of msg.content) { |
| 65 | if (part.type === "text") { |
| 66 | result.push({ type: "text", text: part.text }) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return result |
| 72 | } |
| 73 | |
| 74 | let inputTokens = 0 |
| 75 | try { |
| 76 | inputTokens = await this.countTokens([{ type: "text", text: systemPrompt }, ...toContentBlocks(messages)]) |
| 77 | } catch (err) { |
| 78 | console.error("[LmStudio] Failed to count input tokens:", err) |
| 79 | inputTokens = 0 |
| 80 | } |
| 81 | |
| 82 | let assistantText = "" |
| 83 | |
| 84 | try { |
| 85 | const params: OpenAI.Chat.ChatCompletionCreateParamsStreaming & { draft_model?: string } = { |
| 86 | model: this.getModel().id, |
| 87 | messages: openAiMessages, |
| 88 | temperature: this.options.modelTemperature ?? LMSTUDIO_DEFAULT_TEMPERATURE, |
| 89 | stream: true, |
| 90 | tools: this.convertToolsForOpenAI(metadata?.tools), |
| 91 | tool_choice: metadata?.tool_choice, |
| 92 | parallel_tool_calls: metadata?.parallelToolCalls ?? true, |
| 93 | } |
| 94 | |
| 95 | if (this.options.lmStudioSpeculativeDecodingEnabled && this.options.lmStudioDraftModelId) { |
| 96 | params.draft_model = this.options.lmStudioDraftModelId |
nothing calls this directly
no test coverage detected