Add Readme example to example_test.go (#298)
* Add speech to text example in docs * Add caption formats for audio transcription * Add caption example to README * Address sanity check errors * Add tests for decodeResponse * Use typechecker for audio response format * Decoding response refactors * Migrated examples to example_test.go * Add some executable examples * Update error docs * Avoid linting example files which break conventions * Restore README examples * Enable linting for example_test
This commit is contained in:
6
examples/README.md
Normal file
6
examples/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
To run an example:
|
||||
|
||||
```
|
||||
export OPENAI_API_KEY="<your key here>"
|
||||
go run ./example/<target>
|
||||
```
|
||||
42
examples/chatbot/main.go
Normal file
42
examples/chatbot/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
func main() {
|
||||
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
|
||||
|
||||
req := openai.ChatCompletionRequest{
|
||||
Model: openai.GPT3Dot5Turbo,
|
||||
Messages: []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: openai.ChatMessageRoleSystem,
|
||||
Content: "you are a helpful chatbot",
|
||||
},
|
||||
},
|
||||
}
|
||||
fmt.Println("Conversation")
|
||||
fmt.Println("---------------------")
|
||||
fmt.Print("> ")
|
||||
s := bufio.NewScanner(os.Stdin)
|
||||
for s.Scan() {
|
||||
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: s.Text(),
|
||||
})
|
||||
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)
|
||||
req.Messages = append(req.Messages, resp.Choices[0].Message)
|
||||
fmt.Print("> ")
|
||||
}
|
||||
}
|
||||
26
examples/completion/main.go
Normal file
26
examples/completion/main.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
func main() {
|
||||
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
|
||||
resp, err := client.CreateCompletion(
|
||||
context.Background(),
|
||||
openai.CompletionRequest{
|
||||
Model: openai.GPT3Ada,
|
||||
MaxTokens: 5,
|
||||
Prompt: "Lorem ipsum",
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("Completion error: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(resp.Choices[0].Text)
|
||||
}
|
||||
28
examples/images/main.go
Normal file
28
examples/images/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
func main() {
|
||||
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
|
||||
|
||||
respUrl, err := client.CreateImage(
|
||||
context.Background(),
|
||||
openai.ImageRequest{
|
||||
Prompt: "Parrot on a skateboard performs a trick, cartoon style, natural light, high detail",
|
||||
Size: openai.CreateImageSize256x256,
|
||||
ResponseFormat: openai.CreateImageResponseFormatURL,
|
||||
N: 1,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("Image creation error: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(respUrl.Data[0].URL)
|
||||
}
|
||||
35
examples/voice-to-text/main.go
Normal file
35
examples/voice-to-text/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("please provide a filename to convert to text")
|
||||
return
|
||||
}
|
||||
if _, err := os.Stat(os.Args[1]); errors.Is(err, os.ErrNotExist) {
|
||||
fmt.Printf("file %s does not exist\n", os.Args[1])
|
||||
return
|
||||
}
|
||||
|
||||
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
|
||||
resp, err := client.CreateTranscription(
|
||||
context.Background(),
|
||||
openai.AudioRequest{
|
||||
Model: openai.Whisper1,
|
||||
FilePath: os.Args[1],
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("Transcription error: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(resp.Text)
|
||||
}
|
||||
Reference in New Issue
Block a user