chore: read and write openai settings
This commit is contained in:
parent
48195371e2
commit
d9b0625b17
@ -21,6 +21,7 @@ import { twMerge } from 'tailwind-merge'
|
|||||||
import { MainViewState } from '@/constants/screens'
|
import { MainViewState } from '@/constants/screens'
|
||||||
|
|
||||||
import { useActiveModel } from '@/hooks/useActiveModel'
|
import { useActiveModel } from '@/hooks/useActiveModel'
|
||||||
|
import { useEngineSettings } from '@/hooks/useEngineSettings'
|
||||||
import { getDownloadedModels } from '@/hooks/useGetDownloadedModels'
|
import { getDownloadedModels } from '@/hooks/useGetDownloadedModels'
|
||||||
|
|
||||||
import { useMainViewState } from '@/hooks/useMainViewState'
|
import { useMainViewState } from '@/hooks/useMainViewState'
|
||||||
@ -38,6 +39,16 @@ export default function DropdownListSidebar() {
|
|||||||
const [selected, setSelected] = useState<Model | undefined>()
|
const [selected, setSelected] = useState<Model | undefined>()
|
||||||
const { setMainViewState } = useMainViewState()
|
const { setMainViewState } = useMainViewState()
|
||||||
const { activeModel, stateModel } = useActiveModel()
|
const { activeModel, stateModel } = useActiveModel()
|
||||||
|
const [opeenAISettings, setOpenAISettings] = useState<
|
||||||
|
{ api_key: string } | undefined
|
||||||
|
>(undefined)
|
||||||
|
const { readOpenAISettings, saveOpenAISettings } = useEngineSettings()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
readOpenAISettings().then((settings) => {
|
||||||
|
setOpenAISettings(settings)
|
||||||
|
})
|
||||||
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getDownloadedModels().then((downloadedModels) => {
|
getDownloadedModels().then((downloadedModels) => {
|
||||||
@ -129,20 +140,20 @@ export default function DropdownListSidebar() {
|
|||||||
</SelectContent>
|
</SelectContent>
|
||||||
</Select>
|
</Select>
|
||||||
|
|
||||||
{selected?.engine === 'openai' && (
|
{selected?.engine === InferenceEngine.openai && (
|
||||||
<div className="mt-4">
|
<div className="mt-4">
|
||||||
<label
|
<label
|
||||||
id="thread-title"
|
id="thread-title"
|
||||||
className="mb-2 inline-block font-bold text-gray-600 dark:text-gray-300"
|
className="mb-2 inline-block font-bold text-gray-600 dark:text-gray-300"
|
||||||
>
|
>
|
||||||
API_KEY
|
API Key
|
||||||
</label>
|
</label>
|
||||||
<Input
|
<Input
|
||||||
id="assistant-instructions"
|
id="assistant-instructions"
|
||||||
placeholder="Enter your API_KEY"
|
placeholder="Enter your API_KEY"
|
||||||
value=""
|
defaultValue={opeenAISettings?.api_key}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
console.log(e.target.value)
|
saveOpenAISettings({ apiKey: e.target.value })
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
23
web/hooks/useEngineSettings.ts
Normal file
23
web/hooks/useEngineSettings.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { join } from 'path'
|
||||||
|
|
||||||
|
import { fs } from '@janhq/core'
|
||||||
|
|
||||||
|
export const useEngineSettings = () => {
|
||||||
|
const readOpenAISettings = async () => {
|
||||||
|
const settings = await fs.readFile(join('engines', 'openai.json'))
|
||||||
|
if (settings) {
|
||||||
|
return JSON.parse(settings)
|
||||||
|
}
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
const saveOpenAISettings = async ({
|
||||||
|
apiKey,
|
||||||
|
}: {
|
||||||
|
apiKey: string | undefined
|
||||||
|
}) => {
|
||||||
|
const settings = await readOpenAISettings()
|
||||||
|
settings.api_key = apiKey
|
||||||
|
await fs.writeFile(join('engines', 'openai.json'), JSON.stringify(settings))
|
||||||
|
}
|
||||||
|
return { readOpenAISettings, saveOpenAISettings }
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user