Spaces:
Sleeping
Sleeping
Download debug_retrieval.py from bagustenan/backend: direct link, hf CLI and curl.
- Browser
- Download file 2.38 kB
-
https://huggingface.co/spaces/bagustenan/backend/resolve/main/debug_retrieval.py
- Command line
-
hf download hf://spaces/bagustenan/backend/debug_retrieval.py
-
curl -L -o debug_retrieval.py https://huggingface.co/spaces/bagustenan/backend/resolve/main/debug_retrieval.py
2.38 kB
| import os | |
| import google.generativeai as genai | |
| # import google.genai as genai | |
| from qdrant_client import QdrantClient | |
| from dotenv import load_dotenv | |
| from typing import List | |
| # Load env vars | |
| load_dotenv() | |
| # Setup API Keys | |
| GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") | |
| QDRANT_URL = os.getenv("QDRANT_URL") | |
| QDRANT_API_KEY = os.getenv("QDRANT_API_KEY") | |
| print(f"Connecting to Qdrant at {QDRANT_URL}...") | |
| client = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY) | |
| # Check collection info | |
| try: | |
| info = client.get_collection("inda_collection") | |
| print(f"Collection Status: {info.status}") | |
| print(f"Points Count: {info.points_count}") | |
| print(f"Vectors Config: {info.config.params.vectors}") | |
| except Exception as e: | |
| print(f"Error getting collection info: {e}") | |
| exit() | |
| # Custom Embedding Logic (Same as in tools.py) | |
| class GoogleGenAIEmbeddingsCustom: | |
| def __init__(self, api_key, model="models/gemini-embedding-001", output_dimensionality=None): | |
| genai.configure(api_key=api_key) | |
| self.model = model | |
| self.output_dimensionality = output_dimensionality | |
| def embed_query(self, text: str) -> List[float]: | |
| print(f"Generating embedding for query: '{text}'...") | |
| result = genai.embed_content( | |
| model=self.model, | |
| content=text, | |
| task_type="retrieval_query", | |
| output_dimensionality=self.output_dimensionality | |
| ) | |
| return result['embedding'] | |
| # Initialize Embeddings | |
| embeddings = GoogleGenAIEmbeddingsCustom( | |
| api_key=GOOGLE_API_KEY, | |
| model="models/gemini-embedding-001", | |
| output_dimensionality=1536 | |
| ) | |
| # Test Query | |
| query_text = "wisatawan mancanegara" | |
| try: | |
| query_vector = embeddings.embed_query(query_text) | |
| print(f"Query Embedding Dimension: {len(query_vector)}") | |
| if len(query_vector) != 1536: | |
| print("CRITICAL ERROR: Dimension mismatch! Expected 1536.") | |
| else: | |
| print("Dimension check PASSED.") | |
| # Perform Search | |
| print("\nSearching in Qdrant...") | |
| search_result = client.search( | |
| collection_name="inda_collection", | |
| query_vector=query_vector, | |
| limit=5 | |
| ) | |
| print(f"Found {len(search_result)} results:") | |
| for hit in search_result: | |
| print(f"\nScore: {hit.score}") | |
| print(f"Payload: {hit.payload}") | |
| except Exception as e: | |
| print(f"Error during search test: {e}") | |