Skip to content

Commit

Permalink
Build response string more efficiently
Browse files Browse the repository at this point in the history
This is definitely a dumb premature optimization, but this does some basic string builder stuff so that we're not duplicating strings all willy nilly.
  • Loading branch information
bcspragu committed Dec 30, 2024
1 parent ac55173 commit f3d0a4b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 10 deletions.
63 changes: 53 additions & 10 deletions cmd/kagi/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"

"github.com/bcspragu/kagi/api"
Expand Down Expand Up @@ -47,28 +50,68 @@ func run(args []string) error {
return fmt.Errorf("error performing query: %w", err)
}

response := respond(resp, query)
response, err := respond(resp, query)
if err != nil {
return fmt.Errorf("failed to build response: %w", err)
}
fmt.Print(response)

return nil
}

func respond(resp *api.FastGPTResponse, query string) (response string) {
// remove all repeated newlines or empty lines from the output
answer := strings.ReplaceAll(resp.Data.Output, "\n\n", "\n")

response = "# " + query + "\n" + answer + "\n"
func respond(resp *api.FastGPTResponse, query string) (string, error) {
var buf bytes.Buffer
buf.WriteString("# ")
buf.WriteString(query)
buf.WriteRune('\n')
if err := streamAndRemoveDoubleNewlines(resp.Data.Output, &buf); err != nil {
return "", fmt.Errorf("failed to remove double newlines: %w", err)
}
buf.WriteRune('\n')

// If there are no references, return early
if len(resp.Data.References) == 0 {
return
return buf.String(), nil
}

response += "\n# References\n"
buf.WriteString("\n# References\n")

for i, ref := range resp.Data.References {
response += fmt.Sprintf("%d. %s - %s - %s\n", i+1, ref.Title, ref.Link, ref.Snippet)
// fmt.Sprintf("%d. %s - %s - %s\n", i+1, ref.Title, ref.Link, ref.Snippet)
buf.WriteString(strconv.Itoa(i + 1))
buf.WriteString(". ")
buf.WriteString(ref.Title)
buf.WriteString(" - ")
buf.WriteString(ref.Link)
buf.WriteString(" - ")
buf.WriteString(ref.Snippet)
buf.WriteRune('\n')
}

return
return buf.String(), nil
}

// Remove all repeated newlines or empty lines from the given string
func streamAndRemoveDoubleNewlines(inp string, buf *bytes.Buffer) error {
r := strings.NewReader(inp)

sc := bufio.NewScanner(r)

first := true
for sc.Scan() {
if sc.Text() == "" {
continue
}
if !first {
buf.WriteRune('\n')
}
first = false
buf.WriteString(sc.Text())
}

if err := sc.Err(); err != nil {
return fmt.Errorf("error reading input: %w", err)
}

return nil
}
33 changes: 33 additions & 0 deletions cmd/kagi/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"bytes"
"testing"
)

func TestStreamAndRemoveDoubleNewlines(t *testing.T) {
inp := `Hello
I am a string with
some double newlines
And a triple newline!`

var buf bytes.Buffer
if err := streamAndRemoveDoubleNewlines(inp, &buf); err != nil {
t.Fatalf("streamAndRemoveDoubleNewlines: %v", err)
}

want := `Hello
I am a string with
some double newlines
And a triple newline!`

got := buf.String()

if got != want {
t.Errorf("unexpected output = %q, want %q", got, want)
}
}

0 comments on commit f3d0a4b

Please sign in to comment.