{"id":37065554,"url":"https://github.com/phelps-sg/openai-pygenerator","last_synced_at":"2026-01-14T07:40:31.362Z","repository":{"id":166371974,"uuid":"641855894","full_name":"phelps-sg/openai-pygenerator","owner":"phelps-sg","description":"Type-annoted wrapper for OpenAI Python API which provides a generator over completions with retry functionality when encountering RateLimitError","archived":false,"fork":false,"pushed_at":"2023-11-18T10:21:56.000Z","size":134,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-28T09:32:17.295Z","etag":null,"topics":["generator","gpt","gpt-3","gpt-4","gpt4-api","llm","openai-api","python","ratelimiterror","retry"],"latest_commit_sha":null,"homepage":"","language":"Python","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/phelps-sg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":["phelps-sg"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2023-05-17T09:58:23.000Z","updated_at":"2023-11-22T11:17:31.000Z","dependencies_parsed_at":"2023-11-16T07:29:11.163Z","dependency_job_id":null,"html_url":"https://github.com/phelps-sg/openai-pygenerator","commit_stats":null,"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"purl":"pkg:github/phelps-sg/openai-pygenerator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phelps-sg%2Fopenai-pygenerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phelps-sg%2Fopenai-pygenerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phelps-sg%2Fopenai-pygenerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phelps-sg%2Fopenai-pygenerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phelps-sg","download_url":"https://codeload.github.com/phelps-sg/openai-pygenerator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phelps-sg%2Fopenai-pygenerator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28413470,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T05:26:33.345Z","status":"ssl_error","status_checked_at":"2026-01-14T05:21:57.251Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["generator","gpt","gpt-3","gpt-4","gpt4-api","llm","openai-api","python","ratelimiterror","retry"],"created_at":"2026-01-14T07:40:30.687Z","updated_at":"2026-01-14T07:40:31.356Z","avatar_url":"https://github.com/phelps-sg.png","language":"Python","funding_links":["https://github.com/sponsors/phelps-sg"],"categories":[],"sub_categories":[],"readme":"# openai-pygenerator\n\n[![GitHub Workflow Status](https://github.com/phelps-sg/openai-pygenerator/actions/workflows/python-package.yml/badge.svg)](https://github.com/phelps-sg/openai-pygenerator/actions/workflows/python-package.yml)\n![GitHub release (latest by date)](https://img.shields.io/github/v/release/phelps-sg/openai-pygenerator)\n![GitHub](https://img.shields.io/github/license/phelps-sg/openai-pygenerator?color=blue)\n\n## Import Note\n\nThe openai Python API versions 1.0.0 and later now incorporate retry functionality and type annotations. This package has been migrated to the new API, but should only be used as a temporary measure to ensure backward compatibility.  If you are using this package in production, consider rewriting your code so that it uses the new openai API directly.\n\n## Overview\n\nThis is a simple type-annotated wrapper around the OpenAI Python API which:\n- provides configurable retry functionality,\n- reduces the default timeout from 10 minutes to 20 seconds (configurable), \n- provides a simple class to manage chat session state, and\n- provides a generator over completions.\n  \nIt can also be used to chain together completions from different prompts in a very straightforward [functional-programming](https://docs.python.org/3/howto/functional.html) style using [Python generators](https://docs.python.org/3/howto/functional.html#generators).\n\n## Installation\n\n~~~bash\npip install openai-pygenerator\n~~~\n\n## Basic usage\n\nIn the example below we will retry automatically if there is a `RateLimitError`.\n\n~~~python\nfrom openai_pygenerator import ChatSession\n \nsession = ChatSession()\nsolution = session.ask(\"What is the square root of 256?\")\nprint(solution)\nworking = session.ask(\"Show your working\")\nprint(working)\nprint(\"Transcript:\")\nprint(session.transcript)\n~~~\n\n## Completion pipelines and overriding parameters\n\n~~~python\nfrom typing import Iterable\n\nfrom openai_pygenerator import (\n    ChatSession,\n    Completions,\n    completer,\n    content,\n    next_completion,\n    user_message,\n)\n\nhigh_temp_completions = completer(temperature=0.8)\n\n\ndef heading(message: str, margin: int = 80) -\u003e None:\n    print()\n    print(\"-\" * margin)\n    print(message)\n    print(\"-\" * margin)\n    print()\n\n\ndef example_square_root(session: ChatSession) -\u003e None:\n    solution = session.ask(\"What is the square root of 256?\")\n    print(solution)\n    working = session.ask(\"Show your working\")\n    print(working)\n\n    heading(\"Session transcript:\")\n    print(session.transcript)\n\n\ndef creative_answer(prompt: str, num_completions: int = 1) -\u003e Completions:\n    return high_temp_completions([user_message(prompt)], n=num_completions)\n\n\ndef pick_color(num_completions: int) -\u003e Completions:\n    return creative_answer(\n        \"Pick a color at random and then just tell me your choice, e.g. 'red'\",\n        num_completions,\n    )\n\n\ndef generate_sentence(color_completions: Completions) -\u003e Iterable[str]:\n    for color_completion in color_completions:\n        color = content(color_completion)\n        result = next_completion(\n            creative_answer(f\"Write a sentence about the color {color}.\")\n        )\n        if result is not None:\n            yield content(result)\n\n\nif __name__ == \"__main__\":\n    heading(\"Find square root - using environment variables for parameters\")\n    example_square_root(session=ChatSession())\n\n    heading(\"Find square root - overriding temperature, max_tokens, max_retries\")\n    example_square_root(\n        session=ChatSession(\n            generate=completer(temperature=0.5, max_tokens=300, max_retries=5)\n        )\n    )\n\n    heading(\"Example completion pipeline\")\n    for sentence in generate_sentence(pick_color(num_completions=10)):\n        print(sentence)\n~~~\n\n## Running \n\n~~~bash\nexport OPENAI_API_KEY=\u003ckey\u003e\npython src/openai_pygenerator/example.py\n~~~\n\n## Configuration\n\nTo override default parameters use the following shell environment variables:\n\n~~~bash\nexport GPT_MODEL=gpt-3.5-turbo\nexport GPT_TEMPERATURE=0.2\nexport GPT_MAX_TOKENS=500\nexport GPT_MAX_RETRIES=5\nexport GPT_REQUEST_TIMEOUT_SECONDS=20\nexport OPENAI_API_KEY=\u003ckey\u003e\npython src/openai_pygenerator/example.py\n~~~\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphelps-sg%2Fopenai-pygenerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphelps-sg%2Fopenai-pygenerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphelps-sg%2Fopenai-pygenerator/lists"}