Skip to content

Commit

Permalink
Merge pull request #104 from karrot0/main
Browse files Browse the repository at this point in the history
feat: lm studio integration
  • Loading branch information
wonderwhy-er authored Nov 11, 2024
2 parents c968948 + 32ae66a commit 4b492b9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ OPENAI_LIKE_API_KEY=
# You only need this environment variable set if you want to use Mistral models
MISTRAL_API_KEY=


# Get LMStudio Base URL from LM Studio Developer Console
# Make sure to enable CORS
# Example: http://localhost:1234
LMSTUDIO_API_BASE_URL=

# Get your xAI API key
# https://x.ai/api
# You only need this environment variable set if you want to use xAI models
Expand Down
3 changes: 3 additions & 0 deletions app/components/chat/BaseChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const ModelSelector = ({ model, setModel, provider, setProvider, modelList, prov
<option key="OpenAILike" value="OpenAILike">
OpenAILike
</option>
<option key="LMStudio" value="LMStudio">
LMStudio
</option>
</select>
<select
value={model}
Expand Down
2 changes: 2 additions & 0 deletions app/lib/.server/llm/api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export function getBaseURL(cloudflareEnv: Env, provider: string) {
switch (provider) {
case 'OpenAILike':
return env.OPENAI_LIKE_API_BASE_URL || cloudflareEnv.OPENAI_LIKE_API_BASE_URL;
case 'LMStudio':
return env.LMSTUDIO_API_BASE_URL || cloudflareEnv.LMSTUDIO_API_BASE_URL || "http://localhost:1234";
case 'Ollama':
let baseUrl = env.OLLAMA_API_BASE_URL || cloudflareEnv.OLLAMA_API_BASE_URL || "http://localhost:11434";
if (env.RUNNING_IN_DOCKER === 'true') {
Expand Down
15 changes: 13 additions & 2 deletions app/lib/.server/llm/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ export function getOpenRouterModel(apiKey: string, model: string) {
return openRouter.chat(model);
}

export function getLMStudioModel(baseURL: string, model: string) {
const lmstudio = createOpenAI({
baseUrl: `${baseURL}/v1`,
apiKey: "",
});

return lmstudio(model);
}

export function getXAIModel(apiKey: string, model: string) {
const openai = createOpenAI({
baseURL: 'https://api.x.ai/v1',
Expand All @@ -105,13 +114,15 @@ export function getModel(provider: string, model: string, env: Env, apiKeys?: Re
case 'OpenRouter':
return getOpenRouterModel(apiKey, model);
case 'Google':
return getGoogleModel(apiKey, model)
return getGoogleModel(apiKey, model);
case 'OpenAILike':
return getOpenAILikeModel(baseURL,apiKey, model);
case 'Deepseek':
return getDeepseekModel(apiKey, model)
return getDeepseekModel(apiKey, model);
case 'Mistral':
return getMistralModel(apiKey, model);
case 'LMStudio':
return getLMStudioModel(baseURL, model);
case 'xAI':
return getXAIModel(apiKey, model);
default:
Expand Down
22 changes: 20 additions & 2 deletions app/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,28 @@ async function getOpenAILikeModels(): Promise<ModelInfo[]> {
}

}

async function getLMStudioModels(): Promise<ModelInfo[]> {
try {
const base_url = import.meta.env.LMSTUDIO_API_BASE_URL || "http://localhost:1234";
const response = await fetch(`${base_url}/v1/models`);
const data = await response.json() as any;
return data.data.map((model: any) => ({
name: model.id,
label: model.id,
provider: 'LMStudio',
}));
} catch (e) {
return [];
}
}


async function initializeModelList(): Promise<void> {
const ollamaModels = await getOllamaModels();
const openAiLikeModels = await getOpenAILikeModels();
MODEL_LIST = [...ollamaModels, ...openAiLikeModels, ...staticModels];
const lmstudioModels = await getLMStudioModels();
MODEL_LIST = [...ollamaModels,...openAiLikeModels, ...staticModels,...lmstudioModels,];
}
initializeModelList().then();
export { getOllamaModels, getOpenAILikeModels, initializeModelList };
export { getOllamaModels,getOpenAILikeModels,getLMStudioModels,initializeModelList };
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default defineConfig((config) => {
chrome129IssuePlugin(),
config.mode === 'production' && optimizeCssModules({ apply: 'build' }),
],
envPrefix:["VITE_","OPENAI_LIKE_API_","OLLAMA_API_BASE_URL"],
envPrefix:["VITE_","OPENAI_LIKE_API_","OLLAMA_API_BASE_URL","LMSTUDIO_API_BASE_URL"],
css: {
preprocessorOptions: {
scss: {
Expand Down
1 change: 1 addition & 0 deletions worker-configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ interface Env {
OPENAI_LIKE_API_KEY: string;
OPENAI_LIKE_API_BASE_URL: string;
DEEPSEEK_API_KEY: string;
LMSTUDIO_API_BASE_URL: string;
}

0 comments on commit 4b492b9

Please sign in to comment.