Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/all-in-aigc/openai-go
OpenAI Go SDK.
https://github.com/all-in-aigc/openai-go
chatgpt chatgpt-sdk openai-go openai-sdk
Last synced: about 2 months ago
JSON representation
OpenAI Go SDK.
- Host: GitHub
- URL: https://github.com/all-in-aigc/openai-go
- Owner: all-in-aigc
- License: mit
- Created: 2023-01-16T04:49:09.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-30T03:35:12.000Z (12 months ago)
- Last Synced: 2024-06-19T18:09:14.228Z (6 months ago)
- Topics: chatgpt, chatgpt-sdk, openai-go, openai-sdk
- Language: Go
- Homepage: https://gpts.works
- Size: 364 KB
- Stars: 58
- Watchers: 5
- Forks: 18
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# openai-go
openai go sdk
## Preparation
sign in [openai platform](https://platform.openai.com/api-keys), and get your own API_KEY.
![](./images/get_api_key.jpg)
## Quick Start
1. install openai-go sdk
```shell
go get -u github.com/all-in-aigc/openai-go
```2. request api with openai-go client
```go
package mainimport (
"fmt"
"log"
"time"openai "github.com/all-in-aigc/openai-go"
)func main() {
apiKey := "xxx" // your openai apikey, or azure openai apikey// new openai client
cli, _ := openai.NewClient(&openai.Options{
ApiKey: apiKey,
Timeout: 30 * time.Second,
Debug: true,
BaseUri: "https://xxx.com/openai", // use a proxy api, default baseuri is https://api.openai.com// use azure openai
// BaseUri: "https://xxx.openai.azure.com/openai/deployments/gpt-35-turbo-16k", // your azure openai endpoint
// ApiVersion: "2023-07-01-preview", // azure openai api version
})// request api
uri := "/v1/models"res, err := cli.Get(uri)
if err != nil {
log.Fatalf("request api failed: %v", err)
}for _, v := range res.Get("data").Array() {
fmt.Printf("model id: %s\n", v.Get("id").String())
}
}
```> see available apis in [OpenAI documents](https://platform.openai.com/docs/api-reference/completions/create)
## Examples
there are some examples under the `examples` folder, check and see how to request other apis.
- [Create chat completion](https://platform.openai.com/docs/api-reference/chat/create)
```go
cli := getClient()uri := "/v1/chat/completions"
params := map[string]interface{}{
"model": "gpt-3.5-turbo",
"messages": []map[string]interface{}{
{"role": "user", "content": "hello"},
},
}res, err := cli.Post(uri, params)
if err != nil {
log.Fatalf("request api failed: %v", err)
}message := res.Get("choices.0.message.content").String()
fmt.Printf("message is: %s", message)
// Output: xxx
```- [Create Chat Completion With Function Call](https://platform.openai.com/docs/api-reference/chat/create)
```go
userQuestion := "What is the weather like in Boston?"uri := "/v1/chat/completions"
params := map[string]interface{}{
"model": "gpt-3.5-turbo",
"messages": []map[string]interface{}{
{
"role": "user",
"content": userQuestion,
},
},
"functions": getFuncs(),
}res, err := cli.Post(uri, params)
if err != nil {
log.Fatalf("request api failed: %v", err)
}funcName := res.Get("choices.0.message.function_call.name").String()
funcArgs := res.Get("choices.0.message.function_call.arguments").String()if funcName == "" || funcArgs == "" {
log.Fatalf("function call get args failed: %s", res)
}fmt.Printf("function call name: %s, args: %v", funcName, funcArgs)
```- [Create Completion](https://beta.openai.com/docs/api-reference/completions/create)
```go
uri := "/v1/completions"
params := map[string]interface{}{
"model": "text-davinci-003",
"prompt": "say hello three times",
"max_tokens": 2048,
"temperature": 0.9,
"n": 1,
"stream": false,
}res, err := cli.Post(uri, params)
if err != nil {
log.Fatalf("request api failed: %v", err)
}fmt.Println(res.GetString("choices.0.text"))
```- [Create Edit](https://beta.openai.com/docs/api-reference/edits/create)
```go
uri := "/v1/edits"
params := map[string]interface{}{
"model": "text-davinci-edit-001",
"input": "Are you hapy today?",
"instruction": "fix mistake",
"temperature": 0.9,
"n": 1,
}res, err := cli.Post(uri, params)
if err != nil {
log.Fatalf("request api failed: %v", err)
}fmt.Println(res.GetString("choices.0.text"))
```- [Create Image](https://beta.openai.com/docs/api-reference/images/create)
```go
uri := "/v1/images/generations"
params := map[string]interface{}{
"prompt": "a beautiful girl with big eyes",
"n": 1,
"size": "256x256",
"response_format": "url",
}res, err := cli.Post(uri, params)
if err != nil {
log.Fatalf("request api failed: %v", err)
}fmt.Println(res.GetString("data.0.url"))
```## More
> if this project is helpful to you, buy me a coffee๐