lmcli/pkg/cli/openai.go

73 lines
1.9 KiB
Go
Raw Normal View History

package cli
2023-10-30 15:23:07 -06:00
import (
"context"
2023-10-30 15:45:21 -06:00
"errors"
"io"
"strings"
2023-11-04 16:56:22 -06:00
2023-10-30 15:23:07 -06:00
openai "github.com/sashabaranov/go-openai"
)
func CreateChatCompletionRequest(model string, messages []Message, maxTokens int) openai.ChatCompletionRequest {
chatCompletionMessages := []openai.ChatCompletionMessage{}
2023-11-04 16:56:22 -06:00
for _, m := range messages {
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,
})
}
return openai.ChatCompletionRequest{
Model: model,
2023-11-04 16:56:22 -06:00
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)
2023-10-30 15:23:07 -06:00
if err != nil {
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
// CreateChatCompletionStream submits a streaming Chat Completion API request
// and both returns and streams the response to the provided output channel.
// May return a partial response if an error occurs mid-stream.
func CreateChatCompletionStream(model string, messages []Message, maxTokens int, output chan<- string) (string, error) {
client := openai.NewClient(*config.OpenAI.APIKey)
req := CreateChatCompletionRequest(model, messages, maxTokens)
2023-10-30 15:45:21 -06:00
defer close(output)
stream, err := client.CreateChatCompletionStream(context.Background(), req)
2023-10-30 15:45:21 -06:00
if err != nil {
return "", err
2023-10-30 15:45:21 -06:00
}
defer stream.Close()
sb := strings.Builder{}
2023-10-30 15:45:21 -06:00
for {
response, e := stream.Recv()
if errors.Is(e, io.EOF) {
break
2023-10-30 15:45:21 -06:00
}
if e != nil {
err = e
break
2023-10-30 15:45:21 -06:00
}
chunk := response.Choices[0].Delta.Content
output <- chunk
sb.WriteString(chunk)
2023-10-30 15:45:21 -06:00
}
return sb.String(), err
2023-10-30 15:45:21 -06:00
}