2023-11-04 12:20:13 -06:00
|
|
|
package cli
|
2023-10-30 15:23:07 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-30 15:45:21 -06:00
|
|
|
"errors"
|
|
|
|
"io"
|
2023-11-04 16:56:22 -06:00
|
|
|
|
2023-10-30 15:23:07 -06:00
|
|
|
openai "github.com/sashabaranov/go-openai"
|
|
|
|
)
|
|
|
|
|
2023-11-04 16:56:22 -06:00
|
|
|
func CreateChatCompletionRequest(system string, messages []Message) *openai.ChatCompletionRequest {
|
2023-11-04 16:07:06 -06:00
|
|
|
chatCompletionMessages := []openai.ChatCompletionMessage{
|
|
|
|
{
|
2023-11-04 16:56:22 -06:00
|
|
|
Role: "system",
|
2023-11-04 16:07:06 -06:00
|
|
|
Content: system,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:56:22 -06:00
|
|
|
for _, m := range messages {
|
2023-10-30 16:23:27 -06:00
|
|
|
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
|
2023-11-04 16:56:22 -06:00
|
|
|
Role: m.Role,
|
2023-10-30 15:23:07 -06:00
|
|
|
Content: m.OriginalContent,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:07:06 -06:00
|
|
|
return &openai.ChatCompletionRequest{
|
2023-10-30 16:23:27 -06:00
|
|
|
Model: openai.GPT4,
|
|
|
|
MaxTokens: 256,
|
2023-11-04 16:56:22 -06:00
|
|
|
Messages: chatCompletionMessages,
|
|
|
|
Stream: true,
|
2023-10-30 16:23:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateChatCompletion accepts a slice of Message and returns the response
|
|
|
|
// of the Large Language Model.
|
|
|
|
func CreateChatCompletion(system string, messages []Message) (string, error) {
|
2023-11-04 11:30:03 -06:00
|
|
|
client := openai.NewClient(config.OpenAI.APIKey)
|
2023-10-30 15:23:07 -06:00
|
|
|
resp, err := client.CreateChatCompletion(
|
|
|
|
context.Background(),
|
2023-11-04 16:07:06 -06:00
|
|
|
*CreateChatCompletionRequest(system, messages),
|
2023-10-30 15:23:07 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2023-11-04 16:07:06 -06:00
|
|
|
return "", err
|
2023-10-30 15:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Choices[0].Message.Content, nil
|
|
|
|
}
|
2023-10-30 15:45:21 -06:00
|
|
|
|
2023-11-05 00:44:06 -06:00
|
|
|
// CreateChatCompletionStream submits an streaming Chat Completion API request
|
|
|
|
// and sends the received data to the output channel.
|
|
|
|
func CreateChatCompletionStream(system string, messages []Message, output chan string) error {
|
2023-11-04 11:30:03 -06:00
|
|
|
client := openai.NewClient(config.OpenAI.APIKey)
|
2023-10-30 15:45:21 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-11-04 16:07:06 -06:00
|
|
|
req := CreateChatCompletionRequest(system, messages)
|
2023-10-30 16:23:27 -06:00
|
|
|
req.Stream = true
|
2023-10-30 15:45:21 -06:00
|
|
|
|
2023-11-05 00:44:06 -06:00
|
|
|
defer close(output)
|
|
|
|
|
2023-11-04 16:07:06 -06:00
|
|
|
stream, err := client.CreateChatCompletionStream(ctx, *req)
|
2023-10-30 15:45:21 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-05 00:44:06 -06:00
|
|
|
output <- response.Choices[0].Delta.Content
|
2023-10-30 15:45:21 -06:00
|
|
|
}
|
|
|
|
}
|