Include system prompt in OpenAI chat completion requests

This commit is contained in:
Matt Low 2023-11-04 22:07:06 +00:00
parent 04478cbbd1
commit 5c6ec5e4e2

View File

@ -8,8 +8,14 @@ import (
openai "github.com/sashabaranov/go-openai"
)
func CreateChatCompletionRequest(messages []Message) (openai.ChatCompletionRequest) {
var chatCompletionMessages []openai.ChatCompletionMessage
func CreateChatCompletionRequest(system string, messages []Message) (*openai.ChatCompletionRequest) {
chatCompletionMessages := []openai.ChatCompletionMessage{
{
Role: "system",
Content: system,
},
}
for _, m := range(messages) {
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
Role: m.Role,
@ -17,7 +23,7 @@ func CreateChatCompletionRequest(messages []Message) (openai.ChatCompletionReque
})
}
return openai.ChatCompletionRequest{
return &openai.ChatCompletionRequest{
Model: openai.GPT4,
MaxTokens: 256,
Messages: chatCompletionMessages,
@ -31,24 +37,24 @@ func CreateChatCompletion(system string, messages []Message) (string, error) {
client := openai.NewClient(config.OpenAI.APIKey)
resp, err := client.CreateChatCompletion(
context.Background(),
CreateChatCompletionRequest(messages),
*CreateChatCompletionRequest(system, messages),
)
if err != nil {
return "", fmt.Errorf("ChatCompletion error: %v\n", err)
return "", err
}
return resp.Choices[0].Message.Content, nil
}
func CreateChatCompletionStream(system string, messages []Message, output io.Writer) (error) {
func CreateChatCompletionStream(system string, messages []Message, output io.Writer) error {
client := openai.NewClient(config.OpenAI.APIKey)
ctx := context.Background()
req := CreateChatCompletionRequest(messages)
req := CreateChatCompletionRequest(system, messages)
req.Stream = true
stream, err := client.CreateChatCompletionStream(ctx, req)
stream, err := client.CreateChatCompletionStream(ctx, *req)
if err != nil {
return err
}