Skip to content

Commit 098e4da

Browse files
committed
Updated server docs
1 parent 8863fab commit 098e4da

File tree

1 file changed

+119
-43
lines changed

1 file changed

+119
-43
lines changed

docs/server/usage.mdx

Lines changed: 119 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
---
2-
title: Server Usage
3-
---
1+
# Server Usage Guide
42

53
## Starting the Server
64

@@ -27,35 +25,45 @@ async_interpreter.server.run(port=8000) # Default port is 8000, but you can cus
2725
Connect to the WebSocket server at `ws://localhost:8000/`.
2826

2927
### Message Format
30-
Messages must follow the LMC format with start and end flags. For detailed specifications, see the [LMC messages documentation](https://docs.openinterpreter.com/protocols/lmc-messages).
28+
The server uses an extended message format that allows for rich, multi-part messages. Here's the basic structure:
3129

32-
Basic message structure:
3330
```json
34-
{"role": "user", "type": "message", "start": true}
31+
{"role": "user", "start": true}
3532
{"role": "user", "type": "message", "content": "Your message here"}
36-
{"role": "user", "type": "message", "end": true}
33+
{"role": "user", "end": true}
3734
```
3835

36+
### Multi-part Messages
37+
You can send complex messages with multiple components:
38+
39+
1. Start with `{"role": "user", "start": true}`
40+
2. Add various types of content (message, file, image, etc.)
41+
3. End with `{"role": "user", "end": true}`
42+
43+
### Content Types
44+
You can include various types of content in your messages:
45+
46+
- Text messages: `{"role": "user", "type": "message", "content": "Your text here"}`
47+
- File paths: `{"role": "user", "type": "file", "content": "path/to/file"}`
48+
- Images: `{"role": "user", "type": "image", "format": "path", "content": "path/to/photo"}`
49+
- Audio: `{"role": "user", "type": "audio", "format": "wav", "content": "path/to/audio.wav"}`
50+
3951
### Control Commands
4052
To control the server's behavior, send the following commands:
4153

4254
1. Stop execution:
4355
```json
44-
{"role": "user", "type": "command", "start": True},
4556
{"role": "user", "type": "command", "content": "stop"}
46-
{"role": "user", "type": "command", "end": True}
4757
```
4858
This stops all execution and message processing.
4959

5060
2. Execute code block:
5161
```json
52-
{"role": "user", "type": "command", "start": True},
5362
{"role": "user", "type": "command", "content": "go"}
54-
{"role": "user", "type": "command", "end": True}
5563
```
5664
This executes a generated code block and allows the agent to proceed.
5765

58-
**Important**: If `auto_run` is set to `False`, the agent will pause after generating code blocks. You must send the "go" command to continue execution.
66+
**Note**: If `auto_run` is set to `False`, the agent will pause after generating code blocks. You must send the "go" command to continue execution.
5967

6068
### Completion Status
6169
The server indicates completion with the following message:
@@ -71,8 +79,46 @@ If an error occurs, the server will send an error message in the following forma
7179
```
7280
Your client should be prepared to handle these error messages appropriately.
7381

74-
### Example WebSocket Interaction
75-
Here's a simple example demonstrating the WebSocket interaction:
82+
## Code Execution Review
83+
84+
After code blocks are executed, you'll receive a review message:
85+
86+
```json
87+
{
88+
"role": "assistant",
89+
"type": "review",
90+
"content": "Review of the executed code, including safety assessment and potential irreversible actions."
91+
}
92+
```
93+
94+
This review provides important information about the safety and potential impact of the executed code. Pay close attention to these messages, especially when dealing with operations that might have significant effects on your system.
95+
96+
The `content` field of the review message may have two possible formats:
97+
98+
1. If the code is deemed completely safe, the content will be exactly `"<SAFE>"`.
99+
2. Otherwise, it will contain an explanation of why the code might be unsafe or have irreversible effects.
100+
101+
Example of a safe code review:
102+
```json
103+
{
104+
"role": "assistant",
105+
"type": "review",
106+
"content": "<SAFE>"
107+
}
108+
```
109+
110+
Example of a potentially unsafe code review:
111+
```json
112+
{
113+
"role": "assistant",
114+
"type": "review",
115+
"content": "This code performs file deletion operations which are irreversible. Please review carefully before proceeding."
116+
}
117+
```
118+
119+
## Example WebSocket Interaction
120+
121+
Here's an example demonstrating the WebSocket interaction:
76122

77123
```python
78124
import websockets
@@ -81,21 +127,25 @@ import asyncio
81127

82128
async def websocket_interaction():
83129
async with websockets.connect("ws://localhost:8000/") as websocket:
84-
# Send a message
85-
await websocket.send(json.dumps({"role": "user", "type": "message", "start": True}))
86-
await websocket.send(json.dumps({"role": "user", "type": "message", "content": "What's 2 + 2?"}))
87-
await websocket.send(json.dumps({"role": "user", "type": "message", "end": True}))
130+
# Send a multi-part user message
131+
await websocket.send(json.dumps({"role": "user", "start": True}))
132+
await websocket.send(json.dumps({"role": "user", "type": "message", "content": "Analyze this image:"}))
133+
await websocket.send(json.dumps({"role": "user", "type": "image", "format": "path", "content": "path/to/image.jpg"}))
134+
await websocket.send(json.dumps({"role": "user", "end": True}))
88135

89136
# Receive and process messages
90137
while True:
91138
message = await websocket.recv()
92139
data = json.loads(message)
93140

94141
if data.get("type") == "message":
95-
print(data.get("content", ""), end="", flush=True)
142+
print(f"Assistant: {data.get('content', '')}")
143+
elif data.get("type") == "review":
144+
print(f"Code Review: {data.get('content')}")
96145
elif data.get("type") == "error":
97146
print(f"Error: {data.get('content')}")
98147
elif data == {"role": "assistant", "type": "status", "content": "complete"}:
148+
print("Interaction complete")
99149
break
100150

101151
asyncio.run(websocket_interaction())
@@ -104,7 +154,7 @@ asyncio.run(websocket_interaction())
104154
## HTTP API
105155

106156
### Modifying Settings
107-
To change server settings, send a POST request to `http://localhost:8000/settings`. The payload should conform to [the interpreter object's settings](https://docs.openinterpreter.com/settings/all-settings).
157+
To change server settings, send a POST request to `http://localhost:8000/settings`. The payload should conform to the interpreter object's settings.
108158

109159
Example:
110160
```python
@@ -126,9 +176,56 @@ Example:
126176
```python
127177
response = requests.get("http://localhost:8000/settings/custom_instructions")
128178
print(response.json())
129-
# Output: {"custom_instructions": "You only write react."}
179+
# Output: {"custom_instructions": "You only write Python code."}
180+
```
181+
182+
## OpenAI-Compatible Endpoint
183+
184+
The server provides an OpenAI-compatible endpoint at `/openai`. This allows you to use the server with any tool or library that's designed to work with the OpenAI API.
185+
186+
### Chat Completions Endpoint
187+
188+
The chat completions endpoint is available at:
189+
190+
```
191+
[server_url]/openai/chat/completions
192+
```
193+
194+
To use this endpoint, set the `api_base` in your OpenAI client or configuration to `[server_url]/openai`. For example:
195+
196+
```python
197+
import openai
198+
199+
openai.api_base = "http://localhost:8000/openai" # Replace with your server URL if different
200+
openai.api_key = "dummy" # The key is not used but required by the OpenAI library
201+
202+
response = openai.ChatCompletion.create(
203+
model="gpt-3.5-turbo", # This model name is ignored, but required
204+
messages=[
205+
{"role": "system", "content": "You are a helpful assistant."},
206+
{"role": "user", "content": "What's the capital of France?"}
207+
]
208+
)
209+
210+
print(response.choices[0].message['content'])
130211
```
131212

213+
Note that only the chat completions endpoint (`/chat/completions`) is implemented. Other OpenAI API endpoints are not available.
214+
215+
When using this endpoint:
216+
- The `model` parameter is required but ignored.
217+
- The `api_key` is required by the OpenAI library but not used by the server.
218+
219+
## Best Practices
220+
221+
1. Always handle the "complete" status message to ensure your client knows when the server has finished processing.
222+
2. If `auto_run` is set to `False`, remember to send the "go" command to execute code blocks and continue the interaction.
223+
3. Implement proper error handling in your client to manage potential connection issues, unexpected server responses, or server-sent error messages.
224+
4. Use the AsyncInterpreter class when working with the server in Python to ensure compatibility with asynchronous operations.
225+
5. Pay attention to the code execution review messages for important safety and operational information.
226+
6. Utilize the multi-part user message structure for complex inputs, including file paths and images.
227+
7. When sending file paths or image paths, ensure they are accessible to the server.
228+
132229
## Advanced Usage: Accessing the FastAPI App Directly
133230

134231
The FastAPI app is exposed at `async_interpreter.server.app`. This allows you to add custom routes or host the app using Uvicorn directly.
@@ -151,25 +248,4 @@ if __name__ == "__main__":
151248
uvicorn.run(app, host="0.0.0.0", port=8000)
152249
```
153250

154-
## Using Docker
155-
156-
You can also run the server using Docker. First, build the Docker image from the root of the repository:
157-
158-
```bash
159-
docker build -t open-interpreter .
160-
```
161-
162-
Then, run the container:
163-
164-
```bash
165-
docker run -p 8000:8000 open-interpreter
166-
```
167-
168-
This will expose the server on port 8000 of your host machine.
169-
170-
## Best Practices
171-
1. Always handle the "complete" status message to ensure your client knows when the server has finished processing.
172-
2. If `auto_run` is set to `False`, remember to send the "go" command to execute code blocks and continue the interaction.
173-
3. Implement proper error handling in your client to manage potential connection issues, unexpected server responses, or server-sent error messages.
174-
4. Use the AsyncInterpreter class when working with the server in Python to ensure compatibility with asynchronous operations.
175-
5. When deploying in production, consider using the Docker container for easier setup and consistent environment across different machines.
251+
This guide covers all aspects of using the server, including the WebSocket API, HTTP API, OpenAI-compatible endpoint, code execution review, and various features. It provides clear explanations and examples for users to understand how to interact with the server effectively.

0 commit comments

Comments
 (0)