-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): support edit chat engine knowledge graph options
- Loading branch information
Showing
9 changed files
with
245 additions
and
22 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
110 changes: 110 additions & 0 deletions
110
frontend/app/src/components/chat-engine/edit-boolean-form.tsx
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
'use client'; | ||
|
||
import { type ChatEngine, type ChatEngineOptions, updateChatEngine } from '@/api/chat-engines'; | ||
import { useManagedDialog } from '@/components/managed-dialog'; | ||
import { Button } from '@/components/ui/button'; | ||
import { DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'; | ||
import { Form, FormControl, FormField, FormItem, FormMessage } from '@/components/ui/form'; | ||
import { Switch } from '@/components/ui/switch'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useTransition } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { toast } from 'sonner'; | ||
import { z } from 'zod'; | ||
|
||
const schema = z.object({ | ||
enabled: z.boolean(), | ||
}); | ||
|
||
type BooleanOptionConfig = { | ||
name: string | ||
get: (options: ChatEngineOptions) => boolean | ||
set: (options: ChatEngineOptions, newValue: boolean) => void | ||
} | ||
|
||
const config = { | ||
'kg.enabled': { | ||
name: 'Enable KnowledgeGraph', | ||
get: opt => opt.knowledge_graph.enabled, | ||
set: (opt, nv) => opt.knowledge_graph.enabled = nv, | ||
}, | ||
'kg.include_meta': { | ||
name: 'KnowledgeGraph Include Meta', | ||
get: opt => opt.knowledge_graph.include_meta, | ||
set: (opt, nv) => opt.knowledge_graph.include_meta = nv, | ||
}, | ||
'kg.with_degree': { | ||
name: 'KnowledgeGraph With Degree', | ||
get: opt => opt.knowledge_graph.with_degree, | ||
set: (opt, nv) => opt.knowledge_graph.with_degree = nv, | ||
}, | ||
'kg.using_intent_search': { | ||
name: 'KnowledgeGraph Using Intent Search', | ||
get: opt => opt.knowledge_graph.using_intent_search, | ||
set: (opt, nv) => opt.knowledge_graph.using_intent_search = nv, | ||
}, | ||
} satisfies Record<string, BooleanOptionConfig>; | ||
|
||
export interface EditBooleanFormProps { | ||
chatEngine: ChatEngine; | ||
type: keyof typeof config; | ||
} | ||
|
||
export function EditBooleanForm ({ type, chatEngine }: EditBooleanFormProps) { | ||
const router = useRouter(); | ||
const [transitioning, startTransition] = useTransition(); | ||
const { setOpen } = useManagedDialog(); | ||
const { name, get, set } = config[type]; | ||
|
||
const form = useForm<{ enabled: boolean }>({ | ||
resolver: zodResolver(schema), | ||
defaultValues: { | ||
enabled: get(chatEngine.engine_options), | ||
}, | ||
}); | ||
|
||
const handleSubmit = form.handleSubmit(async (data) => { | ||
const options: ChatEngineOptions = { | ||
knowledge_graph: { ...chatEngine.engine_options.knowledge_graph }, | ||
llm: { ...chatEngine.engine_options.llm }, | ||
}; | ||
set(options, data.enabled); | ||
await updateChatEngine(chatEngine.id, { engine_options: options }); | ||
startTransition(() => { | ||
router.refresh(); | ||
}); | ||
toast(`ChatEngine's ${name} successfully updated.`); | ||
setOpen(false); | ||
}); | ||
|
||
return ( | ||
<> | ||
<DialogHeader> | ||
<DialogTitle>{name}</DialogTitle> | ||
</DialogHeader> | ||
<Form {...form}> | ||
<form id="update-form" className="space-y-4" onSubmit={handleSubmit}> | ||
<FormField | ||
name="enabled" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormControl> | ||
<FormControl> | ||
<Switch name={field.name} onBlur={field.onBlur} checked={field.value} onCheckedChange={field.onChange} disabled={field.disabled} /> | ||
</FormControl> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</form> | ||
</Form> | ||
<DialogFooter> | ||
<Button type="submit" form="update-form" disabled={form.formState.disabled || form.formState.isSubmitting || transitioning}> | ||
Update | ||
</Button> | ||
</DialogFooter> | ||
</> | ||
); | ||
} |
113 changes: 113 additions & 0 deletions
113
frontend/app/src/components/chat-engine/edit-integer-form.tsx
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use client'; | ||
|
||
import { type ChatEngine, type ChatEngineOptions, updateChatEngine } from '@/api/chat-engines'; | ||
import { useManagedDialog } from '@/components/managed-dialog'; | ||
import { Button } from '@/components/ui/button'; | ||
import { DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'; | ||
import { Form, FormControl, FormField, FormItem, FormMessage } from '@/components/ui/form'; | ||
import { Slider } from '@/components/ui/slider'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useTransition } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { toast } from 'sonner'; | ||
import { z } from 'zod'; | ||
|
||
const schema = z.object({ | ||
value: z.coerce.number().int(), | ||
}); | ||
|
||
type BooleanOptionConfig = { | ||
name: string | ||
get: (options: ChatEngineOptions) => number | ||
set: (options: ChatEngineOptions, newValue: number) => void | ||
min: number | ||
max: number | ||
} | ||
|
||
const config = { | ||
'kg.depth': { | ||
name: 'KnowledgeGraph Depth', | ||
get: opt => opt.knowledge_graph.depth, | ||
set: (opt, nv) => opt.knowledge_graph.depth = nv, | ||
min: 0, | ||
max: 20, | ||
}, | ||
} satisfies Record<string, BooleanOptionConfig>; | ||
|
||
export interface EditIntFormProps { | ||
chatEngine: ChatEngine; | ||
type: keyof typeof config; | ||
} | ||
|
||
export function EditIntForm ({ type, chatEngine }: EditIntFormProps) { | ||
const router = useRouter(); | ||
const [transitioning, startTransition] = useTransition(); | ||
const { setOpen } = useManagedDialog(); | ||
const { name, min, max, get, set } = config[type]; | ||
|
||
const form = useForm<{ value: number }>({ | ||
resolver: zodResolver(schema), | ||
defaultValues: { | ||
value: get(chatEngine.engine_options), | ||
}, | ||
}); | ||
|
||
const handleSubmit = form.handleSubmit(async (data) => { | ||
const options: ChatEngineOptions = { | ||
knowledge_graph: { ...chatEngine.engine_options.knowledge_graph }, | ||
llm: { ...chatEngine.engine_options.llm }, | ||
}; | ||
set(options, data.value); | ||
await updateChatEngine(chatEngine.id, { engine_options: options }); | ||
startTransition(() => { | ||
router.refresh(); | ||
}); | ||
toast(`ChatEngine's ${name} successfully updated.`); | ||
setOpen(false); | ||
}); | ||
|
||
return ( | ||
<> | ||
<DialogHeader> | ||
<DialogTitle>{name}</DialogTitle> | ||
</DialogHeader> | ||
<Form {...form}> | ||
<form id="update-form" className="space-y-4" onSubmit={handleSubmit}> | ||
<FormField | ||
name="value" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormControl> | ||
<FormControl> | ||
<div className='flex gap-2 items-center'> | ||
<Slider | ||
min={min} | ||
max={max} | ||
step={1} | ||
name={field.name} | ||
onBlur={field.onBlur} | ||
value={[field.value]} | ||
onValueChange={([value]) => field.onChange(value)} | ||
disabled={field.disabled} | ||
/> | ||
<span className='block flex-shrink-0 w-20 text-right font-mono'> | ||
{field.value} | ||
</span> | ||
</div> | ||
</FormControl> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</form> | ||
</Form> | ||
<DialogFooter> | ||
<Button type="submit" form="update-form" disabled={form.formState.disabled || form.formState.isSubmitting || transitioning}> | ||
Update | ||
</Button> | ||
</DialogFooter> | ||
</> | ||
); | ||
} |
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
15 changes: 9 additions & 6 deletions
15
frontend/app/src/components/chat-engine/knowledge-graph-details.tsx
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