mirror of
https://github.com/hazemKrimi/touch-programming.git
synced 2026-05-01 18:20:26 +00:00
Parse the response from the model properly and stream it to the client
This commit is contained in:
+4
-3
@@ -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);
|
||||||
})();
|
})();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
+49
-43
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@@ -18,55 +17,52 @@ 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
|
|
||||||
chunk = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !p.removedLanguageName && p.removedStartingBackticks && p.processedChunks <= 3 {
|
if !strings.Contains(parser.language, text) && !strings.Contains(text, "```") && !strings.Contains(text, "``") {
|
||||||
if strings.Contains(language, string(chunk)) {
|
if strings.Contains(text, "\n") {
|
||||||
chunk = nil
|
parser.currentLines += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(chunk) == "\n" {
|
if parser.currentLines == parser.maxLines-1 {
|
||||||
chunk = nil
|
indexOfNewLine := strings.Index(text, "\n")
|
||||||
p.removedLanguageName = true
|
|
||||||
|
if indexOfNewLine > -1 {
|
||||||
|
parser.channel <- []byte(text[:indexOfNewLine])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
parser.channel <- nil
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user