Skip to content

Commit

Permalink
fix: remote engine revamp issues
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-menlo committed Jan 17, 2025
1 parent df6110b commit f328c35
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 16 deletions.
11 changes: 10 additions & 1 deletion web/containers/ModelDropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,22 @@ const ModelDropdown = ({
const modelId = activeAssistant?.model?.id

const model = downloadedModels.find((model) => model.id === modelId)
setSelectedModel(model)
if (model) {
if (

Check warning on line 198 in web/containers/ModelDropdown/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

198 line is not covered with tests
engines?.[model.engine]?.[0].type === 'local' ||
(engines?.[model.engine]?.[0].api_key?.length ?? 0) > 0
)
setSelectedModel(model)
} else {
setSelectedModel(undefined)
}
}, [
recommendedModel,
activeThread,
downloadedModels,
setSelectedModel,
activeAssistant?.model?.id,
engines,
])

const isLocalEngine = useCallback(
Expand Down
7 changes: 3 additions & 4 deletions web/containers/Providers/ModelHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ export default function ModelHandler() {

// Check model engine; we don't want to generate a title when it's not a local engine. remote model using first promp
if (
!isLocalEngine(engines, activeModelRef.current?.engine as InferenceEngine)
activeModelRef.current?.engine !== InferenceEngine.cortex &&
activeModelRef.current?.engine !== InferenceEngine.cortex_llamacpp
) {
const updatedThread: Thread = {
...thread,
Expand Down Expand Up @@ -396,9 +397,7 @@ export default function ModelHandler() {

// 2. Update the title with the result of the inference
setTimeout(() => {
const engine = EngineManager.instance().get(
messageRequest.model?.engine ?? activeModelRef.current?.engine ?? ''
)
const engine = EngineManager.instance().get(InferenceEngine.cortex)
engine?.inference(messageRequest)
}, 1000)
}
Expand Down
2 changes: 1 addition & 1 deletion web/hooks/useActiveModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export function useActiveModel() {
}
if (!activeModel) return

const engine = EngineManager.instance().get(activeModel.engine)
const engine = EngineManager.instance().get(InferenceEngine.cortex)
engine?.stopInference()
}, [activeModel, stateModel, stopModel])

Expand Down
1 change: 1 addition & 0 deletions web/hooks/useConfigurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const useConfigurations = () => {

useEffect(() => {
configurePullOptions()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

return {
Expand Down
13 changes: 9 additions & 4 deletions web/hooks/useRecommendedModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { atom, useAtomValue } from 'jotai'
import { activeModelAtom } from './useActiveModel'

import { activeAssistantAtom } from '@/helpers/atoms/Assistant.atom'
import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
import { activeThreadAtom } from '@/helpers/atoms/Thread.atom'

Expand All @@ -30,6 +31,7 @@ export default function useRecommendedModel() {
const activeThread = useAtomValue(activeThreadAtom)
const downloadedModels = useAtomValue(downloadedModelsAtom)
const activeAssistant = useAtomValue(activeAssistantAtom)
const engines = useAtomValue(installedEnginesAtom)

const getAndSortDownloadedModels = useCallback(async (): Promise<Model[]> => {
const models = downloadedModels.sort((a, b) =>
Expand All @@ -45,7 +47,12 @@ export default function useRecommendedModel() {
const getRecommendedModel = useCallback(async (): Promise<
Model | undefined
> => {
const models = await getAndSortDownloadedModels()
const models = (await getAndSortDownloadedModels()).filter((e: Model) =>
engines?.[e.engine]?.[0].type === 'local' ||
(engines?.[e.engine]?.[0].api_key?.length ?? 0) > 0
? true
: false
)

if (!activeThread || !activeAssistant) return
const modelId = activeAssistant.model.id
Expand All @@ -63,10 +70,8 @@ export default function useRecommendedModel() {
}

// sort the model, for display purpose

if (models.length === 0) {
// if we have no downloaded models, then can't recommend anything
console.debug("No downloaded models, can't recommend anything")
setRecommendedModel(undefined)
return
}
Expand Down Expand Up @@ -94,7 +99,7 @@ export default function useRecommendedModel() {

setRecommendedModel(lastUsedModel)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [getAndSortDownloadedModels, activeThread])
}, [getAndSortDownloadedModels, activeThread, engines])

useEffect(() => {
getRecommendedModel()
Expand Down
1 change: 1 addition & 0 deletions web/hooks/useStarterScreen.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { useMemo } from 'react'

import { InferenceEngine, EngineConfig } from '@janhq/core'
Expand Down
6 changes: 4 additions & 2 deletions web/screens/Hub/ModelList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useAtomValue } from 'jotai'

import ModelItem from '@/screens/Hub/ModelList/ModelItem'

import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'

type Props = {
Expand All @@ -14,6 +15,7 @@ type Props = {

const ModelList = ({ models }: Props) => {
const downloadedModels = useAtomValue(downloadedModelsAtom)
const engines = useAtomValue(installedEnginesAtom)
const sortedModels: Model[] = useMemo(() => {
const featuredModels: Model[] = []
const remoteModels: Model[] = []
Expand All @@ -22,7 +24,7 @@ const ModelList = ({ models }: Props) => {
models.forEach((m) => {
if (m.metadata?.tags?.includes('Featured')) {
featuredModels.push(m)
} else if (m.format === 'api') {
} else if (engines?.[m.engine]?.[0]?.type === 'remote') {
remoteModels.push(m)
} else if (downloadedModels.map((m) => m.id).includes(m.id)) {
localModels.push(m)
Expand All @@ -40,7 +42,7 @@ const ModelList = ({ models }: Props) => {
...remainingModels,
...remoteModels,
]
}, [models, downloadedModels])
}, [models, downloadedModels, engines])

return (
<div className="relative h-full w-full flex-shrink-0">
Expand Down
9 changes: 7 additions & 2 deletions web/screens/LocalServer/LocalServerLeftPanel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Fragment, useCallback, useState } from 'react'

import { EngineManager, Model, ModelSettingParams } from '@janhq/core'
import {
EngineManager,
InferenceEngine,
Model,
ModelSettingParams,
} from '@janhq/core'
import { Button, Tooltip, Select, Input, Checkbox } from '@janhq/joi'

import { useAtom, useAtomValue, useSetAtom } from 'jotai'
Expand Down Expand Up @@ -94,7 +99,7 @@ const LocalServerLeftPanel = () => {
localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false')
setFirstTimeVisitAPIServer(false)
}
const engine = EngineManager.instance().get((model as Model).engine)
const engine = EngineManager.instance().get(InferenceEngine.cortex)
engine?.loadModel(model as Model)
// startModel(selectedModel.id, false).catch((e) => console.error(e))
setIsLoading(false)
Expand Down
4 changes: 4 additions & 0 deletions web/screens/Settings/Engines/RemoteEngineSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable react/no-unescaped-entities */
/* eslint-disable @typescript-eslint/no-unused-vars */

import React, { useCallback, useRef, useState, useEffect } from 'react'

import {
EngineConfig as OriginalEngineConfig,
InferenceEngine,
events,
EngineEvent,
} from '@janhq/core'

interface EngineConfig extends OriginalEngineConfig {
Expand Down Expand Up @@ -64,6 +67,7 @@ const RemoteEngineSettings = ({
set(updatedEngine, field, value)
await updateEngine(name, updatedEngine)
mutate()
events.emit(EngineEvent.OnEngineUpdate, {})
}, 300)
},
[engine, name, mutate]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const AssistantSetting: React.FC<Props> = ({ componentData }) => {
setEngineParamsUpdate,
stopModel,
updateThreadMetadata,
resetGenerating,
]
)

Expand Down
4 changes: 2 additions & 2 deletions web/screens/Thread/ThreadCenterPanel/LoadModelError/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EngineManager } from '@janhq/core'
import { EngineManager, InferenceEngine } from '@janhq/core'
import { useAtomValue, useSetAtom } from 'jotai'

import ModalTroubleShooting, {
Expand Down Expand Up @@ -35,7 +35,7 @@ const LoadModelError = () => {
setMainState(MainViewState.Settings)
if (activeAssistant?.model.engine) {
const engine = EngineManager.instance().get(
activeAssistant.model.engine
InferenceEngine.cortex
)
engine?.name && setSelectedSettingScreen(engine.name)
}
Expand Down

0 comments on commit f328c35

Please sign in to comment.