Skip to content

Commit a85094d

Browse files
committed
add better request handling to openai-compatible server
1 parent 2751057 commit a85094d

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

interpreter/interpreter.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ def default_system_message(self):
207207
print("Error adding system capability for cwd")
208208

209209
system_message += "</SYSTEM_CAPABILITY>"
210-
210+
211211
# Add web search capability if enabled
212212
if (
213213
os.environ.get("INTERPRETER_EXPERIMENTAL_WEB_SEARCH", "false").lower()
214214
== "true"
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
@@ -249,7 +249,7 @@ async def async_respond(self, user_input=None):
249249
provider = self.provider # Keep existing provider if set
250250
max_tokens = self.max_tokens # Keep existing max_tokens if set
251251

252-
if self.model == "claude-3-5-sonnet-latest":
252+
if self.model == "claude-3-5-sonnet":
253253
# For some reason, Litellm can't find the model info for claude-3-5-sonnet-latest
254254
provider = "anthropic"
255255

@@ -971,6 +971,12 @@ async def async_chat(self):
971971
except KeyboardInterrupt:
972972
self._spinner.stop()
973973
except asyncio.CancelledError:
974+
print("ASYNC CHAT INSIDE INTERPRETER CANCELLED ERROR HERE")
975+
print("ASYNC CHAT INSIDE INTERPRETER CANCELLED ERROR HERE")
976+
print("ASYNC CHAT INSIDE INTERPRETER CANCELLED ERROR HERE")
977+
print("ASYNC CHAT INSIDE INTERPRETER CANCELLED ERROR HERE")
978+
print("ASYNC CHAT INSIDE INTERPRETER CANCELLED ERROR HERE")
979+
print("ASYNC CHAT INSIDE INTERPRETER CANCELLED ERROR HERE")
974980
self._spinner.stop()
975981

976982
print()

interpreter/profiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Profile:
3232
def __init__(self):
3333
# Default values if no profile exists
3434
# Model configuration
35-
self.model = "claude-3-5-sonnet-latest" # The LLM model to use
35+
self.model = "claude-3-5-sonnet" # The LLM model to use
3636
self.provider = (
3737
None # The model provider (e.g. anthropic, openai) None will auto-detect
3838
)

interpreter/server.py

Lines changed: 24 additions & 17 deletions
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,27 @@ def __init__(self, interpreter):
3536
# Setup routes
3637
self.app.post("/chat/completions")(self.chat_completion)
3738

39+
# Add a field to track the current request task
40+
self._current_request: Optional[Task] = None
41+
3842
async def chat_completion(self, request: Request):
3943
"""Main chat completion endpoint"""
44+
# Cancel any existing request
45+
if self._current_request and not self._current_request.done():
46+
self._current_request.cancel()
47+
try:
48+
await self._current_request
49+
except CancelledError:
50+
pass
51+
4052
body = await request.json()
53+
if self.interpreter.debug:
54+
print("Request body:", body)
4155
try:
4256
req = ChatCompletionRequest(**body)
4357
except Exception as e:
44-
print("Validation error:", str(e)) # Debug print
45-
print("Request body:", body) # Print the request body
58+
print("Validation error:", str(e))
59+
print("Request body:", body)
4660
raise
4761

4862
# Filter out system message
@@ -75,18 +89,6 @@ async def _stream_response(self):
7589
delta["function_call"] = choice.delta.function_call
7690
if choice.delta.tool_calls is not None:
7791
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-
# ]
9092

9193
choices.append(
9294
{
@@ -108,11 +110,16 @@ async def _stream_response(self):
108110
data["system_fingerprint"] = chunk.system_fingerprint
109111

110112
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
113+
114+
except CancelledError:
115+
# Handle cancellation gracefully
116+
print("Request cancelled - cleaning up...")
117+
114118
raise
119+
except Exception as e:
120+
print(f"Error in stream: {str(e)}")
115121
finally:
122+
# Always send DONE message and cleanup
116123
yield "data: [DONE]\n\n"
117124

118125
def run(self):

0 commit comments

Comments
 (0)