import { useState } from 'react' import path from 'path' import os from 'os' import { dialog, getCurrentWindow } from '@electron/remote' const API_URL = 'http://127.0.0.1:7734' type Message = { sender: 'bot' | 'human' content: string } const userInfo = os.userInfo() async function generate(prompt: string, model: string, callback: (res: string) => void) { const result = await fetch(`${API_URL}/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ prompt, model, }), }) if (!result.ok) { 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 = /}\s*{/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([]) const [model, setModel] = useState('') const [generating, setGenerating] = useState(false) return (

{path.basename(model).replace('.bin', '')}

{model ? (
{messages.map((m, i) => (
{m.sender === 'human' ? (
{userInfo.username[0].toUpperCase()}
) : (
{path.basename(model)[0].toUpperCase()}
)}
{m.content} {m.sender === 'bot' && generating && i === messages.length - 1 && ( )}
))}
) : (

No model selected

)}
{model && ( )}
) }