Attempt at controlling formatting constraints

This commit is contained in:
2025-01-18 18:25:45 +01:00
parent 3247c0132a
commit d564a21db4
3 changed files with 33 additions and 19 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> <link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Code Typing Test Component</title> <title>Touch Programming</title>
</head> </head>
<body> <body>
<div id="root"></div> <div id="root"></div>
+4 -1
View File
@@ -11,7 +11,9 @@ function App() {
useEffect(() => { useEffect(() => {
(async function() { (async function() {
const response = await fetch(`${import.meta.env.VITE_API_URL}/generate?lang=scala&lines=20`); setCode('');
const response = await fetch(`${import.meta.env.VITE_API_URL}/generate?lang=go&lines=10`);
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder(); const decoder = new TextDecoder();
@@ -22,6 +24,7 @@ function App() {
if (done) { if (done) {
setLoaded(true); setLoaded(true);
setCode(prev => prev.trim());
break; break;
} }
} }
+28 -17
View File
@@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"strings"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@@ -17,39 +18,49 @@ import (
) )
type CodeBlockParser struct { type CodeBlockParser struct {
processed int processedChunks int
foundStart bool removedStartingBackticks bool
foundEnd bool removedLanguageName bool
removedEndingBackticks bool
} }
func NewCodeBlockParser() *CodeBlockParser { func NewCodeBlockParser() *CodeBlockParser {
return &CodeBlockParser{ return &CodeBlockParser{
processed: 0, processedChunks: 0,
foundStart: false, removedStartingBackticks: false,
foundEnd: false, removedLanguageName: false,
removedEndingBackticks: false,
} }
} }
func (p *CodeBlockParser) ParseStream(chunk []byte) []byte { // TODO: Use channels to stream date at a specific byte size to make control restrictions properly.
if !p.foundStart { func (p *CodeBlockParser) ParseStream(chunk []byte, language string) []byte {
if !p.removedStartingBackticks {
if bytes.Contains(chunk, []byte("```")) { if bytes.Contains(chunk, []byte("```")) {
p.foundStart = true p.removedStartingBackticks = true
chunk = nil chunk = nil
} }
} }
if p.foundStart && p.processed == 1 { if p.removedStartingBackticks && p.processedChunks <= 3 {
chunk = nil if strings.Contains(language, string(chunk)) {
chunk = nil
}
if string(chunk) == "\n" {
chunk = nil
p.removedLanguageName = true
}
} }
if p.foundStart && !p.foundEnd { if p.removedStartingBackticks && !p.removedEndingBackticks {
if bytes.Contains(chunk, []byte("```")) { if bytes.Contains(chunk, []byte("```")) {
p.foundEnd = true p.removedEndingBackticks = true
chunk = nil chunk = nil
} }
} }
p.processed += 1 p.processedChunks += 1
return chunk return chunk
} }
@@ -99,13 +110,13 @@ func main() {
parser := NewCodeBlockParser() parser := NewCodeBlockParser()
ollamaCtx := context.Background() ollamaCtx := context.Background()
prompt := []llms.MessageContent{ prompt := []llms.MessageContent{
llms.TextParts(llms.ChatMessageTypeSystem, `You must only generate code without any descriptions. Also don't include any code comments and use spaces instead of tabs for spacing. Most importantly. Most importantly you must remove any markdown code fences that wrap the content!`), llms.TextParts(llms.ChatMessageTypeSystem, `You must only generate code without any descriptions or formatting like markdown code fences with backticks. Also don't include any code comments and use spaces instead of tabs for spacing. Most importantly respect the number of lines provided you get asked to generate!`),
llms.TextParts(llms.ChatMessageTypeHuman, fmt.Sprintf(` llms.TextParts(llms.ChatMessageTypeHuman, fmt.Sprintf(`
Generate a maximum of %d lines of code from a well known open source project in the %s programming language.`, lines, lang)), Generate exactly %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 {
cleaned := parser.ParseStream(chunk) cleaned := parser.ParseStream(chunk, lang)
if len(cleaned) > 0 { if len(cleaned) > 0 {
ctx.Response().Write(cleaned) ctx.Response().Write(cleaned)