Skip to content
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

feat: add dynamic openrouter model list #233

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/components/chat/BaseChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const ModelSelector = ({ model, setModel, provider, setProvider, modelList, prov
</option>
</select>
<select
key={provider}
value={model}
onChange={(e) => setModel(e.target.value)}
className="flex-1 p-2 rounded-lg border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary focus:outline-none focus:ring-2 focus:ring-bolt-elements-focus transition-all"
Expand Down
4 changes: 2 additions & 2 deletions app/lib/.server/llm/stream-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ function extractPropertiesFromMessage(message: Message): { model: string; provid
}

export function streamText(
messages: Messages,
env: Env,
messages: Messages,
env: Env,
options?: StreamingOptions,
apiKeys?: Record<string, string>
) {
Expand Down
52 changes: 47 additions & 5 deletions app/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,34 @@ async function getOpenAILikeModels(): Promise<ModelInfo[]> {
} catch (e) {
return []
}
}

type OpenRouterModelsResponse = {
data: {
name: string;
id: string;
context_length: number;
pricing: {
prompt: number;
completion: number;
}
}[]
};

async function getOpenRouterModels(): Promise<ModelInfo[]> {
const data: OpenRouterModelsResponse = await (await fetch('https://openrouter.ai/api/v1/models', {
headers: {
'Content-Type': 'application/json'
}
})).json();

return data.data.sort((a, b) => a.name.localeCompare(b.name)).map(m => ({
name: m.id,
label: `${m.name} - in:$${(m.pricing.prompt * 1_000_000).toFixed(
2)} out:$${(m.pricing.completion * 1_000_000).toFixed(2)} - context ${Math.floor(
m.context_length / 1000)}k`,
provider: 'OpenRouter'
}));
}

async function getLMStudioModels(): Promise<ModelInfo[]> {
Expand All @@ -124,11 +151,26 @@ async function getLMStudioModels(): Promise<ModelInfo[]> {
}



async function initializeModelList(): Promise<void> {
const ollamaModels = await getOllamaModels();
const openAiLikeModels = await getOpenAILikeModels();
const lmstudioModels = await getLMStudioModels();
MODEL_LIST = [...ollamaModels,...openAiLikeModels, ...staticModels,...lmstudioModels,];
const [
ollamaModels,
openAiLikeModels,
openRouterModels,
lmstudioModels
] = await Promise.all([
getOllamaModels(),
getOpenAILikeModels(),
getOpenRouterModels(),
getLMStudioModels(),
]);
MODEL_LIST = [
...ollamaModels,
...openAiLikeModels,
...staticModels,
...openRouterModels,
...lmstudioModels
];
}
initializeModelList().then();
export { getOllamaModels,getOpenAILikeModels,getLMStudioModels,initializeModelList };
export { getOllamaModels,getOpenAILikeModels,getLMStudioModels,initializeModelList, getOpenRouterModels };