|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | + |
| 3 | +vi.mock('@langchain/openai', () => { |
| 4 | + const MockChatOpenAI = vi.fn(function (this: any, opts: any) { |
| 5 | + this.invoke = vi.fn().mockResolvedValue({ content: 'mock response' }) |
| 6 | + this._opts = opts |
| 7 | + }) |
| 8 | + const MockOpenAIEmbeddings = vi.fn(function (this: any, opts: any) { |
| 9 | + this.embedDocuments = vi.fn().mockResolvedValue([[0.1, 0.2]]) |
| 10 | + this.embedQuery = vi.fn().mockResolvedValue([0.1, 0.2]) |
| 11 | + this._opts = opts |
| 12 | + }) |
| 13 | + return { ChatOpenAI: MockChatOpenAI, OpenAIEmbeddings: MockOpenAIEmbeddings } |
| 14 | +}) |
| 15 | + |
| 16 | +import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai' |
| 17 | +import { makeLLM, makeEmbeddings } from '../cheaperinference' |
| 18 | + |
| 19 | +describe('Cheaper Inference LLM provider', () => { |
| 20 | + beforeEach(() => { |
| 21 | + vi.clearAllMocks() |
| 22 | + }) |
| 23 | + |
| 24 | + it('should create a ChatOpenAI instance with Cheaper Inference defaults', () => { |
| 25 | + const llm = makeLLM({ cheaperinference: 'test-key' }) |
| 26 | + |
| 27 | + expect(ChatOpenAI).toHaveBeenCalledWith( |
| 28 | + expect.objectContaining({ |
| 29 | + model: 'gpt-5.4-mini', |
| 30 | + apiKey: 'test-key', |
| 31 | + configuration: { baseURL: 'https://api.cheaperinference.com/v1' }, |
| 32 | + }), |
| 33 | + ) |
| 34 | + expect(typeof llm.invoke).toBe('function') |
| 35 | + expect(typeof llm.call).toBe('function') |
| 36 | + }) |
| 37 | + |
| 38 | + it('should use configured model and base URL', () => { |
| 39 | + makeLLM({ |
| 40 | + cheaperinference: 'key', |
| 41 | + cheaperinference_model: 'claude-sonnet-5', |
| 42 | + cheaperinference_base: 'https://gateway.example.test/v1', |
| 43 | + }) |
| 44 | + |
| 45 | + expect(ChatOpenAI).toHaveBeenCalledWith( |
| 46 | + expect.objectContaining({ |
| 47 | + model: 'claude-sonnet-5', |
| 48 | + configuration: { baseURL: 'https://gateway.example.test/v1' }, |
| 49 | + }), |
| 50 | + ) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should create embeddings with the OpenAI settings', () => { |
| 54 | + makeEmbeddings({ openai: 'openai-key' }) |
| 55 | + |
| 56 | + expect(OpenAIEmbeddings).toHaveBeenCalledWith( |
| 57 | + expect.objectContaining({ |
| 58 | + model: 'text-embedding-3-large', |
| 59 | + apiKey: 'openai-key', |
| 60 | + }), |
| 61 | + ) |
| 62 | + }) |
| 63 | +}) |
0 commit comments