pull-pal/llm/openai.go
Moby von Briesen 10c77854a9 Create "local git client" and "openai client"
OpenAIClient can be used to interact with OpenAI's API. Untested at the
moment (except with an API key that does not yet have perms)
Local git client splits the "git" functionality (e.g. make commits,
checkout branches, etc...) away from the "github" client

I also removed a lot of duplicate code for the different commands in cmd
And created a basic "local issue" command to send a "code change
request" to the llm, and receive a code change response. Eventually, I
want this to make the corresponding local git changes, for the user to
check.

TODO: github client should only be responsible for github-specific
actions, e.g. opening a PR or listing issues/comments
2023-04-25 20:32:08 -04:00

44 lines
940 B
Go

package llm
import (
"context"
"fmt"
"github.com/sashabaranov/go-openai"
)
type OpenAIClient struct {
client *openai.Client
}
func NewOpenAIClient(token string) *OpenAIClient {
return &OpenAIClient{
client: openai.NewClient(token),
}
}
func (oc *OpenAIClient) EvaluateCCR(ctx context.Context, req CodeChangeRequest) (res CodeChangeResponse, err error) {
resp, err := oc.client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
// TODO is this the correct role for my prompts?
Role: openai.ChatMessageRoleUser,
Content: req.String(),
},
},
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return res, err
}
// TODO use different choices/different options in different branches/worktrees?
choice := resp.Choices[0].Message.Content
return ParseCodeChangeResponse(choice), nil
}