-
Notifications
You must be signed in to change notification settings - Fork 204
providers: add OpenAI compatible provider #185
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
Open
saem
wants to merge
30
commits into
rohitg00:main
Choose a base branch
from
saem:openai-compatible-providers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
caecd86
providers: add OpenAI compatible provider
saem 79b408e
remove the use of mocks
saem 3627620
restore openrouter
saem e233eaf
merge main and resolve local LLM provider conflicts surgically
saem bf12968
use actual base urls everywhere
saem 5a5ac74
fix fs-watcher intermitent timeout
saem 909f05a
restore openrouter
saem b631a7d
remove .gemini directory
saem ebd45af
undo further openrouter changes
saem 585af16
missed a `!`
saem 2e20a85
fix broken api key check
saem 13da1a1
missed vllm
saem b5c4961
don't try to use llama3 as an embedding model
saem edf60f2
fix bad baseurl guidance in readme
saem b14480f
require url and model parameters for lm studio and vllm
saem f95cd2c
more forgiving baseUrl handling
saem 0e93384
use a timeout abort signal in OpenAIProvider
saem e15c9b4
clean-up vllm in embedding providers test setup
saem 22aae21
provide all necessary params
saem 1fabbaf
openai reasoning models set correct max tokens param
saem 144aa4a
set default base url for vllm provider
saem 7acacb9
add a small wait to ensure chokidar is ready
saem 6334687
clean-up mocks
saem ac16ed5
remove unused `extraHeaders` in OpenAIProviders
saem 2238d12
require api key to connect to openapi proper
saem 3979281
address feedback
saem e419ed8
Merge branch 'main' into openai-compatible-providers
saem ba6b298
fix the changelog
saem 43f6780
reasoning model detection if there are prefixes
saem 5994cfa
README: note that OpenAIEmbeddingProvider env var fallback remain
saem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| import type { MemoryProvider } from "../types.js"; | ||
|
|
||
| /** | ||
| * Generic OpenAI-compatible provider. | ||
| * Works with OpenAI, LM Studio, Ollama, vLLM, Groq, OpenRouter, etc. | ||
| */ | ||
| export class OpenAIProvider implements MemoryProvider { | ||
| constructor( | ||
| public name: string, | ||
| private apiKey: string | null, | ||
| private model: string, | ||
| private maxTokens: number, | ||
| private baseUrl: string, | ||
| private timeoutMs: number = 60_000, | ||
| ) {} | ||
|
|
||
| async compress(systemPrompt: string, userPrompt: string): Promise<string> { | ||
| return this.call(systemPrompt, userPrompt); | ||
| } | ||
|
|
||
| async summarize(systemPrompt: string, userPrompt: string): Promise<string> { | ||
| return this.call(systemPrompt, userPrompt); | ||
| } | ||
|
|
||
| private async call( | ||
| systemPrompt: string, | ||
| userPrompt: string, | ||
| ): Promise<string> { | ||
| const base = this.baseUrl.replace(/\/+$/, ""); | ||
| const path = base.endsWith("/v1") ? "/chat/completions" : "/v1/chat/completions"; | ||
| const url = `${base}${path}`; | ||
|
|
||
| // Detect reasoning models (o1, o3, etc) which require max_completion_tokens. | ||
| // We check for the pattern o1- or o3- anywhere in the string to handle prefixes. | ||
| const isReasoningModel = /\bo[13]-/.test(this.model); | ||
|
|
||
| const body: Record<string, any> = { | ||
| model: this.model, | ||
| messages: [ | ||
| { role: "system", content: systemPrompt }, | ||
| { role: "user", content: userPrompt }, | ||
| ], | ||
| }; | ||
|
|
||
| if (isReasoningModel) { | ||
| body.max_completion_tokens = this.maxTokens; | ||
| } else { | ||
| body.max_tokens = this.maxTokens; | ||
| } | ||
|
|
||
| const headers: Record<string, string> = { | ||
| "Content-Type": "application/json", | ||
| }; | ||
| if (this.apiKey) { | ||
| headers.Authorization = `Bearer ${this.apiKey}`; | ||
| } | ||
|
|
||
| const response = await fetch(url, { | ||
| method: "POST", | ||
| signal: AbortSignal.timeout(this.timeoutMs), | ||
| headers, | ||
| body: JSON.stringify(body), | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| if (!response.ok) { | ||
| const text = await response.text(); | ||
| throw new Error(`${this.name} API error (${response.status}): ${text}`); | ||
| } | ||
|
|
||
| const data = (await response.json()) as { | ||
| choices?: Array<{ message?: { content?: string } }>; | ||
| }; | ||
| const content = data.choices?.[0]?.message?.content; | ||
| if (!content) { | ||
| throw new Error( | ||
| `${this.name} returned unexpected response: ${JSON.stringify(data).slice(0, 200)}`, | ||
| ); | ||
| } | ||
| return content; | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.