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: 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' && }
))}
) }