-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update chat model selection and settings
- Loading branch information
1 parent
dbfbbc6
commit f6d3cb4
Showing
7 changed files
with
85 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,56 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { useSettings } from './useSettings' | ||
import axios from 'axios' | ||
import { AvailableModels } from '../config/settings' | ||
import type { ModelInfo } from '../config/settings' | ||
|
||
export const useChatModels = () => { | ||
const [settings, setSettings] = useSettings() | ||
const [dynamicModels, setDynamicModels] = useState<string[]>([]) | ||
const [models, setModels] = useState<ModelInfo[]>([]) | ||
const chatSettings = settings.chat | ||
const activeChatModel = chatSettings.model | ||
|
||
const fetchLocalModels = useCallback(async () => { | ||
if (chatSettings.showLocalModels) { | ||
const { | ||
data: { models }, | ||
} = await axios<{ models: { name: string }[] }>( | ||
'http://localhost:11434/api/tags', | ||
) | ||
if (models) { | ||
setDynamicModels(models.map((m) => m.name)) | ||
const fetchAvailableModels = useCallback(async () => { | ||
if (chatSettings.openAIKey) { | ||
try { | ||
const baseUrl = | ||
chatSettings.openAiBaseUrl || 'https://api.openai.com/v1' | ||
const { data } = await axios.get(`${baseUrl}/models`, { | ||
headers: { | ||
Authorization: `Bearer ${chatSettings.openAIKey}`, | ||
}, | ||
}) | ||
|
||
// Filter for chat-capable models | ||
const chatModels = data.data.filter( | ||
(model: ModelInfo) => model.capabilities?.completion_chat === true, | ||
) | ||
|
||
setModels(chatModels) | ||
} catch (error) { | ||
console.log('Failed to fetch models:', error) | ||
setModels([]) | ||
} | ||
} else { | ||
setDynamicModels([]) | ||
} | ||
}, [chatSettings.showLocalModels]) | ||
}, [chatSettings.openAIKey, chatSettings.openAiBaseUrl]) | ||
|
||
useEffect(() => { | ||
fetchLocalModels() | ||
}, [fetchLocalModels]) | ||
|
||
const availableModels = [ | ||
...Object.entries(AvailableModels), | ||
...dynamicModels.map((m) => [m, m]), | ||
] | ||
fetchAvailableModels() | ||
}, [fetchAvailableModels]) | ||
|
||
const setActiveChatModel = (model: AvailableModels) => { | ||
const setActiveChatModel = (model: ModelInfo) => { | ||
setSettings({ | ||
...settings, | ||
chat: { | ||
...chatSettings, | ||
model: model, | ||
model, | ||
}, | ||
}) | ||
} | ||
|
||
return { | ||
availableModels, | ||
models, | ||
activeChatModel, | ||
setActiveChatModel, | ||
fetchLocalModels, | ||
fetchAvailableModels, | ||
} | ||
} |
Oops, something went wrong.