Files
llm-in-text/src/utils/api.js

31 lines
899 B
JavaScript
Raw Normal View History

export async function fetchSuggestion(prefix, suffix, apiUrl = 'http://localhost:8000/v1/completions') {
const res = await fetch(apiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prefix, suffix, languageId: 'markdown' }),
})
if (!res.ok) {
throw new Error(`HTTP ${res.status}`)
}
const reader = res.body?.getReader()
if (!reader) throw new Error('No reader available')
let text = ''
while (true) {
const { done, value } = await reader.read()
if (done) break
const chunk = new TextDecoder().decode(value)
const lines = chunk.split('\n').filter(l => l.startsWith('data: '))
for (const line of lines) {
try {
const data = JSON.parse(line.slice(6))
if (data.content) text += data.content
if (data.done || data.error) break
} catch (e) {}
}
}
return text
}