-
Notifications
You must be signed in to change notification settings - Fork 108
Add support for custom providers and local models #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import type { LatConfig } from '../config.js'; | ||
|
|
||
| export type EmbeddingProvider = { | ||
| name: string; | ||
| apiBase: string; | ||
| model: string; | ||
| dimensions: number; | ||
| headers: (key: string) => Record<string, string>; | ||
| headers: (key?: string) => Record<string, string>; | ||
| }; | ||
|
|
||
| const openai: EmbeddingProvider = { | ||
|
|
@@ -28,8 +30,32 @@ const vercel: EmbeddingProvider = { | |
| }), | ||
| }; | ||
|
|
||
| export function detectProvider(key: string): EmbeddingProvider { | ||
| if (key.startsWith('REPLAY_LAT_LLM_KEY::')) { | ||
| function customProvider(config: LatConfig): EmbeddingProvider | null { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe check more carefully.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, probably want to extract it as variable instead of IIFE |
||
| const base = process.env.LAT_LLM_BASE ?? config.llm_base; | ||
| if (!base) return null; | ||
| return { | ||
| name: 'custom', | ||
| apiBase: base, | ||
| model: process.env.LAT_LLM_MODEL ?? config.llm_model ?? 'default', | ||
| dimensions: | ||
| process.env.LAT_LLM_DIMENSIONS != null | ||
| ? Number(process.env.LAT_LLM_DIMENSIONS) | ||
| : (config.llm_dimensions ?? 1536), | ||
| headers: (k) => { | ||
| const h: Record<string, string> = { | ||
| 'Content-Type': 'application/json', | ||
| }; | ||
| if (k) h['Authorization'] = `Bearer ${k}`; | ||
| return h; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export function detectProvider( | ||
| key: string | undefined, | ||
| config: LatConfig = {}, | ||
| ): EmbeddingProvider { | ||
| if (key?.startsWith('REPLAY_LAT_LLM_KEY::')) { | ||
| const replayUrl = key.slice('REPLAY_LAT_LLM_KEY::'.length); | ||
| return { | ||
| name: 'replay', | ||
|
|
@@ -39,6 +65,13 @@ export function detectProvider(key: string): EmbeddingProvider { | |
| headers: () => ({ 'Content-Type': 'application/json' }), | ||
| }; | ||
| } | ||
|
|
||
| const custom = customProvider(config); | ||
| if (custom) return custom; | ||
|
|
||
| if (!key) { | ||
| throw new Error('No API key configured.'); | ||
| } | ||
| if (key.startsWith('sk-ant-')) { | ||
| throw new Error( | ||
| "Anthropic doesn't offer an embedding model. Set LAT_LLM_KEY to an OpenAI (sk-...) or Vercel AI Gateway (vck_...) key.", | ||
|
|
@@ -47,6 +80,6 @@ export function detectProvider(key: string): EmbeddingProvider { | |
| if (key.startsWith('vck_')) return vercel; | ||
| if (key.startsWith('sk-')) return openai; | ||
| throw new Error( | ||
| `Unrecognized LAT_LLM_KEY prefix. Supported: OpenAI (sk-...), Vercel AI Gateway (vck_...).`, | ||
| `Unrecognized LAT_LLM_KEY prefix. Supported: OpenAI (sk-...), Vercel AI Gateway (vck_...), or set LAT_LLM_BASE for a custom endpoint.`, | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this check? Can it not simply stay as is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to allow connecting to local llama-server without authentication.