Skip to content

Commit

Permalink
fix: ollama connection error
Browse files Browse the repository at this point in the history
  • Loading branch information
longy2k committed Jan 14, 2024
1 parent fb9f286 commit e9301db
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 54 deletions.
59 changes: 33 additions & 26 deletions src/components/FetchModelList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export async function fetchOpenAIBaseModels(plugin: BMOGPT) {

const models = list.data.map((model) => model.id);
plugin.settings.openAIBaseModels = models;
console.log(models);

return models;
}
Expand All @@ -21,9 +20,13 @@ export async function fetchOpenAIBaseModels(plugin: BMOGPT) {
export async function fetchOllamaModels(plugin: BMOGPT) {
const ollamaRestAPIUrl = plugin.settings.ollamaRestAPIUrl;

if (!ollamaRestAPIUrl) {
return;
}
// URL Validation
try {
new URL(ollamaRestAPIUrl);
} catch (error) {
// console.error('Invalid OLLAMA URL:', ollamaRestAPIUrl);
return;
}

try {
const response = await requestUrl({
Expand All @@ -39,6 +42,7 @@ export async function fetchOllamaModels(plugin: BMOGPT) {
const models = jsonData.models.map((model: { name: string; }) => model.name);
plugin.settings.ollamaModels = models;


return models;

} catch (error) {
Expand All @@ -47,32 +51,35 @@ export async function fetchOllamaModels(plugin: BMOGPT) {
}

export async function fetchOpenAIRestAPIModels(plugin: BMOGPT) {
const openAIRestAPIUrl = plugin.settings.openAIRestAPIUrl;
const openAIRestAPIUrl = plugin.settings.openAIRestAPIUrl;

if (!openAIRestAPIUrl) {
return;
}
// URL Validation
try {
new URL(openAIRestAPIUrl);
} catch (error) {
// console.error('Invalid OpenAI Rest API URL:', openAIRestAPIUrl);
return;
}

const url = openAIRestAPIUrl + '/v1/models';
const url = openAIRestAPIUrl + '/v1/models';

try {
const response = await requestUrl({
url: url,
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});

const jsonData = response.json;
try {
const response = await requestUrl({
url: url,
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});

const models = jsonData.data.map((model: { id: number; }) => model.id);
const jsonData = response.json;
const models = jsonData.data.map((model: { id: number; }) => model.id);

plugin.settings.openAIRestAPIModels = models;
plugin.settings.openAIRestAPIModels = models;

return models;
return models;

} catch (error) {
console.error('Error:', error);
}
}
} catch (error) {
console.error('Error:', error);
}
}
1 change: 0 additions & 1 deletion src/components/settings/ConnectionSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export function addConnectionSettings(containerEl: HTMLElement, plugin: BMOGPT,
.setValue(plugin.settings.openAIRestAPIUrl || DEFAULT_SETTINGS.openAIRestAPIUrl)
.onChange(async (value) => {
plugin.settings.openAIRestAPIUrl = value ? value : DEFAULT_SETTINGS.openAIRestAPIUrl;
console.log(plugin.settings.openAIRestAPIUrl);
await plugin.saveSettings();
})
.inputEl.addEventListener('focusout', async () => {
Expand Down
27 changes: 7 additions & 20 deletions src/components/settings/GeneralSettings.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
import { DropdownComponent, Notice, Setting, SettingTab } from "obsidian";
import BMOGPT, { DEFAULT_SETTINGS } from "src/main";
import { ANTHROPIC_MODELS, OPENAI_MODELS } from "src/view";
import { fetchOpenAIBaseModels } from "../FetchModelList";
import { fetchOllamaModels, fetchOpenAIBaseModels, fetchOpenAIRestAPIModels } from "../FetchModelList";

export function addGeneralSettings(containerEl: HTMLElement, plugin: BMOGPT, SettingTab: SettingTab, openAIRestAPIModels: string[], ollamaModels: string[]) {
export async function addGeneralSettings(containerEl: HTMLElement, plugin: BMOGPT, SettingTab: SettingTab) {
containerEl.createEl('h2', {text: 'General'});

// new Setting(containerEl)
// .setName('API Key')
// .setDesc('Insert API Key from OpenAI or Anthropic.')
// .addText(text => text
// .setPlaceholder('insert-api-key')
// .setValue(plugin.settings.apiKey ? `${plugin.settings.apiKey.slice(0, 6)}-...${plugin.settings.apiKey.slice(-4)}` : "")
// .onChange(async (value) => {
// plugin.settings.apiKey = value;
// await plugin.saveSettings();
// })
// .inputEl.addEventListener('focusout', async () => {
// SettingTab.display();
// })
// );

// Function to add options to dropdown
const addOptionsToDropdown = (dropdown: DropdownComponent, models: string[]) => {
Expand Down Expand Up @@ -50,6 +35,7 @@ export function addGeneralSettings(containerEl: HTMLElement, plugin: BMOGPT, Set
}
}
if (plugin.settings.ollamaRestAPIUrl && plugin.settings.ollamaModels && plugin.settings.ollamaModels.length > 0) {
const ollamaModels = await fetchOllamaModels(plugin);
try {
ollamaModels.forEach((model: string) => {
dropdown.addOption(model, model);
Expand All @@ -59,11 +45,12 @@ export function addGeneralSettings(containerEl: HTMLElement, plugin: BMOGPT, Set
});
}
catch (error) {
console.error('Error:', error);
// console.error('Error:', error);
new Notice('Ollama connection error.');
}
}
if (plugin.settings.openAIRestAPIUrl && openAIRestAPIModels && openAIRestAPIModels.length > 0) {
if (plugin.settings.openAIRestAPIUrl && plugin.settings.openAIRestAPIModels && plugin.settings.openAIRestAPIModels.length > 0) {
const openAIRestAPIModels = await fetchOpenAIRestAPIModels(plugin);
try {
openAIRestAPIModels.forEach((model: string) => {
dropdown.addOption(model, model);
Expand All @@ -73,7 +60,7 @@ export function addGeneralSettings(containerEl: HTMLElement, plugin: BMOGPT, Set
});
}
catch (error) {
console.error('Error:', error);
// console.error('Error:', error);
new Notice('LocalAI connection error.');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const DEFAULT_SETTINGS: BMOSettings = {
promptFolderPath: '',
prompt: '',
openAIBaseUrl: 'https://api.openai.com/v1',
ollamaRestAPIUrl: 'http://localhost:11434',
ollamaRestAPIUrl: '',
allowOllamaStream: false,
openAIRestAPIUrl: '',
referenceCurrentNote: false,
Expand Down
8 changes: 2 additions & 6 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { addAppearanceSettings } from './components/settings/AppearanceSettings'
import { addChatHistorySettings } from './components/settings/ChatHistorySettings';
import { addOllamaSettings } from './components/settings/OllamaSettings';
import { addConnectionSettings } from './components/settings/ConnectionSettings';
import { fetchOpenAIRestAPIModels, fetchOllamaModels } from './components/FetchModelList';
import { addPromptSettings } from './components/settings/PromptSettings';
// import { fetchOllamaModels, fetchOpenAIRestAPIModels } from './components/FetchModelList';

export class BMOSettingTab extends PluginSettingTab {
plugin: BMOGPT;
Expand All @@ -32,14 +32,10 @@ export class BMOSettingTab extends PluginSettingTab {

containerEl.createEl('p', {text: 'Type `/help` in chat for commands.'});

// Fetch models
const openAIRestAPIModels = await fetchOpenAIRestAPIModels(this.plugin);
const ollamaModels = await fetchOllamaModels(this.plugin);

// Display settings
addConnectionSettings(this.containerEl, this.plugin, this);
addOllamaSettings(this.containerEl, this.plugin, this);
addGeneralSettings(this.containerEl, this.plugin, this, openAIRestAPIModels, ollamaModels);
addGeneralSettings(this.containerEl, this.plugin, this);
addAppearanceSettings(this.containerEl, this.plugin, this);
addChatHistorySettings(this.containerEl, this.plugin, this);
addPromptSettings(this.containerEl, this.plugin, this);
Expand Down

0 comments on commit e9301db

Please sign in to comment.