Parameterize maxTokens
This commit is contained in:
parent
0c2149663f
commit
7cb2a4e017
@ -7,6 +7,9 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TODO: allow setting with flag
|
||||||
|
const MAX_TOKENS = 256
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "lmcli",
|
Use: "lmcli",
|
||||||
Short: "Interact with Large Language Models",
|
Short: "Interact with Large Language Models",
|
||||||
@ -120,7 +123,7 @@ var newCmd = &cobra.Command{
|
|||||||
response <- HandleDelayedResponse(receiver)
|
response <- HandleDelayedResponse(receiver)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err = CreateChatCompletionStream(messages, receiver)
|
err = CreateChatCompletionStream(messages, MAX_TOKENS, receiver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("%v\n", err)
|
Fatal("%v\n", err)
|
||||||
}
|
}
|
||||||
@ -160,7 +163,7 @@ var promptCmd = &cobra.Command{
|
|||||||
|
|
||||||
receiver := make(chan string)
|
receiver := make(chan string)
|
||||||
go HandleDelayedResponse(receiver)
|
go HandleDelayedResponse(receiver)
|
||||||
err := CreateChatCompletionStream(messages, receiver)
|
err := CreateChatCompletionStream(messages, MAX_TOKENS, receiver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("%v\n", err)
|
Fatal("%v\n", err)
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
openai "github.com/sashabaranov/go-openai"
|
openai "github.com/sashabaranov/go-openai"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateChatCompletionRequest(messages []Message) *openai.ChatCompletionRequest {
|
func CreateChatCompletionRequest(messages []Message, maxTokens int) openai.ChatCompletionRequest {
|
||||||
chatCompletionMessages := []openai.ChatCompletionMessage{}
|
chatCompletionMessages := []openai.ChatCompletionMessage{}
|
||||||
for _, m := range messages {
|
for _, m := range messages {
|
||||||
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
|
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
|
||||||
@ -17,23 +17,19 @@ func CreateChatCompletionRequest(messages []Message) *openai.ChatCompletionReque
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return &openai.ChatCompletionRequest{
|
return openai.ChatCompletionRequest{
|
||||||
Model: openai.GPT4,
|
Model: openai.GPT3Dot5Turbo,
|
||||||
MaxTokens: 256,
|
|
||||||
Messages: chatCompletionMessages,
|
Messages: chatCompletionMessages,
|
||||||
Stream: true,
|
MaxTokens: maxTokens,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateChatCompletion submits a Chat Completion API request and returns the
|
// CreateChatCompletion submits a Chat Completion API request and returns the
|
||||||
// response.
|
// response.
|
||||||
func CreateChatCompletion(messages []Message) (string, error) {
|
func CreateChatCompletion(messages []Message, maxTokens int) (string, error) {
|
||||||
client := openai.NewClient(config.OpenAI.APIKey)
|
client := openai.NewClient(config.OpenAI.APIKey)
|
||||||
resp, err := client.CreateChatCompletion(
|
req := CreateChatCompletionRequest(messages, maxTokens)
|
||||||
context.Background(),
|
resp, err := client.CreateChatCompletion(context.Background(), req)
|
||||||
*CreateChatCompletionRequest(messages),
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -43,16 +39,13 @@ func CreateChatCompletion(messages []Message) (string, error) {
|
|||||||
|
|
||||||
// CreateChatCompletionStream submits a streaming Chat Completion API request
|
// CreateChatCompletionStream submits a streaming Chat Completion API request
|
||||||
// and streams the received response to the provided output channel.
|
// and streams the received response to the provided output channel.
|
||||||
func CreateChatCompletionStream(messages []Message, output chan string) error {
|
func CreateChatCompletionStream(messages []Message, maxTokens int, output chan string) error {
|
||||||
client := openai.NewClient(config.OpenAI.APIKey)
|
client := openai.NewClient(config.OpenAI.APIKey)
|
||||||
ctx := context.Background()
|
req := CreateChatCompletionRequest(messages, maxTokens)
|
||||||
|
|
||||||
req := CreateChatCompletionRequest(messages)
|
|
||||||
req.Stream = true
|
|
||||||
|
|
||||||
defer close(output)
|
defer close(output)
|
||||||
|
|
||||||
stream, err := client.CreateChatCompletionStream(ctx, *req)
|
stream, err := client.CreateChatCompletionStream(context.Background(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user