fix: groq frequently stops during long responses (#2584)

This commit is contained in:
Louis 2024-04-02 22:19:34 +07:00 committed by GitHub
parent 3fded8fd20
commit fe89901a65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -60,14 +60,20 @@ export function requestInference(
} }
const text = decoder.decode(value) const text = decoder.decode(value)
const lines = text.trim().split('\n') const lines = text.trim().split('\n')
let cachedLines = ''
for (const line of lines) { for (const line of lines) {
if (line.startsWith('data: ') && !line.includes('data: [DONE]')) { try {
const data = JSON.parse(line.replace('data: ', '')) const toParse = cachedLines + line
content += data.choices[0]?.delta?.content ?? '' if (!line.includes('data: [DONE]')) {
if (content.startsWith('assistant: ')) { const data = JSON.parse(toParse.replace('data: ', ''))
content = content.replace('assistant: ', '') content += data.choices[0]?.delta?.content ?? ''
if (content.startsWith('assistant: ')) {
content = content.replace('assistant: ', '')
}
if (content !== '') subscriber.next(content)
} }
subscriber.next(content) } catch {
cachedLines = line
} }
} }
} }