Code Cleanup and Quality of Life

Checks to make sure repo is indexed before generating code.
Don't generate tests for changes to tests.
Remove unused code.
Fix bootstrapping issue with langchaingo tables.
This commit is contained in:
2025-04-20 08:31:26 -04:00
parent 4b8b8132fd
commit 25f8cae8cb
9 changed files with 282 additions and 256 deletions

View File

@@ -3,17 +3,11 @@ package llm
import (
"ai-code-assistant/pkg/database"
"context"
"fmt"
"github.com/cenkalti/backoff/v5"
"github.com/tmc/langchaingo/callbacks"
"github.com/tmc/langchaingo/schema"
"github.com/tmc/langchaingo/vectorstores"
"github.com/tmc/langchaingo/vectorstores/pgvector"
"log/slog"
"slices"
"strconv"
"strings"
"time"
)
type RelevantDocs struct {
@@ -96,56 +90,3 @@ func (rd *RelevantDocs) GetRelevantFileChunks(ctx context.Context, query string)
return chunks, nil
}
func (rd *RelevantDocs) RankChunks(ctx context.Context, query string, chunks []*FileChunkID) error {
var didErr error
slices.SortFunc(chunks, func(a, b *FileChunkID) int {
if didErr != nil {
return 0
}
retr, err := rd.CompareChunks(ctx, query, a, b)
if err != nil {
didErr = err
}
return retr
})
return didErr
}
func (rd *RelevantDocs) CompareChunks(ctx context.Context, query string, chunk1, chunk2 *FileChunkID) (int, error) {
slog.Info("comparing chunks", "chunk_1_name", chunk1.Name, "chunk_1_id", chunk1.ChunkID, "chunk_2_name", chunk2.Name, "chunk_2_id", chunk2.ChunkID)
prompt := `Given the following two pieces of code pick the most relevant chunk to the task described below. Reply as a json object in the format {"chunk_id": "<chunk>"}. Only reply in JSON. Do not include any explanation or code.`
prompt += "\n\n" + query + "\n\n"
// Now that we have candidates we need to compare them against each other to find the most appropriate place to
// inject them.
prompt += "-- chunk_id: chunk_1 --\n"
prompt += chunk1.Doc.PageContent
prompt += "-- chunk_id: chunk_2 --\n"
prompt += chunk2.Doc.PageContent
op := func() (int, error) {
rsp, err := rd.llm.CodePrompt(ctx, prompt)
if err != nil {
return 0, err
}
if strings.Contains(rsp, "chunk_1") {
return -1, nil
} else if strings.Contains(rsp, "chunk_2") {
return 1, nil
}
return 0, fmt.Errorf("compare response didn't contain a chunk id: %s", rsp)
}
return backoff.Retry(ctx, op, backoff.WithBackOff(backoff.NewConstantBackOff(10*time.Millisecond)), backoff.WithMaxTries(1))
}