LangChain – Quick Guide

Table Of Contents:

  1. Quick Start
  2. Build A Basic Agent
  3. Building A Real World Agent.

(1) Quickstart

pip install -U langchain deepagents
export OPENAI_API_KEY = "your-api-key"

(2) Building A Basic Agent

from langchain.agents import create_agent

def get_weather(city:str)->str:
    """Get Weather For A Given City"""
    return f"It's Always Sunny In {city}!"
    
agent = create_agent(
        model = "openai:gpt-5.5",
        tools = [get_weather],
        system_prompt = "You Are A Helpful Assistant",
        )

result = agent.invoke(
         {"message":[{"role":"user", "content":"What Is The Weather In Bhubaneswar"}]}
         )

print(result["message"][-1].content_blocks)

(3) Building A Real World Agent

SYSTEM_PROMPT = """
You are a literary data assistant.

IMPORTANT RULES:

1. If a user provides a URL, ALWAYS call fetch_text_from_url.
2. Never say you cannot access URLs.
3. Never answer questions about a URL from memory.
4. Before answering any question about a URL:
   - call fetch_text_from_url(url)
   - read the returned text
   - then answer.
5. The fetch_text_from_url tool is your ONLY way to access URL content.

Available tools:

fetch_text_from_url(url: str)
    Fetches text from a URL.

You MUST use the tool whenever a URL is present.
"""
import urllib.error
import urllib.request

from langchan.tools import tool
from langchain.chat_models import init_chat_model
from langgraph.checkpoint.memory import InMemorySaver

@tool
def fetch_text_from_url(url:str)-> str:
    """Fetch the document from a URL.
    """
    req = urllib.request.Request(
        url,
        headers={"User-Agent": "Mozilla/5.0 (compatible; quickstart-research/1.0)"},
    )
    try:
        with urllib.request.urlopen(req, timeout=120) as resp:
            raw = resp.read()
    except urllib.error.URLError as e:
        return f"Fetch failed: {e}"
    text = raw.decode("utf-8", errors="replace")
    return text
model = init_chat_model(
        "ollama:qwen3:0.6b",
        temperature=0.5,
        timeout=300,
        max_token=25000,
        )
from langgraph.checkpoint.memory import InMemorySaver
checkpointer = InMemorySaver()

from langchain.agents import create_agent
from langchain.deep_agents import create_deep_agent
agent = create_agent(
        model = model,
        tools = [fetch_text_from_url],
        system_prompt = SYSTEM_PROMPT
        )
deep_agent = create_deep_agent(
              model = model,
              tools = [fetch_text_from_url],
              system_prompt = SYSTEM_PROMPT,
    )
agent_result = agent.invoke(
    {"messages": [{"role": "user", "content": content}]},
    config={"configurable": {"thread_id": "great-gatsby-lc"}},
)
deep_agent_result = deep_agent.invoke(
    {"messages": [{"role": "user", "content": content}]},
    config={"configurable": {"thread_id": "great-gatsby-da"}},
)
print(agent_result["messages"][-1].content_blocks)

Leave a Reply

Your email address will not be published. Required fields are marked *