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

[WIP] Allow to implement custom endpoints #809

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/cat/looking_glass/cheshire_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from typing import List, Dict
from typing_extensions import Protocol

from fastapi.routing import APIRouter
from fastapi import FastAPI

from langchain_core.language_models.llms import BaseLLM
from langchain.base_language import BaseLanguageModel
from langchain_core.language_models.chat_models import BaseChatModel
Expand Down Expand Up @@ -82,6 +85,11 @@ def __init__(self):
# allows plugins to do something after the cat bootstrap is complete
self.mad_hatter.execute_hook("after_cat_bootstrap", cat=self)

def load_custom_endpoints(self):
custom_endpoints_router = APIRouter(prefix="/custom")
custom_endpoints_router = self.mad_hatter.execute_hook("load_custom_endpoints", custom_endpoints_router, cat=self)
return custom_endpoints_router

def load_natural_language(self):
"""Load Natural Language related objects.

Expand Down
25 changes: 24 additions & 1 deletion core/cat/mad_hatter/core_plugin/hooks/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from cat.mad_hatter.decorators import hook
from langchain.docstore.document import Document
from fastapi.routing import APIRouter


# Called before cat bootstrap
Expand Down Expand Up @@ -331,4 +332,26 @@ def before_cat_stores_episodic_memory(doc: Document, cat) -> Document:
`when`: timestamp to track when it's been uploaded.

"""
return doc
return doc


# Hook called to extend custom endpoint routes in cat rest api
@hook(priority=0)
def load_custom_endpoints(custom_endpoint_router: APIRouter, cat) -> None:
"""Hook the custom endpoints router loading.

Allow to extend custom endpoint routes in FastAPI

Parameters
----------
custom_endpoint_router : APIRouter
FastAPI APIRouter instance.
cat : CheshireCat
Cheshire Cat instance.

Returns
-------
custom_endpoint_router : APIRouter
extended FastAPI APIRouter instance with custom routes.
"""
return custom_endpoint_router
3 changes: 3 additions & 0 deletions core/cat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ async def lifespan(app: FastAPI):
# - Starlette allows this: https://www.starlette.io/applications/#storing-state-on-the-app-instance
app.state.ccat = CheshireCat()

# Load custom endpoints from cat tools
app.include_router(router=app.state.ccat.load_custom_endpoints())

# Dict of pseudo-sessions (key is the user_id)
app.state.strays = {}

Expand Down