https://github.com/lsongdev/openai-go
OpenAI Golang Library
https://github.com/lsongdev/openai-go
Last synced: about 1 year ago
JSON representation
OpenAI Golang Library
- Host: GitHub
- URL: https://github.com/lsongdev/openai-go
- Owner: lsongdev
- Created: 2022-12-06T09:24:52.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-01T07:32:23.000Z (over 2 years ago)
- Last Synced: 2025-02-13T22:23:46.581Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# openai.go
> OpenAI Library in Golang
## Example
### Completion API with GPT-3 Model
```go
package main
import (
"log"
"github.com/song940/openai-go/openai"
)
func main() {
config := openai.Configuration{
APIKey: "your api key",
}
client, err := openai.NewClient(config)
if err != nil {
log.Fatal(err)
}
req := openai.CompletionRequest{
Model: openai.GPT3TextDavinci003,
MaxTokens: 2048,
Prompt: "Say this is a test",
}
resp, err := client.CreateCompletion(req)
if err != nil {
panic(err)
}
log.Println(resp.Choices[0].Text)
}
```
### Chat API with ChatAGPT Model
```go
package main
import (
"log"
"github.com/song940/openai-go/openai"
)
func main() {
config := openai.Configuration{
APIKey: "your api key",
}
client, err := openai.NewClient(config)
if err != nil {
log.Fatal(err)
}
message := openai.ChatCompletionMessage{
Role: openai.RoleUser,
Content: "Hello!",
}
req := openai.ChatCompletionRequest{
Model: "gpt-3.5-turbo",
MaxTokens: 2048,
Messages: []openai.ChatCompletionMessage{
message,
},
}
resp, err := client.CreateChatCompletion(req)
if err != nil {
panic(err)
}
log.Println(resp.Choices[0].Message.Role)
log.Println(resp.Choices[0].Message.Content)
}
```
## License
This Project is licensed under the MIT License.