跳到正文
DEV Community· Sudhir Sharma·· 3 小時前AI 評分43

為 AI Agent 建立 PyPI 記憶:如何為 Cognee 開發 Python 套件連接器

Giving AI Agents Memory of PyPI: Building a Python Package Connector for Cognee

AI 導讀

WeMakeDevs 與 Cognee 為 Cognee 開發 PyPI 連接器,從 PyPI 公開 API 擷取套件資料,加入其記憶層並建立知識圖譜。連接器可擷取套件中繼資料、依賴關係及 README 與 API 概覽,無需下載套件檔案,亦不需外部 API 金鑰。項目以 9 項測試驗證,涵蓋套件查詢、缺失套件處理及 Markdown 正規化。

正文

Sudhir Sharma

When building AI coding assistants or developer agents, one of the biggest challenges is hallucination of library APIs, outdated functions, and deprecated parameters. Large Language Models frequently invent methods that don’t exist or assume library versions from their pre-training cutoffs.

To solve this, we built a native PyPI Data-Source Connector for Cognee (the open-source memory engine and knowledge graph for AI agents) as part of Mergetober 2026.


The Architecture: How It Works

The connector interfaces directly with the official, public PyPI JSON REST API (https://pypi.org/pypi/{package}/json) to ingest live specifications into Cognee’s semantic and graph memory layers:

  1. Metadata Ingestion: Extracts package summaries, licenses, author records, and official repository/documentation URLs.
  2. Dependency Extraction: Parses requires_dist so the agent understands the exact library dependency graph without downloading wheels locally.
  3. Structured Markdown Generation: Normalizes the package's complete README and API overview into clean markdown chunks.
  4. Knowledge Graph Construction: When ingested via cognee.add() and cognee.cognify(), Cognee extracts entity nodes for libraries, functions, and requirements, linking them deterministically in the graph.

Code Walkthrough

Here is how simple it is to use the connector:

import asyncio
import cognee
from cognee_community_connector_pypi import PyPIConnector

async def main():
    connector = PyPIConnector()

    # 1. Fetch live specifications for critical dependencies
    packages = ["cognee", "fastapi", "pydantic"]
    documents = connector.fetch_documents(packages)

    # 2. Ingest into Cognee memory
    for doc in documents:
        await cognee.add(doc["text"])

    # 3. Build knowledge graph & embeddings
    await cognee.cognify()

    # 4. Query the memory graph
    results = await cognee.search("What are the key dependencies and license of fastapi?")
    print(results)

if __name__ == "__main__":
    asyncio.run(main())

Robustness & Testing

The connector is built for production reliability:

  • Zero External API Keys: Connects directly to PyPI's public endpoints.
  • Resilient Error Handling: Gracefully distinguishes between 404 missing packages and server transport errors.
  • 100% Test Coverage: Verified with a comprehensive 9-test suite covering single package lookup, missing package resilience, markdown normalization, and batch streaming generators.

Links & Contributions

來源:DEV Community · dev.to