|
1 | 1 | "use client" |
2 | 2 |
|
3 | | -import { useState } from "react" |
4 | | -import { useForm } from "react-hook-form" |
5 | | -import type { ChatCompletionRequestMessage } from "openai" |
| 3 | +import { useChat } from "ai/react" |
6 | 4 | import ReactTextareaAutosize from "react-textarea-autosize" |
7 | 5 |
|
8 | 6 | import SendIcon from "../assets/send-icon" |
9 | | -import { useChat, type Message } from "@/hooks/use-chat" |
10 | 7 |
|
11 | | -type FormData = { prompt: string } |
12 | | - |
13 | | -async function getOpenAIResponse(messages: ChatCompletionRequestMessage[]) { |
14 | | - const response = await fetch("/api/openai", { |
15 | | - method: "POST", |
16 | | - body: JSON.stringify(messages), |
17 | | - headers: { "Content-Type": "application/json" }, |
18 | | - }) |
19 | | - |
20 | | - if (response?.ok) { |
21 | | - return response.json() |
22 | | - } else { |
23 | | - throw response?.statusText |
24 | | - } |
25 | | -} |
26 | | - |
27 | | -async function tokenizePrompt(prompt: string) { |
28 | | - const response = await fetch("/api/tokenize", { |
29 | | - method: "POST", |
30 | | - body: prompt, |
31 | | - }) |
32 | | - |
33 | | - if (response?.ok) { |
34 | | - return response.json() |
35 | | - } else { |
36 | | - throw response?.statusText |
37 | | - } |
38 | | -} |
39 | | - |
40 | | -const createMessage = async (prompt: string): Promise<Message> => { |
41 | | - const tokenizedPrompt = await tokenizePrompt(prompt) |
42 | | - |
43 | | - return { |
44 | | - data: { role: "user", content: prompt }, |
45 | | - metadata: { creationDate: new Date(), tokens: tokenizedPrompt.length }, |
46 | | - } |
47 | | -} |
48 | | - |
49 | | -const Form = () => { |
50 | | - const [prompt, setPrompt] = useState<string>("") |
51 | | - const { register, handleSubmit } = useForm<FormData>() |
52 | | - const { messages, addMessage, setStatus } = useChat() |
53 | | - |
54 | | - const onSubmit = async (data: FormData) => { |
55 | | - const message = await createMessage(prompt) |
56 | | - |
57 | | - addMessage(message) |
58 | | - setStatus("loading") |
59 | | - setPrompt("") |
60 | | - |
61 | | - try { |
62 | | - const trimMessages = [...messages, message].map(({ data }) => data) |
63 | | - const { result } = await getOpenAIResponse(trimMessages) |
64 | | - const botMessage = { |
65 | | - data: result, |
66 | | - metadata: { creationDate: new Date() }, |
67 | | - } as Message |
68 | | - |
69 | | - addMessage(botMessage) |
70 | | - setStatus("ready") |
71 | | - } catch (error) { |
72 | | - console.log(error) |
73 | | - } |
74 | | - } |
| 8 | +const Form = ({ id }: { id: string }) => { |
| 9 | + const { input, handleInputChange, handleSubmit } = useChat({ id }) |
75 | 10 |
|
76 | 11 | return ( |
77 | 12 | <div className="form-container"> |
78 | | - <form onSubmit={handleSubmit(onSubmit)}> |
| 13 | + <form onSubmit={handleSubmit}> |
79 | 14 | <ReactTextareaAutosize |
80 | 15 | rows={1} |
81 | 16 | autoFocus |
82 | 17 | minRows={1} |
83 | 18 | maxRows={8} |
84 | | - value={prompt} |
| 19 | + value={input} |
85 | 20 | cacheMeasurements |
86 | | - {...register("prompt")} |
| 21 | + onChange={handleInputChange} |
87 | 22 | placeholder="Envoyez un message..." |
88 | | - onChange={({ target: { value } }: { target: { value: string } }) => |
89 | | - setPrompt(value) |
90 | | - } |
91 | 23 | /> |
92 | 24 | <button type="submit"> |
93 | 25 | <SendIcon /> |
|
0 commit comments