import { useState } from 'react' import './App.css' import Dropdown from 'react-dropdown'; import 'react-dropdown/style.css'; function App() { const [prompt, setPrompt] = useState('') const [isLoading, setIsLoading] = useState(false) const [modelType,setModelType] = useState("pre") const [chat, setChat] = useState([]) // Function to parse text and identify code blocks const parseTextWithCodeBlocks = (text) => { const codeBlockRegex = /```(\w+)?\s*([\s\S]+?)\s*```/g; const parts = []; let lastIndex = 0; let match; while ((match = codeBlockRegex.exec(text)) !== null) { // Add text before the code block if (match.index > lastIndex) { parts.push({ type: 'text', content: text.slice(lastIndex, match.index) }); } // Add the code block parts.push({ type: 'code', language: match[1] || '', content: match[2] }); lastIndex = match.index + match[0].length; } // Add remaining text after last code block if (lastIndex < text.length) { parts.push({ type: 'text', content: text.slice(lastIndex) }); } return parts; } const handleSubmit = async () => { chat.push({ src: "USER", "text": prompt }) setIsLoading(true) let system_prompt = " ### " const res = await fetch('http://127.0.0.1:8000/chat', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ prompt:modelType === "fine" ? prompt + system_prompt : prompt }), }) const reader = res.body.getReader() const decoder = new TextDecoder('utf-8') while (true) { const { value, done } = await reader.read() if (done) break const chunk = decoder.decode(value) if (chat[chat.length - 1].src === "AI") { chat[chat.length - 1].text += chunk } else { chat.push({ src: "AI", text: chunk }) chat[chat.length - 1].text = chunk } setChat([...chat]) } setIsLoading(false) setPrompt("") } return (
{part.content}
{msg.text})}