'use client'; /*eslint-disable*/ import Link from '@/components/link/Link'; import MessageBoxChat from '@/components/MessageBox'; import { ChatBody, OpenAIModel } from '@/types/types'; import { Accordion, AccordionButton, AccordionIcon, AccordionItem, AccordionPanel, Box, Button, Flex, Icon, Image, Img, Input, Text, useColorModeValue, } from '@chakra-ui/react'; import { useEffect, useState } from 'react'; import { MdAutoAwesome, MdBolt, MdEdit, MdPerson } from 'react-icons/md'; import Bg from '../public/img/chat/bg-image.png'; export default function Chat(props: { apiKeyApp: string }) { // *** If you use .env.local variable for your API key, method which we recommend, use the apiKey variable commented below const { apiKeyApp } = props; // Input States const [inputOnSubmit, setInputOnSubmit] = useState(''); const [inputCode, setInputCode] = useState(''); // Response message const [outputCode, setOutputCode] = useState(''); // ChatGPT model const [model, setModel] = useState('gpt-3.5-turbo'); // Loading state const [loading, setLoading] = useState(false); // API Key // const [apiKey, setApiKey] = useState(apiKeyApp); const borderColor = useColorModeValue('gray.200', 'whiteAlpha.200'); const inputColor = useColorModeValue('navy.700', 'white'); const iconColor = useColorModeValue('brand.500', 'white'); const bgIcon = useColorModeValue( 'linear-gradient(180deg, #FBFBFF 0%, #CACAFF 100%)', 'whiteAlpha.200', ); const brandColor = useColorModeValue('brand.500', 'white'); const buttonBg = useColorModeValue('white', 'whiteAlpha.100'); const gray = useColorModeValue('gray.500', 'white'); const buttonShadow = useColorModeValue( '14px 27px 45px rgba(112, 144, 176, 0.2)', 'none', ); const textColor = useColorModeValue('navy.700', 'white'); const placeholderColor = useColorModeValue( { color: 'gray.500' }, { color: 'whiteAlpha.600' }, ); const handleTranslate = async () => { const apiKey = apiKeyApp; setInputOnSubmit(inputCode); // Chat post conditions(maximum number of characters, valid message etc.) const maxCodeLength = model === 'gpt-3.5-turbo' ? 700 : 700; if (!apiKeyApp?.includes('sk-') && !apiKey?.includes('sk-')) { alert('Please enter an API key.'); return; } if (!inputCode) { alert('Please enter your message.'); return; } if (inputCode.length > maxCodeLength) { alert( `Please enter code less than ${maxCodeLength} characters. You are currently at ${inputCode.length} characters.`, ); return; } setOutputCode(' '); setLoading(true); const controller = new AbortController(); const body: ChatBody = { inputCode, model, apiKey, }; // -------------- Fetch -------------- const response = await fetch('/api/chatAPI', { method: 'POST', headers: { 'Content-Type': 'application/json', }, signal: controller.signal, body: JSON.stringify(body), }); if (!response.ok) { setLoading(false); if (response) { alert( 'Something went wrong went fetching from the API. Make sure to use a valid API key.', ); } return; } const data = response.body; if (!data) { setLoading(false); alert('Something went wrong'); return; } const reader = data.getReader(); const decoder = new TextDecoder(); let done = false; while (!done) { setLoading(true); const { value, done: doneReading } = await reader.read(); done = doneReading; const chunkValue = decoder.decode(value); setOutputCode((prevCode) => prevCode + chunkValue); } 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); // }; // *** Initializing apiKey with .env.local value // useEffect(() => { // ENV file verison // const apiKeyENV = process.env.NEXT_PUBLIC_OPENAI_API_KEY // if (apiKey === undefined || null) { // setApiKey(apiKeyENV) // } // }, []) const handleChange = (Event: any) => { setInputCode(Event.target.value); }; return ( {/* Model Change */} setModel('gpt-3.5-turbo')} > GPT-3.5 setModel('gpt-4')} > GPT-4 No plugins added This is a cool text example. {/* Main Box */} {inputOnSubmit} {/* Chat Input */} Free Research Preview. ChatGPT may produce inaccurate information about people, places, or facts. ChatGPT May 12 Version ); }