Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions API keys
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"context"
"fmt"
"log"
"os"

openai "github.com/sashabaranov/go-openai"
)

func main() {
// Best Practice: load the API key from an environment variable.
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
log.Fatalln("Missing OPENAI_API_KEY environment variable")
}

client := openai.NewClient(apiKey)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "Hello, world!",
},
},
},
)

if err != nil {
// Using log provides timestamp and source file information, which is
// helpful for debugging.
log.Printf("ChatCompletion error: %v\n", err)
return
}

// The response may contain multiple choices, so it's good practice
// to check the length of the Choices array.
if len(resp.Choices) > 0 {
fmt.Println(resp.Choices[0].Message.Content)
} else {
fmt.Println("No choices returned from the API.")
}
}
Loading