{"id":21561882,"url":"https://github.com/0x9ef/openai-go","last_synced_at":"2025-04-10T12:06:31.426Z","repository":{"id":65146342,"uuid":"583427080","full_name":"0x9ef/openai-go","owner":"0x9ef","description":"OpenAI GPT-3/3.5/4 API client written in Go","archived":false,"fork":false,"pushed_at":"2023-04-13T05:56:31.000Z","size":119,"stargazers_count":20,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T10:56:26.611Z","etag":null,"topics":["api","babbage","client","codex","curie","davinci","go","golang","gpt-3","gpt-4","openai","whisper"],"latest_commit_sha":null,"homepage":"","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/0x9ef.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-29T18:42:49.000Z","updated_at":"2024-12-15T04:48:48.000Z","dependencies_parsed_at":"2024-06-20T00:10:47.272Z","dependency_job_id":"9b486bf3-f6ef-4de9-9ae6-9d46c68dd879","html_url":"https://github.com/0x9ef/openai-go","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0x9ef%2Fopenai-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0x9ef%2Fopenai-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0x9ef%2Fopenai-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0x9ef%2Fopenai-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/0x9ef","download_url":"https://codeload.github.com/0x9ef/openai-go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248217009,"owners_count":21066633,"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":["api","babbage","client","codex","curie","davinci","go","golang","gpt-3","gpt-4","openai","whisper"],"created_at":"2024-11-24T09:29:06.917Z","updated_at":"2025-04-10T12:06:31.409Z","avatar_url":"https://github.com/0x9ef.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Golang OpenAI API Client\n\nAn Golang native implementation to easily interacting with OpenAI API.\n\nhttps://beta.openai.com/docs/api-reference/\n\n## Usage\n\nYou can use environment variable to store API secret key\n```sh\nexport OPENAI_KEY=[YOUR_KEY]\n```\n\nTo initialize engine, use this:\n```go\ne := openai.New(os.Getenv(\"OPENAI_KEY\"))\n```\n\n### Tips \n\n#### Model\nIf you want to use the most powerful model to generate text outputs, ensure that you are using \"text-davinci-003\". This model is defined as constant `openai.ModelTextDavinci003`.\n\n#### Text edition\nYou can use the bundle Completion+Edit to regenerate the response based on the last context.\n```go\ne := openai.New(os.Getenv(\"OPENAI_KEY\"))\nctx := context.Background()\ncompletionResp, err := e.Completion(ctx, \u0026openai.CompletionOptions{\n\t// Choose model, you can see list of available models in models.go file\n\tModel: openai.ModelTextDavinci001,\n\t// Number of completion tokens to generate response. By default - 1024\n\tMaxTokens: 1200,\n\t// Text to completion\n\tPrompt: []string{\"Write a little bit of Wikipedia. What is that?\"},\n})\n\neditResp, err := e.Edit(ctx, \u0026EditOptions{\n\tModel:       ModelTextDavinci001,\n\tInput:       completionResp.Choices[0],\n\tInstruction: \"Please rewrite a bit more and add more information about Wikipedia in different aspects. Please build based on that for 4 topics\",\n})\n```\n\n### Text completion example \nGiven a prompt, the model will return one or more predicted completions. \n\n**Note**: the default number of completion tokens is 1024, if you want to increase or decrease this limit, you should change `MaxTokens` parameter. \n \n```go\ne := openai.New(os.Getenv(\"OPENAI_KEY\"))\nr, err := e.Completion(context.Background(), \u0026openai.CompletionOptions{\n\t// Choose model, you can see list of available models in models.go file\n\tModel: openai.ModelTextDavinci001,\n\t// Number of completion tokens to generate response. By default - 1024\n\tMaxTokens: 1200,\n\t// Text to completion\n\tPrompt: []string{\"Write a little bit of Wikipedia. What is that?\"},\n})\n```\n\nYou will get the next output:\n```json\n{\n  \"id\": \"cmpl-6SrcYDLCVT7xyHKVNuSLNuhRvwOJ1\",\n  \"object\": \"text_completion\",\n  \"created\": 1672337322,\n  \"model\": \"text-davinci-001\",\n  \"choices\": [\n    {\n      \"text\": \"\\n\\nWikipedia is a free online encyclopedia, created and edited by volunteers.\",\n      \"index\": 0,\n      \"finish_reason\": \"stop\"\n    }\n  ],\n  \"usage\": {\n    \"prompt_tokens\": 11,\n    \"completion_tokens\": 15,\n    \"total_tokens\": 26\n  }\n}\n```\n\nTo get only the text you should use the next code:\n```go\nfmt.Println(r.Choices[0].Text)\n```\n\nSo, the full code will be:\n```go\npackage main \n\n\nimport (\n\t\"context\"\n\t\"encoding/json\"\n\t\"log\"\n\t\"os\"\n\t\"testing\"\n  \"github.com/0x9ef/openai-go\"\n)\n\nfunc main() {\n\te := openai.New(os.Getenv(\"OPENAI_KEY\"))\n\tr, err := e.Completion(context.Background(), \u0026openai.CompletionOptions{\n\t\t// Choose model, you can see list of available models in models.go file\n\t\tModel: openai.ModelTextDavinci001,\n\t\t// Text to completion\n\t\tPrompt: []string{\"Write a little bit of Wikipedia. What is that?\"},\n\t})\n\n\tif b, err := json.MarshalIndent(r, \"\", \"  \"); err != nil {\n\t\tpanic(err)\n\t} else {\n\t\tfmt.Println(string(b))\n\t}\n\n\t// Wikipedia is a free online encyclopedia, created and edited by volunteers.\n\tfmt.Println(\"What is the Wikipedia?\", r.Choices[0].Text)\n}\n```\n\n### Models list/retrieve \nLists the currently available models, and provides basic information about each one such as the owner and availability.\n\n```go\ne := openai.New(os.Getenv(\"OPENAI_KEY\"))\nr, err := e.ListModels(context.Background())\nif err != nil {\n\tlog.Fatal(err)\n}\n```\n\nYou will get the next output:\n```json\n{\n  \"data\": [\n    {\n      \"id\": \"babbage\",\n      \"object\": \"model\",\n      \"owned_by\": \"openai\"\n    },\n    {\n      \"id\": \"ada\",\n      \"object\": \"model\",\n      \"owned_by\": \"openai\"\n    },\n    {\n      \"id\": \"text-davinci-002\",\n      \"object\": \"model\",\n      \"owned_by\": \"openai\"\n    },\n    {\n      \"id\": \"davinci\",\n      \"object\": \"model\",\n      \"owned_by\": \"openai\"\n    },\n    ...\n  ]\n}\n``` \n\nTo retrieve information about specified model instead of all models, you can do this:\n\n```go\ne := openai.New(os.Getenv(\"OPENAI_KEY\"))\nr, err := e.RetrieveModel(context.Background(), \u0026openai.RetrieveModelOptions{\n\tID: openai.ModelDavinci,\n})\nif err != nil {\n\tlog.Fatal(err)\n}\n```\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0x9ef%2Fopenai-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F0x9ef%2Fopenai-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0x9ef%2Fopenai-go/lists"}