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: fetch model list on client #2252

Merged
merged 1 commit into from Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/settings/llm/Ollama/index.tsx
Expand Up @@ -18,12 +18,12 @@ const OllamaProvider = memo(() => {
label: t('llm.checker.title'),
minWidth: undefined,
}}
modelList={{ showModelFetcher: true }}
provider={ModelProvider.Ollama}
showApiKey={false}
showBrowserRequest
showEndpoint
title={<Ollama.Combine size={24} />}
// modelList={{ showModelFetcher: true }}
/>
);
});
Expand Down
13 changes: 7 additions & 6 deletions src/libs/agent-runtime/ollama/index.ts
Expand Up @@ -4,6 +4,7 @@

import { OpenAIChatMessage } from '@/libs/agent-runtime';
import { OllamaStream } from '@/libs/agent-runtime/ollama/stream';
import { ChatModelCard } from '@/types/llm';

import { LobeRuntimeAI } from '../BaseAI';
import { AgentRuntimeErrorType } from '../error';
Expand Down Expand Up @@ -64,12 +65,12 @@
}
}

// async models(): Promise<ChatModelCard[]> {
// const list = await this.client.list();
// return list.models.map((model) => ({
// id: model.name,
// }));
// }
async models(): Promise<ChatModelCard[]> {
const list = await this.client.list();
return list.models.map((model) => ({
id: model.name,
}));
}

Check warning on line 73 in src/libs/agent-runtime/ollama/index.ts

View check run for this annotation

Codecov / codecov/patch

src/libs/agent-runtime/ollama/index.ts#L69-L73

Added lines #L69 - L73 were not covered by tests

private buildOllamaMessages(messages: OpenAIChatMessage[]) {
return messages.map((message) => this.convertContentToOllamaMessage(message));
Expand Down
14 changes: 14 additions & 0 deletions src/services/models.ts
@@ -1,7 +1,10 @@
import { createHeaderWithAuth } from '@/services/_auth';
import { useGlobalStore } from '@/store/global';
import { modelConfigSelectors } from '@/store/global/selectors';
import { ChatModelCard } from '@/types/llm';

import { API_ENDPOINTS } from './_url';
import { initializeWithClientStore } from './chat';

class ModelsService {
getChatModels = async (provider: string): Promise<ChatModelCard[] | undefined> => {
Expand All @@ -10,6 +13,17 @@ class ModelsService {
provider,
});
try {
/**
* Use browser agent runtime
*/
const enableFetchOnClient = modelConfigSelectors.isProviderFetchOnClient(provider)(
useGlobalStore.getState(),
);
if (enableFetchOnClient) {
const agentRuntime = await initializeWithClientStore(provider, {});
return agentRuntime.models();
}

const res = await fetch(API_ENDPOINTS.chatModels(provider), { headers });
if (!res.ok) return;

Expand Down