forked from stackblitz/bolt.new
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Update constants.ts and fix model name bug
- Loading branch information
Showing
5 changed files
with
143 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { json } from '@remix-run/cloudflare'; | ||
import { MODEL_LIST } from '~/utils/constants'; | ||
import { initializeModelList } from '~/utils/tools'; | ||
|
||
export async function loader() { | ||
return json(MODEL_LIST); | ||
const modelList = await initializeModelList(); | ||
return json(modelList); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import type { ModelInfo, OllamaApiResponse, OllamaModel } from '~/utils/types'; | ||
import { getStaticModels,setModelList } from '~/utils/constants'; | ||
import { env } from 'node:process'; | ||
|
||
|
||
export let MODEL_LIST: ModelInfo[] = [...getStaticModels()]; | ||
|
||
export function getAPIKey(provider: string) { | ||
switch (provider) { | ||
case 'Anthropic': | ||
return env.ANTHROPIC_API_KEY; | ||
case 'OpenAI': | ||
return env.OPENAI_API_KEY; | ||
case 'Google': | ||
return env.GOOGLE_GENERATIVE_AI_API_KEY; | ||
case 'Groq': | ||
return env.GROQ_API_KEY; | ||
case 'OpenRouter': | ||
return env.OPEN_ROUTER_API_KEY; | ||
case 'Deepseek': | ||
return env.DEEPSEEK_API_KEY; | ||
case 'Mistral': | ||
return env.MISTRAL_API_KEY; | ||
case "OpenAILike": | ||
return import.meta.env.OPENAI_LIKE_API_KEY || env.OPENAI_LIKE_API_KEY; | ||
default: | ||
return ""; | ||
} | ||
} | ||
export function getBaseURL( provider: string){ | ||
switch (provider) { | ||
case 'OpenAILike': | ||
return import.meta.env.OPENAI_LIKE_API_BASE_URL || env.OPENAI_LIKE_API_BASE_URL || ""; | ||
case 'Ollama': | ||
return import.meta.env.OLLAMA_API_BASE_URL || env.OLLAMA_API_BASE_URL || "http://localhost:11434"; | ||
default: | ||
return ""; | ||
} | ||
} | ||
|
||
let isInitialized = false; | ||
async function getOllamaModels(): Promise<ModelInfo[]> { | ||
try { | ||
const base_url = getBaseURL("Ollama") ; | ||
const response = await fetch(`${base_url}/api/tags`); | ||
const data = await response.json() as OllamaApiResponse; | ||
return data.models.map((model: OllamaModel) => ({ | ||
name: model.name, | ||
label: `${model.name} (${model.details.parameter_size})`, | ||
provider: 'Ollama', | ||
})); | ||
} catch (e) { | ||
return [{ | ||
name: "Empty", | ||
label: "Empty", | ||
provider: "Ollama" | ||
}]; | ||
} | ||
} | ||
|
||
async function getOpenAILikeModels(): Promise<ModelInfo[]> { | ||
try { | ||
const base_url = getBaseURL("OpenAILike") ; | ||
if (!base_url) { | ||
return [{ | ||
name: "Empty", | ||
label: "Empty", | ||
provider: "OpenAILike" | ||
}]; | ||
} | ||
const api_key = getAPIKey("OpenAILike") ?? ""; | ||
const response = await fetch(`${base_url}/models`, { | ||
headers: { | ||
Authorization: `Bearer ${api_key}`, | ||
} | ||
}); | ||
const res = await response.json() as any; | ||
return res.data.map((model: any) => ({ | ||
name: model.id, | ||
label: model.id, | ||
provider: 'OpenAILike', | ||
})); | ||
}catch (e) { | ||
console.error(e); | ||
return [{ | ||
name: "Empty", | ||
label: "Empty", | ||
provider: "OpenAILike" | ||
}]; | ||
} | ||
} | ||
|
||
|
||
async function initializeModelList(): Promise<ModelInfo[]> { | ||
if (isInitialized) { | ||
return MODEL_LIST; | ||
} | ||
isInitialized = true; | ||
const ollamaModels = await getOllamaModels(); | ||
const openAiLikeModels = await getOpenAILikeModels(); | ||
MODEL_LIST = [...getStaticModels(), ...ollamaModels, ...openAiLikeModels]; | ||
setModelList(MODEL_LIST); | ||
return MODEL_LIST; | ||
} | ||
|
||
export { getOllamaModels, getOpenAILikeModels, initializeModelList }; |