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

enable semantic cache #304

Merged
merged 1 commit into from
Sep 24, 2024
Merged
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
35 changes: 23 additions & 12 deletions backend/app/api/admin_routes/semantic_cache.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,57 @@
from typing import Optional, List
from typing import Optional, List, Dict

from fastapi import APIRouter
from app.models import Document
from app.api.deps import SessionDep, CurrentSuperuserDep
from app.rag.chat_config import ChatEngineConfig, get_default_embedding_model
from app.rag.chat_config import ChatEngineConfig
from app.rag.semantic_cache import SemanticCacheManager, SemanticItem

router = APIRouter()


@router.post("/admin/semantic_cache")
async def retrieve_documents(
async def add_semantic_cache(
session: SessionDep,
user: CurrentSuperuserDep,
question: str,
answer: str,
namespace: str = "default",
metadata: Optional[dict] = None,
chat_engine: str = "default",
) -> List[Document]:
) -> Dict:
chat_engine_config = ChatEngineConfig.load_from_db(session, chat_engine)
_dspy_lm = chat_engine_config.get_dspy_lm(session)

scm = SemanticCacheManager(
dspy_llm=_dspy_lm,
)

return scm.add_cache(
session,
item=SemanticItem(question=question, answer=answer),
namespace=namespace,
metadata=metadata,
)
try:
scm.add_cache(
session,
item=SemanticItem(question=question, answer=answer),
namespace=namespace,
metadata=metadata,
)
except Exception as e:
return {
"status": "failed",
"message": str(e),
}

return {
"status": "success",
}


@router.get("/admin/semantic_cache")
async def retrieve_documents(
async def search_semantic_cache(
session: SessionDep,
user: CurrentSuperuserDep,
query: str,
namespace: str = "default",
chat_engine: str = "default",
) -> List[Document]:
) -> Dict:
chat_engine_config = ChatEngineConfig.load_from_db(session, chat_engine)
_dspy_lm = chat_engine_config.get_dspy_lm(session)

Expand Down
Loading