Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 47 additions & 33 deletions interpreter/core/async_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

last_start_time = 0

_server_dependencies_installed = False
try:
import janus
import uvicorn
Expand All @@ -33,7 +34,8 @@
)
from fastapi.responses import JSONResponse, PlainTextResponse, StreamingResponse
from starlette.status import HTTP_403_FORBIDDEN
except:
_server_dependencies_installed = True
except ImportError:
# Server dependencies are not required by the main package.
pass

Expand Down Expand Up @@ -952,51 +954,63 @@ class Server:
DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 8000

def __init__(self, async_interpreter, host=None, port=None):
self.app = FastAPI()
router = create_router(async_interpreter)
self.authenticate = authenticate_function

# Add authentication middleware
@self.app.middleware("http")
async def validate_api_key(request: Request, call_next):
# Ignore authentication for the /heartbeat route
if request.url.path == "/heartbeat":
return await call_next(request)

api_key = request.headers.get("X-API-KEY")
if self.authenticate(api_key):
response = await call_next(request)
return response
else:
return JSONResponse(
status_code=HTTP_403_FORBIDDEN,
content={"detail": "Authentication failed"},
)
if _server_dependencies_installed:
def __init__(self, async_interpreter, host=None, port=None):
self.app = FastAPI()
router = create_router(async_interpreter)
self.authenticate = authenticate_function

# Add authentication middleware
@self.app.middleware("http")
async def validate_api_key(request: Request, call_next):
# Ignore authentication for the /heartbeat route
if request.url.path == "/heartbeat":
return await call_next(request)

api_key = request.headers.get("X-API-KEY")
if self.authenticate(api_key):
response = await call_next(request)
return response
else:
return JSONResponse(
status_code=HTTP_403_FORBIDDEN,
content={"detail": "Authentication failed"},
)

self.app.include_router(router)
h = host or os.getenv("INTERPRETER_HOST", Server.DEFAULT_HOST)
p = port or int(os.getenv("INTERPRETER_PORT", Server.DEFAULT_PORT))
self.config = uvicorn.Config(app=self.app, host=h, port=p)
self.uvicorn_server = uvicorn.Server(self.config)
self.app.include_router(router)
h = host or os.getenv("INTERPRETER_HOST", Server.DEFAULT_HOST)
p = port or int(os.getenv("INTERPRETER_PORT", Server.DEFAULT_PORT))
self.config = uvicorn.Config(app=self.app, host=h, port=p)
self.uvicorn_server = uvicorn.Server(self.config)
else:
def __init__(self, *args, **kwargs):
pass

@property
def host(self):
return self.config.host
if _server_dependencies_installed:
return self.config.host
else:
return Server.DEFAULT_HOST

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

@property
def port(self):
return self.config.port
if _server_dependencies_installed:
return self.config.port
else:
return Server.DEFAULT_PORT

@port.setter
def port(self, value):
self.config.port = value
self.uvicorn_server = uvicorn.Server(self.config)
if _server_dependencies_installed:
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:
Expand Down
9 changes: 9 additions & 0 deletions interpreter/core/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import requests
import tokentrim as tt
import httpx

from .run_text_llm import run_text_llm

Expand Down Expand Up @@ -448,6 +449,14 @@ def fixed_litellm_completions(**params):
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
except httpx.RemoteProtocolError as e:
print(
"\n\n---",
"Warning: Encountered a remote protocol error. This is likely due to a bug in the local LLM server. We will attempt to reconnect and continue.",
"---\n\n",
)
time.sleep(1)
continue
except Exception as e:
if attempt == 0:
# Store the first error
Expand Down
Loading