import { useState } from 'react' const API_URL = 'http://127.0.0.1:5001' type Message = { sender: string content: string } async function completion(prompt: string, callback: (res: string) => void) { const result = await fetch(`${API_URL}/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ prompt: `A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions. ### Human: Hello, Assistant. ### Assistant: Hello. How may I help you today? ### Human: ${prompt}`, model: 'ggml-model-q4_0', }), }) if (!result.ok || !result.body) { return } let reader = result.body.getReader() while (true) { const { done, value } = await reader.read() if (done) { break } let decoder = new TextDecoder() let str = decoder.decode(value) let re = /}{/g str = '[' + str.replace(re, '},{') + ']' let messages = JSON.parse(str) for (const message of messages) { const choice = message.choices[0] if (choice.finish_reason === 'stop') { break } callback(choice.text) } } return } export default function () { const [prompt, setPrompt] = useState('') const [messages, setMessages] = useState([]) return (

LLaMa

Meta Platforms, Inc.

{messages.map((m, i) => (
{m.sender === 'human' ? (
H
) : (
L
)}
{m.content} {m.sender === 'bot' && }
))}
) }