Skip to content

Commit

Permalink
Merge pull request #73 from goniszewski/71-ollama-url-from-settings-i…
Browse files Browse the repository at this point in the history
…s-not-respected
  • Loading branch information
goniszewski committed Mar 17, 2024
2 parents 9581dea + 46d29ed commit 11362fc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 73 deletions.
28 changes: 12 additions & 16 deletions src/lib/components/AddBookmarkForm/AddBookmarkForm.svelte
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<script lang="ts">
import type { Metadata } from '$lib/types/Metadata.type';
import _ from 'lodash';
import Select from 'svelte-select';
import { writable, type Writable } from 'svelte/store';
import { page } from '$app/stores';
import { enhance } from '$app/forms';
import { onDestroy } from 'svelte';
import { page } from '$app/stores';
import { generateTags } from '$lib/integrations/ollama';
import { showToast } from '$lib/utils/show-toast';
import { validateUrlRegex } from '$lib/utils/regex-library';
import { user } from '$lib/pb';
import { IconInfoCircle } from '@tabler/icons-svelte';
import { searchEngine } from '$lib/stores/search.store';
import { userSettingsStore } from '$lib/stores/user-settings.store';
import type { Metadata } from '$lib/types/Metadata.type';
import { validateUrlRegex } from '$lib/utils/regex-library';
import { addBookmarkToSearchIndex } from '$lib/utils/search';
import { showToast } from '$lib/utils/show-toast';
import { IconInfoCircle } from '@tabler/icons-svelte';
import _ from 'lodash';
import { onDestroy } from 'svelte';
import Select from 'svelte-select';
import { writable, type Writable } from 'svelte/store';
const defaultFormValues: Metadata = {
url: '',
Expand Down Expand Up @@ -136,18 +136,14 @@
.then((data) => {
metadata = data?.metadata || { ...defaultFormValues };
if (
!metadata.content_text ||
!$user.model.settings?.llm ||
!$user.model.settings.llm.enabled
)
if (!metadata.content_text || !$userSettingsStore?.llm || !$userSettingsStore.llm.enabled)
return;
$loadingTags = true;
const generateTagsPromise = generateTags(
metadata.content_text,
$user.model.settings?.llm
$userSettingsStore?.llm?.ollama
);
showToast
Expand Down
75 changes: 32 additions & 43 deletions src/lib/integrations/ollama/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { llmSettings } from '$lib/types/UserSettings.type';
import type { ollamaSettings } from '$lib/types/UserSettings.type';
import { validateUrlRegex } from '$lib/utils/regex-library';

export const defaultConfig = {
url: 'http://localhost:11434/api',
url: 'http://localhost:11434',
defaultOptions: {
stream: false
},
Expand All @@ -18,8 +18,21 @@ export const defaultConfig = {
}
};

export async function listAvailableModels() {
return fetch(`${defaultConfig.url}/tags`)
const getValidApiUrl = (url: string) => {
switch (url.length > 0) {
case url.endsWith('/api/'):
return url.slice(0, -5);
case url.endsWith('/api'):
return url.slice(0, -4);
case url.endsWith('/'):
return url.slice(0, -1);
default:
return url;
}
};

export async function listAvailableModels(providedConfig: Partial<ollamaSettings> = {}) {
return fetch(`${getValidApiUrl(providedConfig?.url || defaultConfig.url)}/api/tags`)
.then((res) => res.json())
.then(
(res: {
Expand All @@ -34,17 +47,13 @@ export async function listAvailableModels() {
async function generate(
prompt: string,
model: string,
options: {
system?: string;
} = {
...defaultConfig.roles.summarize
}
providedConfig: Partial<ollamaSettings> = {}
): Promise<string> {
return fetch(`${defaultConfig.url}/generate`, {
return fetch(`${getValidApiUrl(providedConfig?.url || defaultConfig.url)}/api/generate`, {
method: 'POST',
body: JSON.stringify({
...defaultConfig.defaultOptions,
system: options.system || defaultConfig.roles.summarize.system,
system: providedConfig?.summarize?.system || defaultConfig.roles.summarize.system,
prompt,
model
})
Expand All @@ -56,42 +65,22 @@ async function generate(
export async function summarize(
prompt: string,
model: string,
options: {
system?: string;
} = {}
providedConfig: Partial<ollamaSettings> = {}
) {
return generate(prompt, model, options);
return generate(prompt, model, providedConfig);
}

export async function generateTags(prompt: string, options: llmSettings) {
export async function generateTags(prompt: string, options: ollamaSettings) {
const serializedPrompt = prompt.replace(validateUrlRegex, '');

switch (options.provider) {
case 'ollama':
const response = await generate(
serializedPrompt.length > 1000 ? serializedPrompt.slice(0, 1000) : serializedPrompt,
options.ollama?.model || 'orca-mini',
{
system: defaultConfig.roles.generateTags.system
}
);
const response = await generate(
serializedPrompt.length > 1000 ? serializedPrompt.slice(0, 1000) : serializedPrompt,
options?.model || 'orca-mini',
options
);

return response
.split(',')
.slice(0, 3)
.map((tag) => (tag.split(':')[1] || tag).toLowerCase().trim());
}
}

export async function getModels(url: string) {
return fetch(`${url}/api/tags`)
.then((res) => res.json())
.then(
(res: {
models: {
name: string;
description: string;
}[];
}) => res.models.map((model) => model.name)
);
return response
.split(',')
.slice(0, 3)
.map((tag) => (tag.split(':')[1] || tag).toLowerCase().trim());
}
26 changes: 14 additions & 12 deletions src/lib/types/UserSettings.type.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import type { sortByType } from '$lib/utils/sort-bookmarks';

export type ollamaSettings = {
url: string;
model: string;
generateTags: {
enabled: boolean;
system: string;
};
summarize: {
enabled: boolean;
system: string;
};
};

export type llmSettings = {
enabled: boolean;
provider: 'ollama' | 'openai';
ollama: {
url: string;
model: string;
generateTags: {
enabled: boolean;
system: string;
};
summarize: {
enabled: boolean;
system: string;
};
};
ollama: ollamaSettings;
openai: {
apiKey: string;
};
Expand Down
4 changes: 2 additions & 2 deletions src/routes/settings/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { applyAction, enhance } from '$app/forms';
import { defaultConfig, getModels } from '$lib/integrations/ollama';
import { defaultConfig, listAvailableModels } from '$lib/integrations/ollama';
import { user } from '$lib/pb';
import { userSettingsStore } from '$lib/stores/user-settings.store';
import type { UserSettings } from '$lib/types/UserSettings.type';
Expand All @@ -22,7 +22,7 @@
const url =
document.querySelector<HTMLInputElement>('[name="llmOllamaUrl"]')?.value ||
'http://localhost:11434';
const models = await getModels(url);
const models = await listAvailableModels({ url });
llmModels.set({ models, fetched: true });
showToast.success('Connection successful!');
}
Expand Down

0 comments on commit 11362fc

Please sign in to comment.