Parse the response from the model properly and stream it to the client

This commit is contained in:
2025-01-24 00:53:49 +01:00
parent ec1e3fa810
commit 059202cd1e
2 changed files with 53 additions and 46 deletions
+4 -3
View File
@@ -13,7 +13,7 @@ function App() {
(async function() { (async function() {
setCode(''); setCode('');
const response = await fetch(`${import.meta.env.VITE_API_URL}/generate?lang=ocaml&lines=20`); const response = await fetch(`${import.meta.env.VITE_API_URL}/generate?lang=java&lines=50`);
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder(); const decoder = new TextDecoder();
@@ -23,11 +23,12 @@ function App() {
setCode(prev => prev + decoder.decode(value)); setCode(prev => prev + decoder.decode(value));
if (done) { if (done) {
setLoaded(true);
setCode(prev => prev.trim());
break; break;
} }
} }
setCode(prev => prev.trim());
setLoaded(true);
})(); })();
}, []); }, []);
+45 -39
View File
@@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"log" "log"
@@ -19,54 +18,51 @@ import (
type CodeBlockParser struct { type CodeBlockParser struct {
channel chan []byte channel chan []byte
processedChunks int language string
removedStartingBackticks bool maxLines int
removedLanguageName bool currentLines int
removedEndingBackticks bool
} }
func NewCodeBlockParser() *CodeBlockParser { func NewCodeBlockParser(lang string, lines int) *CodeBlockParser {
return &CodeBlockParser{ return &CodeBlockParser{
channel: make(chan []byte), channel: make(chan []byte),
processedChunks: 0, language: lang,
removedStartingBackticks: false, maxLines: lines,
removedLanguageName: false, currentLines: 0,
removedEndingBackticks: false,
} }
} }
func (p *CodeBlockParser) ParseStream(chunk []byte, language string) { func (parser *CodeBlockParser) ParseStream(chunk []byte) {
if !p.removedStartingBackticks { text := string(chunk)
if bytes.Contains(chunk, []byte("```")) {
p.removedStartingBackticks = true if !strings.Contains(parser.language, text) && !strings.Contains(text, "```") && !strings.Contains(text, "``") {
chunk = nil if strings.Contains(text, "\n") {
} parser.currentLines += 1
} }
if !p.removedLanguageName && p.removedStartingBackticks && p.processedChunks <= 3 { if parser.currentLines == parser.maxLines-1 {
if strings.Contains(language, string(chunk)) { indexOfNewLine := strings.Index(text, "\n")
chunk = nil
if indexOfNewLine > -1 {
parser.channel <- []byte(text[:indexOfNewLine])
return
} }
if string(chunk) == "\n" { parser.channel <- nil
chunk = nil return
p.removedLanguageName = true
}
} }
if p.removedStartingBackticks && !p.removedEndingBackticks { if parser.currentLines >= parser.maxLines {
if bytes.Contains(chunk, []byte("```")) { parser.channel <- nil
chunk = nil return
p.removedEndingBackticks = true
}
} }
if p.removedEndingBackticks { parser.channel <- chunk
chunk = nil return
} else {
parser.channel <- nil
return
} }
p.processedChunks += 1
p.channel <- chunk
} }
func main() { func main() {
@@ -112,24 +108,34 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
parser := NewCodeBlockParser() parser := NewCodeBlockParser(lang, lines)
ollamaCtx := context.Background() ollamaCtx := context.Background()
prompt := []llms.MessageContent{ prompt := []llms.MessageContent{
llms.TextParts(llms.ChatMessageTypeHuman, fmt.Sprintf(` llms.TextParts(llms.ChatMessageTypeHuman, fmt.Sprintf(`
You must only generate code without any descriptions or code comments or formatting like markdown code fences with backticks. Use spaces instead of tabs for spacing. Generate accurately according to the number of lines you get provided. Generate exactly between %d and %d lines of code from a well known open source project in the %s programming language.`, lines/2, lines, lang)), You must only generate code without any descriptions or code comments and use spaces instead of tabs for spacing. Generate a maximum of %d lines of code from a well known open source project in the %s programming language.`, lines, lang)),
} }
if _, err := llm.GenerateContent(ollamaCtx, prompt, llms.WithStreamingFunc(func(streamCtx context.Context, chunk []byte) error { if _, err := llm.GenerateContent(ollamaCtx, prompt, llms.WithStreamingFunc(func(streamCtx context.Context, chunk []byte) error {
go parser.ParseStream(chunk, lang) go parser.ParseStream(chunk)
select { select {
case cleaned := <-parser.channel: case chunk := <-parser.channel:
if len(cleaned) > 0 { if len(chunk) > 0 {
ctx.Response().Write(cleaned) ctx.Response().Write(chunk)
ctx.Response().Flush() ctx.Response().Flush()
} }
if parser.currentLines == parser.maxLines {
cnx, _, err := ctx.Response().Hijack()
if err != nil {
log.Fatal(err)
return ctx.String(http.StatusInternalServerError, err.Error())
} }
cnx.Close()
}
}
return nil return nil
})); err != nil { })); err != nil {
log.Fatal(err) log.Fatal(err)