jan/web/hooks/useUpdateModelParameters.ts
hiro e6c10202e0
feat: Jan can see (#2069)
* feat: jan can see

feat: Add GPT-4 Vision model (Preview)

fix: Add visionModel as property in ModelInfo

fix: Fix condition to load local messages in useSetActiveThread hook

feat: Enable Image as input for chat

fix: Update model parameters in JSON files for remote GPT models

fix: Add thread as optional

fix: Add support for message as image

fix: Linter

fix: Update proxyModel to proxy_model and add textModel

chore: Change proxyModel to proxy_model

fix: Update settings with visionModel and textModel

fix: vision model passed through the retrieval tool

fix: linter

* fix: could not load image and request is not able to be sent

---------

Co-authored-by: Louis <louis@jan.ai>
2024-03-05 08:33:09 +07:00

99 lines
2.6 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import {
ConversationalExtension,
ExtensionTypeEnum,
InferenceEngine,
Thread,
ThreadAssistantInfo,
} from '@janhq/core'
import { useAtomValue, useSetAtom } from 'jotai'
import { selectedModelAtom } from '@/containers/DropdownListSidebar'
import { toRuntimeParams, toSettingParams } from '@/utils/modelParam'
import { extensionManager } from '@/extension'
import {
ModelParams,
activeThreadStateAtom,
getActiveThreadModelParamsAtom,
setThreadModelParamsAtom,
threadsAtom,
} from '@/helpers/atoms/Thread.atom'
export type UpdateModelParameter = {
params?: ModelParams
modelId?: string
engine?: InferenceEngine
}
export default function useUpdateModelParameters() {
const threads = useAtomValue(threadsAtom)
const setThreadModelParams = useSetAtom(setThreadModelParamsAtom)
const activeThreadState = useAtomValue(activeThreadStateAtom)
const activeModelParams = useAtomValue(getActiveThreadModelParamsAtom)
const selectedModel = useAtomValue(selectedModelAtom)
const updateModelParameter = async (
threadId: string,
settings: UpdateModelParameter,
overwrite: boolean = false
) => {
const thread = threads.find((thread) => thread.id === threadId)
if (!thread) {
console.error(`Thread ${threadId} not found`)
return
}
if (!activeThreadState) {
console.error('No active thread')
return
}
const params = settings.modelId
? settings.params
: { ...activeModelParams, ...settings.params }
const updatedModelParams: ModelParams = {
...params,
}
// update the state
setThreadModelParams(thread.id, updatedModelParams)
const assistants = thread.assistants.map(
(assistant: ThreadAssistantInfo) => {
const runtimeParams = toRuntimeParams(updatedModelParams)
const settingParams = toSettingParams(updatedModelParams)
assistant.model.parameters = {
...(overwrite ? {} : assistant.model.parameters),
...runtimeParams,
}
assistant.model.settings = {
...(overwrite ? {} : assistant.model.settings),
...settingParams,
}
if (selectedModel) {
assistant.model.id = settings.modelId ?? selectedModel?.id
assistant.model.engine = settings.engine ?? selectedModel?.engine
}
return assistant
}
)
// update thread
const updatedThread: Thread = {
...thread,
assistants,
}
await extensionManager
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
?.saveThread(updatedThread)
}
return { updateModelParameter }
}