Skip to content

Commit 67458c9

Browse files
committed
Replace gopenai with go-openai and update chat command
1 parent 6d4a00e commit 67458c9

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

cmd/chat.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ Copyright © 2023 Chandler <chandler@chand1012.dev>
44
package cmd
55

66
import (
7+
"context"
78
"fmt"
89
"os"
910

10-
gopenai "github.com/CasualCodersProjects/gopenai"
1111
"github.com/chand1012/ottodocs/pkg/config"
12+
"github.com/sashabaranov/go-openai"
1213
"github.com/spf13/cobra"
1314
)
1415

@@ -31,26 +32,35 @@ If '-q' is not specified, the user will be prompted to enter a question.
3132
os.Exit(1)
3233
}
3334

34-
openai := gopenai.NewOpenAI(&gopenai.OpenAIOpts{
35-
APIKey: conf.APIKey,
36-
})
35+
client := openai.NewClient(conf.APIKey)
3736

3837
// if the question is not provided, prompt the user for it
3938
if question == "" {
4039
fmt.Print("What would you like to chat ChatGPT?\n> ")
4140
fmt.Scanln(&question)
4241
}
4342

44-
// chat ChatGPT the question
45-
resp, err := openai.CreateChatSimple(question, 0) // 0 sets the max tokens to 1024
43+
resp, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{
44+
Model: conf.Model,
45+
Messages: []openai.ChatCompletionMessage{
46+
{
47+
Content: question,
48+
Role: openai.ChatMessageRoleUser,
49+
},
50+
},
51+
})
52+
4653
if err != nil {
47-
log.Errorf("Error: %s", err)
54+
log.Error(err)
4855
os.Exit(1)
4956
}
5057

51-
message := resp.Choices[0].Message.Content
58+
if len(resp.Choices) == 0 {
59+
log.Error("No choices returned")
60+
os.Exit(1)
61+
}
5262

53-
fmt.Println(message)
63+
fmt.Println(resp.Choices[0].Message.Content)
5464
},
5565
}
5666

cmd/commit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ var commitCmd = &cobra.Command{
6868
}
6969
fmt.Println(output)
7070
}
71+
os.Exit(0)
7172
}
7273

7374
if plain {

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/chand1012/ottodocs
33
go 1.20
44

55
require (
6-
github.com/CasualCodersProjects/gopenai v0.3.0
76
github.com/chand1012/git2gpt v0.4.1
87
github.com/chand1012/memory v0.3.0
98
github.com/charmbracelet/log v0.2.1

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2-
github.com/CasualCodersProjects/gopenai v0.3.0 h1:vTFe2Rl/At+LnRL0q/joNHYZGVQd19HXFaQT6aNnpWg=
3-
github.com/CasualCodersProjects/gopenai v0.3.0/go.mod h1:NGGTkFg60IYsXynphxExU11U4YqWDKEg/XqP9sVvwEY=
42
github.com/RoaringBitmap/roaring v0.4.23/go.mod h1:D0gp8kJQgE1A4LQ5wFLggQEyvDi06Mq5mKs52e1TwOo=
53
github.com/RoaringBitmap/roaring v1.2.3 h1:yqreLINqIrX22ErkKI0vY47/ivtJr6n+kMhVOVmhWBY=
64
github.com/RoaringBitmap/roaring v1.2.3/go.mod h1:plvDsJQpxOC5bw8LRteu/MLWHsHez/3y6cubLI4/1yE=

0 commit comments

Comments
 (0)