|
| 1 | +import { ChatOpenAI } from "@langchain/openai"; |
| 2 | + |
| 3 | +/** |
| 4 | + * ChatLMStudio extends ChatOpenAI with the Responses API (/v1/responses) |
| 5 | + * for LM Studio local inference. |
| 6 | + * |
| 7 | + * Patches LangChain/OpenAI SDK compatibility issues with LM Studio: |
| 8 | + * - Ensures text.format is always set (LM Studio requires it) |
| 9 | + * - Removes strict:null from tool definitions (LM Studio rejects it) |
| 10 | + */ |
| 11 | +export interface ChatLMStudioInput { |
| 12 | + modelName?: string; |
| 13 | + apiKey?: string; |
| 14 | + configuration?: any; |
| 15 | + temperature?: number; |
| 16 | + maxTokens?: number; |
| 17 | + topP?: number; |
| 18 | + frequencyPenalty?: number; |
| 19 | + streaming?: boolean; |
| 20 | + streamUsage?: boolean; |
| 21 | + [key: string]: any; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Create a fetch wrapper that sanitizes request bodies for LM Studio |
| 26 | + * compatibility. This intercepts at the HTTP level, which is the last |
| 27 | + * stop before the request is sent, guaranteeing all null values in |
| 28 | + * tools are stripped regardless of which LangChain code path produced them. |
| 29 | + */ |
| 30 | +function createLMStudioFetch(baseFetch?: typeof globalThis.fetch): typeof globalThis.fetch { |
| 31 | + const underlyingFetch = baseFetch || globalThis.fetch; |
| 32 | + |
| 33 | + return async (input: string | URL | Request, init?: RequestInit): Promise<Response> => { |
| 34 | + if (init?.body && typeof init.body === "string") { |
| 35 | + try { |
| 36 | + const body = JSON.parse(init.body); |
| 37 | + let modified = false; |
| 38 | + |
| 39 | + // Strip null/undefined values from tool definitions |
| 40 | + if (Array.isArray(body.tools)) { |
| 41 | + body.tools = body.tools.map((tool: Record<string, unknown>) => { |
| 42 | + const cleaned: Record<string, unknown> = {}; |
| 43 | + for (const [key, value] of Object.entries(tool)) { |
| 44 | + if (value !== null && value !== undefined) { |
| 45 | + cleaned[key] = value; |
| 46 | + } |
| 47 | + } |
| 48 | + return cleaned; |
| 49 | + }); |
| 50 | + modified = true; |
| 51 | + } |
| 52 | + |
| 53 | + if (modified) { |
| 54 | + init = { ...init, body: JSON.stringify(body) }; |
| 55 | + } |
| 56 | + } catch { |
| 57 | + // Not JSON, pass through unchanged |
| 58 | + } |
| 59 | + } |
| 60 | + return underlyingFetch(input, init); |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +export class ChatLMStudio extends ChatOpenAI { |
| 65 | + constructor(fields: ChatLMStudioInput) { |
| 66 | + const originalFetch = fields.configuration?.fetch; |
| 67 | + |
| 68 | + super({ |
| 69 | + ...fields, |
| 70 | + useResponsesApi: true, |
| 71 | + configuration: { |
| 72 | + ...fields.configuration, |
| 73 | + // Wrap fetch to sanitize request bodies for LM Studio compatibility |
| 74 | + fetch: createLMStudioFetch(originalFetch), |
| 75 | + }, |
| 76 | + // modelKwargs is spread LAST in ChatOpenAIResponses.invocationParams(), |
| 77 | + // overriding the computed `text` field. Without this, LangChain emits |
| 78 | + // `text: { format: undefined }` (serializes to `text: {}`) which LM Studio |
| 79 | + // rejects with "Required: text.format". |
| 80 | + modelKwargs: { |
| 81 | + ...fields.modelKwargs, |
| 82 | + text: { format: { type: "text" } }, |
| 83 | + }, |
| 84 | + }); |
| 85 | + } |
| 86 | +} |
0 commit comments