-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (provider/openai-compatible): Add 'apiKey' option for concise di…
…rect use. (vercel#4293)
- Loading branch information
Showing
6 changed files
with
232 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
packages/openai-compatible/src/openai-compatible-provider.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
import { createOpenAICompatible } from './openai-compatible-provider'; | ||
import { OpenAICompatibleChatLanguageModel } from './openai-compatible-chat-language-model'; | ||
import { OpenAICompatibleCompletionLanguageModel } from './openai-compatible-completion-language-model'; | ||
import { OpenAICompatibleEmbeddingModel } from './openai-compatible-embedding-model'; | ||
import { OpenAICompatibleChatSettings } from './openai-compatible-chat-settings'; | ||
|
||
const OpenAICompatibleChatLanguageModelMock = vi.mocked( | ||
OpenAICompatibleChatLanguageModel, | ||
); | ||
const OpenAICompatibleCompletionLanguageModelMock = vi.mocked( | ||
OpenAICompatibleCompletionLanguageModel, | ||
); | ||
const OpenAICompatibleEmbeddingModelMock = vi.mocked( | ||
OpenAICompatibleEmbeddingModel, | ||
); | ||
|
||
vi.mock('./openai-compatible-chat-language-model', () => ({ | ||
OpenAICompatibleChatLanguageModel: vi.fn(), | ||
})); | ||
|
||
vi.mock('./openai-compatible-completion-language-model', () => ({ | ||
OpenAICompatibleCompletionLanguageModel: vi.fn(), | ||
})); | ||
|
||
vi.mock('./openai-compatible-embedding-model', () => ({ | ||
OpenAICompatibleEmbeddingModel: vi.fn(), | ||
})); | ||
|
||
describe('OpenAICompatibleProvider', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('createOpenAICompatible', () => { | ||
it('should throw error if baseURL is not provided', () => { | ||
expect(() => createOpenAICompatible({ name: 'test-provider' })).toThrow( | ||
'Base URL is required', | ||
); | ||
}); | ||
|
||
it('should throw error if name is not provided', () => { | ||
expect(() => | ||
createOpenAICompatible({ baseURL: 'https://api.example.com' }), | ||
).toThrow('Provider name is required'); | ||
}); | ||
|
||
it('should create provider with correct configuration', () => { | ||
const options = { | ||
baseURL: 'https://api.example.com', | ||
name: 'test-provider', | ||
apiKey: 'test-api-key', | ||
headers: { 'Custom-Header': 'value' }, | ||
}; | ||
|
||
const provider = createOpenAICompatible(options); | ||
const model = provider('model-id'); | ||
|
||
const constructorCall = | ||
OpenAICompatibleChatLanguageModelMock.mock.calls[0]; | ||
const config = constructorCall[2]; | ||
const headers = config.headers(); | ||
|
||
expect(headers).toEqual({ | ||
Authorization: 'Bearer test-api-key', | ||
'Custom-Header': 'value', | ||
}); | ||
expect(config.provider).toBe('test-provider.chat'); | ||
expect(config.url({ modelId: 'model-id', path: '/v1/chat' })).toBe( | ||
'https://api.example.com/v1/chat', | ||
); | ||
}); | ||
|
||
it('should create headers without Authorization when no apiKey provided', () => { | ||
const options = { | ||
baseURL: 'https://api.example.com', | ||
name: 'test-provider', | ||
headers: { 'Custom-Header': 'value' }, | ||
}; | ||
|
||
const provider = createOpenAICompatible(options); | ||
provider('model-id'); | ||
|
||
const constructorCall = | ||
OpenAICompatibleChatLanguageModelMock.mock.calls[0]; | ||
const config = constructorCall[2]; | ||
const headers = config.headers(); | ||
|
||
expect(headers).toEqual({ | ||
'Custom-Header': 'value', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('model creation methods', () => { | ||
const defaultOptions = { | ||
baseURL: 'https://api.example.com', | ||
name: 'test-provider', | ||
apiKey: 'test-api-key', | ||
headers: { 'Custom-Header': 'value' }, | ||
}; | ||
|
||
it('should create chat model with correct configuration', () => { | ||
const provider = createOpenAICompatible(defaultOptions); | ||
const settings: OpenAICompatibleChatSettings = {}; | ||
|
||
provider.chatModel('chat-model', settings); | ||
|
||
const constructorCall = | ||
OpenAICompatibleChatLanguageModelMock.mock.calls[0]; | ||
const config = constructorCall[2]; | ||
const headers = config.headers(); | ||
|
||
expect(headers).toEqual({ | ||
Authorization: 'Bearer test-api-key', | ||
'Custom-Header': 'value', | ||
}); | ||
expect(config.provider).toBe('test-provider.chat'); | ||
expect(config.url({ modelId: 'model-id', path: '/v1/chat' })).toBe( | ||
'https://api.example.com/v1/chat', | ||
); | ||
}); | ||
|
||
it('should create completion model with correct configuration', () => { | ||
const provider = createOpenAICompatible(defaultOptions); | ||
const settings: OpenAICompatibleChatSettings = {}; | ||
|
||
provider.completionModel('completion-model', settings); | ||
|
||
const constructorCall = | ||
OpenAICompatibleCompletionLanguageModelMock.mock.calls[0]; | ||
const config = constructorCall[2]; | ||
const headers = config.headers(); | ||
|
||
expect(headers).toEqual({ | ||
Authorization: 'Bearer test-api-key', | ||
'Custom-Header': 'value', | ||
}); | ||
expect(config.provider).toBe('test-provider.completion'); | ||
expect( | ||
config.url({ modelId: 'completion-model', path: '/v1/completions' }), | ||
).toBe('https://api.example.com/v1/completions'); | ||
}); | ||
|
||
it('should create embedding model with correct configuration', () => { | ||
const provider = createOpenAICompatible(defaultOptions); | ||
const settings: OpenAICompatibleChatSettings = {}; | ||
|
||
provider.textEmbeddingModel('embedding-model', settings); | ||
|
||
const constructorCall = OpenAICompatibleEmbeddingModelMock.mock.calls[0]; | ||
const config = constructorCall[2]; | ||
const headers = config.headers(); | ||
|
||
expect(headers).toEqual({ | ||
Authorization: 'Bearer test-api-key', | ||
'Custom-Header': 'value', | ||
}); | ||
expect(config.provider).toBe('test-provider.embedding'); | ||
expect( | ||
config.url({ modelId: 'embedding-model', path: '/v1/embeddings' }), | ||
).toBe('https://api.example.com/v1/embeddings'); | ||
}); | ||
|
||
it('should use languageModel as default when called as function', () => { | ||
const provider = createOpenAICompatible(defaultOptions); | ||
const settings: OpenAICompatibleChatSettings = {}; | ||
|
||
provider('model-id', settings); | ||
|
||
expect(OpenAICompatibleChatLanguageModel).toHaveBeenCalledWith( | ||
'model-id', | ||
settings, | ||
expect.objectContaining({ | ||
provider: 'test-provider.chat', | ||
defaultObjectGenerationMode: 'tool', | ||
}), | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters