Wrap AI CLI into OpenAI compatible API: from stdin, JSON output to timeout handling (with runnable examples)
In a nutshell, connecting CLI to HTTP revolves around input, program lifecycle, and output format; the path /v1/chat/completions does not represent full protocol support. The problem encountered was that there was already an AI CLI in the terminal that could answer questions, but the program to be connected only sent HTTP requests, and the two were speaking different languages. The initial thought was to run the CLI once after receiving the question and then return the answer. However, it was only when the actual connection was attempted that it was discovered that the areas prone to errors were all in the middle: how to pass long prompts? Which part of the JSON printed by the CLI was the answer? How long should HTTP wait when the command was stuck? This article condensed the actual practices used in packaging services into an example that could run locally. First, a fake CLI was used to verify the process and protocol, which did not require model keys and would not consume model usage. The Python packaging process was tested, and no real model was called during this test. When replacing with a real CLI, login, flags, and output format still needed to be verified separately. First, decide where to be compatible. The example only accepted one text user message and returned a complete JSON once. …
An article discusses connecting a Command-Line Interface (CLI) to HTTP, specifically implementing a chat completion service. The example provided uses a fake CLI and a Python script to wrap the service, allowing an HTTP client to send a text message and receive a response in JSON format. The service only supports a limited set of features, including a single text user message and a fixed model named "cli-demo", and does not support streaming, multiple tool calls, or other parameters.
The implementation follows the OpenAI Chat Completions API, but only partially. The example code is written in Python and can be run locally without using a real model.
Written by urgent.news from Dev.to's report — not a translation of it. Machine-written — may contain errors; check the original before relying on it.