Langchain Model Advanced
This section introduces two advanced features of Chat Model: `bind_tools()` (tool binding) and `with_structured_output()` (structured output). They are the foundation for Agents and structured data extraction.\\n\\n* * *\\n\\n## bind_tools() β Let the model know which tools are available\\n\\nOrdinary models can only generate text. But after calling `bind_tools()`, the model can return `tool_call` in its response, telling the program "I need to call this tool".\\n\\n## Example\\n\\n```python\\nfrom dotenv import load_dotenv\\n\\nload_dotenv()\\n\\nfrom langchain.chat_models import init_chat_model\\n\\nmodel = init_chat_model("deepseek:deepseek-vModel direct reply-flash", temperature=0)\\n\\n# Describe tools using dictionaries (OpenAI function calling format)\\ntools = [\\n {\\n "type": "function",\\n "function": {\\n "name": "get_weather",\\n "description": "Query the weather of a specified city",\\n "parameters": {\\n "type": "object",\\n "properties": {\\n "city": {\\n "type": "string",\\n "description": "City name, such as Hangzhou, Beijing"\\n }\\n },\\n "required": \\n }\\n }\\n }\\n]\\n\\n# bind_tools() binds tools to the model\\n# The model now "knows" that get_weather is available\\nmodel_with_tools = model.bind_tools(tools)\\n\\n# Ask a question that requires a tool\\nresponse = model_with_tools.invoke("HangzhouHow is the weather today?")\\n\\n# Check if the model requested a tool call\\nif response.tool_calls:\\n print("The model requests to call the following tools:")\\n for tc in response.tool_calls:\\n print(f" Tool name: {tc['name']}")\\n print(f" Parameter: {tc['args']}")\\n print(f" Call ID: {tc['id']}")\\nelse:\\n print(f"Model direct reply: {response.content}")\\n\\nOutput:\\n\\nThe model requests to call the following tools:\\n Tool name: get_weather\\n Parameter: {'city': 'Hangzhou'}\\n Call ID: call_abcPerform mathematical calculationsCall IDThe mathematical expression to calculate, such asdefModel direct replyQuery the weather condition of a specified cityTool name\\n\\n> Note that the model does not actually execute the `get_weather` function. `bind_tools()` only tells the model "you have a tool available", and the model returns a request to call the tool. The actual execution is completed by the Agent or code you write yourself.\\n\\n* * *\\n\\n## Describing tools with Pydantic models\\n\\nFor complex tools, using Pydantic models to define parameter structures is clearer than writing dictionaries manually:\\n\\n## Example\\n\\n```python\\nfrom pydantic import BaseModel, Field\\nfrom langchain.chat_models import init_chat_model\\n\\n# Define tool parameter structures using Pydantic\\nclass WeatherInput(BaseModel):\\n """Query the weather condition of a specified city"""\\n city: str = Field(description="City name, such as Hangzhou, Beijing")\\n unit: str = Field(\\n default="celsius",\\n description="Temperature unit, celsius or fahrenheit"\\n )\\n\\nclass CalculatorInput(BaseModel):\\n """Perform mathematical calculations"""\\n expression: str = Field(\\n description="Mathematical expression to calculate, such as '(The mathematical expression to calculate, such as + Query the weather condition of a specified city) * Call ID'"\\n )\\n\\nmodel = init_chat_model("deepseek:deepseek-vModel direct reply-flash", temperature=0)\\n\\n# Pass Pydantic models, LangChain automatically converts to tool descriptions\\nmodel_with_tools = model.bind_tools([WeatherInput, CalculatorInput])\\n\\n# Test complex scenario\\nresponse = model_wi
YouTip