37 lines
836 B
Go
37 lines
836 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
openai "github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
// CreateChatCompletion accepts a slice of Message and returns the response
|
|
// of the Large Language Model.
|
|
func CreateChatCompletion(system string, messages []Message) (string, error) {
|
|
client := openai.NewClient(os.Getenv("OPENAI_APIKEY"))
|
|
|
|
var openaiMessages []openai.ChatCompletionMessage
|
|
for _, m := range(messages) {
|
|
openaiMessages = append(openaiMessages, openai.ChatCompletionMessage{
|
|
Role: m.Role,
|
|
Content: m.OriginalContent,
|
|
})
|
|
}
|
|
|
|
resp, err := client.CreateChatCompletion(
|
|
context.Background(),
|
|
openai.ChatCompletionRequest{
|
|
Model: openai.GPT4,
|
|
Messages: openaiMessages,
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
return "", fmt.Errorf("ChatCompletion error: %v\n", err)
|
|
}
|
|
|
|
return resp.Choices[0].Message.Content, nil
|
|
}
|