Retrieve similar environments using FAISS similarity search. Args: query: Query string to search for k: Number of results to return (default: 4) Returns: List of dictionaries containing environment information with similarity
(self, query: str, k: int = 4)
| 1197 | return contract_text |
| 1198 | |
| 1199 | async def retrieve(self, query: str, k: int = 4) -> List[Dict[str, Any]]: |
| 1200 | """Retrieve similar environments using FAISS similarity search. |
| 1201 | |
| 1202 | Args: |
| 1203 | query: Query string to search for |
| 1204 | k: Number of results to return (default: 4) |
| 1205 | |
| 1206 | Returns: |
| 1207 | List of dictionaries containing environment information with similarity scores |
| 1208 | """ |
| 1209 | if self._faiss_service is None: |
| 1210 | logger.warning("| ⚠️ FAISS service not initialized, cannot retrieve environments") |
| 1211 | return [] |
| 1212 | |
| 1213 | try: |
| 1214 | from src.environment.faiss.types import FaissSearchRequest |
| 1215 | |
| 1216 | request = FaissSearchRequest( |
| 1217 | query=query, |
| 1218 | k=k, |
| 1219 | fetch_k=k * 5 # Fetch more candidates before filtering |
| 1220 | ) |
| 1221 | |
| 1222 | result = await self._faiss_service.search_similar(request) |
| 1223 | |
| 1224 | if not result.success: |
| 1225 | logger.warning(f"| ⚠️ FAISS search failed: {result.message}") |
| 1226 | return [] |
| 1227 | |
| 1228 | # Extract documents and scores from result |
| 1229 | documents = [] |
| 1230 | if result.extra and "documents" in result.extra: |
| 1231 | docs = result.extra["documents"] |
| 1232 | scores = result.extra.get("scores", []) |
| 1233 | |
| 1234 | for doc, score in zip(docs, scores): |
| 1235 | # Extract environment name from metadata |
| 1236 | metadata = doc.get("metadata", {}) if isinstance(doc, dict) else {} |
| 1237 | env_name = metadata.get("name", "") |
| 1238 | |
| 1239 | # Get environment config if available |
| 1240 | env_config = None |
| 1241 | if env_name and env_name in self._environment_configs: |
| 1242 | env_config = self._environment_configs[env_name] |
| 1243 | |
| 1244 | documents.append({ |
| 1245 | "name": env_name, |
| 1246 | "description": metadata.get("description", ""), |
| 1247 | "score": float(score), |
| 1248 | "content": doc.get("page_content", "") if isinstance(doc, dict) else str(doc), |
| 1249 | "config": env_config.model_dump() if env_config else None |
| 1250 | }) |
| 1251 | |
| 1252 | return documents |
| 1253 | |
| 1254 | except Exception as e: |
| 1255 | logger.error(f"| ❌ Error retrieving environments: {e}") |
| 1256 | return [] |
nothing calls this directly
no test coverage detected