Package: openai-cli
InstallationTo install OpenAI CLI in Python virtual environment, run:pip install openai-cliToken authenticationOpenAI API requires authentication token, which can be obtained on this page:https://beta.openai.com/account/api-keysProvide token to the CLI either through a command-line argument (-t/--token<TOKEN>)
or through an environment variable (OPENAI_API_TOKEN).UsageCurrently 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: ^CRun 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 URLCLI invokeshttps://api.openai.com/v1/completionsby default.
To override the endpoint URL, setOPENAI_API_URLenvironment variable.Example usageHere’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:$mkdirexamples$touchexamples/__init__.py$echo"Write Python function to calculate Fibonacci numbers"|openaicomplete-|black->examples/fib.py$(echo'Write unit tests for this Python module named "fib":\n';catexamples/fib.py)|openaicomplete-|black->examples/test_fib.py$pytest-vexamples/test_fib.py==============================testsessionstarts==============================examples/test_fib.py::TestFibonacci::test_eighth_fibonacci_numberPASSED[10%]examples/test_fib.py::TestFibonacci::test_fifth_fibonacci_numberPASSED[20%]examples/test_fib.py::TestFibonacci::test_first_fibonacci_numberPASSED[30%]examples/test_fib.py::TestFibonacci::test_fourth_fibonacci_numberPASSED[40%]examples/test_fib.py::TestFibonacci::test_negative_inputPASSED[50%]examples/test_fib.py::TestFibonacci::test_ninth_fibonacci_numberPASSED[60%]examples/test_fib.py::TestFibonacci::test_second_fibonacci_numberPASSED[70%]examples/test_fib.py::TestFibonacci::test_seventh_fibonacci_numberPASSED[80%]examples/test_fib.py::TestFibonacci::test_sixth_fibonacci_numberPASSED[90%]examples/test_fib.py::TestFibonacci::test_third_fibonacci_numberPASSED[100%]===============================10passedin0.02s==============================$catexamples/fib.pydefFibonacci(n):ifn<0:print("Incorrect input")# First Fibonacci number is 0elifn==1:return0# Second Fibonacci number is 1elifn==2:return1else:returnFibonacci(n-1)+Fibonacci(n-2)$catexamples/test_fib.pyimportunittestfrom.fibimportFibonacciclassTestFibonacci(unittest.TestCase):deftest_negative_input(self):self.assertEqual(Fibonacci(-1),None)deftest_first_fibonacci_number(self):self.assertEqual(Fibonacci(1),0)deftest_second_fibonacci_number(self):self.assertEqual(Fibonacci(2),1)deftest_third_fibonacci_number(self):self.assertEqual(Fibonacci(3),1)deftest_fourth_fibonacci_number(self):self.assertEqual(Fibonacci(4),2)deftest_fifth_fibonacci_number(self):self.assertEqual(Fibonacci(5),3)deftest_sixth_fibonacci_number(self):self.assertEqual(Fibonacci(6),5)deftest_seventh_fibonacci_number(self):self.assertEqual(Fibonacci(7),8)deftest_eighth_fibonacci_number(self):self.assertEqual(Fibonacci(8),13)deftest_ninth_fibonacci_number(self):self.assertEqual(Fibonacci(9),21)if__name__=="__main__":unittest.main()$(echo"Add type annotations for this Python code";catexamples/fib.py)|openaicomplete-|black-|teetmp&&mvtmpexamples/fib.pydefFibonacci(n:int)->int:ifn<0:print("Incorrect input")# First Fibonacci number is 0elifn==1:return0# Second Fibonacci number is 1elifn==2:return1else:returnFibonacci(n-1)+Fibonacci(n-2)$mypyexamples/fib.pyexamples/fib.py:1:error:Missingreturnstatement[return]Found1errorin1file(checked1sourcefile)$(echo"Fix mypy warnings in this Python code";catexamples/fib.py;mypyexamples/fib.py)|openaicomplete-|black-|teetmp&&mvtmpexamples/fib.pydefFibonacci(n:int)->int:ifn<0:print("Incorrect input")# First Fibonacci number is 0elifn==1:return0# Second Fibonacci number is 1elifn==2:return1else:returnFibonacci(n-1)+Fibonacci(n-2)returnNone# Added return statement$mypyexamples/fib.pyexamples/fib.py:12:error:Incompatiblereturnvaluetype(got"None",expected"int")[return-value]Found1errorin1file(checked1sourcefile)$(echo"Fix mypy warnings in this Python code";catexamples/fib.py;mypyexamples/fib.py)|openaicomplete-|black-|teetmp&&mvtmpexamples/fib.pydefFibonacci(n:int)->int:ifn<0:print("Incorrect input")# First Fibonacci number is 0elifn==1:return0# Second Fibonacci number is 1elifn==2:return1else:returnFibonacci(n-1)+Fibonacci(n-2)return0# Changed return statement to return 0$mypyexamples/fib.pySuccess:noissuesfoundin1sourcefile$(echo"Rewrite these tests to use pytest.parametrized";catexamples/test_fib.py)|openaicomplete-|black-|teetmp&&mvtmpexamples/test_fib.pyimportpytestfrom.fibimportFibonacci@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)],)deftest_fibonacci(n,expected):assertFibonacci(n)==expected