|
| 1 | +"""LangChain v1 style weather agent example. |
| 2 | +https://docs.langchain.com/oss/python/langchain-quickstart |
| 3 | +
|
| 4 | +This example mirrors the pattern from the LangChain v1 Quickstart docs, |
| 5 | +adapted to this repo's multiple-provider model configuration. |
| 6 | +""" |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import os |
| 10 | +from dataclasses import dataclass |
| 11 | + |
| 12 | +import azure.identity |
| 13 | +from dotenv import load_dotenv |
| 14 | +from langchain.agents import create_agent |
| 15 | +from langchain_core.runnables import RunnableConfig |
| 16 | +from langchain_core.tools import tool |
| 17 | +from langchain_openai import AzureChatOpenAI, ChatOpenAI |
| 18 | +from langgraph.checkpoint.memory import InMemorySaver |
| 19 | +from langgraph.runtime import get_runtime |
| 20 | +from rich import print |
| 21 | + |
| 22 | +load_dotenv(override=True) |
| 23 | +API_HOST = os.getenv("API_HOST", "github") |
| 24 | + |
| 25 | +if API_HOST == "azure": |
| 26 | + token_provider = azure.identity.get_bearer_token_provider( |
| 27 | + azure.identity.DefaultAzureCredential(), |
| 28 | + "https://cognitiveservices.azure.com/.default", |
| 29 | + ) |
| 30 | + model = AzureChatOpenAI( |
| 31 | + azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"), |
| 32 | + azure_deployment=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"), |
| 33 | + openai_api_version=os.environ.get("AZURE_OPENAI_VERSION"), |
| 34 | + azure_ad_token_provider=token_provider, |
| 35 | + ) |
| 36 | +elif API_HOST == "github": |
| 37 | + model = ChatOpenAI( |
| 38 | + model=os.getenv("GITHUB_MODEL", "gpt-4o"), |
| 39 | + base_url="https://models.inference.ai.azure.com", |
| 40 | + api_key=os.environ.get("GITHUB_TOKEN"), |
| 41 | + ) |
| 42 | +elif API_HOST == "ollama": |
| 43 | + model = ChatOpenAI( |
| 44 | + model=os.environ.get("OLLAMA_MODEL", "llama3.1"), |
| 45 | + base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"), |
| 46 | + api_key="none", |
| 47 | + ) |
| 48 | +else: |
| 49 | + model = ChatOpenAI(model=os.getenv("OPENAI_MODEL", "gpt-4o-mini")) |
| 50 | + |
| 51 | + |
| 52 | +system_prompt = """You are an expert weather forecaster, who speaks in puns. |
| 53 | +
|
| 54 | +You have access to two tools: |
| 55 | +
|
| 56 | +- get_weather_for_location: use this to get the weather for a specific location |
| 57 | +- get_user_location: use this to get the user's location |
| 58 | +
|
| 59 | +If a user asks you for the weather, make sure you know the location. |
| 60 | +If you can tell from the question that they mean whereever they are, |
| 61 | +use the get_user_location tool to find their location.""" |
| 62 | + |
| 63 | +# Mock user locations keyed by user id (string) |
| 64 | +USER_LOCATION = { |
| 65 | + "1": "Florida", |
| 66 | + "2": "SF", |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +@dataclass |
| 71 | +class UserContext: |
| 72 | + user_id: str |
| 73 | + |
| 74 | + |
| 75 | +@tool |
| 76 | +def get_weather(city: str) -> str: |
| 77 | + """Get weather for a given city.""" |
| 78 | + return f"It's always sunny in {city}!" |
| 79 | + |
| 80 | + |
| 81 | +@tool |
| 82 | +def get_user_info(config: RunnableConfig) -> str: |
| 83 | + """Retrieve user information based on user ID.""" |
| 84 | + runtime = get_runtime(UserContext) |
| 85 | + user_id = runtime.context.user_id |
| 86 | + return USER_LOCATION[user_id] |
| 87 | + |
| 88 | + |
| 89 | +@dataclass |
| 90 | +class WeatherResponse: |
| 91 | + conditions: str |
| 92 | + punny_response: str |
| 93 | + |
| 94 | + |
| 95 | +checkpointer = InMemorySaver() |
| 96 | + |
| 97 | +agent = create_agent( |
| 98 | + model=model, |
| 99 | + prompt=system_prompt, |
| 100 | + tools=[get_user_info, get_weather], |
| 101 | + response_format=WeatherResponse, |
| 102 | + checkpointer=checkpointer, |
| 103 | +) |
| 104 | + |
| 105 | + |
| 106 | +def main(): |
| 107 | + config = {"configurable": {"thread_id": "1"}} |
| 108 | + context = UserContext(user_id="1") |
| 109 | + |
| 110 | + r1 = agent.invoke( |
| 111 | + {"messages": [{"role": "user", "content": "what is the weather outside?"}]}, config=config, context=context |
| 112 | + ) |
| 113 | + print(r1.get("structured_response")) |
| 114 | + |
| 115 | + r2 = agent.invoke( |
| 116 | + {"messages": [{"role": "user", "content": "Thanks"}]}, |
| 117 | + config=config, |
| 118 | + context=context, |
| 119 | + ) |
| 120 | + print(r2.get("structured_response")) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments