parse openAI api responses. get fully automated behavior kind of working

This commit is contained in:
Moby von Briesen 2023-05-02 22:07:10 -04:00
parent 99bd1929b5
commit 3a312b2b9b
3 changed files with 18 additions and 9 deletions

View File

@ -91,14 +91,19 @@ func parseFiles(filesSection string) []File {
// first item in the list is just gonna be "Files:"
fileStringList = fileStringList[1:]
replacer := strings.NewReplacer(
"\\n", "\n",
"\\\"", "\"",
"```", "",
)
fileList := make([]File, len(fileStringList))
for i, f := range fileStringList {
fileParts := strings.Split(f, "contents:")
path := strings.TrimSpace(fileParts[0])
// TODO currently, copy-pasting code from chatgpt also copies some additional text
// the following separates this unintended section from the actual intended contents of the file
contentParts := strings.Split(fileParts[1], "Copy code")
contents := strings.TrimSpace(contentParts[1])
path := replacer.Replace(fileParts[0])
path = strings.TrimSpace(path)
contents := replacer.Replace(fileParts[1])
contents = strings.TrimSpace(contents)
fileList[i] = File{
Path: path,

View File

@ -2,17 +2,19 @@ package llm
import (
"context"
"fmt"
"github.com/sashabaranov/go-openai"
"go.uber.org/zap"
)
type OpenAIClient struct {
log *zap.Logger
client *openai.Client
}
func NewOpenAIClient(token string) *OpenAIClient {
func NewOpenAIClient(log *zap.Logger, token string) *OpenAIClient {
return &OpenAIClient{
log: log,
client: openai.NewClient(token),
}
}
@ -32,12 +34,14 @@ func (oc *OpenAIClient) EvaluateCCR(ctx context.Context, req CodeChangeRequest)
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
oc.log.Error("chat completion error", zap.Error(err))
return res, err
}
// TODO use different choices/different options in different branches/worktrees?
choice := resp.Choices[0].Message.Content
oc.log.Debug("got response from llm", zap.String("output", choice))
return ParseCodeChangeResponse(choice), nil
}

View File

@ -51,7 +51,7 @@ func NewPullPal(ctx context.Context, log *zap.Logger, listIssueOptions vc.ListIs
vcClient: ghClient,
localGitClient: localGitClient,
openAIClient: llm.NewOpenAIClient(openAIToken),
openAIClient: llm.NewOpenAIClient(log.Named("openaiClient"), openAIToken),
}, nil
}