import { useAuthenticatedData } from '@/app/common/auth'; import React, { useEffect } from 'react'; import useSWR from 'swr'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import styles from './modelPicker.module.css'; export interface Model { id: number; chat_model: string; } // Custom fetcher function to fetch options const fetchOptionsRequest = async (url: string) => { const response = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); return response.json(); }; export const useOptionsRequest = (url: string) => { const { data, error } = useSWR(url, fetchOptionsRequest); return { data, isLoading: !error && !data, isError: error, }; }; const fetchSelectedModel = async (url: string) => { const response = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); return response.json(); } export const useSelectedModel = (url: string) => { const { data, error } = useSWR(url, fetchSelectedModel); return { data, isLoading: !error && !data, isError: error, } } interface ModelPickerProps { disabled?: boolean; setModelUsed?: (model: Model) => void; initialModel?: Model; } export const ModelPicker: React.FC = (props: ModelPickerProps) => { const { data: models } = useOptionsRequest('/api/config/chat/model/options'); const { data: selectedModel } = useSelectedModel('/api/config/chat/model'); const [openLoginDialog, setOpenLoginDialog] = React.useState(false); let userData = useAuthenticatedData(); useEffect(() => { if (props.setModelUsed && selectedModel) { props.setModelUsed(selectedModel); } }, [selectedModel]); if (!models) { return
Loading...
; } function onSelect(model: Model) { if (!userData) { setOpenLoginDialog(true); return; } if (props.setModelUsed) { props.setModelUsed(model); } fetch('/api/config/chat/model' + '?id=' + String(model.id), { method: 'POST', body: JSON.stringify(model) }) .then((response) => { if (!response.ok) { throw new Error('Failed to select model'); } }) .catch((error) => { console.error('Failed to select model', error); }); } function isSelected(model: Model) { if (props.initialModel) { return model.id === props.initialModel.id; } return selectedModel?.id === model.id; } return (
You must be logged in to configure your model. Once you create an account with Khoj, you can configure your model and use a whole suite of other features. Check out our documentation to learn more. Cancel { window.location.href = window.location.origin + '/login'; }}> Sign in
); };