'use client'; /*eslint-disable*/ import MessageBoxChat from '@/components/MessageBoxChat'; import DashboardLayout from '@/components/layout'; import Bg from '@/public/img/ai-chat/bg-image.png'; import { Database } from '@/types_db'; import { Button, Flex, Icon, Image, Input, Text, useColorModeValue } from '@chakra-ui/react'; import { User } from '@supabase/supabase-js'; import endent from 'endent'; import { useState } from 'react'; import { MdAutoAwesome, MdEdit, MdPerson } from 'react-icons/md'; type Subscription = Database['public']['Tables']['subscriptions']['Row']; type Product = Database['public']['Tables']['products']['Row']; type Price = Database['public']['Tables']['prices']['Row']; interface ProductWithPrices extends Product { prices: Price[]; } interface PriceWithProduct extends Price { products: Product | null; } interface SubscriptionWithProduct extends Subscription { prices: PriceWithProduct | null; } interface Props { user: User | null | undefined; products: ProductWithPrices[]; subscription: SubscriptionWithProduct | null; userDetails: { [x: string]: any } | null; } export default function AiAssistant(props: Props) { // *** If you use .env.local variable for your API key, method which we recommend, use the apiKey variable commented below // Input States const [inputMessage, setInputMessage] = useState(''); const [submitMessage, setSubmitMessage] = useState(''); // Loading state const [loading, setLoading] = useState(false); const [assistant, setAssistant] = useState(Object); const [thread, setThread] = useState(Object); const [res_message, setResMessage] = useState(Object); const gray = useColorModeValue('gray.500', 'white'); const brandColor = useColorModeValue('brand.500', 'white'); const gradientBg2 = useColorModeValue( 'linear-gradient(15.46deg, #4A25E1 26.3%, #7B5AFF 86.4%)', 'linear-gradient(180deg, rgba(255, 255, 255, 0.07) 0%, rgba(255, 255, 255, 0.31) 100%)' ); const textColor = useColorModeValue('#120F43', 'white'); const placeholderColor = useColorModeValue( { color: 'gray.500' }, { color: 'whiteAlpha.600' } ); const borderColor = useColorModeValue('gray.200', 'whiteAlpha.200'); const createPrompt = (inputMessage: string) => { const data = (inputMessage: string) => { return endent` do me this: ${inputMessage} `; }; if (inputMessage) { return data(inputMessage); } }; const getAssistant = async () => { const gptResponse = await fetch( 'https://api.openai.com/v1/assistants/' + process.env.NEXT_PUBLIC_OPENAI_ASSISTANT_KEY, { method: 'GET', headers: { Authorization: `Bearer ${process.env.NEXT_PUBLIC_OPENAI_API_KEY}`, 'Content-Type': 'application/json', 'OpenAI-Beta': 'assistants=v1' } } ); const assistant = await gptResponse.json(); return assistant; }; const createThread = async () => { const gptResponse = await fetch('https://api.openai.com/v1/threads', { method: 'POST', headers: { Authorization: `Bearer ${process.env.NEXT_PUBLIC_OPENAI_API_KEY}`, 'Content-Type': 'application/json', 'OpenAI-Beta': 'assistants=v1' } }); const thread = await gptResponse.json(); return thread; }; const createMessage = async (thread_id: string) => { const prompt = createPrompt(inputMessage); const gptResponse = await fetch( 'https://api.openai.com/v1/threads/' + thread_id + '/messages', { method: 'POST', headers: { Authorization: `Bearer ${process.env.NEXT_PUBLIC_OPENAI_API_KEY}`, 'Content-Type': 'application/json', 'OpenAI-Beta': 'assistants=v1' }, body: JSON.stringify({ role: 'user', // content: topic, content: prompt }) } ); const message = await gptResponse.json(); return message; }; const getMessage = async (thread_id: string, message_id: string) => { // https://api.openai.com/v1/threads/{thread_id}/messages/{message_id} const gptResponse = await fetch( 'https://api.openai.com/v1/threads/' + thread_id + '/messages', { method: 'GET', headers: { Authorization: `Bearer ${process.env.NEXT_PUBLIC_OPENAI_API_KEY}`, 'Content-Type': 'application/json', 'OpenAI-Beta': 'assistants=v1' } } ); const message = await gptResponse.json(); console.log('I get the message.'); console.log(message); return message; }; const runAssistant = async (thread_id: string, assistant_id: string) => { const gptResponse = await fetch( 'https://api.openai.com/v1/threads/' + thread_id + '/runs', { method: 'POST', headers: { Authorization: `Bearer ${process.env.NEXT_PUBLIC_OPENAI_API_KEY}`, 'Content-Type': 'application/json', 'OpenAI-Beta': 'assistants=v1' }, body: JSON.stringify({ assistant_id: assistant_id }) } ); const run_res = await gptResponse.json(); return run_res; }; const getRunAssistant = async (run_id: string, thread_id: string) => { const gptResponse = await fetch( 'https://api.openai.com/v1/threads/' + thread_id + '/runs/' + run_id, { method: 'GET', headers: { Authorization: `Bearer ${process.env.NEXT_PUBLIC_OPENAI_API_KEY}`, 'Content-Type': 'application/json', 'OpenAI-Beta': 'assistants=v1' } } ); const run_res = await gptResponse.json(); console.log('I get the status.'); console.log(run_res); return run_res; }; const deleteThread = async (thread_id: string) => { if (thread === undefined) { return; } const gptResponse = await fetch( 'https://api.openai.com/v1/threads/' + thread_id, { method: 'DELETE', headers: { Authorization: `Bearer ${process.env.NEXT_PUBLIC_OPENAI_API_KEY}`, 'Content-Type': 'application/json', 'OpenAI-Beta': 'assistants=v1' } } ); const thread_res = await gptResponse.json(); console.log(thread_res); return thread_res; }; const handleSubmit = async (e: any) => { e.preventDefault(); setLoading(true); // save the keys in storage browser // @ts-ignore localStorage.setItem('open_ai_key', process.env.NEXT_PUBLIC_OPENAI_API_KEY); localStorage.setItem( 'assistant_key', // @ts-ignore process.env.NEXT_PUBLIC_OPENAI_ASSISTANT_KEY ); const assistant_res = await getAssistant(); setAssistant(assistant_res); const thread_res = await createThread(); setThread(thread_res); const message = await createMessage(thread_res.id); let runAssistantResponse = await runAssistant( thread_res.id, assistant_res.id ); console.log(runAssistantResponse); while (runAssistantResponse.status !== 'completed') { runAssistantResponse = await getRunAssistant( runAssistantResponse.id, thread_res.id ); if (runAssistantResponse.status === 'completed') { console.log('Message is : '); const call_response = await getMessage(thread_res.id, message.id); setResMessage(call_response); console.log(await deleteThread(thread_res.id)); } else { // sleep for 2 second await new Promise((r) => setTimeout(r, 2000)); } } console.log(assistant); console.log(thread); console.log(message); console.log(runAssistantResponse); setSubmitMessage(inputMessage); setLoading(false); }; // -------------- Copy Response -------------- // const copyToClipboard = (text: string) => { // const el = document.createElement('textarea'); // el.value = text; // document.body.appendChild(el); // el.select(); // document.execCommand('copy'); // document.body.removeChild(el); // }; const handleChange = (Event: any) => { setInputMessage(Event.target.value); }; return (  {/* Model Change */} Please make sure that you have set the environmental variable for the Assistant Key. {/* Main Box */} {submitMessage} {/* Chat Input */} Free Research Preview. ChatGPT may produce inaccurate information about people, places, or facts. Consider checking important information. ); }