Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documentation for examples are added #842

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions examples/chatbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,40 @@ import (
)

func main() {
// Initialize the OpenAI client with the API key from the environment variable
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

// Create a ChatCompletionRequest with the initial system message
req := openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Model: openai.GPT3Dot5Turbo, // Specifies the model to use
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "you are a helpful chatbot",
Content: "you are a helpful chatbot",// System message setting the chatbot's role
},
},
}

// Start the conversation loop
fmt.Println("Conversation")
fmt.Println("---------------------")
fmt.Print("> ")
s := bufio.NewScanner(os.Stdin)
for s.Scan() {

// Append the user's input to the request messages
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: s.Text(),
})
// Send the request to the OpenAI API
resp, err := client.CreateChatCompletion(context.Background(), req)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
continue
}
fmt.Printf("%s\n\n", resp.Choices[0].Message.Content)
// Append the chatbot's response to the conversation
req.Messages = append(req.Messages, resp.Choices[0].Message)
fmt.Print("> ")
}
Expand Down
14 changes: 11 additions & 3 deletions examples/completion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ import (
)

func main() {
// Initialize the OpenAI client with the API key from the environment variable
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

// Create a CompletionRequest with the desired model, max tokens, and prompt
resp, err := client.CreateCompletion(
context.Background(),
openai.CompletionRequest{
Model: openai.GPT3Babbage002,
MaxTokens: 5,
Prompt: "Lorem ipsum",

Model: openai.GPT3Babbage002, // Specifies the model to use (GPT-3 Ada in this case)
MaxTokens: 5, // Limits the number of tokens (words/characters) in the response
Prompt: "Lorem ipsum", // The prompt or input text to generate a completion for

},
)
// Handle any errors that occur during the API request
if err != nil {
fmt.Printf("Completion error: %v\n", err)
return
}

// Print the generated text completion from the API response
fmt.Println(resp.Choices[0].Text)
}
2 changes: 2 additions & 0 deletions examples/images/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
)

func main() {
// Initialize the OpenAI client with the API key
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

// Create an image based on a text prompt
respUrl, err := client.CreateImage(
context.Background(),
openai.ImageRequest{
Expand Down
6 changes: 5 additions & 1 deletion examples/voice-to-text/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import (
)

func main() {
// Check if the user has provided a filename as an argument
if len(os.Args) < 2 {
fmt.Println("please provide a filename to convert to text")
return
}
// Check if the provided file exists
if _, err := os.Stat(os.Args[1]); errors.Is(err, os.ErrNotExist) {
fmt.Printf("file %s does not exist\n", os.Args[1])
return
}

// Initialize the OpenAI client with the API key
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

// Create a transcription request
resp, err := client.CreateTranscription(
context.Background(),
openai.AudioRequest{
Expand Down
Loading