{"id":17188269,"url":"https://github.com/jclem/openai-go","last_synced_at":"2025-03-25T04:39:57.326Z","repository":{"id":194145579,"uuid":"690222906","full_name":"jclem/openai-go","owner":"jclem","description":"An OpenAI client for Go","archived":false,"fork":false,"pushed_at":"2023-09-14T19:51:02.000Z","size":44,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-24T10:52:53.152Z","etag":null,"topics":[],"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/jclem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2023-09-11T19:18:58.000Z","updated_at":"2023-09-12T17:09:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"bcb61189-de89-4dfe-b1da-99592ab9b4e2","html_url":"https://github.com/jclem/openai-go","commit_stats":null,"previous_names":["jclem/openai-go"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fopenai-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fopenai-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fopenai-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fopenai-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jclem","download_url":"https://codeload.github.com/jclem/openai-go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245401374,"owners_count":20609163,"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":[],"created_at":"2024-10-15T01:08:33.369Z","updated_at":"2025-03-25T04:39:57.301Z","avatar_url":"https://github.com/jclem.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# OpenAI Go Client\n\n**This is an OpenAI client for Go I wrote for personal use. Do not expect it to\nnecessarily be up to date quickly.**\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/jclem/openai-go.svg)](https://pkg.go.dev/github.com/jclem/openai-go)\n\n## Usage\n\n### Creating a client\n\n```go\nimport \"github.com/jclem/openai-go\"\n\nclient := openai.NewClient(openai.WithKey(yourAPIKey))\n\n// Optionally, provide a custom HTTP \"Do\"-er.\nclient = openai.NewClient(\n\topenai.WithKey(yourApiKey),\n\topenai.WithDoer(http.DefaultClient),\n)\n```\n\n### Making a completion request\n\nUse the client's chat service to create a completion call.\n\n```go\nimport \"github.com/jclem/openai-go/pkg/chat\"\n\ncomp, err := client.Chat.CreateCompletion(\n\tcontext.Background(),\n\t\"gpt-4\",\n\t[]chat.Message{chat.NewMessage(\"user\", chat.WithMessageContent(\"Hello, world\"))},\n\tchat.WithTemperature(0.6),\n)\n\n// Various methods exist to easily read the completion.\ncontent, ok := comp.GetContentAt(0)\n```\n\n### Making a streaming completion request\n\nUse the client's chat service to create a streaming completion call.\n\n```go\nimport \"github.com/jclem/openai-go/pkg/chat\"\n\nstream, err := client.Chat.CreateStreamingCompletion(\n\tcontext.Background(),\n\t\"gpt-4\",\n\t[]chat.Message{chat.NewMessage(\"user\", chat.WithMessageContent(\"Hello, world\"))},\n\tchat.WithTemperature(0.6),\n)\n\n// Call `stream.Next()` to get the next stream completion object. It'll return\n// `nil, nil` when done.\nfor {\n\tchunk, err := stream.Next()\n\n\tif errors.Is(err, chat.ErrStreamDone) {\n\t\tbreak\n\t}\n\n\tif err != nil {\n\t\t// Handle error.\n\t}\n\n\t// Various methods exist to easily read the stream chunk.\n\tcontent, ok := chunk.GetContentAt(0)\n\tif ok {\n\t\tfmt.Printf(content)\n\t}\n}\n\n// The caller must close the stream.\nif closeErr := stream.Close(); closeErr != nil {\n\t// Handle error.\n}\n```\n\n### Creating embeddings\n\nUse `CreateEmbeddings` to create embeddings, and get back a parsed response.\n\n```go\nresp, err := client.Embeddings.Create(\n\tcontext.Background(),\n\t\"text-embedding-ada-002\",\n\t[]string{\"HEllo, world.\"},\n)\n\nvar embedding []float64 = resp.Data[0].Embedding\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjclem%2Fopenai-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjclem%2Fopenai-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjclem%2Fopenai-go/lists"}