MCPcopy Create free account
hub / github.com/Azure-Samples/rag-postgres-openai-python / Chat

Function Chat

src/frontend/src/pages/chat/Chat.tsx:18–355  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16import { VectorSettings } from "../../components/VectorSettings";
17
18const Chat = () => {
19 const [isConfigPanelOpen, setIsConfigPanelOpen] = useState(false);
20 const [promptTemplate, setPromptTemplate] = useState<string>("");
21 const [temperature, setTemperature] = useState<number>(0.3);
22 const [retrieveCount, setRetrieveCount] = useState<number>(3);
23 const [retrievalMode, setRetrievalMode] = useState<RetrievalMode>(RetrievalMode.Hybrid);
24 const [useAdvancedFlow, setUseAdvancedFlow] = useState<boolean>(true);
25 const [shouldStream, setShouldStream] = useState<boolean>(true);
26
27 const lastQuestionRef = useRef<string>("");
28 const chatMessageStreamEnd = useRef<HTMLDivElement | null>(null);
29
30 const [isLoading, setIsLoading] = useState<boolean>(false);
31 const [isStreaming, setIsStreaming] = useState<boolean>(false);
32 const [error, setError] = useState<unknown>();
33
34 const [activeCitation, setActiveCitation] = useState<string>();
35 const [activeAnalysisPanelTab, setActiveAnalysisPanelTab] = useState<AnalysisPanelTabs | undefined>(undefined);
36
37 const [selectedAnswer, setSelectedAnswer] = useState<number>(0);
38 const [answers, setAnswers] = useState<[user: string, response: RAGChatCompletion][]>([]);
39 const [streamedAnswers, setStreamedAnswers] = useState<[user: string, response: RAGChatCompletion][]>([]);
40
41 const handleAsyncRequest = async (question: string, answers: [string, RAGChatCompletion][], responseBody: ReadableStream<Uint8Array>) => {
42 let answer = "";
43 let chatCompletion: RAGChatCompletion = {
44 context: {
45 data_points: {},
46 thoughts: []
47 },
48 output_text: ""
49 };
50 const updateState = (newContent: string) => {
51 return new Promise(resolve => {
52 setTimeout(() => {
53 answer += newContent;
54 const latestCompletion: RAGChatCompletion = {
55 ...chatCompletion,
56 output_text: answer
57 };
58 setStreamedAnswers([...answers, [question, latestCompletion]]);
59 resolve(null);
60 }, 33);
61 });
62 };
63 try {
64 setIsStreaming(true);
65 for await (const event of readNDJSONStream<RAGChatCompletionDelta>(responseBody)) {
66 if (event.error) {
67 throw new Error(event.error);
68 }
69 if (event.type === "response.context" && event.context) {
70 chatCompletion.context = { ...chatCompletion.context, ...event.context };
71 } else if (event.type === "response.output_text.delta" && event.delta !== undefined) {
72 setIsLoading(false);
73 await updateState(event.delta);
74 }
75 }

Callers

nothing calls this directly

Calls 3

onShowCitationFunction · 0.85
onToggleTabFunction · 0.85
makeApiRequestFunction · 0.85

Tested by

no test coverage detected