quivr/frontend/app/chat/[chatId]/context/ChatContext.tsx
Mamadou DICKO 59fe7b089b
feat(chat): use openai function for answer (#354)
* feat(chat): use openai function for answer (backend)

* feat(chat): use openai function for answer (frontend)

* chore: refacto BrainPicking

* feat: update chat creation logic

* feat: simplify chat system logic

* feat: set default method to gpt-3.5-turbo-0613

* feat: use user own openai key

* feat(chat): slightly improve prompts

* feat: add global error interceptor

* feat: remove unused endpoints

* docs: update chat system doc

* chore(linter): add unused import remove config

* feat: improve dx

* feat: improve OpenAiFunctionBasedAnswerGenerator prompt
2023-06-22 17:50:06 +02:00

49 lines
1.0 KiB
TypeScript

"use client";
import { createContext, useContext, useState } from "react";
import { ChatHistory } from "../types";
type ChatContextProps = {
history: ChatHistory[];
setHistory: (history: ChatHistory[]) => void;
addToHistory: (message: ChatHistory) => void;
};
export const ChatContext = createContext<ChatContextProps | undefined>(
undefined
);
export const ChatProvider = ({
children,
}: {
children: JSX.Element | JSX.Element[];
}): JSX.Element => {
const [history, setHistory] = useState<ChatHistory[]>([]);
const addToHistory = (message: ChatHistory) => {
setHistory((prevHistory) => [...prevHistory, message]);
};
return (
<ChatContext.Provider
value={{
history,
setHistory,
addToHistory,
}}
>
{children}
</ChatContext.Provider>
);
};
export const useChatContext = (): ChatContextProps => {
const context = useContext(ChatContext);
if (context === undefined) {
throw new Error("useChatContext must be used inside ChatProvider");
}
return context;
};