{"id":17641332,"url":"https://github.com/all-in-aigc/openai-go","last_synced_at":"2025-04-30T22:24:25.928Z","repository":{"id":65408403,"uuid":"589429286","full_name":"all-in-aigc/openai-go","owner":"all-in-aigc","description":"OpenAI Go SDK.","archived":false,"fork":false,"pushed_at":"2023-12-30T03:35:12.000Z","size":373,"stargazers_count":63,"open_issues_count":0,"forks_count":23,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-30T20:33:42.869Z","etag":null,"topics":["chatgpt","chatgpt-sdk","openai-go","openai-sdk"],"latest_commit_sha":null,"homepage":"https://gpts.works","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/all-in-aigc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-01-16T04:49:09.000Z","updated_at":"2025-03-14T04:50:50.000Z","dependencies_parsed_at":"2023-12-30T04:26:32.206Z","dependency_job_id":"25ea54c7-07d9-43a3-8c4f-bc1b821d011a","html_url":"https://github.com/all-in-aigc/openai-go","commit_stats":null,"previous_names":["all-in-aigc/openai-go","chatgp/gpt3","all-in-aigc/gpt"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/all-in-aigc%2Fopenai-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/all-in-aigc%2Fopenai-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/all-in-aigc%2Fopenai-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/all-in-aigc%2Fopenai-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/all-in-aigc","download_url":"https://codeload.github.com/all-in-aigc/openai-go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251790513,"owners_count":21644228,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatgpt","chatgpt-sdk","openai-go","openai-sdk"],"created_at":"2024-10-23T07:05:16.582Z","updated_at":"2025-04-30T22:24:25.903Z","avatar_url":"https://github.com/all-in-aigc.png","language":"Go","funding_links":["https://www.buymeacoffee.com/idoubi"],"categories":[],"sub_categories":[],"readme":"# openai-go\n\nopenai go sdk\n\n## Preparation\n\nsign in [openai platform](https://platform.openai.com/api-keys), and get your own API_KEY.\n\n![](./images/get_api_key.jpg)\n\n## Quick Start\n\n1. install openai-go sdk\n\n```shell\ngo get -u github.com/all-in-aigc/openai-go\n```\n\n2. request api with openai-go client\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\topenai \"github.com/all-in-aigc/openai-go\"\n)\n\nfunc main() {\n\tapiKey := \"xxx\" // your openai apikey, or azure openai apikey\n\n\t// new openai client\n\tcli, _ := openai.NewClient(\u0026openai.Options{\n\t\tApiKey:  apiKey,\n\t\tTimeout: 30 * time.Second,\n\t\tDebug:   true,\n\t\tBaseUri: \"https://xxx.com/openai\", // use a proxy api, default baseuri is https://api.openai.com\n\n\t\t// use azure openai\n\t\t// BaseUri: \"https://xxx.openai.azure.com/openai/deployments/gpt-35-turbo-16k\", // your azure openai endpoint\n\t\t// ApiVersion: \"2023-07-01-preview\", // azure openai api version\n\t})\n\n\t// request api\n\turi := \"/v1/models\"\n\n\tres, err := cli.Get(uri)\n\n\tif err != nil {\n\t\tlog.Fatalf(\"request api failed: %v\", err)\n\t}\n\n\tfor _, v := range res.Get(\"data\").Array() {\n\t\tfmt.Printf(\"model id: %s\\n\", v.Get(\"id\").String())\n\t}\n}\n```\n\n\u003e see available apis in [OpenAI documents](https://platform.openai.com/docs/api-reference/completions/create)\n\n## Examples\n\nthere are some examples under the `examples` folder, check and see how to request other apis.\n\n- [Create chat completion](https://platform.openai.com/docs/api-reference/chat/create)\n\n```go\ncli := getClient()\n\nuri := \"/v1/chat/completions\"\nparams := map[string]interface{}{\n\t\"model\":       \"gpt-3.5-turbo\",\n\t\"messages\":      []map[string]interface{}{\n\t\t{\"role\": \"user\", \"content\": \"hello\"},\n\t},\n}\n\nres, err := cli.Post(uri, params)\nif err != nil {\n\tlog.Fatalf(\"request api failed: %v\", err)\n}\n\nmessage := res.Get(\"choices.0.message.content\").String()\n\nfmt.Printf(\"message is: %s\", message)\n// Output: xxx\n```\n\n- [Create Chat Completion With Function Call](https://platform.openai.com/docs/api-reference/chat/create)\n\n```go\nuserQuestion := \"What is the weather like in Boston?\"\n\nuri := \"/v1/chat/completions\"\nparams := map[string]interface{}{\n\t\"model\": \"gpt-3.5-turbo\",\n\t\"messages\": []map[string]interface{}{\n\t\t{\n\t\t\t\"role\":    \"user\",\n\t\t\t\"content\": userQuestion,\n\t\t},\n\t},\n\t\"functions\": getFuncs(),\n}\n\nres, err := cli.Post(uri, params)\nif err != nil {\n\tlog.Fatalf(\"request api failed: %v\", err)\n}\n\nfuncName := res.Get(\"choices.0.message.function_call.name\").String()\nfuncArgs := res.Get(\"choices.0.message.function_call.arguments\").String()\n\nif funcName == \"\" || funcArgs == \"\" {\n\tlog.Fatalf(\"function call get args failed: %s\", res)\n}\n\nfmt.Printf(\"function call name: %s, args: %v\", funcName, funcArgs)\n```\n\n- [Create Completion](https://beta.openai.com/docs/api-reference/completions/create)\n\n```go\nuri := \"/v1/completions\"\nparams := map[string]interface{}{\n\t\"model\":       \"text-davinci-003\",\n\t\"prompt\":      \"say hello three times\",\n\t\"max_tokens\":  2048,\n\t\"temperature\": 0.9,\n\t\"n\":           1,\n\t\"stream\":      false,\n}\n\nres, err := cli.Post(uri, params)\n\nif err != nil {\n\tlog.Fatalf(\"request api failed: %v\", err)\n}\n\nfmt.Println(res.GetString(\"choices.0.text\"))\n```\n\n- [Create Edit](https://beta.openai.com/docs/api-reference/edits/create)\n\n```go\nuri := \"/v1/edits\"\nparams := map[string]interface{}{\n\t\"model\":       \"text-davinci-edit-001\",\n\t\"input\":       \"Are you hapy today?\",\n\t\"instruction\": \"fix mistake\",\n\t\"temperature\": 0.9,\n\t\"n\":           1,\n}\n\nres, err := cli.Post(uri, params)\n\nif err != nil {\n\tlog.Fatalf(\"request api failed: %v\", err)\n}\n\nfmt.Println(res.GetString(\"choices.0.text\"))\n```\n\n- [Create Image](https://beta.openai.com/docs/api-reference/images/create)\n\n```go\nuri := \"/v1/images/generations\"\nparams := map[string]interface{}{\n\t\"prompt\":          \"a beautiful girl with big eyes\",\n\t\"n\":               1,\n\t\"size\":            \"256x256\",\n\t\"response_format\": \"url\",\n}\n\nres, err := cli.Post(uri, params)\n\nif err != nil {\n\tlog.Fatalf(\"request api failed: %v\", err)\n}\n\nfmt.Println(res.GetString(\"data.0.url\"))\n```\n\n## More\n\n\u003e if this project is helpful to you, buy me a coffee😄\n\n\u003ca href=\"https://www.buymeacoffee.com/idoubi\" target=\"_blank\"\u003e\u003cimg src=\"https://cdn.buymeacoffee.com/buttons/default-orange.png\" alt=\"Buy Me A Coffee\" height=\"41\" width=\"174\"\u003e\u003c/a\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fall-in-aigc%2Fopenai-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fall-in-aigc%2Fopenai-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fall-in-aigc%2Fopenai-go/lists"}