Skip to main content
Add Bhumi as a custom tool in your LangChain agent so it can answer questions about local farms and food sourcing.

Define the tool

from langchain.tools import tool
import requests

BHUMI_API_KEY = "bh_live_your_key_here"

@tool
def search_local_farms(query: str) -> str:
    """Search for local farms by location and products.
    Input should be a JSON string with keys: lat, lng, radius (optional), products (optional).
    Example: {"lat": 45.52, "lng": -122.68, "products": "raw milk,eggs"}
    """
    import json
    params = json.loads(query)

    response = requests.get(
        "https://api.bhumifarms.co/v1/search",
        params=params,
        headers={"X-API-Key": BHUMI_API_KEY},
        timeout=10,
    )

    data = response.json()["data"]
    results = []
    for farm in data[:5]:
        results.append(
            f"- {farm['name']} ({farm.get('city', '')}, {farm.get('state', '')}) "
            f"— {farm.get('distance_mi', '?')}mi — {', '.join(farm.get('products', [])[:5])}"
        )

    return f"Found {len(data)} farms:\n" + "\n".join(results)

Use in an agent

from langchain.agents import initialize_agent, AgentType
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4")
agent = initialize_agent(
    tools=[search_local_farms],
    llm=llm,
    agent=AgentType.OPENAI_FUNCTIONS,
)

result = agent.run("Find farms with raw milk near Portland, Oregon")
print(result)

Alternative: Use the MCP server directly

If your agent framework supports MCP (like Claude), skip the REST API wrapper and connect the Bhumi MCP server directly. See MCP Server setup.