|
| 1 | +import pandas as pd |
| 2 | +import structlog |
| 3 | +from qdrant_client import QdrantClient |
| 4 | + |
| 5 | +from flare_ai_rag.config import config |
| 6 | +from flare_ai_rag.openrouter.client import OpenRouterClient |
| 7 | +from flare_ai_rag.responder.config import ResponderConfig |
| 8 | +from flare_ai_rag.responder.responder import OpenRouterResponder |
| 9 | +from flare_ai_rag.retriever.config import QdrantConfig |
| 10 | +from flare_ai_rag.retriever.qdrant_collection import generate_collection |
| 11 | +from flare_ai_rag.retriever.qdrant_retriever import QdrantRetriever |
| 12 | +from flare_ai_rag.router.config import RouterConfig |
| 13 | +from flare_ai_rag.router.router import QueryRouter |
| 14 | +from flare_ai_rag.utils import loader |
| 15 | + |
| 16 | +logger = structlog.get_logger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +def setup_clients(input_config: dict) -> tuple[OpenRouterClient, QdrantClient]: |
| 20 | + """Initialize OpenRouter and Qdrant clients.""" |
| 21 | + # Setup OpenRouter client. |
| 22 | + openrouter_client = OpenRouterClient( |
| 23 | + api_key=config.open_router_api_key, base_url=config.open_router_base_url |
| 24 | + ) |
| 25 | + |
| 26 | + # Setup Qdrant client. |
| 27 | + qdrant_config = QdrantConfig.load(input_config["qdrant_config"]) |
| 28 | + qdrant_client = QdrantClient(host=qdrant_config.host, port=qdrant_config.port) |
| 29 | + |
| 30 | + return openrouter_client, qdrant_client |
| 31 | + |
| 32 | + |
| 33 | +def setup_router( |
| 34 | + openrouter_client: OpenRouterClient, input_config: dict |
| 35 | +) -> QueryRouter: |
| 36 | + """Initialize the query router.""" |
| 37 | + router_model_config = input_config["router_model"] |
| 38 | + router_config = RouterConfig.load(router_model_config) |
| 39 | + return QueryRouter(client=openrouter_client, config=router_config) |
| 40 | + |
| 41 | + |
| 42 | +def setup_responder( |
| 43 | + openrouter_client: OpenRouterClient, input_config: dict |
| 44 | +) -> OpenRouterResponder: |
| 45 | + """Initialize the responder.""" |
| 46 | + responder_config = input_config["responder_model"] |
| 47 | + responder_config = ResponderConfig.load(responder_config) |
| 48 | + return OpenRouterResponder( |
| 49 | + client=openrouter_client, responder_config=responder_config |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def setup_retriever( |
| 54 | + qdrant_client: QdrantClient, |
| 55 | + input_config: dict, |
| 56 | + df_docs: pd.DataFrame, |
| 57 | + collection: str | None = None, |
| 58 | +) -> QdrantRetriever: |
| 59 | + """Initialize the Qdrant retriever.""" |
| 60 | + qdrant_config = QdrantConfig.load(input_config["qdrant_config"]) |
| 61 | + |
| 62 | + # (Re)generate qdrant collection |
| 63 | + if collection: |
| 64 | + generate_collection( |
| 65 | + df_docs, qdrant_client, qdrant_config, collection_name=collection |
| 66 | + ) |
| 67 | + # Return retriever |
| 68 | + return QdrantRetriever(client=qdrant_client, qdrant_config=qdrant_config) |
| 69 | + |
| 70 | + |
| 71 | +def main() -> None: |
| 72 | + # Load input configuration. |
| 73 | + input_config = loader.load_json(config.input_path / "input_parameters.json") |
| 74 | + |
| 75 | + # Setup clients. |
| 76 | + openrouter_client, qdrant_client = setup_clients(input_config) |
| 77 | + |
| 78 | + # Setup the router. |
| 79 | + router = setup_router(openrouter_client, input_config) |
| 80 | + |
| 81 | + # Process user query. |
| 82 | + query = loader.load_txt(config.input_path / "query.txt") |
| 83 | + classification = router.route_query(query) |
| 84 | + logger.info("Queried classified.", classification=classification) |
| 85 | + |
| 86 | + if classification == "ANSWER": |
| 87 | + df_docs = pd.read_csv(config.data_path / "docs.csv", delimiter=",") |
| 88 | + logger.info("Loaded CSV Data.", num_rows=len(df_docs)) |
| 89 | + |
| 90 | + # Retrieve docs |
| 91 | + retriever = setup_retriever( |
| 92 | + qdrant_client, input_config, df_docs, collection="docs_collection" |
| 93 | + ) |
| 94 | + retrieved_docs = retriever.semantic_search(query, top_k=5) |
| 95 | + |
| 96 | + # Prepare answer |
| 97 | + responder = setup_responder(openrouter_client, input_config) |
| 98 | + answer = responder.generate_response(query, retrieved_docs) |
| 99 | + logger.info("Answer retrieved.", answer=answer) |
| 100 | + elif classification == "CLARIFY": |
| 101 | + logger.info("Your query needs clarification. Please provide more details.") |
| 102 | + elif classification == "REJECT": |
| 103 | + logger.info("Your query has been rejected as it is out of scope.") |
| 104 | + else: |
| 105 | + logger.info("Unexpected classification.", classification=classification) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments