百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
O

openai-cli

> 编程语言
开源

用于 OpenAI API 的命令行客户端

196 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 OpenAI API 的命令行客户端

OpenAI command-line client

Installation

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

bash
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 ) 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  int:
    if n  int:
    if n  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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Python

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言