forked from langtail/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.tsx
125 lines (108 loc) · 3.1 KB
/
chat.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use client";
import React, { useState, useEffect, useRef } from "react";
import styles from "./chat.module.css";
import Markdown from "react-markdown";
import zod from "zod";
import { AiLoading } from "./AiLoading";
import { useChat } from "@ai-sdk/react";
const UserMessage = ({ text }: { text: string | null }) => {
return <div className={styles.userMessage}>{text}</div>;
};
const AssistantMessage = ({ text }: { text: string | null }) => {
return (
<div className={styles.assistantMessage}>
<Markdown>{text}</Markdown>
</div>
);
};
const Message = ({ role, content }: ChatMessage) => {
switch (role) {
case "user":
return <UserMessage text={content} />;
case "assistant":
return <AssistantMessage text={content} />;
default:
return null;
}
};
type ChatProps = {
onAiStart?: () => void;
onAiEnd?: () => void;
functionCallHandler?: (toolCall: {
toolName: string;
toolCallId: string;
args: unknown;
}) => void;
};
export type ChatMessage = {
content: string | null;
role: "user" | "assistant" | "tool" | "function" | "data" | "system";
deltaPlaceholder?: boolean;
};
const Chat = ({ onAiStart, functionCallHandler }: ChatProps) => {
const [generatingResponse, setGeneratingResponse] = useState<boolean>(false);
const [inputDisabled, setInputDisabled] = useState(false);
const handleRunCompleted = () => {
setInputDisabled(false);
setGeneratingResponse(false);
};
const { messages, input, handleInputChange, handleSubmit } = useChat({
maxToolRoundtrips: 5,
api: "/api/langtail",
onFinish: handleRunCompleted,
// run client-side tools that are automatically executed:
async onToolCall(message) {
if (message.toolCall.toolName === "get_weather") {
functionCallHandler?.(message.toolCall);
}
},
});
// automatically scroll to bottom of chat
const messagesEndRef = useRef<HTMLDivElement | null>(null);
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
};
useEffect(() => {
scrollToBottom();
}, [messages]);
const handleFormSubmit = (e) => {
e.preventDefault();
setGeneratingResponse(true);
scrollToBottom();
handleSubmit(e);
onAiStart?.();
};
return (
<div className={styles.chatContainer}>
<div className={styles.messages}>
{messages
.filter((msg) => msg.content)
.map((msg, index) => (
<Message key={index} role={msg.role} content={msg.content} />
))}
{generatingResponse && <AiLoading />}
<div ref={messagesEndRef} />
</div>
<form
onSubmit={handleFormSubmit}
className={`${styles.inputForm} ${styles.clearfix}`}
>
<input
type="text"
className={styles.input}
value={input}
onChange={handleInputChange}
placeholder="Enter your question"
/>
<button
type="submit"
className={styles.button}
disabled={inputDisabled}
>
Send
</button>
</form>
</div>
);
};
export default Chat;