Skip to content

Commit

Permalink
Closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Kardbord committed Apr 8, 2023
1 parent 1b0d165 commit ed92b7b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ examples/finetunes/finetunes
examples/images/images
examples/models/models
examples/moderations/moderations
examples/chat/chat
dist/
7 changes: 7 additions & 0 deletions chat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Chat

Bindings for the [chat](https://platform.openai.com/docs/api-reference/chat) [endpoint](https://api.openai.com/v1/chat/completions).

## Example

See [chat-example.go](../examples/chat/chat-example.go).
60 changes: 60 additions & 0 deletions examples/chat/chat-example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

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

"github.com/TannerKvarfordt/gopenai/authentication"
"github.com/TannerKvarfordt/gopenai/chat"
)

const OpenAITokenEnv = "OPENAI_API_KEY"

func init() {
key := os.Getenv(OpenAITokenEnv)
authentication.SetAPIKey(key)
}

const (
model string = "gpt-3.5-turbo"
quit string = "quit"
)

func main() {
fmt.Println("Welcome to ChatGPT! Quit at any time by typing \"quit\".\nStart the conversation by typing a message, and hitting enter.")

chatlog := make([]chat.Chat, 0, 10)
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("User: ")
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error: ", err)
return
}
if strings.ToLower(strings.TrimSpace(input)) == quit {
fmt.Println("Goodbye!")
return
}

fmt.Println("ChatGPT is thinking...")
r, _, err := chat.MakeModeratedRequest(&chat.Request{
Model: model,
Messages: append(chatlog, chat.Chat{
Role: chat.UserRole,
Content: input,
}),
User: "https://github.com/TannerKvarfordt/gopenai",
}, nil)

if err != nil {
fmt.Println("Error: ", err)
continue
}

chatlog = append(chatlog, r.Choices[0].Message)
fmt.Println("ChatGPT:", r.Choices[0].Message.Content)
}
}

0 comments on commit ed92b7b

Please sign in to comment.