---
title: LangChain Integration
description: Use PostGrad as a knowledge source in your LangChain agents.
---

import { Callout } from 'fumadocs-ui/components/callout';

## Overview

PostGrad can serve as a retriever or tool in your LangChain agent, giving it access to structured business knowledge extracted from real meetings.

## Using the PostGrad SDK (Coming Soon)

Once the Python SDK is released, you can wrap it as a LangChain tool:

```python
from langchain.tools import tool
from postgrad import PostGrad

client = PostGrad(api_key="pg_live_YOUR_API_KEY")

@tool
def search_business_knowledge(query: str) -> str:
    """Search PostGrad for structured business knowledge on a topic."""
    results = client.search(query, limit=5)
    return "\n\n".join(
        f"**{e.title}** (confidence: {e.confidence})\n{e.content}"
        for e in results.data
    )
```

### Complete Agent Example

```python
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate

llm = ChatOpenAI(model="gpt-4o")

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a business strategy advisor with access to real operator knowledge."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, [search_business_knowledge], prompt)
executor = AgentExecutor(agent=agent, tools=[search_business_knowledge])

result = executor.invoke({"input": "What pricing strategies work for AI consulting?"})
print(result["output"])
```

## Using the REST API Directly

<Callout type="info">
This approach works today without waiting for the SDK.
</Callout>

```python
import requests
from langchain.tools import tool

API_KEY = "pg_live_YOUR_API_KEY"

@tool
def search_business_knowledge(query: str) -> str:
    """Search PostGrad for structured business knowledge on a topic."""
    response = requests.get(
        "https://postgrad.io/api/v1/knowledge/search",
        headers={
            "Authorization": f"Bearer {API_KEY}",
            # "all" fans out across every feed the user is subscribed to
            # and merges results by relevance. Best default for an agent
            # tool because the LLM doesn't know which feed to pick.
            "X-PostGrad-Feed": "all",
        },
        params={"q": query, "limit": 5},
    )
    response.raise_for_status()
    data = response.json()

    return "\n\n".join(
        f"**{e['title']}** (from {e.get('feed_name', 'unknown feed')}, confidence: {e['confidence']})\n{e['content']}"
        for e in data["data"]
    )
```

## As a LangChain Retriever

For RAG pipelines, you can implement a custom retriever:

```python
from langchain_core.retrievers import BaseRetriever
from langchain_core.documents import Document
import requests


class PostGradRetriever(BaseRetriever):
    api_key: str
    base_url: str = "https://postgrad.io/api/v1"

    def _get_relevant_documents(self, query: str) -> list[Document]:
        response = requests.get(
            f"{self.base_url}/knowledge/search",
            headers={
                "Authorization": f"Bearer {self.api_key}",
                "X-PostGrad-Feed": "all",  # merge results across all subscribed feeds
            },
            params={"q": query, "limit": 10},
        )
        response.raise_for_status()
        data = response.json()

        return [
            Document(
                page_content=entry["content"],
                metadata={
                    "title": entry["title"],
                    "category": entry["category"],
                    "confidence": entry["confidence"],
                    "feed_name": entry.get("feed_name"),
                    "feed_id": entry.get("feed_id"),
                },
            )
            for entry in data["data"]
        ]


retriever = PostGradRetriever(api_key="pg_live_YOUR_API_KEY")
docs = retriever.invoke("deal evaluation criteria")
```
