This repository was archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Add llm cache APIs #99
Open
Akshaj000
wants to merge
3
commits into
aiplanethub:main
Choose a base branch
from
Akshaj000:llm-cache-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,4 +7,5 @@ | |
| VECTORDB = "/vectordb" | ||
| ETL = "/etl" | ||
| PROMPT_ENGINE = "/prompt-engine" | ||
| LLM_CACHE = "/llm-cache" | ||
| MODEL = "/model" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class BaseCacheRequestModel(BaseModel): | ||
| session_id: int | ||
| query: str | ||
| metadata: dict = None | ||
|
|
||
|
|
||
| class GetCacheRequestModel(BaseCacheRequestModel): | ||
| pass | ||
|
|
||
|
|
||
| class SetCacheRequestModel(BaseCacheRequestModel): | ||
| response: str | ||
|
|
||
|
|
||
| class CacheResponseModel(BaseCacheRequestModel): | ||
| response: str |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from fastapi import APIRouter | ||
|
|
||
| from genai_stack.constant import API, LLM_CACHE | ||
| from genai_stack.genai_server.settings.settings import settings | ||
| from genai_stack.genai_server.models.cache_models import ( | ||
| GetCacheRequestModel, SetCacheRequestModel, CacheResponseModel | ||
| ) | ||
| from genai_stack.genai_server.services.cache_service import LLMCacheService | ||
|
|
||
| service = LLMCacheService(store=settings.STORE) | ||
|
|
||
| router = APIRouter(prefix=API + LLM_CACHE, tags=["llm_cache"]) | ||
|
|
||
|
|
||
| @router.get("/get-cache") | ||
| def get_cache(data: GetCacheRequestModel) -> CacheResponseModel: | ||
| return service.get_cache(data=data) | ||
|
|
||
|
|
||
| @router.post("/set-cache") | ||
| def set_cache(data: SetCacheRequestModel) -> CacheResponseModel: | ||
| return service.set_cache(data=data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from fastapi import HTTPException | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from genai_stack.genai_platform.services import BaseService | ||
| from genai_stack.genai_server.models.cache_models import GetCacheRequestModel, SetCacheRequestModel, CacheResponseModel | ||
| from genai_stack.genai_server.settings.config import stack_config | ||
| from genai_stack.genai_server.utils import get_current_stack | ||
| from genai_stack.genai_store.schemas import StackSessionSchema | ||
|
|
||
|
|
||
| class LLMCacheService(BaseService): | ||
|
|
||
| def get_cache(self, data: GetCacheRequestModel) -> CacheResponseModel: | ||
| with Session(self.engine) as session: | ||
| stack_session = session.get(StackSessionSchema, data.session_id) | ||
| if stack_session is None: | ||
| raise HTTPException(status_code=404, detail=f"Session {data.session_id} not found") | ||
| stack = get_current_stack(config=stack_config, session=stack_session) | ||
| response = stack.llm_cache.get_cache( | ||
| query=data.query, | ||
| metadata=data.metadata | ||
| ) | ||
| return CacheResponseModel( | ||
| session_id=data.session_id, | ||
| query=data.query, | ||
| metadata=data.metadata, | ||
| response=response | ||
| ) | ||
|
|
||
| def set_cache(self, data: SetCacheRequestModel) -> CacheResponseModel: | ||
| with Session(self.engine) as session: | ||
| stack_session = session.get(StackSessionSchema, data.session_id) | ||
| if stack_session is None: | ||
| raise HTTPException(status_code=404, detail=f"Session {data.session_id} not found") | ||
| stack = get_current_stack(config=stack_config, session=stack_session) | ||
| stack.llm_cache.set_cache( | ||
| query=data.query, | ||
| response=data.response, | ||
| metadata=data.metadata | ||
| ) | ||
| return CacheResponseModel( | ||
| session_id=data.session_id, | ||
| query=data.query, | ||
| metadata=data.metadata, | ||
| response=data.response | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| """Tests for `genai_server`.""" | ||
| import unittest | ||
| import requests | ||
|
|
||
|
|
||
| class TestLLMCacheAPIs(unittest.TestCase): | ||
|
|
||
| def setUp(self) -> None: | ||
| self.base_url = "http://127.0.0.1:5000/api/llm-cache" | ||
|
|
||
| def test_set_cache(self): | ||
| response = requests.post( | ||
| url=self.base_url + "/set-cache", | ||
| json={ | ||
| "session_id": 1, | ||
| "query": "Where is sunil from ?", | ||
| "response": "Sunil is from Hyderabad.", | ||
| "metadata": {"source": "/path", "page": 1} | ||
| } | ||
| ) | ||
| assert response.status_code == 200 | ||
| assert response.json() | ||
| data = response.json() | ||
| assert "query" in data.keys() | ||
| assert "metadata" in data.keys() | ||
| assert "response" in data.keys() | ||
|
|
||
| def test_get_cache(self): | ||
| response = requests.get( | ||
| url=self.base_url + "/get-cache", | ||
| json={ | ||
| "session_id": 1, | ||
| "query": "Where is sunil from ?" | ||
| } | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.json() | ||
| data = response.json() | ||
| assert "query" in data.keys() | ||
| assert "metadata" in data.keys() | ||
| assert "response" in data.keys() | ||
|
|
||
| def test_get_and_set(self): | ||
| query = "Where is sunil from ?" | ||
| metadata = {"source": "/path", "page": 1} | ||
| output = "Sunil is from Hyderabad." | ||
| response = requests.post( | ||
| url=self.base_url + "/set-cache", | ||
| json={ | ||
| "session_id": 1, | ||
| "query": query, | ||
| "response": output, | ||
| "metadata": metadata | ||
| } | ||
| ) | ||
| assert response.status_code == 200 | ||
| assert response.json() | ||
| data = response.json() | ||
| assert "query" in data.keys() and data.get("query") == query | ||
| assert "metadata" in data.keys() and data.get("metadata") == metadata | ||
| assert "response" in data.keys() and data.get("response") == output | ||
|
|
||
| response = requests.get( | ||
| url=self.base_url + "/get-cache", | ||
| json={ | ||
| "session_id": 1, | ||
| "query": query | ||
| } | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.json() | ||
| data = response.json() | ||
| assert "query" in data.keys() and data.get("query") == query | ||
| assert "response" in data.keys() and data.get("response") == output | ||
|
|
||
| response = requests.get( | ||
| url=self.base_url + "/get-cache", | ||
| json={ | ||
| "session_id": 1, | ||
| "query": "Where is sunil from ?", | ||
| "metadata": {"source": "/pathdiff", "page": 1} | ||
| } | ||
| ) | ||
| assert response.status_code != 200 | ||
|
|
||
| response = requests.get( | ||
| url=self.base_url + "/get-cache", | ||
| json={ | ||
| "session_id": 1, | ||
| "query": "Where is sunil from ?", | ||
| "metadata": metadata | ||
| } | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.