返回 JoyAI-Echo
openai-api.md
1 # OpenAI-Compatible API
2
3 nanobot can expose a minimal OpenAI-compatible endpoint for local integrations:
4
5 ```bash
6 pip install "echo-director-agent[api]"
7 nanobot serve
8 ```
9
10 By default, the API binds to `127.0.0.1:8900`. You can change this in `config.json`.
11
12 ## Behavior
13
14 - Session isolation: pass `"session_id"` in the request body to isolate conversations; omit for a shared default session (`api:default`)
15 - Single-message input: each request must contain exactly one `user` message
16 - Fixed model: omit `model`, or pass the same model shown by `/v1/models`
17 - Streaming: set `stream=true` to receive Server-Sent Events (`text/event-stream`) with OpenAI-compatible delta chunks, terminated by `data: [DONE]`; omit or set `stream=false` for a single JSON response
18 - **File uploads**: supports images, PDF, Word (.docx), Excel (.xlsx), PowerPoint (.pptx) via JSON base64 or `multipart/form-data` (max 10MB per file)
19 - API requests run in the synthetic `api` channel, so the `message` tool does **not** automatically deliver to Telegram/Discord/etc. To proactively send to another chat, call `message` with an explicit `channel` and `chat_id` for an enabled channel.
20
21 Example tool call for cross-channel delivery from an API session:
22
23 ```json
24 {
25 "content": "Build finished successfully.",
26 "channel": "telegram",
27 "chat_id": "123456789"
28 }
29 ```
30
31 If `channel` points to a channel that is not enabled in your config, nanobot will queue the outbound event but no platform delivery will occur.
32
33 ## Endpoints
34
35 - `GET /health`
36 - `GET /v1/models`
37 - `POST /v1/chat/completions`
38
39 ## curl
40
41 ```bash
42 curl http://127.0.0.1:8900/v1/chat/completions \
43 -H "Content-Type: application/json" \
44 -d '{
45 "messages": [{"role": "user", "content": "hi"}],
46 "session_id": "my-session"
47 }'
48 ```
49
50 ## File Upload (JSON base64)
51
52 Send images inline using the OpenAI multimodal content format:
53
54 ```bash
55 curl http://127.0.0.1:8900/v1/chat/completions \
56 -H "Content-Type: application/json" \
57 -d '{
58 "messages": [{"role": "user", "content": [
59 {"type": "text", "text": "Describe this image"},
60 {"type": "image_url", "image_url": {"url": "data:image/png;base64,iVBOR..."}}
61 ]}]
62 }'
63 ```
64
65 ## File Upload (multipart/form-data)
66
67 Upload any supported file type (images, PDF, Word, Excel, PPT) via multipart:
68
69 ```bash
70 # Single file
71 curl http://127.0.0.1:8900/v1/chat/completions \
72 -F "message=Summarize this report" \
73 -F "files=@report.docx"
74
75 # Multiple files with session isolation
76 curl http://127.0.0.1:8900/v1/chat/completions \
77 -F "message=Compare these files" \
78 -F "files=@chart.png" \
79 -F "files=@data.xlsx" \
80 -F "session_id=my-session"
81 ```
82
83 Supported file types:
84 - **Images**: PNG, JPEG, GIF, WebP (sent to AI as base64 for vision analysis)
85 - **Documents**: PDF, Word (.docx), Excel (.xlsx), PowerPoint (.pptx) (text extracted and sent to AI)
86 - **Text**: TXT, Markdown, CSV, JSON, etc. (read directly)
87
88 ## Python (`requests`)
89
90 ```python
91 import requests
92
93 resp = requests.post(
94 "http://127.0.0.1:8900/v1/chat/completions",
95 json={
96 "messages": [{"role": "user", "content": "hi"}],
97 "session_id": "my-session", # optional: isolate conversation
98 },
99 timeout=120,
100 )
101 resp.raise_for_status()
102 print(resp.json()["choices"][0]["message"]["content"])
103 ```
104
105 ## Python (`openai`)
106
107 ```python
108 from openai import OpenAI
109
110 client = OpenAI(
111 base_url="http://127.0.0.1:8900/v1",
112 api_key="dummy",
113 )
114
115 resp = client.chat.completions.create(
116 model="MiniMax-M2.7",
117 messages=[{"role": "user", "content": "hi"}],
118 extra_body={"session_id": "my-session"}, # optional: isolate conversation
119 )
120 print(resp.choices[0].message.content)
121 ```
122
122 lines MARKDOWN