Skip to content

Commit d437f1d

Browse files
authored
Merge pull request #1549 from benxu3/development
add better request handling to openai-compatible server
2 parents 9a8e034 + 928da86 commit d437f1d

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

interpreter/interpreter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def default_system_message(self):
215215
):
216216
system_message = system_message.replace(
217217
"</SYSTEM_CAPABILITY>",
218-
"* For fast web searches (like up-to-date docs) curl https://api.openinterpreter.com/v0/browser/search?query=your+search+query\n</SYSTEM_CAPABILITY>",
218+
"* For any web search requests, curl https://api.openinterpreter.com/v0/browser/search?query=your+search+query\n</SYSTEM_CAPABILITY>",
219219
)
220220

221221
# Update system prompt for Mac OS, if computer tool is enabled

interpreter/server.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from fastapi import FastAPI, Request
99
from fastapi.responses import JSONResponse, StreamingResponse
1010
from pydantic import BaseModel
11+
from asyncio import CancelledError, Task
1112

1213

1314
class ChatCompletionRequest(BaseModel):
@@ -35,14 +36,17 @@ def __init__(self, interpreter):
3536
# Setup routes
3637
self.app.post("/chat/completions")(self.chat_completion)
3738

39+
3840
async def chat_completion(self, request: Request):
3941
"""Main chat completion endpoint"""
4042
body = await request.json()
43+
if self.interpreter.debug:
44+
print("Request body:", body)
4145
try:
4246
req = ChatCompletionRequest(**body)
4347
except Exception as e:
44-
print("Validation error:", str(e)) # Debug print
45-
print("Request body:", body) # Print the request body
48+
print("Validation error:", str(e))
49+
print("Request body:", body)
4650
raise
4751

4852
# Filter out system message
@@ -75,18 +79,6 @@ async def _stream_response(self):
7579
delta["function_call"] = choice.delta.function_call
7680
if choice.delta.tool_calls is not None:
7781
pass
78-
# Convert tool_calls to dict representation
79-
# delta["tool_calls"] = [
80-
# {
81-
# "index": tool_call.index,
82-
# "id": tool_call.id,
83-
# "type": tool_call.type,
84-
# "function": {
85-
# "name": tool_call.function.name,
86-
# "arguments": tool_call.function.arguments
87-
# }
88-
# } for tool_call in choice.delta.tool_calls
89-
# ]
9082

9183
choices.append(
9284
{
@@ -108,11 +100,16 @@ async def _stream_response(self):
108100
data["system_fingerprint"] = chunk.system_fingerprint
109101

110102
yield f"data: {json.dumps(data)}\n\n"
111-
except asyncio.CancelledError:
112-
# Set stop flag when stream is cancelled
113-
self.interpreter._stop_flag = True
103+
104+
except CancelledError:
105+
# Handle cancellation gracefully
106+
print("Request cancelled - cleaning up...")
107+
114108
raise
109+
except Exception as e:
110+
print(f"Error in stream: {str(e)}")
115111
finally:
112+
# Always send DONE message and cleanup
116113
yield "data: [DONE]\n\n"
117114

118115
def run(self):

0 commit comments

Comments
 (0)