Include system prompt in OpenAI chat completion requests
This commit is contained in:
parent
04478cbbd1
commit
5c6ec5e4e2
@ -8,8 +8,14 @@ import (
|
|||||||
openai "github.com/sashabaranov/go-openai"
|
openai "github.com/sashabaranov/go-openai"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateChatCompletionRequest(messages []Message) (openai.ChatCompletionRequest) {
|
func CreateChatCompletionRequest(system string, messages []Message) (*openai.ChatCompletionRequest) {
|
||||||
var chatCompletionMessages []openai.ChatCompletionMessage
|
chatCompletionMessages := []openai.ChatCompletionMessage{
|
||||||
|
{
|
||||||
|
Role: "system",
|
||||||
|
Content: system,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
for _, m := range(messages) {
|
for _, m := range(messages) {
|
||||||
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
|
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
|
||||||
Role: m.Role,
|
Role: m.Role,
|
||||||
@ -17,7 +23,7 @@ func CreateChatCompletionRequest(messages []Message) (openai.ChatCompletionReque
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return openai.ChatCompletionRequest{
|
return &openai.ChatCompletionRequest{
|
||||||
Model: openai.GPT4,
|
Model: openai.GPT4,
|
||||||
MaxTokens: 256,
|
MaxTokens: 256,
|
||||||
Messages: chatCompletionMessages,
|
Messages: chatCompletionMessages,
|
||||||
@ -31,24 +37,24 @@ func CreateChatCompletion(system string, messages []Message) (string, error) {
|
|||||||
client := openai.NewClient(config.OpenAI.APIKey)
|
client := openai.NewClient(config.OpenAI.APIKey)
|
||||||
resp, err := client.CreateChatCompletion(
|
resp, err := client.CreateChatCompletion(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
CreateChatCompletionRequest(messages),
|
*CreateChatCompletionRequest(system, messages),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("ChatCompletion error: %v\n", err)
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp.Choices[0].Message.Content, nil
|
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)
|
client := openai.NewClient(config.OpenAI.APIKey)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
req := CreateChatCompletionRequest(messages)
|
req := CreateChatCompletionRequest(system, messages)
|
||||||
req.Stream = true
|
req.Stream = true
|
||||||
|
|
||||||
stream, err := client.CreateChatCompletionStream(ctx, req)
|
stream, err := client.CreateChatCompletionStream(ctx, *req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user