Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto PR from main to live #19

Merged
merged 2 commits into from
Mar 18, 2024
Merged
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
17 changes: 17 additions & 0 deletions app/libs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .context import Context
from .base_handler import Handler, DefaultCompletionHandler, ExceptionHandler, FallbackHandler
from .provider_handler import ProviderSelectionHandler
from .vision_handler import ImageMessageHandler
from .tools_handler import ToolExtractionHandler, ToolResponseHandler

__all__ = [
"Context",
"Handler",
"DefaultCompletionHandler",
"ExceptionHandler",
"ProviderSelectionHandler",
"ImageMessageHandler",
"ToolExtractionHandler",
"ToolResponseHandler",
"FallbackHandler",
]
62 changes: 62 additions & 0 deletions app/libs/base_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from abc import ABC, abstractmethod
from .context import Context
from fastapi.responses import JSONResponse
import traceback

class Handler(ABC):
"""Abstract Handler class for building the chain of handlers."""

_next_handler: "Handler" = None

def set_next(self, handler: "Handler") -> "Handler":
self._next_handler = handler
return handler

@abstractmethod
async def handle(self, context: Context):
if self._next_handler:
try:
return await self._next_handler.handle(context)
except Exception as e:
_exception_handler: "Handler" = ExceptionHandler()
# Extract the stack trace and log the exception
return await _exception_handler.handle(context, e)


class DefaultCompletionHandler(Handler):
async def handle(self, context: Context):
if context.is_normal_chat:
# Assuming context.client is set and has a method for creating chat completions
completion = context.client.route(
messages=context.messages,
**context.client.clean_params(context.params),
)
context.response = completion.model_dump()
return JSONResponse(content=context.response, status_code=200)

return await super().handle(context)


class FallbackHandler(Handler):
async def handle(self, context: Context):
# This handler does not pass the request further down the chain.
# It acts as a fallback when no other handler has processed the request.
if not context.response:
# The default action when no other handlers have processed the request
context.response = {"message": "No suitable action found for the request."}
return JSONResponse(content=context.response, status_code=400)

# If there's already a response set in the context, it means one of the handlers has processed the request.
return JSONResponse(content=context.response, status_code=200)


class ExceptionHandler(Handler):
async def handle(self, context: Context, exception: Exception):
print(f"Error processing the request: {exception}")
print(traceback.format_exc())
return JSONResponse(
content={"error": "An unexpected error occurred. " + str(exception)},
status_code=500,
)


Loading
Loading