Skip to content

Commit

Permalink
add recommendation logic (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jul 21, 2023
1 parent a448587 commit 831793a
Showing 1 changed file with 58 additions and 15 deletions.
73 changes: 58 additions & 15 deletions internal/hey/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package hey

import (
"bufio"
"context"
"fmt"
"os"
"strings"

"github.com/sashabaranov/go-openai"
"github.com/spf13/cobra"
Expand All @@ -19,24 +22,64 @@ func NewCommand(openaiApiKey string) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
// get client
openAIClient := openai.NewClient(openaiApiKey)
resp, err := openAIClient.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "Hello",
},
},

messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: `Below is a conversation with an AI chatbot.
The bot suggests recommended artists to the interlocutor.
Through a question-and-answer session, the bot asks the interlocutor what kind of music he or she likes and what kind of mood he or she is in. Based on the information obtained, we will suggest recommended artists. However, please observe the following rules.
[Rule]
1. Three questions will be asked.
2. Ask only one question at a time.
3. Recommend three artists.
4. Recommend artists based on three answers.
5. Output <end> at the end of a sentence when recommending artists
Then, please ask a question.`,
},
)
if err != nil {
return fmt.Errorf("chat completion error: %v", err)
}

// show response
fmt.Println(resp.Choices[0].Message.Content)
scanner := bufio.NewScanner(os.Stdin)
for {
resp, err := openAIClient.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: messages,
},
)
if err != nil {
return fmt.Errorf("chat completion error: %v", err)
}

// show AI response
respMessage := resp.Choices[0].Message.Content

// termination check
if strings.Contains(respMessage, "<end>") {
sp := strings.Split(respMessage, "<end>")
recommendMessage := strings.Join(sp, "")
fmt.Println(recommendMessage)
break
}

fmt.Println(respMessage)
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleAssistant,
Content: respMessage,
})

// get user answer
fmt.Printf("> ")
scanner.Scan()
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: scanner.Text(),
})
}

return nil
},
Expand Down

0 comments on commit 831793a

Please sign in to comment.