Auto-Coder的CLI 和 Python API
auto-coder.chat 是 auto-coder 系列软件中的一款由人工智能驱动的交互式编码助手,其目标在于提高编码效率并降低学习门槛,适应各个水平的用户。
个人使用了之后,感觉非常不错,特别推荐!使用方法和记录:把AI装进你的terminal终端:Auto-Coder终极编程助手体验-CSDN博客
文档:CLI 和 Python API - 飞书云文档
auto-coder除了在终端交互使用,还可以直接使用命令来完成单个任务,也可以使用python 调用api来完成任务。
直接终端命令行单问题启动
进入目录,然后把问题传送给auto-coder.run
cd YOUR PROJECT
echo "这个项目是干嘛用的" | auto-coder.run --model v3_chat
具体实践输出
echo "这个项目是干嘛用的" | auto-coder.run --model 12gpt-4o
/home/skywalk/minipy312/lib/python3.12/site-packages/autocoder/common/buildin_tokenizer.py:1: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.import pkg_resources
─────────────────────────────────────────────────────────────── Starting Auto-Coder: autocoder ───────────────────────────────────────────────────────────────
╭─────────────────────────────────────────────────────────────────────── 🎯 Objective ───────────────────────────────────────────────────────────────────────╮
│ Query: │
│ /new 这个项目是干嘛用的 │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Successfully set configuration: model = 12gpt-4o
Conversation ID: bcb4fc7a-8e7c-46d7-8cfe-46a86883b7be
为了理解这个项目的目的,我将首先查看项目的文件结构和内容,以便找到相关的文档或代码注释来提供更多信息。我将开始使用 `list_files` 工具列出当前工作目录中的文件和文件夹。╭─ 🛠️ Action: ListFilesTool ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Tool: ListFilesTool │
│ path: . │
│ recursive: False │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ ✅ Tool Result: ListFilesTool ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Status: Success │
│ Message: Successfully listed contents of '.' (Recursive: False). Found 7 items. │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
[".autocoderignore",".gitignore","actions/","fin/","nohup.out","output.txt","thunderbolt_game/"
]
Token usage: Input=0, Output=72, Total=72
当前工作目录包含以下文件和文件夹:1. `.autocoderignore`
2.`.gitignore`
3. `actions/` (目录)
4. `fin/` (目录)
5. `nohup.out`
6. `output.txt`
7. `thunderbolt_game/` (目录)为了进一步了解这个项目的目的,我将查看 `output.txt` 文件的内容,因为它通常可能包含有关项目的描述或输出信息。╭─ 🛠️ Action: ReadFileTool ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Tool: ReadFileTool │
│ path: output.txt │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ ✅ Tool Result: ReadFileTool ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Status: Success │
│ Message: output.txt │
│ │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Token usage: Input=0, Output=112, Total=112
各种输入变化
直接输入问题
auto-coder.run --model v3_chat "Write a function to calculate Fibonacci numbers"
使用cat输入问题
cat prompt.md | auto-coder.run --model v3_chat
继续对话
auto-coder.run --model v3_chat --continue "继续修改xxxxx"
恢复某次对话?
# 回复某次对话
auto-coder.run --model v3_chat --resume 550e8400-e29b-41d4-a716-446655440000 "继续修改"
批量任务执行
windows脚本
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansionecho 开始遍历任务列表执行...
echo.REM 定义任务列表
set tasks[0]=任务1
set tasks[1]=任务2set index=0
for /F "tokens=2 delims==" %%a in ('set tasks[') do (set "task=%%a"echo 正在执行第 !index! 个任务: !task!echo !task! | auto-coder.run --model v3_chatecho 执行完成,等待 3 秒后继续...timeout /t 3 /nobreak >nulecho ----------------------------------------echo.set /a index+=1
)echo 所有任务执行完成!endlocalpause
linux脚本
#!/bin/bash# 定义要执行任务列表
tasks=("任务1""任务2"
)# 遍历任务列表
echo "开始任务列表执行..."
for task in "${task[@]}"; doecho "正在执行任务: $task"echo "$task" | auto-coder.run --model v3_chatecho "执行完成,等待 3 秒后继续..."sleep 3echo "----------------------------------------"
done
python api调用
同步异步代码
import asyncio
from autocoder.sdk import query, query_sync, AutoCodeOptions# 同步查询
response = query_sync("Write a function to calculate Fibonacci numbers",options = AutoCodeOptions( model="v3_chat"))
print(response)# 异步查询
async def async_example():options = AutoCodeOptions( model="v3_chat")async for message in query("Explain how Python decorators work", options):print(f"[{message.role}] {message.content}")asyncio.run(async_example())
测试的时候,使用本地大模型,但是有报错,准备多尝试一下
配置了这几个模型
│ 12deepseek-v3 * │ deepseek-v3 │ http://192.168.1.12:1337/v1 │ 0.00 │ 0.00 │ 0.000 │
├─────────────────────────────────────────┼───────────────────────────────┼───────────────────────────────┼────────────────┼────────────────┼────────────────┤
│ 13deepseek-v3 * │ deepseek-v3 │ http://192.168.1.13:1337/v1 │ 0.00 │ 0.00 │ 0.000 │
├─────────────────────────────────────────┼───────────────────────────────┼───────────────────────────────┼────────────────┼────────────────┼────────────────┤
│ 13gpt-4o * │ gpt-4o │ http://192.168.1.13:1337/v1 │ 0.00 │ 0.00 │ 0.000 │
├─────────────────────────────────────────┼───────────────────────────────┼───────────────────────────────┼────────────────┼────────────────┼────────────────┤
│ 12gpt-4o * │ gpt-4o │ http://192.168.1.12:1337/v1 │ 0.00 │ 0.00 │ 0.000 │
├─────────────────────────────────────────┼───────────────────────────────┼───────────────────────────────┼────────────────┼────────────────┼────────────────┤
│ g4fgpt-4o *
最后又用了本地调用
/models /add_model name=127gpt-4o model_name=gpt-4o base_url=http://127.0.0.1:1337/v1
/models /add 127gpt-4o <YOUR_API_KEY>
/conf model:127gpt-4o/models /add_model name=12gpt-4o model_name=gpt-4o base_url=http://192.168.1.12:1337/v1
/models /add 12gpt-4o <YOUR_API_KEY>
/conf model:12gpt-4o
测试成功!输出
python testautocoder.py
/home/skywalk/py312/lib/python3.12/site-packages/autocoder/common/buildin_tokenizer.py:1: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.import pkg_resources
────────────────────────────────────────────────────────────── Starting Auto-Coder: my-project ───────────────────────────────────────────────────────────────
╭─────────────────────────────────────────────────────────────────────── 🎯 Objective ───────────────────────────────────────────────────────────────────────╮
│ Query: │
│ /new Write a function to calculate Fibonacci numbers, save to fibonaccifun.py file │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Successfully set configuration: model = 127gpt-4o
Conversation ID: 4a3363bf-5827-48ff-b703-bfc4c37f14a7
Certainly! Here's a simple Python function to calculate Fibonacci numbers, which will be saved to a file named `fibonaccifun.py`.```python
def fibonacci(n):""" Calculate the nth Fibonacci number.:param n: The position in the Fibonacci sequence (n >= 0). :return: The nth Fibonacci number."""if n < 0:raise ValueError("Input cannot be negative")elif n == 0:return0
elif n == 1:return 1 a, b = 0, 1for _ in range(2, n + 1):a, b = b, a + b return bif __name__ == "__main__":# Example usageindex = 10print(f"The {index}th Fibonacci number is {fibonacci(index)}")
```To save this code to a file named `fibonaccifun.py`, you can copy the code into a text editor and save it with that filename, or you can use the following Python script to write it directly to a file:```python
code = """
def fibonacci(n):\""" Calculate the nth Fibonacci number.:param n: The position in the Fibonacci sequence (n >= 0).:return: The nth Fibonaccinumber.\"""if n < 0:raise ValueError("Input cannot be negative")elif n == 0:return0
elif n == 1:return 1 a, b = 0, 1for _ in range(2, n + 1):a, b = b, a + b return bif __name__ == "__main__": # Example usage index = 10print(f"The {index}th Fibonacci number is {fibonacci(index)}")
"""with open('fibonaccifun.py', 'w') as file:file.write(code)
```Running this script will create the `fibonaccifun.py` file with the Fibonacci function inside it.Token usage: Input=0, Output=372, Total=372
Understood. Please provide the specific task you would like me to perform so I can choose the appropriate tool to proceed.Token usage: Input=0, Output=24, Total=24
^@I will use the `write_to_file` tool to create the `fibonaccifun.py` file with the Fibonacci function.Here’s the code I will write to the file:```python
def fibonacci(n):"""Calculate the nth Fibonacci number.:param n: The position in the Fibonacci sequence (n >= 0). :return: The nth Fibonacci number."""if n < 0:raise ValueError("Input cannot be negative")elif n== 0:return0
elif n == 1:return 1 a, b = 0, 1for _ in range(2, n + 1): a, b = b, a + b return bif __name__ == "__main__":# Example usageindex = 10print(f"The {index}th Fibonaccinumber is{fibonacci(index)}")
```Now, I will proceed to save this code into the `fibonaccifun.py` file.╭─ 🛠️ Action: WriteToFileTool ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Tool: WriteToFileTool │
│ path: fibonaccifun.py │
│ content: │
│ def fibonacci(n): │
│ """ │
│ Calculate the nth F...index}th Fibonacci number is {fibonacci(index)}") │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ ✅ Tool Result: WriteToFileTool ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Status: Success │
│ Message: fibonaccifun.py │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
{"content": "\ndef fibonacci(n):\n \"\"\"\n Calculate the nth Fibonacci number.\n\n :param n: The position in the Fibonacci sequence (n >= 0).\n
...range(2, n + 1):\n a, b = b, a + b\n return b\n\nif __name__ == \"__main__\":\n # Example usage\n index = 10\n print(f\"The {index}th Fibo
}
Token usage: Input=0, Output=405, Total=405
The `fibonaccifun.py` file has been successfully created with the Fibonacci function.
To demonstrate the result, you can run the file using thefollowing command:```bash
python3 fibonaccifun.py
```This will execute the script and print the 10th Fibonacci number.Token usage: Input=0, Output=61, Total=61
^@^@I will use the `execute_command` tool to run the `fibonaccifun.py` file and display the output.╭─ 🛠️ Action: ExecuteCommandTool ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Tool: ExecuteCommandTool │
│ command: python3 fibonaccifun.py │
│ requires_approval: False │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Using run_cmd_subprocess: python3 fibonaccifun.py
Running command: python3 fibonaccifun.py
SHELL: /bin/bash
The 10th Fibonacci number is 55
╭─ ✅ Tool Result: ExecuteCommandTool ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Status: Success │
│ Message: Command executed successfully. │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯1 The 10th Fibonacci number is 552
Token usage: Input=0, Output=56, Total=56
The 10th Fibonacci number is **55**. Let me know if you'd like help with Fibonacci-related calculations or explanations! 😊Token usage: Input=0, Output=25, Total=25
^@Understood. Please provide the specific task or request you'd like me to perform, and I will proceed accordingly using the appropriate tools or actions.Token usage: Input=0, Output=28, Total=28
I will use the `attempt_completion` tool to finalize the task of creating the Fibonacci function and executing the script.╭─ 🛠️ Action: AttemptCompletionTool ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Tool: AttemptCompletionTool │
│ result: │
│ The `fibonaccifun.py` file has been successfully ..., and the 10th Fibonacci number calculated is 55. │
│ │
│ command: None │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Token usage: Input=0, Output=64, Total=64
✅ Successfully updated YAML file: /home/skywalk/work/my-project/actions/000000000002_chat_action.yml
╭────────────────────────────────────────── Git Commit Summary ───────────────────────────────────────────╮
│ Commit Information (Use /revert to revert this commit) │
│ ┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │
│ ┃ Attribute ┃ Value ┃ │
│ ┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │
│ │ Commit Hash │ d1b80b10268cf7b43a655e26ca2c18c4f5a4d2cb │ │
│ │ Commit Message │ /new Write a function to calculate Fibonacci numbers, save to fibonaccifun.py file │ │
│ │ │ auto_coder_000000000002_chat_action.yml │ │
│ │ Changed Files │ No files changed │ │
│ └────────────────┴────────────────────────────────────────────────────────────────────────────────────┘ │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ 🏁 Task Completion ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ The fibonaccifun.py file has been successfully created, and the 10th Fibonacci number calculated is 55. │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────── Auto-Coder Finished Successfully ──────────────────────────────────────────────────────────────
异步那块没通过....
总结
同步测试通过了,异步测试没通过。
调试
测试api调用12gpt-4o时报错502 Bad Gateway
Successfully set configuration: model = 12gpt-4o
Conversation ID: 213d54a3-2c1e-4186-949d-f647bc2fc5c7
502 Bad Gateway
Unable to reach the origin service. The service may be down or it may not be responding to traffic from cloudflared
Token usage: Input=0, Output=1, Total=1
换模型试试
测试时报错Segmentation fault (core dumped)
Successfully set configuration: model = 13gpt-4o
Conversation ID: 54e1e38a-2ca2-44ce-a8ae-a072cb0cdd58
An unexpected error occurred during agent execution: {e}
Traceback (most recent call last):
File "/home/skywalk/minipy312/lib/python3.12/site-packages/autocoder/common/v2/agent/runner/sdk_runner.py", line 32, in run
for agent_event in event_stream:
^^^^^^^^^^^^
File "/home/skywalk/minipy312/lib/python3.12/site-packages/autocoder/common/v2/agent/agentic_edit.py", line 1591, in analyze
for event in parsed_events:
^^^^^^^^^^^^^
File "/home/skywalk/minipy312/lib/python3.12/site-packages/autocoder/common/v2/agent/agentic_edit.py", line 1878, in stream_and_parse_llm_response
for content_chunk, metadata in generator:
^^^^^^^^^
File "/home/skywalk/minipy312/lib/python3.12/site-packages/autocoder/common/utils_code_auto_generate.py", line 88, in stream_chat_with_continue
for res in stream_generator:
^^^^^^^^^^^^^^^^
File "/home/skywalk/minipy312/lib/python3.12/site-packages/byzerllm/utils/client/simple_byzerllm_client.py", line 594, in stream_chat_oai
for chunk in response:
^^^^^^^^
File "/home/skywalk/minipy312/lib/python3.12/site-packages/openai/_streaming.py", line 46, in __iter__
for item in self._iterator:
^^^^^^^^^^^^^^
File "/home/skywalk/minipy312/lib/python3.12/site-packages/openai/_streaming.py", line 72, in __stream__
raise APIError(
openai.APIError: MissingAuthError: Response 401: Authentication failed.
╭─ 🔥 Error ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Error Type: BridgeError │
│ Message: [BRIDGE_ERROR] Bridge error: run_auto_command failed: MissingAuthError: Response 401: Authentication failed. │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Traceback (most recent call last):
File "/home/skywalk/work/autocoder/testautocoder.py", line 5, in <module>
response = query_sync("写一个雷电小游戏",options = AutoCodeOptions(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/skywalk/minipy312/lib/python3.12/site-packages/autocoder/sdk/__init__.py", line 91, in query_sync
return core.query_sync(f"/new {prompt}", show_terminal)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/skywalk/minipy312/lib/python3.12/site-packages/autocoder/sdk/core/auto_coder_core.py", line 653, in query_sync
raise e
File "/home/skywalk/minipy312/lib/python3.12/site-packages/autocoder/sdk/core/auto_coder_core.py", line 636, in query_sync
raise BridgeError(f"Query failed: {event.data.get('error', 'Unknown error')}")
autocoder.sdk.exceptions.BridgeError: [BRIDGE_ERROR] Bridge error: Query failed: [BRIDGE_ERROR] Bridge error: run_auto_command failed: MissingAuthError: Response 401: Authentication failed.
Segmentation fault (core dumped)
不知道是不是FreeBSD系统的问题,换一个操作系统试试。
执行测试的时候python testautocoder.py 报错
python testautocoder.py
/home/skywalk/py312/lib/python3.12/site-packages/autocoder/common/git_utils.py:298: SyntaxWarning: invalid escape sequence '\ '
没有配置好大模型服务器。使用g4f配置好大模型服务器。
使用Ubuntu,异步的时候还是core dumped
File "/home/skywalk/py312/lib/python3.12/site-packages/autocoder/sdk/core/bridge.py", line 129, in call_run_auto_command
raise BridgeError(
autocoder.sdk.exceptions.BridgeError: [BRIDGE_ERROR] Bridge error: run_auto_command failed: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/skywalk/work/my-project/testasyncautocoder.py", line 15, in <module>
asyncio.run(async_example())
File "/home/skywalk/py312/lib/python3.12/asyncio/runners.py", line 195, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/home/skywalk/py312/lib/python3.12/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/skywalk/py312/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/home/skywalk/work/my-project/testasyncautocoder.py", line 12, in async_example
async for message in query("Explain how Python decorators work", options):
File "/home/skywalk/py312/lib/python3.12/site-packages/autocoder/sdk/__init__.py", line 61, in query
async for message in core.query_stream(f"/new {prompt}", show_terminal):
File "/home/skywalk/py312/lib/python3.12/site-packages/autocoder/sdk/core/auto_coder_core.py", line 605, in query_stream
raise e
File "/home/skywalk/py312/lib/python3.12/site-packages/autocoder/sdk/core/auto_coder_core.py", line 549, in query_stream
for event in event_stream:
^^^^^^^^^^^^
File "/home/skywalk/py312/lib/python3.12/site-packages/autocoder/sdk/core/bridge.py", line 144, in call_run_auto_command
raise BridgeError(
autocoder.sdk.exceptions.BridgeError: [BRIDGE_ERROR] Bridge error: run_auto_command failed: [BRIDGE_ERROR] Bridge error: run_auto_command failed: timed out
Segmentation fault (core dumped)
大约问题要么是大模型调用出问题,要么是异步出了问题。
AI说:
核心问题诊断
-
桥接层超时
BridgeError
显示run_auto_command
调用超时,可能因子进程未响应或IPC通信阻塞12- 段错误(Segmentation fault)通常由内存越界或C扩展模块崩溃引发34
-
环境兼容性
- Python 3.12可能与某些C扩展库存在兼容性问题(如未适配新版ABI)35
- 缺少CUDA运行时库(
libcudart.so.12
)会导致GPU加速模块加载失败3
估计不成了,先放弃吧。
回头用自建deepseek再试试。