Skip to content

Commit

Permalink
Stop server
Browse files Browse the repository at this point in the history
  • Loading branch information
KillianLucas committed Jul 22, 2024
1 parent 72b9723 commit 24e07c9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
55 changes: 34 additions & 21 deletions interpreter/core/async_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,6 @@ def accumulate(self, chunk):
chunk_copy["content"] = ""
self.messages.append(chunk_copy)

print("ADDED CHUNK:", chunk)
print("MESSAGES IS NOW:", self.messages)
# time.sleep(5)

elif type(chunk) == bytes:
if self.messages[-1]["content"] == "": # We initialize as an empty string ^
self.messages[-1]["content"] = b"" # But it actually should be bytes
Expand Down Expand Up @@ -282,7 +278,7 @@ async def home():
} else {
var startMessageBlock = {
"role": "user",
"type": "message",
//"type": "message",
"start": true
};
ws.send(JSON.stringify(startMessageBlock));
Expand All @@ -296,7 +292,7 @@ async def home():
var endMessageBlock = {
"role": "user",
"type": "message",
//"type": "message",
"end": true
};
ws.send(JSON.stringify(endMessageBlock));
Expand Down Expand Up @@ -649,21 +645,38 @@ async def chat_completion(request: ChatCompletionRequest):


class Server:
def __init__(self, async_interpreter, host=host, port=port):
def __init__(self, async_interpreter, host="127.0.0.1", port=8000):
self.app = FastAPI()
router = create_router(async_interpreter)
self.app.include_router(router)
self.host = host
self.port = port

def run(self, retries=5, *args, **kwargs):
if "host" in kwargs:
self.host = kwargs.pop("host")
if "port" in kwargs:
self.port = kwargs.pop("port")
if "app" in kwargs:
self.app = kwargs.pop("app")

self.config = uvicorn.Config(app=self.app, host=host, port=port)
self.uvicorn_server = uvicorn.Server(self.config)

@property
def host(self):
return self.config.host

@host.setter
def host(self, value):
self.config.host = value
self.uvicorn_server = uvicorn.Server(self.config)

@property
def port(self):
return self.config.port

@port.setter
def port(self, value):
self.config.port = value
self.uvicorn_server = uvicorn.Server(self.config)

def run(self, host=None, port=None, retries=5):
if host is not None:
self.host = host
if port is not None:
self.port = port

# Print server information
if self.host == "0.0.0.0":
print(
"Warning: Using host `0.0.0.0` will expose Open Interpreter over your local network."
Expand All @@ -672,12 +685,12 @@ def run(self, retries=5, *args, **kwargs):
s.connect(("8.8.8.8", 80)) # Google's public DNS server
print(f"Server will run at http://{s.getsockname()[0]}:{self.port}")
s.close()
else:
print(f"Server will run at http://{self.host}:{self.port}")

for _ in range(retries):
try:
uvicorn.run(
app=self.app, host=self.host, port=self.port, *args, **kwargs
)
self.uvicorn_server.run()
break
except KeyboardInterrupt:
break
Expand Down
4 changes: 3 additions & 1 deletion tests/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,10 @@ async def test_fastapi_server():
loop = asyncio.get_event_loop()
loop.run_until_complete(test_fastapi_server())

async_interpreter.server.uvicorn_server.should_exit = True

# Wait for the server thread to finish
server_thread.join(timeout=1)
server_thread.join(timeout=3)


def test_hallucinations():
Expand Down

0 comments on commit 24e07c9

Please sign in to comment.