├── LICENSE ├── README.md ├── client.go ├── examples ├── .gitignore ├── chat_test.go ├── client_test.go ├── completion_test.go ├── edit_test.go ├── function_call_test.go ├── image_test.go └── model_test.go ├── go.mod ├── go.sum ├── images └── get_api_key.jpg └── option.go /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Miguel Piedrafita 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # openai-go 2 | 3 | openai go sdk 4 | 5 | ## Preparation 6 | 7 | sign in [openai platform](https://platform.openai.com/api-keys), and get your own API_KEY. 8 | 9 | ![](./images/get_api_key.jpg) 10 | 11 | ## Quick Start 12 | 13 | 1. install openai-go sdk 14 | 15 | ```shell 16 | go get -u github.com/all-in-aigc/openai-go 17 | ``` 18 | 19 | 2. request api with openai-go client 20 | 21 | ```go 22 | package main 23 | 24 | import ( 25 | "fmt" 26 | "log" 27 | "time" 28 | 29 | openai "github.com/all-in-aigc/openai-go" 30 | ) 31 | 32 | func main() { 33 | apiKey := "xxx" // your openai apikey, or azure openai apikey 34 | 35 | // new openai client 36 | cli, _ := openai.NewClient(&openai.Options{ 37 | ApiKey: apiKey, 38 | Timeout: 30 * time.Second, 39 | Debug: true, 40 | BaseUri: "https://xxx.com/openai", // use a proxy api, default baseuri is https://api.openai.com 41 | 42 | // use azure openai 43 | // BaseUri: "https://xxx.openai.azure.com/openai/deployments/gpt-35-turbo-16k", // your azure openai endpoint 44 | // ApiVersion: "2023-07-01-preview", // azure openai api version 45 | }) 46 | 47 | // request api 48 | uri := "/v1/models" 49 | 50 | res, err := cli.Get(uri) 51 | 52 | if err != nil { 53 | log.Fatalf("request api failed: %v", err) 54 | } 55 | 56 | for _, v := range res.Get("data").Array() { 57 | fmt.Printf("model id: %s\n", v.Get("id").String()) 58 | } 59 | } 60 | ``` 61 | 62 | > see available apis in [OpenAI documents](https://platform.openai.com/docs/api-reference/completions/create) 63 | 64 | ## Examples 65 | 66 | there are some examples under the `examples` folder, check and see how to request other apis. 67 | 68 | - [Create chat completion](https://platform.openai.com/docs/api-reference/chat/create) 69 | 70 | ```go 71 | cli := getClient() 72 | 73 | uri := "/v1/chat/completions" 74 | params := map[string]interface{}{ 75 | "model": "gpt-3.5-turbo", 76 | "messages": []map[string]interface{}{ 77 | {"role": "user", "content": "hello"}, 78 | }, 79 | } 80 | 81 | res, err := cli.Post(uri, params) 82 | if err != nil { 83 | log.Fatalf("request api failed: %v", err) 84 | } 85 | 86 | message := res.Get("choices.0.message.content").String() 87 | 88 | fmt.Printf("message is: %s", message) 89 | // Output: xxx 90 | ``` 91 | 92 | - [Create Chat Completion With Function Call](https://platform.openai.com/docs/api-reference/chat/create) 93 | 94 | ```go 95 | userQuestion := "What is the weather like in Boston?" 96 | 97 | uri := "/v1/chat/completions" 98 | params := map[string]interface{}{ 99 | "model": "gpt-3.5-turbo", 100 | "messages": []map[string]interface{}{ 101 | { 102 | "role": "user", 103 | "content": userQuestion, 104 | }, 105 | }, 106 | "functions": getFuncs(), 107 | } 108 | 109 | res, err := cli.Post(uri, params) 110 | if err != nil { 111 | log.Fatalf("request api failed: %v", err) 112 | } 113 | 114 | funcName := res.Get("choices.0.message.function_call.name").String() 115 | funcArgs := res.Get("choices.0.message.function_call.arguments").String() 116 | 117 | if funcName == "" || funcArgs == "" { 118 | log.Fatalf("function call get args failed: %s", res) 119 | } 120 | 121 | fmt.Printf("function call name: %s, args: %v", funcName, funcArgs) 122 | ``` 123 | 124 | - [Create Completion](https://beta.openai.com/docs/api-reference/completions/create) 125 | 126 | ```go 127 | uri := "/v1/completions" 128 | params := map[string]interface{}{ 129 | "model": "text-davinci-003", 130 | "prompt": "say hello three times", 131 | "max_tokens": 2048, 132 | "temperature": 0.9, 133 | "n": 1, 134 | "stream": false, 135 | } 136 | 137 | res, err := cli.Post(uri, params) 138 | 139 | if err != nil { 140 | log.Fatalf("request api failed: %v", err) 141 | } 142 | 143 | fmt.Println(res.GetString("choices.0.text")) 144 | ``` 145 | 146 | - [Create Edit](https://beta.openai.com/docs/api-reference/edits/create) 147 | 148 | ```go 149 | uri := "/v1/edits" 150 | params := map[string]interface{}{ 151 | "model": "text-davinci-edit-001", 152 | "input": "Are you hapy today?", 153 | "instruction": "fix mistake", 154 | "temperature": 0.9, 155 | "n": 1, 156 | } 157 | 158 | res, err := cli.Post(uri, params) 159 | 160 | if err != nil { 161 | log.Fatalf("request api failed: %v", err) 162 | } 163 | 164 | fmt.Println(res.GetString("choices.0.text")) 165 | ``` 166 | 167 | - [Create Image](https://beta.openai.com/docs/api-reference/images/create) 168 | 169 | ```go 170 | uri := "/v1/images/generations" 171 | params := map[string]interface{}{ 172 | "prompt": "a beautiful girl with big eyes", 173 | "n": 1, 174 | "size": "256x256", 175 | "response_format": "url", 176 | } 177 | 178 | res, err := cli.Post(uri, params) 179 | 180 | if err != nil { 181 | log.Fatalf("request api failed: %v", err) 182 | } 183 | 184 | fmt.Println(res.GetString("data.0.url")) 185 | ``` 186 | 187 | ## More 188 | 189 | > if this project is helpful to you, buy me a coffee😄 190 | 191 | Buy Me A Coffee 192 | 193 | -------------------------------------------------------------------------------- /client.go: -------------------------------------------------------------------------------- 1 | package openai 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "time" 7 | 8 | "github.com/idoubi/goutils/request" 9 | ) 10 | 11 | // Client: OpenAI client 12 | type Client struct { 13 | opts *Options // custom options 14 | requestClient *request.Client 15 | } 16 | 17 | // NewClient: new OpenAI Client 18 | func NewClient(opts *Options) (*Client, error) { 19 | // set default options 20 | if opts.Timeout <= 0 { 21 | opts.Timeout = 30 * time.Second 22 | } 23 | // set default api baseuri 24 | if opts.BaseUri == "" { 25 | opts.BaseUri = "https://api.openai.com" 26 | } else { 27 | if strings.Contains(opts.BaseUri, "openai.azure.com") { 28 | opts.isAzure = true 29 | } 30 | } 31 | 32 | cli := &Client{opts: opts} 33 | 34 | // set request client 35 | cli.requestClient = request.NewClient(&request.Options{ 36 | BaseUri: opts.BaseUri, 37 | Debug: opts.Debug, 38 | Timeout: opts.Timeout, 39 | }) 40 | 41 | return cli, nil 42 | } 43 | 44 | // Get: request api with get method 45 | func (cli *Client) Get(uri string, args ...map[string]interface{}) (*request.Result, error) { 46 | if cli.opts.isAzure { 47 | uri = strings.TrimPrefix(uri, "/v1") + "?api-version=" + cli.opts.ApiVersion 48 | } 49 | 50 | params, headers := cli.parseArgs(args...) 51 | 52 | return cli.getRequestClient().Get(uri, params, headers) 53 | } 54 | 55 | // Post: request api with post method 56 | func (cli *Client) Post(uri string, args ...map[string]interface{}) (*request.Result, error) { 57 | if cli.opts.isAzure { 58 | uri = strings.TrimPrefix(uri, "/v1") + "?api-version=" + cli.opts.ApiVersion 59 | } 60 | 61 | data, headers := cli.parseArgs(args...) 62 | 63 | // use default model 64 | if _, ok := data["model"]; !ok { 65 | data["model"] = cli.opts.Model 66 | } 67 | 68 | return cli.getRequestClient().Post(uri, data, headers) 69 | } 70 | 71 | // parseArgs: parse request args and append api_key 72 | func (cli *Client) parseArgs(args ...map[string]interface{}) (map[string]interface{}, map[string]interface{}) { 73 | params := map[string]interface{}{} 74 | headers := map[string]interface{}{} 75 | 76 | if len(args) > 0 { 77 | params = args[0] 78 | } 79 | if len(args) > 1 { 80 | headers = args[1] 81 | } 82 | 83 | if cli.opts.isAzure { 84 | headers["api-key"] = cli.opts.ApiKey 85 | } else { 86 | headers["Authorization"] = fmt.Sprintf("Bearer %s", cli.opts.ApiKey) 87 | } 88 | 89 | return params, headers 90 | } 91 | 92 | // getRequestClient: get request handler 93 | func (cli *Client) getRequestClient() *request.Client { 94 | return cli.requestClient 95 | } 96 | -------------------------------------------------------------------------------- /examples/.gitignore: -------------------------------------------------------------------------------- 1 | init_test.go -------------------------------------------------------------------------------- /examples/chat_test.go: -------------------------------------------------------------------------------- 1 | package examples 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "strings" 7 | 8 | "github.com/tidwall/gjson" 9 | ) 10 | 11 | func ExampleCreateChatCompletion() { 12 | cli := getClient() 13 | 14 | uri := "/v1/chat/completions" 15 | params := map[string]interface{}{ 16 | "model": "gpt-3.5-turbo", 17 | "messages": []map[string]interface{}{ 18 | {"role": "user", "content": "hello"}, 19 | }, 20 | } 21 | 22 | res, err := cli.Post(uri, params) 23 | if err != nil { 24 | log.Fatalf("request api failed: %v", err) 25 | } 26 | 27 | message := res.Get("choices.0.message.content").String() 28 | fmt.Printf("message is: %s", message) 29 | // Output: xxx 30 | } 31 | 32 | func ExampleCreateChatCompletionWithStream() { 33 | cli := getClient() 34 | 35 | uri := "/v1/chat/completions" 36 | params := map[string]interface{}{ 37 | "model": "gpt-3.5-turbo", 38 | "messages": []map[string]interface{}{ 39 | {"role": "user", "content": "hello"}, 40 | }, 41 | "stream": true, 42 | } 43 | 44 | res, err := cli.Post(uri, params) 45 | if err != nil { 46 | log.Fatalf("request api failed: %v", err) 47 | } 48 | 49 | msgs := []string{} 50 | 51 | for data := range res.Stream() { 52 | d := gjson.ParseBytes(data) 53 | s := d.Get("choices.0.delta.content").String() 54 | msgs = append(msgs, s) 55 | 56 | log.Printf("%s\n", s) 57 | } 58 | 59 | fmt.Printf("message is: %s", strings.Join(msgs, "")) 60 | // Output: xxx 61 | } 62 | -------------------------------------------------------------------------------- /examples/client_test.go: -------------------------------------------------------------------------------- 1 | package examples 2 | 3 | import ( 4 | "time" 5 | 6 | "github.com/all-in-aigc/openai-go" 7 | ) 8 | 9 | var ( 10 | apiBaseUri string 11 | apiKey string 12 | apiVersion string 13 | model string 14 | ) 15 | 16 | func getClient() *openai.Client { 17 | cli, _ := openai.NewClient(&openai.Options{ 18 | BaseUri: apiBaseUri, 19 | ApiKey: apiKey, 20 | Timeout: 30 * time.Second, 21 | Debug: true, 22 | ApiVersion: apiVersion, 23 | Model: model, 24 | }) 25 | 26 | return cli 27 | } 28 | -------------------------------------------------------------------------------- /examples/completion_test.go: -------------------------------------------------------------------------------- 1 | package examples 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "strings" 7 | 8 | "github.com/tidwall/gjson" 9 | ) 10 | 11 | func ExampleCreateCompletion() { 12 | cli := getClient() 13 | 14 | uri := "/v1/completions" 15 | params := map[string]interface{}{ 16 | "model": "text-davinci-003", 17 | "prompt": "say hello to me 10 times", 18 | "max_tokens": 2048, 19 | "temperature": 0.9, 20 | "n": 1, 21 | } 22 | 23 | res, err := cli.Post(uri, params) 24 | if err != nil { 25 | log.Fatalf("request api failed: %v", err) 26 | } 27 | 28 | message := res.Get("choices.0.text").String() 29 | 30 | fmt.Printf("message is: %s", message) 31 | // Output: xxx 32 | } 33 | 34 | func ExampleCreateCompletionWithStream() { 35 | cli := getClient() 36 | 37 | uri := "/v1/completions" 38 | params := map[string]interface{}{ 39 | "model": "text-davinci-003", 40 | "prompt": "say hello to me 10 times", 41 | "max_tokens": 2048, 42 | "temperature": 0.9, 43 | "n": 1, 44 | "stream": true, 45 | } 46 | 47 | res, err := cli.Post(uri, params) 48 | if err != nil { 49 | log.Fatalf("request api failed: %v", err) 50 | } 51 | 52 | msgs := []string{} 53 | 54 | for data := range res.Stream() { 55 | d := gjson.ParseBytes(data) 56 | s := d.Get("choices.0.text").String() 57 | msgs = append(msgs, s) 58 | 59 | log.Printf("%s\n", s) 60 | } 61 | 62 | fmt.Printf("message is: %s", strings.Join(msgs, "")) 63 | // Output: xxx 64 | } 65 | -------------------------------------------------------------------------------- /examples/edit_test.go: -------------------------------------------------------------------------------- 1 | package examples 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | ) 7 | 8 | func ExampleCreateEdit() { 9 | cli := getClient() 10 | 11 | uri := "/v1/edits" 12 | params := map[string]interface{}{ 13 | "model": "text-davinci-edit-001", 14 | "input": "Are you hapy today?", 15 | "instruction": "fix mistake", 16 | "temperature": 0.9, 17 | "n": 1, 18 | } 19 | 20 | res, err := cli.Post(uri, params) 21 | 22 | if err != nil { 23 | log.Fatalf("request api failed: %v", err) 24 | } 25 | 26 | fmt.Println(res.GetString("choices.0.text")) 27 | 28 | // Output: xxx 29 | } 30 | -------------------------------------------------------------------------------- /examples/function_call_test.go: -------------------------------------------------------------------------------- 1 | package examples 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | "github.com/tidwall/gjson" 8 | ) 9 | 10 | func ExampleFunctionCallArgs() { 11 | cli := getClient() 12 | 13 | userQuestion := "What is the weather like in Boston?" 14 | 15 | uri := "/v1/chat/completions" 16 | params := map[string]interface{}{ 17 | "model": "gpt-3.5-turbo", 18 | "messages": []map[string]interface{}{ 19 | { 20 | "role": "user", 21 | "content": userQuestion, 22 | }, 23 | }, 24 | "functions": getFuncs(), 25 | } 26 | 27 | res, err := cli.Post(uri, params) 28 | if err != nil { 29 | log.Fatalf("request api failed: %v", err) 30 | } 31 | 32 | funcName := res.Get("choices.0.message.function_call.name").String() 33 | funcArgs := res.Get("choices.0.message.function_call.arguments").String() 34 | 35 | if funcName == "" || funcArgs == "" { 36 | log.Fatalf("function call get args failed: %s", res) 37 | } 38 | 39 | fmt.Printf("function call name: %s, args: %v", funcName, funcArgs) 40 | // Output: xxx 41 | } 42 | 43 | func ExampleFunctionCallArgsWithStream() { 44 | cli := getClient() 45 | 46 | userQuestion := "What is the weather like in Boston?" 47 | 48 | uri := "/v1/chat/completions" 49 | params := map[string]interface{}{ 50 | "model": "gpt-3.5-turbo", 51 | "messages": []map[string]interface{}{ 52 | { 53 | "role": "user", 54 | "content": userQuestion, 55 | }, 56 | }, 57 | "functions": getFuncs(), 58 | "stream": true, 59 | } 60 | 61 | res, err := cli.Post(uri, params) 62 | if err != nil { 63 | log.Fatalf("request api failed: %v", err) 64 | } 65 | 66 | var funcName string 67 | var funcArgs string 68 | 69 | for data := range res.Stream() { 70 | d := gjson.ParseBytes(data) 71 | 72 | fc := d.Get("choices.0.delta.function_call").String() 73 | if fc != "" { 74 | if name := d.Get("choices.0.delta.function_call.name").String(); name != "" { 75 | funcName = name 76 | } 77 | funcArgs += d.Get("choices.0.delta.function_call.arguments").String() 78 | 79 | continue 80 | } 81 | log.Printf("%s\n", d) 82 | } 83 | 84 | fmt.Printf("function call name: %s, args: %v", funcName, funcArgs) 85 | // Output: xxx 86 | } 87 | 88 | func ExampleFunctionCallSummarize() { 89 | cli := getClient() 90 | 91 | userQuestion := "What is the weather like in Boston?" 92 | 93 | // value get from function call args 94 | funcName := "get_current_weather" 95 | funcArgs := "{\n \"location\": \"Boston, MA\"\n}" 96 | 97 | // value get from api call 98 | apiRes := "" 99 | 100 | uri := "/v1/chat/completions" 101 | params := map[string]interface{}{ 102 | "model": "gpt-3.5-turbo", 103 | "messages": []map[string]interface{}{ 104 | { 105 | "role": "user", 106 | "content": userQuestion, 107 | }, 108 | { 109 | "role": "assistant", 110 | "content": nil, 111 | "function_call": map[string]interface{}{ 112 | "name": funcName, 113 | "arguments": funcArgs, 114 | }, 115 | }, 116 | { 117 | "role": "function", 118 | "name": funcName, 119 | "content": apiRes, 120 | }, 121 | }, 122 | "functions": getFuncs(), 123 | } 124 | 125 | res, err := cli.Post(uri, params) 126 | if err != nil { 127 | log.Fatalf("request api failed: %v", err) 128 | } 129 | 130 | message := res.Get("choices.0.message.content").String() 131 | fmt.Printf("message is: %s", message) 132 | // Output: xxx 133 | } 134 | 135 | func getFuncs() []map[string]interface{} { 136 | funcs := []map[string]interface{}{} 137 | 138 | getWeatherFunc := map[string]interface{}{ 139 | "name": "get_current_weather", 140 | "description": "Get the current weather in a given location", 141 | "parameters": map[string]interface{}{ 142 | "type": "object", 143 | "properties": map[string]interface{}{ 144 | "location": map[string]interface{}{ 145 | "type": "string", 146 | "description": "The city and state, e.g. San Francisco, CA", 147 | }, 148 | "unit": map[string]interface{}{ 149 | "type": "string", 150 | "enum": []string{"celsius", "fahrenheit"}, 151 | }, 152 | }, 153 | "required": []string{"location"}, 154 | }, 155 | } 156 | 157 | funcs = append(funcs, getWeatherFunc) 158 | 159 | return funcs 160 | } 161 | -------------------------------------------------------------------------------- /examples/image_test.go: -------------------------------------------------------------------------------- 1 | package examples 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | ) 7 | 8 | func ExampleCreateImage() { 9 | cli := getClient() 10 | 11 | uri := "/v1/images/generations" 12 | params := map[string]interface{}{ 13 | "prompt": "a beautiful girl with big eyes", 14 | "n": 1, 15 | "size": "256x256", 16 | "response_format": "url", 17 | } 18 | 19 | res, err := cli.Post(uri, params) 20 | 21 | if err != nil { 22 | log.Fatalf("request api failed: %v", err) 23 | } 24 | 25 | fmt.Println(res.GetString("data.0.url")) 26 | 27 | // Output: xxx 28 | } 29 | -------------------------------------------------------------------------------- /examples/model_test.go: -------------------------------------------------------------------------------- 1 | package examples 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | ) 7 | 8 | func ExampleListModels() { 9 | cli := getClient() 10 | 11 | uri := "/v1/models" 12 | 13 | res, err := cli.Get(uri) 14 | 15 | if err != nil { 16 | log.Fatalf("request api failed: %v", err) 17 | } 18 | 19 | for _, v := range res.Get("data").Array() { 20 | fmt.Printf("model id: %s\n", v.Get("id").String()) 21 | } 22 | 23 | // Output: xxx 24 | } 25 | 26 | func ExampleRetrieveModel() { 27 | cli := getClient() 28 | 29 | model := "text-davinci-003" 30 | 31 | uri := fmt.Sprintf("/v1/models/%s", model) 32 | 33 | res, err := cli.Get(uri) 34 | 35 | if err != nil { 36 | log.Fatalf("request api failed: %v", err) 37 | } 38 | 39 | fmt.Println(res.GetString("id")) 40 | 41 | // Output: xxx 42 | } 43 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/all-in-aigc/openai-go 2 | 3 | go 1.16 4 | 5 | require ( 6 | github.com/idoubi/goutils v1.3.2 7 | github.com/tidwall/gjson v1.14.4 8 | ) 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/basgys/goxml2json v1.1.0 h1:4ln5i4rseYfXNd86lGEB+Vi652IsIXIvggKM/BhUKVw= 2 | github.com/basgys/goxml2json v1.1.0/go.mod h1:wH7a5Np/Q4QoECFIU8zTQlZwZkrilY0itPfecMw41Dw= 3 | github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y= 4 | github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA= 5 | github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= 6 | github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= 7 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 8 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 9 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 10 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 11 | github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= 12 | github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= 13 | github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= 14 | github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= 15 | github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 16 | github.com/idoubi/goutils v1.1.0/go.mod h1:BVikG2hf3aDWXsBzBw3X7l4jSuuxNQQVgrxQ6amIAOk= 17 | github.com/idoubi/goutils v1.3.2 h1:mDDCFbque3xdRFW2snSTdxda/KFpArh4zwNGiBQI7XI= 18 | github.com/idoubi/goutils v1.3.2/go.mod h1:JcvkeynjWT9bCIWIEnduABgbLmTOTo/qeuYPkxZsv/I= 19 | github.com/idoubi/goz v1.4.4 h1:3zQZuweKlJvrjC7YLMWbxq/k8Qx4/dflFadYLGuuSlk= 20 | github.com/idoubi/goz v1.4.4/go.mod h1:Nphqo38f4gMwBbdQjLwkEpdXr8sWBVST9Rns2UcIttI= 21 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 22 | github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= 23 | github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= 24 | github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= 25 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 26 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 27 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 28 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 29 | github.com/launchdarkly/eventsource v1.7.1 h1:StoRQeiPyrcQIXjlQ7b5jWMzHW4p+GGczN2r2oBhujg= 30 | github.com/launchdarkly/eventsource v1.7.1/go.mod h1:LHxSeb4OnqznNZxCSXbFghxS/CjIQfzHovNoAqbO/Wk= 31 | github.com/launchdarkly/go-test-helpers/v2 v2.2.0 h1:L3kGILP/6ewikhzhdNkHy1b5y4zs50LueWenVF0sBbs= 32 | github.com/launchdarkly/go-test-helpers/v2 v2.2.0/go.mod h1:L7+th5govYp5oKU9iN7To5PgznBuIjBPn+ejqKR0avw= 33 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 34 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 35 | github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= 36 | github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= 37 | github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= 38 | github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU= 39 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 40 | github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= 41 | github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 42 | github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= 43 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 44 | github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= 45 | github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= 46 | github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= 47 | github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= 48 | github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= 49 | github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= 50 | github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= 51 | github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= 52 | github.com/yoda-of-soda/map2xml v1.0.2 h1:z3Io7yyf2UFovGcy1b4ct50ci9KbLk8YmyjTpMRgXZ4= 53 | github.com/yoda-of-soda/map2xml v1.0.2/go.mod h1:kEZVcMDvg9pYiosS3G3xWl/C2LspDkrYY294J50dqjc= 54 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 55 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 56 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 57 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 58 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 59 | golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 60 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 61 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 62 | golang.org/x/net v0.0.0-20221004154528-8021a29435af/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= 63 | golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q= 64 | golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= 65 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 66 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 67 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 68 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 69 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 70 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 71 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 72 | golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 73 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 74 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 75 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 76 | golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= 77 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 78 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 79 | golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 80 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 81 | golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= 82 | golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 83 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 84 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 85 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 86 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 87 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 88 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 89 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 90 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 91 | gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= 92 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 93 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 94 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 95 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 96 | -------------------------------------------------------------------------------- /images/get_api_key.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/all-in-aigc/openai-go/44ab8e0b83bcc35c68a715e887c4f47a0ac022a7/images/get_api_key.jpg -------------------------------------------------------------------------------- /option.go: -------------------------------------------------------------------------------- 1 | package openai 2 | 3 | import ( 4 | "time" 5 | ) 6 | 7 | // Options can set custom options for OpenAI client 8 | type Options struct { 9 | // Debug is used to output debug message 10 | Debug bool 11 | // Timeout is used to end http request after timeout duration 12 | Timeout time.Duration 13 | // ApiKey is used to authoration 14 | ApiKey string 15 | // BaseUri is used to set api baseuri 16 | BaseUri string 17 | // Model is used to set default model 18 | Model string 19 | // ApiVersion is used to set api version 20 | ApiVersion string 21 | // isAzure is a flag for azure openai 22 | isAzure bool 23 | } 24 | --------------------------------------------------------------------------------