-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add semantic router to router summarization tasks to another pipeline (…
…#25) * Add semantic router to router summarization tasks to another pipeline * Fix formatting * Update requirements
- Loading branch information
Showing
9 changed files
with
78 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,21 +1,12 @@ | ||
from fastapi import APIRouter, Depends | ||
from fastapi import APIRouter | ||
|
||
from auth.user import get_current_api_user | ||
from models.query import RequestPayload, ResponsePayload | ||
from service.vector_database import VectorService, get_vector_service | ||
from service.router import query as _query | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/query", response_model=ResponsePayload) | ||
async def query(payload: RequestPayload, _api_user=Depends(get_current_api_user)): | ||
vector_service: VectorService = get_vector_service( | ||
index_name=payload.index_name, credentials=payload.vector_database | ||
) | ||
chunks = await vector_service.query(input=payload.input, top_k=4) | ||
documents = await vector_service.convert_to_rerank_format(chunks=chunks) | ||
if len(documents): | ||
documents = await vector_service.rerank( | ||
query=payload.input, documents=documents | ||
) | ||
return {"success": True, "data": documents} | ||
async def query(payload: RequestPayload): | ||
output = await _query(payload=payload) | ||
return {"success": True, "data": output} |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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,50 @@ | ||
from typing import List | ||
|
||
from decouple import config | ||
from semantic_router.encoders import CohereEncoder | ||
from semantic_router.layer import RouteLayer | ||
from semantic_router.route import Route | ||
|
||
from models.query import RequestPayload | ||
from service.vector_database import VectorService, get_vector_service | ||
|
||
|
||
def create_route_layer() -> RouteLayer: | ||
routes = [ | ||
Route( | ||
name="summarize", | ||
utterances=[ | ||
"Summmarize the following", | ||
"Could you summarize the", | ||
"Summarize", | ||
"Provide a summary of", | ||
], | ||
score_threshold=0.5, | ||
) | ||
] | ||
encoder = CohereEncoder(cohere_api_key=config("COHERE_API_KEY")) | ||
return RouteLayer(encoder=encoder, routes=routes) | ||
|
||
|
||
async def get_documents(vector_service: VectorService, payload: RequestPayload) -> List: | ||
chunks = await vector_service.query(input=payload.input, top_k=4) | ||
documents = await vector_service.convert_to_rerank_format(chunks=chunks) | ||
|
||
if len(documents): | ||
documents = await vector_service.rerank( | ||
query=payload.input, documents=documents | ||
) | ||
return documents | ||
|
||
|
||
async def query(payload: RequestPayload) -> List: | ||
rl = create_route_layer() | ||
decision = rl(payload.input).name | ||
print(decision) | ||
if decision == "summarize": | ||
return [] | ||
|
||
vector_service: VectorService = get_vector_service( | ||
index_name=payload.index_name, credentials=payload.vector_database | ||
) | ||
return await get_documents(vector_service, payload) |
This file contains 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