|
| 1 | +"""Pydantic-AI Tools backed by :class:`OamonitorRagProvider`. |
| 2 | +
|
| 3 | +Wraps the OAM-CH (Open Access Monitor — Switzerland) per-entity RAG |
| 4 | +search and DuckDB hydration into pydantic-ai ``Tool`` factories. The |
| 5 | +search tool accepts a free-text ``query`` plus an ``entity_type`` |
| 6 | +selector (``journals`` | ``publications`` | ``publishers`` | |
| 7 | +``organisations``); the fetch tool hydrates full records from the local |
| 8 | +DuckDB by ``_id``. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import logging |
| 14 | +from typing import TYPE_CHECKING, Any |
| 15 | + |
| 16 | +from pydantic_ai import Tool |
| 17 | + |
| 18 | +from src.v2.observation.query_log import record_query |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + from src.v2.ingest.providers.oamonitor_rag import OamonitorRagProvider |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | +_SEARCH_DESCRIPTION = ( |
| 26 | + "Semantic search over the Open Access Monitor CH (OAM-CH) index — the " |
| 27 | + "Swiss aggregator of journals, publications, publishers and organisations " |
| 28 | + "with Open Access status (gold / green / hybrid / closed) per entity. " |
| 29 | + "Use this to resolve a journal title to its ISSN + OA color, to look up " |
| 30 | + "a publication's OA status by content match, to identify the OA policy of " |
| 31 | + "a publisher, or to look up Swiss institutions catalogued in OAM. " |
| 32 | + "`entity_type` (REQUIRED) picks the collection: 'journals' (default), " |
| 33 | + "'publications', 'publishers', or 'organisations'. `filters`: optional " |
| 34 | + "allowlist dict ({entity_type, entity_id}); each value may be scalar or " |
| 35 | + "list. `rerank=true` engages the cross-encoder against the embedding text " |
| 36 | + "— useful for fine title disambiguation. Returns thin hits with " |
| 37 | + "entity_type, entity_id, embedding_text." |
| 38 | +) |
| 39 | + |
| 40 | +_FETCH_RECORDS_DESCRIPTION = ( |
| 41 | + "Hydrate one or more OAM-CH records by their upstream `_id`. " |
| 42 | + "`entity_type` (REQUIRED) selects the table: 'journals' (string id), " |
| 43 | + "'publications' (OpenAlex URL id), 'publishers' (slug), 'organisations' " |
| 44 | + "(ROR URL). Returns the full raw upstream payload (`raw`) per id — use " |
| 45 | + "after `search_oamonitor_rag` when you need fields like ISSNs, DOI, " |
| 46 | + "publisher name, country code, OA color etc. that the thin search hit " |
| 47 | + "omits." |
| 48 | +) |
| 49 | + |
| 50 | + |
| 51 | +def make_oamonitor_rag_search_tool(provider: OamonitorRagProvider) -> Tool: |
| 52 | + """Tool factory: semantic search over the OAM-CH per-entity collections.""" |
| 53 | + |
| 54 | + async def search_oamonitor_rag( |
| 55 | + query: str, |
| 56 | + entity_type: str = "journals", |
| 57 | + top_k: int = 10, |
| 58 | + filters: dict[str, Any] | None = None, |
| 59 | + rerank: bool = False, # noqa: FBT001, FBT002 — part of the LLM tool signature |
| 60 | + ) -> list[dict[str, Any]]: |
| 61 | + """Vector search the OAM-CH index. See tool description.""" |
| 62 | + logger.info( |
| 63 | + "tool call: search_oamonitor_rag — entity=%s top_k=%d rerank=%s " |
| 64 | + "filters=%r query=%r", |
| 65 | + entity_type, top_k, rerank, filters, query, |
| 66 | + ) |
| 67 | + record_query(service="oamonitor.rag.search", query=query) |
| 68 | + return await provider.search( |
| 69 | + query, |
| 70 | + entity_type=entity_type, |
| 71 | + top_k=top_k, |
| 72 | + filters=filters, |
| 73 | + rerank=rerank, |
| 74 | + ) |
| 75 | + |
| 76 | + return Tool( |
| 77 | + search_oamonitor_rag, |
| 78 | + name="search_oamonitor_rag", |
| 79 | + description=_SEARCH_DESCRIPTION, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def make_oamonitor_rag_fetch_records_tool(provider: OamonitorRagProvider) -> Tool: |
| 84 | + """Tool factory: hydrate OAM-CH records by `_id` for a given entity table.""" |
| 85 | + |
| 86 | + async def fetch_oamonitor_records( |
| 87 | + ids: list[str], |
| 88 | + entity_type: str, |
| 89 | + ) -> list[dict[str, Any]]: |
| 90 | + """Fetch full OAM-CH records by id. See tool description.""" |
| 91 | + logger.info( |
| 92 | + "tool call: fetch_oamonitor_records — entity=%s ids=%d", |
| 93 | + entity_type, len(ids), |
| 94 | + ) |
| 95 | + record_query( |
| 96 | + service="oamonitor.rag.fetch_records", |
| 97 | + query=",".join(str(i) for i in ids[:10]), |
| 98 | + ) |
| 99 | + return await provider.fetch_records(ids, entity_type=entity_type) |
| 100 | + |
| 101 | + return Tool( |
| 102 | + fetch_oamonitor_records, |
| 103 | + name="fetch_oamonitor_records", |
| 104 | + description=_FETCH_RECORDS_DESCRIPTION, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +__all__ = [ |
| 109 | + "make_oamonitor_rag_fetch_records_tool", |
| 110 | + "make_oamonitor_rag_search_tool", |
| 111 | +] |
0 commit comments