ChatGLM3 Tool Invocation Prompt Template in LangChain Architecture

This article explores the prompt template structure for ChatGLM3 within the LangChain framework, focusing on how the model handles tool execution and knowledge-based responses.

Prompt Template Definition

The following configuration defines the instruction set for the ChatGLM3 agent when processing user requests:

PROMPT_TEMPLATES["agent_chat"] = { "ChatGLM3": """ You may respond using available tools or provide answers directly from your knowledge base. Respond to the user as helpfully and accurately as possible.

You have access to the following tools:
{tools}

To invoke a tool, construct a JSON object containing:
- "action": the tool name
- "action_input": the parameters to pass

Valid "action" values: "Final Answer" or [list of available tools]

Provide only ONE action per JSON blob, as demonstrated below:

{{{{
  "action": $TOOL_NAME,
  "action_input": $INPUT
}}}}

Follow this structured format:

Question: the user query to answer Thought: analyze the reasoning steps Action:

$JSON_BLOB

Observation: the result returned by the tool ... (repeat Thought/Action/Observation as needed) Thought: ready to provide final response Action:

{{{{
  "action": "Final Answer",
  "action_input": "Final response to user"
}}}}

Begin! Always respond witth a valid JSON blob containing a single action. Use tools when necessary; respond directly when appropriate.

history: {history}

Question: {input}

Thought: {agent_scratchpad}

"""
}


Template Analysis

This prompt template instructs the ChatGLM3 model on how to interact with external tools and provide responses within a structured workflow. The template ensures consistent format adherence and logical processing of user queries.

Component Breakdown

Response Flexibility: The model is instructed that it can either utilize tools to retrieve information or respond directly using its trained knowledge. This dual capability allows for flexible handling of different query types.

Tool Access Specification: The {tools} placeholder gets replaced with actual tool definitions during execution, giving the model awareness of available functions.

Tool Invocation Protocol

When the model decides to use a tool, it must construct a JSON payload with two required fields:

{
  "action": "tool_function_name",
  "action_input": {"param1": "value1", "param2": "value2"}
}


The "action" field accepts either "Final Answer" for concluding the interaction or a specific tool name from the available set.

Processing Pipeline

Each query follows this iterative workflow:


Question: Displays the current input requiring resolution
Thought: Contains the model's reasoning about the problem
Action: Contains the JSON blob specifying tool execution or final response
Observation: Displays results from tool execution


This cycle repeats until the model determines it has sufficient information to provide a final answer.

Final Answer Submission

When ready to conclude, the model outputs:

{
  "action": "Final Answer",
  "action_input": "The complete response to the user"
}


Context Management

The template includes {history} for maintaining conversation context across multiple exchanges, allowing the model to understand prior interactions. During execution, {input} gets substituted with the current user question, and {agent_scratchpad} contains the model's internal reasoning trace.

Implementation Pattern

This structured prompting approach enables ChatGLM3 to systematically determine whether external tool invocation is necessary, execute appropriate functions, and synthesize coherent responses based on both tool outputs and internal knowledge.

Tags: LangChain ChatGLM3 LLM prompt-engineering tool-calling

Posted on Tue, 09 Jun 2026 17:29:02 +0000 by shaundunne