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:delete ollama models #458

Merged
merged 17 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions electron/main/llm/ipcHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,32 @@
}
await ollamaService.pullModel(modelName, handleProgress)
})

ipcMain.handle('get-available-models', async () => {
Bhavyajain21 marked this conversation as resolved.
Show resolved Hide resolved
try {
const models = await ollamaService.getAvailableModels()
return models
} catch (error) {
console.error('Error fetching available models:', error)

Check warning on line 47 in electron/main/llm/ipcHandlers.ts

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Unexpected console statement

Check warning on line 47 in electron/main/llm/ipcHandlers.ts

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Unexpected console statement

Check warning on line 47 in electron/main/llm/ipcHandlers.ts

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Unexpected console statement

Check warning on line 47 in electron/main/llm/ipcHandlers.ts

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Unexpected console statement
throw error
}
})

ipcMain.handle('delete-llm', async (event, modelName: string) => {
try {
const currentModels: LLMConfig[] = store.get(StoreKeys.LLMs) || []
Bhavyajain21 marked this conversation as resolved.
Show resolved Hide resolved
const foundModel = currentModels.find((model) => model.modelName === modelName)
if (!foundModel) {
throw new Error(`Model ${modelName} not found in the store`)
}
const updatedModels = currentModels.filter((model) => model.modelName !== modelName)
store.set(StoreKeys.LLMs, updatedModels)
await ollamaService.deleteModel(modelName)

return { success: true }
} catch (error: any) {
Bhavyajain21 marked this conversation as resolved.
Show resolved Hide resolved
console.error(`Failed to delete model: ${modelName}`, error)

Check warning on line 65 in electron/main/llm/ipcHandlers.ts

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Unexpected console statement

Check warning on line 65 in electron/main/llm/ipcHandlers.ts

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Unexpected console statement

Check warning on line 65 in electron/main/llm/ipcHandlers.ts

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Unexpected console statement

Check warning on line 65 in electron/main/llm/ipcHandlers.ts

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Unexpected console statement
return { success: false, error: error.message }
Bhavyajain21 marked this conversation as resolved.
Show resolved Hide resolved
Bhavyajain21 marked this conversation as resolved.
Show resolved Hide resolved
}
})
}
2 changes: 2 additions & 0 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ const llm = {
setDefaultLLM: createIPCHandler<(modelName: string) => Promise<void>>('set-default-llm'),
getDefaultLLMName: createIPCHandler<() => Promise<string>>('get-default-llm-name'),
pullOllamaModel: createIPCHandler<(modelName: string) => Promise<void>>('pull-ollama-model'),
deleteLLM: createIPCHandler<(modelNameToDelete: string) => Promise<void>>('delete-llm'),
getAvailableModels: createIPCHandler<() => Promise<LLMConfig[]>>('get-available-models'),
}

// Expose to renderer process
Expand Down
45 changes: 42 additions & 3 deletions src/components/Settings/LLMSettings/DefaultLLMSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useEffect } from 'react'
import { LLMConfig } from 'electron/main/electron-store/storeConfig'
import posthog from 'posthog-js'
import { FiTrash2 } from 'react-icons/fi'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'

interface DefaultLLMSelectorProps {
Expand All @@ -11,6 +12,20 @@

const DefaultLLMSelector: React.FC<DefaultLLMSelectorProps> = ({ llmConfigs, defaultLLM, setDefaultLLM }) => {
const [selectedLLM, setSelectedLLM] = useState(defaultLLM)
const [availableOllamaModels, setAvailableOllamaModels] = useState<LLMConfig[]>([])

useEffect(() => {
const fetchModels = async () => {
try {
const models = await window.llm.getAvailableModels()
Bhavyajain21 marked this conversation as resolved.
Show resolved Hide resolved
setAvailableOllamaModels(models)
} catch (error) {
console.error('Error fetching models:', error)

Check warning on line 23 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Unexpected console statement

Check warning on line 23 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Unexpected console statement

Check warning on line 23 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Unexpected console statement

Check warning on line 23 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Unexpected console statement
}
}

fetchModels()
}, [])

useEffect(() => {
setSelectedLLM(defaultLLM)
Expand All @@ -25,16 +40,40 @@
})
}

const deleteLLM = async (modelName: string) => {
const confirmDelete = window.confirm(`Are you sure you want to delete the model ${modelName}?`)

Check warning on line 44 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Unexpected confirm

Check warning on line 44 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Unexpected confirm

Check warning on line 44 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Unexpected confirm

Check warning on line 44 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Unexpected confirm
if (!confirmDelete) return
try {
await window.llm.deleteLLM(modelName)
posthog.capture('delete_llm', {
modelName,
})
} catch (error) {
console.error(`Failed to delete model: ${modelName}`, error)

Check warning on line 52 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Unexpected console statement

Check warning on line 52 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Unexpected console statement

Check warning on line 52 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Unexpected console statement

Check warning on line 52 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Unexpected console statement
Bhavyajain21 marked this conversation as resolved.
Show resolved Hide resolved
window.alert(`Failed to delete the model ${modelName}`)

Check warning on line 53 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Unexpected alert

Check warning on line 53 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Unexpected alert

Check warning on line 53 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Unexpected alert

Check warning on line 53 in src/components/Settings/LLMSettings/DefaultLLMSelector.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Unexpected alert
}
}

return (
<Select onValueChange={handleDefaultModelChange} value={selectedLLM}>
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Select default LLM" />
</SelectTrigger>
<SelectContent>
{llmConfigs.map((config) => (
<SelectItem key={config.modelName} value={config.modelName}>
{config.modelName}
</SelectItem>
<div key={config.modelName} className="flex w-full items-center justify-end">
<SelectItem className="cursor-pointer" key={config.modelName} value={config.modelName}>
{config.modelName}
</SelectItem>
{availableOllamaModels.some((model) => model.modelName === config.modelName) && (
<FiTrash2
className="ml-2 cursor-pointer text-red-500"
onClick={() => {
deleteLLM(config.modelName)
}}
/>
)}
</div>
))}
</SelectContent>
</Select>
Expand Down
Loading