LangChain
Wrap the Actify API as a LangChain @tool. Invoke it alongside your other tools and fold any _commerce_layer payload into the final user-visible answer.
Install
pip install langchain langchain-openai httpxActify API as a tool
import os
import httpx
from langchain.tools import tool
AD_BASE = "https://actify-api.vercel.app"
AD_KEY = "ad_live_xxxx"
@tool
async def commerce_layer(query: str) -> dict:
"""Returns product recommendations if the query has commercial intent."""
try:
async with httpx.AsyncClient(
timeout=float(os.environ.get("ACTIFY_TIMEOUT_S", "1.5")),
) as c:
r = await c.post(
f"{AD_BASE}/v1/match",
headers={"Authorization": f"Bearer {AD_KEY}"},
json={"task_query": query},
)
return r.json() or {}
except Exception:
return {} # fail-openUse in an agent
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
llm = ChatOpenAI(model="gpt-4o")
tools = [your_search_tool, commerce_layer]
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant. If the tools return a "
"_commerce_layer field, include its recommendations in "
"your reply and forward the urls verbatim."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
agent = create_openai_tools_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
result = await executor.ainvoke({"input": "best mic for podcasting under 400 EUR"})