Matt Low
8780856854
Add new SetStructDefaults function to handle the "defaults" struct tag. Only works on struct fields which are pointers (in order to be able to distinguish between not set (nil) and zero values). So, the Config struct has been updated to use pointer fields and we now need to dereference those pointers to use them.
67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
|
|
openai "github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
func CreateChatCompletionRequest(model string, messages []Message, maxTokens int) openai.ChatCompletionRequest {
|
|
chatCompletionMessages := []openai.ChatCompletionMessage{}
|
|
for _, m := range messages {
|
|
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
|
|
Role: m.Role,
|
|
Content: m.OriginalContent,
|
|
})
|
|
}
|
|
|
|
return openai.ChatCompletionRequest{
|
|
Model: model,
|
|
Messages: chatCompletionMessages,
|
|
MaxTokens: maxTokens,
|
|
}
|
|
}
|
|
|
|
// CreateChatCompletion submits a Chat Completion API request and returns the
|
|
// response.
|
|
func CreateChatCompletion(model string, messages []Message, maxTokens int) (string, error) {
|
|
client := openai.NewClient(*config.OpenAI.APIKey)
|
|
req := CreateChatCompletionRequest(model, messages, maxTokens)
|
|
resp, err := client.CreateChatCompletion(context.Background(), req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return resp.Choices[0].Message.Content, nil
|
|
}
|
|
|
|
// CreateChatCompletionStream submits a streaming Chat Completion API request
|
|
// and streams the response to the provided output channel.
|
|
func CreateChatCompletionStream(model string, messages []Message, maxTokens int, output chan string) error {
|
|
client := openai.NewClient(*config.OpenAI.APIKey)
|
|
req := CreateChatCompletionRequest(model, messages, maxTokens)
|
|
|
|
defer close(output)
|
|
|
|
stream, err := client.CreateChatCompletionStream(context.Background(), req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer stream.Close()
|
|
|
|
for {
|
|
response, err := stream.Recv()
|
|
if errors.Is(err, io.EOF) {
|
|
return nil
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
output <- response.Choices[0].Delta.Content
|
|
}
|
|
}
|