|
| 1 | +import config from './config.js' |
| 2 | +import { ipcMain } from 'electron' |
| 3 | +import path from 'node:path' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | + |
| 6 | +const __dirname = fileURLToPath(new URL('./', import.meta.url)) |
| 7 | + |
| 8 | +const { baseURL, apiKey, model, enabled } = config.llm |
| 9 | + |
| 10 | +let isInitialized = false |
| 11 | + |
| 12 | +ipcMain.handle('llm-supported', async (event) => { |
| 13 | + return isSupported() |
| 14 | +}) |
| 15 | + |
| 16 | +ipcMain.handle('llm-chat', async (event, args) => { |
| 17 | + return chat(args) |
| 18 | +}) |
| 19 | + |
| 20 | +ipcMain.handle('llm-complete', async (event, args) => { |
| 21 | + return complete(args) |
| 22 | +}) |
| 23 | + |
| 24 | +export async function isSupported () { |
| 25 | + if (!enabled) return false |
| 26 | + const has = await hasModel() |
| 27 | + if (has) return true |
| 28 | + return (apiKey === 'ollama') |
| 29 | +} |
| 30 | + |
| 31 | +export function addPreloads (session) { |
| 32 | + const preloadPath = path.join(__dirname, 'llm-preload.js') |
| 33 | + const preloads = session.getPreloads() |
| 34 | + preloads.push(preloadPath) |
| 35 | + session.setPreloads(preloads) |
| 36 | +} |
| 37 | + |
| 38 | +export async function init () { |
| 39 | + if (!enabled) throw new Error('LLM API is not enabled') |
| 40 | + if (isInitialized) return |
| 41 | + // TODO: prompt for download |
| 42 | + if (apiKey === 'ollama') { |
| 43 | + const has = await hasModel() |
| 44 | + if (!has) { |
| 45 | + await pullModel() |
| 46 | + } |
| 47 | + } |
| 48 | + isInitialized = true |
| 49 | +} |
| 50 | + |
| 51 | +async function listModels () { |
| 52 | + const { data } = await get('./models', 'Unable to list models') |
| 53 | + return data |
| 54 | +} |
| 55 | + |
| 56 | +async function pullModel () { |
| 57 | + await post('/api/pull', { |
| 58 | + name: model |
| 59 | + }, `Unable to pull model ${model}`) |
| 60 | +} |
| 61 | + |
| 62 | +async function hasModel () { |
| 63 | + try { |
| 64 | + const models = await listModels() |
| 65 | + |
| 66 | + return !!models.find(({ id }) => id === model) |
| 67 | + } catch { |
| 68 | + return false |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export async function chat ({ |
| 73 | + messages = [], |
| 74 | + temperature, |
| 75 | + maxTokens, |
| 76 | + stop |
| 77 | +}) { |
| 78 | + await init() |
| 79 | + const { choices } = await post('./chat/completions', { |
| 80 | + messages, |
| 81 | + model, |
| 82 | + temperature, |
| 83 | + max_tokens: maxTokens, |
| 84 | + stop |
| 85 | + }, 'Unable to generate completion') |
| 86 | + |
| 87 | + return choices[0].text |
| 88 | +} |
| 89 | + |
| 90 | +export async function complete ({ |
| 91 | + prompt, |
| 92 | + temperature, |
| 93 | + maxTokens, |
| 94 | + stop |
| 95 | +}) { |
| 96 | + await init() |
| 97 | + const { choices } = await post('./completions', { |
| 98 | + prompt, |
| 99 | + model, |
| 100 | + temperature, |
| 101 | + max_tokens: maxTokens, |
| 102 | + stop |
| 103 | + }, 'Unable to generate completion') |
| 104 | + |
| 105 | + return choices[0].text |
| 106 | +} |
| 107 | + |
| 108 | +async function get (path, errorMessage) { |
| 109 | + const url = new URL(path, baseURL).href |
| 110 | + |
| 111 | + const response = await fetch(url, { |
| 112 | + method: 'GET', |
| 113 | + headers: { |
| 114 | + Authorization: `Bearer ${apiKey}` |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + if (!response.ok) { |
| 119 | + throw new Error(`${errorMessage} ${await response.text()}`) |
| 120 | + } |
| 121 | + |
| 122 | + return await response.json() |
| 123 | +} |
| 124 | + |
| 125 | +async function post (path, data, errorMessage) { |
| 126 | + const url = new URL(path, baseURL).href |
| 127 | + |
| 128 | + const response = await fetch(url, { |
| 129 | + method: 'POST', |
| 130 | + headers: { |
| 131 | + 'Content-Type': 'application/json; charset=utf8', |
| 132 | + Authorization: `Bearer ${apiKey}` |
| 133 | + }, |
| 134 | + body: JSON.stringify(data) |
| 135 | + }) |
| 136 | + |
| 137 | + if (!response.ok) { |
| 138 | + throw new Error(`${errorMessage} ${await response.text()}`) |
| 139 | + } |
| 140 | + |
| 141 | + return await response.json() |
| 142 | +} |
0 commit comments