openai-cli


Nameopenai-cli JSON
Version 1.0.1 PyPI version JSON
download
home_pagehttps://github.com/peterdemin/openai-cli
SummaryCommand-line client for OpenAI API
upload_time2025-02-12 04:17:30
maintainerNone
docs_urlNone
authorPeter Demin
requires_python>=3.12
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            OpenAI command-line client
==========================

Installation
------------

To install OpenAI CLI in Python virtual environment, run::

    pip install openai-cli

Token authentication
--------------------

OpenAI API requires authentication token, which can be obtained on this page:
https://beta.openai.com/account/api-keys

Provide token to the CLI either through a command-line argument (``-t/--token <TOKEN>``)
or through an environment variable (``OPENAI_API_KEY``).

Usage
-----

Currently only text completion API is supported.

Example usage::

    $ echo "Are cats faster than dogs?" | openai complete -
    It depends on the breed of the cat and dog. Generally,
    cats are faster than dogs over short distances,
    but dogs are better at sustained running.

Interactive mode supported (Press Ctrl+C to exit)::

    $ openai repl
    Prompt: Can generative AI replace humans?

    No, generative AI cannot replace humans.
    While generative AI can be used to automate certain tasks,
    it cannot replace the creativity, intuition, and problem-solving
    skills that humans possess.
    Generative AI can be used to supplement human efforts,
    but it cannot replace them.

    Prompt: ^C

Run without arguments to get a short help message::

    $ openai
    Usage: openai [OPTIONS] COMMAND [ARGS]...

    Options:
      --help  Show this message and exit.

    Commands:
      complete  Return OpenAI completion for a prompt from SOURCE.
      repl      Start interactive shell session for OpenAI completion API.

Build a standalone binary using pex and move it into PATH::

    $ make openai && mv openai ~/bin/
    $ openai repl
    Prompt:

Alternative API URL
-------------------

CLI invokes https://api.openai.com/v1/completions by default.
To override the endpoint URL, set ``OPENAI_API_URL`` environment variable.

Example usage
-------------

Here's an example usage scenario, where we first create a Python module
with a Fibonacci function implementation, and then generate a unit test for it:

.. code:: bash

    $ mkdir examples
    $ touch examples/__init__.py
    $ echo "Write Python function to calculate Fibonacci numbers" | openai complete - | black - > examples/fib.py
    $ (echo 'Write unit tests for this Python module named "fib":\n'; cat examples/fib.py) | openai complete - | black - > examples/test_fib.py
    $ pytest -v examples/test_fib.py
    ============================== test session starts ==============================

    examples/test_fib.py::TestFibonacci::test_eighth_fibonacci_number PASSED                                 [ 10%]
    examples/test_fib.py::TestFibonacci::test_fifth_fibonacci_number PASSED                                  [ 20%]
    examples/test_fib.py::TestFibonacci::test_first_fibonacci_number PASSED                                  [ 30%]
    examples/test_fib.py::TestFibonacci::test_fourth_fibonacci_number PASSED                                 [ 40%]
    examples/test_fib.py::TestFibonacci::test_negative_input PASSED                                          [ 50%]
    examples/test_fib.py::TestFibonacci::test_ninth_fibonacci_number PASSED                                  [ 60%]
    examples/test_fib.py::TestFibonacci::test_second_fibonacci_number PASSED                                 [ 70%]
    examples/test_fib.py::TestFibonacci::test_seventh_fibonacci_number PASSED                                [ 80%]
    examples/test_fib.py::TestFibonacci::test_sixth_fibonacci_number PASSED                                  [ 90%]
    examples/test_fib.py::TestFibonacci::test_third_fibonacci_number PASSED                                  [100%]

    =============================== 10 passed in 0.02s ==============================

    $ cat examples/fib.py

.. code:: python

    def Fibonacci(n):
        if n < 0:
            print("Incorrect input")
        # First Fibonacci number is 0
        elif n == 1:
            return 0
        # Second Fibonacci number is 1
        elif n == 2:
            return 1
        else:
            return Fibonacci(n - 1) + Fibonacci(n - 2)

.. code:: bash

    $ cat examples/test_fib.py

.. code:: python

    import unittest
    from .fib import Fibonacci


    class TestFibonacci(unittest.TestCase):
        def test_negative_input(self):
            self.assertEqual(Fibonacci(-1), None)

        def test_first_fibonacci_number(self):
            self.assertEqual(Fibonacci(1), 0)

        def test_second_fibonacci_number(self):
            self.assertEqual(Fibonacci(2), 1)

        def test_third_fibonacci_number(self):
            self.assertEqual(Fibonacci(3), 1)

        def test_fourth_fibonacci_number(self):
            self.assertEqual(Fibonacci(4), 2)

        def test_fifth_fibonacci_number(self):
            self.assertEqual(Fibonacci(5), 3)

        def test_sixth_fibonacci_number(self):
            self.assertEqual(Fibonacci(6), 5)

        def test_seventh_fibonacci_number(self):
            self.assertEqual(Fibonacci(7), 8)

        def test_eighth_fibonacci_number(self):
            self.assertEqual(Fibonacci(8), 13)

        def test_ninth_fibonacci_number(self):
            self.assertEqual(Fibonacci(9), 21)


    if __name__ == "__main__":
        unittest.main()

.. code:: bash

    $ (echo "Add type annotations for this Python code"; cat examples/fib.py) | openai complete - | black - | tee tmp && mv tmp examples/fib.py

.. code:: python

    def Fibonacci(n: int) -> int:
        if n < 0:
            print("Incorrect input")
        # First Fibonacci number is 0
        elif n == 1:
            return 0
        # Second Fibonacci number is 1
        elif n == 2:
            return 1
        else:
            return Fibonacci(n - 1) + Fibonacci(n - 2)

.. code:: bash

    $ mypy examples/fib.py
    examples/fib.py:1: error: Missing return statement  [return]
    Found 1 error in 1 file (checked 1 source file)

.. code:: bash

    $ (echo "Fix mypy warnings in this Python code"; cat examples/fib.py; mypy examples/fib.py) | openai complete - | black - | tee tmp && mv tmp examples/fib.py

.. code:: python

    def Fibonacci(n: int) -> int:
        if n < 0:
            print("Incorrect input")
        # First Fibonacci number is 0
        elif n == 1:
            return 0
        # Second Fibonacci number is 1
        elif n == 2:
            return 1
        else:
            return Fibonacci(n - 1) + Fibonacci(n - 2)
        return None  # Added return statement

.. code:: bash

    $ mypy examples/fib.py
    examples/fib.py:12: error: Incompatible return value type (got "None", expected "int")  [return-value]
    Found 1 error in 1 file (checked 1 source file)

.. code:: bash

    $ (echo "Fix mypy warnings in this Python code"; cat examples/fib.py; mypy examples/fib.py) | openai complete - | black - | tee tmp && mv tmp examples/fib.py

.. code:: python

    def Fibonacci(n: int) -> int:
        if n < 0:
            print("Incorrect input")
        # First Fibonacci number is 0
        elif n == 1:
            return 0
        # Second Fibonacci number is 1
        elif n == 2:
            return 1
        else:
            return Fibonacci(n - 1) + Fibonacci(n - 2)
        return 0  # Changed return statement to return 0

.. code:: bash

    $ mypy examples/fib.py
    Success: no issues found in 1 source file

.. code:: bash

    $ (echo "Rewrite these tests to use pytest.parametrized"; cat examples/test_fib.py) | openai complete - | black - | tee tmp && mv tmp examples/test_fib.py

.. code:: python

    import pytest
    from .fib import Fibonacci


    @pytest.mark.parametrize(
        "n, expected",
        [(1, 0), (2, 1), (3, 1), (4, 2), (5, 3), (6, 5), (7, 8), (8, 13), (9, 21), (10, 34)],
    )
    def test_fibonacci(n, expected):
        assert Fibonacci(n) == expected

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/peterdemin/openai-cli",
    "name": "openai-cli",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": null,
    "keywords": null,
    "author": "Peter Demin",
    "author_email": "peterdemin@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/42/2f/d3562cea333d46dc147dee2b93eb41e04f60885d97340f6a63a7677b5e7d/openai_cli-1.0.1.tar.gz",
    "platform": null,
    "description": "OpenAI command-line client\n==========================\n\nInstallation\n------------\n\nTo install OpenAI CLI in Python virtual environment, run::\n\n    pip install openai-cli\n\nToken authentication\n--------------------\n\nOpenAI API requires authentication token, which can be obtained on this page:\nhttps://beta.openai.com/account/api-keys\n\nProvide token to the CLI either through a command-line argument (``-t/--token <TOKEN>``)\nor through an environment variable (``OPENAI_API_KEY``).\n\nUsage\n-----\n\nCurrently only text completion API is supported.\n\nExample usage::\n\n    $ echo \"Are cats faster than dogs?\" | openai complete -\n    It depends on the breed of the cat and dog. Generally,\n    cats are faster than dogs over short distances,\n    but dogs are better at sustained running.\n\nInteractive mode supported (Press Ctrl+C to exit)::\n\n    $ openai repl\n    Prompt: Can generative AI replace humans?\n\n    No, generative AI cannot replace humans.\n    While generative AI can be used to automate certain tasks,\n    it cannot replace the creativity, intuition, and problem-solving\n    skills that humans possess.\n    Generative AI can be used to supplement human efforts,\n    but it cannot replace them.\n\n    Prompt: ^C\n\nRun without arguments to get a short help message::\n\n    $ openai\n    Usage: openai [OPTIONS] COMMAND [ARGS]...\n\n    Options:\n      --help  Show this message and exit.\n\n    Commands:\n      complete  Return OpenAI completion for a prompt from SOURCE.\n      repl      Start interactive shell session for OpenAI completion API.\n\nBuild a standalone binary using pex and move it into PATH::\n\n    $ make openai && mv openai ~/bin/\n    $ openai repl\n    Prompt:\n\nAlternative API URL\n-------------------\n\nCLI invokes https://api.openai.com/v1/completions by default.\nTo override the endpoint URL, set ``OPENAI_API_URL`` environment variable.\n\nExample usage\n-------------\n\nHere's an example usage scenario, where we first create a Python module\nwith a Fibonacci function implementation, and then generate a unit test for it:\n\n.. code:: bash\n\n    $ mkdir examples\n    $ touch examples/__init__.py\n    $ echo \"Write Python function to calculate Fibonacci numbers\" | openai complete - | black - > examples/fib.py\n    $ (echo 'Write unit tests for this Python module named \"fib\":\\n'; cat examples/fib.py) | openai complete - | black - > examples/test_fib.py\n    $ pytest -v examples/test_fib.py\n    ============================== test session starts ==============================\n\n    examples/test_fib.py::TestFibonacci::test_eighth_fibonacci_number PASSED                                 [ 10%]\n    examples/test_fib.py::TestFibonacci::test_fifth_fibonacci_number PASSED                                  [ 20%]\n    examples/test_fib.py::TestFibonacci::test_first_fibonacci_number PASSED                                  [ 30%]\n    examples/test_fib.py::TestFibonacci::test_fourth_fibonacci_number PASSED                                 [ 40%]\n    examples/test_fib.py::TestFibonacci::test_negative_input PASSED                                          [ 50%]\n    examples/test_fib.py::TestFibonacci::test_ninth_fibonacci_number PASSED                                  [ 60%]\n    examples/test_fib.py::TestFibonacci::test_second_fibonacci_number PASSED                                 [ 70%]\n    examples/test_fib.py::TestFibonacci::test_seventh_fibonacci_number PASSED                                [ 80%]\n    examples/test_fib.py::TestFibonacci::test_sixth_fibonacci_number PASSED                                  [ 90%]\n    examples/test_fib.py::TestFibonacci::test_third_fibonacci_number PASSED                                  [100%]\n\n    =============================== 10 passed in 0.02s ==============================\n\n    $ cat examples/fib.py\n\n.. code:: python\n\n    def Fibonacci(n):\n        if n < 0:\n            print(\"Incorrect input\")\n        # First Fibonacci number is 0\n        elif n == 1:\n            return 0\n        # Second Fibonacci number is 1\n        elif n == 2:\n            return 1\n        else:\n            return Fibonacci(n - 1) + Fibonacci(n - 2)\n\n.. code:: bash\n\n    $ cat examples/test_fib.py\n\n.. code:: python\n\n    import unittest\n    from .fib import Fibonacci\n\n\n    class TestFibonacci(unittest.TestCase):\n        def test_negative_input(self):\n            self.assertEqual(Fibonacci(-1), None)\n\n        def test_first_fibonacci_number(self):\n            self.assertEqual(Fibonacci(1), 0)\n\n        def test_second_fibonacci_number(self):\n            self.assertEqual(Fibonacci(2), 1)\n\n        def test_third_fibonacci_number(self):\n            self.assertEqual(Fibonacci(3), 1)\n\n        def test_fourth_fibonacci_number(self):\n            self.assertEqual(Fibonacci(4), 2)\n\n        def test_fifth_fibonacci_number(self):\n            self.assertEqual(Fibonacci(5), 3)\n\n        def test_sixth_fibonacci_number(self):\n            self.assertEqual(Fibonacci(6), 5)\n\n        def test_seventh_fibonacci_number(self):\n            self.assertEqual(Fibonacci(7), 8)\n\n        def test_eighth_fibonacci_number(self):\n            self.assertEqual(Fibonacci(8), 13)\n\n        def test_ninth_fibonacci_number(self):\n            self.assertEqual(Fibonacci(9), 21)\n\n\n    if __name__ == \"__main__\":\n        unittest.main()\n\n.. code:: bash\n\n    $ (echo \"Add type annotations for this Python code\"; cat examples/fib.py) | openai complete - | black - | tee tmp && mv tmp examples/fib.py\n\n.. code:: python\n\n    def Fibonacci(n: int) -> int:\n        if n < 0:\n            print(\"Incorrect input\")\n        # First Fibonacci number is 0\n        elif n == 1:\n            return 0\n        # Second Fibonacci number is 1\n        elif n == 2:\n            return 1\n        else:\n            return Fibonacci(n - 1) + Fibonacci(n - 2)\n\n.. code:: bash\n\n    $ mypy examples/fib.py\n    examples/fib.py:1: error: Missing return statement  [return]\n    Found 1 error in 1 file (checked 1 source file)\n\n.. code:: bash\n\n    $ (echo \"Fix mypy warnings in this Python code\"; cat examples/fib.py; mypy examples/fib.py) | openai complete - | black - | tee tmp && mv tmp examples/fib.py\n\n.. code:: python\n\n    def Fibonacci(n: int) -> int:\n        if n < 0:\n            print(\"Incorrect input\")\n        # First Fibonacci number is 0\n        elif n == 1:\n            return 0\n        # Second Fibonacci number is 1\n        elif n == 2:\n            return 1\n        else:\n            return Fibonacci(n - 1) + Fibonacci(n - 2)\n        return None  # Added return statement\n\n.. code:: bash\n\n    $ mypy examples/fib.py\n    examples/fib.py:12: error: Incompatible return value type (got \"None\", expected \"int\")  [return-value]\n    Found 1 error in 1 file (checked 1 source file)\n\n.. code:: bash\n\n    $ (echo \"Fix mypy warnings in this Python code\"; cat examples/fib.py; mypy examples/fib.py) | openai complete - | black - | tee tmp && mv tmp examples/fib.py\n\n.. code:: python\n\n    def Fibonacci(n: int) -> int:\n        if n < 0:\n            print(\"Incorrect input\")\n        # First Fibonacci number is 0\n        elif n == 1:\n            return 0\n        # Second Fibonacci number is 1\n        elif n == 2:\n            return 1\n        else:\n            return Fibonacci(n - 1) + Fibonacci(n - 2)\n        return 0  # Changed return statement to return 0\n\n.. code:: bash\n\n    $ mypy examples/fib.py\n    Success: no issues found in 1 source file\n\n.. code:: bash\n\n    $ (echo \"Rewrite these tests to use pytest.parametrized\"; cat examples/test_fib.py) | openai complete - | black - | tee tmp && mv tmp examples/test_fib.py\n\n.. code:: python\n\n    import pytest\n    from .fib import Fibonacci\n\n\n    @pytest.mark.parametrize(\n        \"n, expected\",\n        [(1, 0), (2, 1), (3, 1), (4, 2), (5, 3), (6, 5), (7, 8), (8, 13), (9, 21), (10, 34)],\n    )\n    def test_fibonacci(n, expected):\n        assert Fibonacci(n) == expected\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Command-line client for OpenAI API",
    "version": "1.0.1",
    "project_urls": {
        "Homepage": "https://github.com/peterdemin/openai-cli"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0e1dfcf004db72d7f5554b1a847e46d56bd80336bbd534d92e6b9571c5684f02",
                "md5": "46acab1f925f8205656e827568787e33",
                "sha256": "9708c1e61c8b042e5607e6af1e6dc97b026cff6bb925ca8bd8547d6872f0ee62"
            },
            "downloads": -1,
            "filename": "openai_cli-1.0.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "46acab1f925f8205656e827568787e33",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.12",
            "size": 9853,
            "upload_time": "2025-02-12T04:17:17",
            "upload_time_iso_8601": "2025-02-12T04:17:17.785513Z",
            "url": "https://files.pythonhosted.org/packages/0e/1d/fcf004db72d7f5554b1a847e46d56bd80336bbd534d92e6b9571c5684f02/openai_cli-1.0.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "422fd3562cea333d46dc147dee2b93eb41e04f60885d97340f6a63a7677b5e7d",
                "md5": "ea9f2ad67c4ae0c68370521ee3b4b881",
                "sha256": "fc68a39952ceb04a43ecc27b9d43f848f5f537da57ac30bd941d084f805c10a4"
            },
            "downloads": -1,
            "filename": "openai_cli-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "ea9f2ad67c4ae0c68370521ee3b4b881",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 16854,
            "upload_time": "2025-02-12T04:17:30",
            "upload_time_iso_8601": "2025-02-12T04:17:30.380585Z",
            "url": "https://files.pythonhosted.org/packages/42/2f/d3562cea333d46dc147dee2b93eb41e04f60885d97340f6a63a7677b5e7d/openai_cli-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-12 04:17:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "peterdemin",
    "github_project": "openai-cli",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "openai-cli"
}
        
Elapsed time: 1.62745s