Chain of Thought & ReAct

Source notebook

Repo path: gemini/prompts/examples/chain_of_thought_react.ipynb · Open on GitHub · advanced

Demonstrates CoT prompting and ReAct agents with Vertex AI, LangChain, Wikipedia, and BigQuery.

Summary

This notebook teaches chain-of-thought prompting patterns, including one-shot reasoning, zero-shot step-by-step prompts, self-consistency, and JSON reasoning. It then demonstrates ReAct agents that combine reasoning with tools, starting with a current-date Python function and Wikipedia search, then extending to BigQuery Hacker News comments with custom LangChain tools.

Key code patterns

Initialize Vertex AI and LangChain LLM

PROJECT_ID = ""
LOCATION = ""
MODEL_NAME = "gemini-2.0-flash"
 
import vertexai
vertexai.init(project=PROJECT_ID, location=LOCATION)
 
from langchain_google_vertexai import VertexAI
llm = VertexAI(model_name=MODEL_NAME, max_output_tokens=1000)

Sets the Google Cloud project, region, and Gemini model used for subsequent LangChain calls.

Zero-shot CoT prompt

question = """
Q: The cafeteria had 23 apples.
If they used 20 to make lunch and bought 6 more, how many apples do they have?
A: Let's think step by step.
"""
print(llm.invoke(question))

Adds a step-by-step instruction so the model produces intermediate reasoning before the answer.

Self-consistency chain

planner = PromptTemplate.from_template(context + one_shot_exemplar + " {input}") | VertexAI() | StrOutputParser()
answer_1 = PromptTemplate.from_template("{base_response} A:") | VertexAI(temperature=0) | StrOutputParser()
answer_2 = PromptTemplate.from_template("{base_response} A:") | VertexAI(temperature=0.3) | StrOutputParser()
answer_3 = PromptTemplate.from_template("{base_response} A:") | VertexAI(temperature=0.5) | StrOutputParser()

Generates multiple reasoning completions with different temperatures so the most common answer can be selected.

Structured ReAct agent

t_get_current_date = StructuredTool.from_function(get_current_date)
tools = [t_get_current_date]
agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)
agent.invoke("What's today's date?")

Wraps a Python function as a LangChain tool so the agent can act on information outside the model.

BigQuery custom tools

bq = bigquery.Client(project=PROJECT_ID)
 
def get_comment_by_id(id: str) -> str:
    QUERY = "SELECT text FROM bigquery-public-data.hacker_news.full WHERE ID = {id} LIMIT 1".format(id=id)
    df = bq.query(QUERY).to_dataframe()
    return df["text"].values.tolist()[0]

Uses BigQuery query results as tool outputs that a ReAct agent can reason over.

Models & APIs used

  • Models: gemini-2.0-flash
  • APIs / services: Vertex AI, BigQuery
  • SDKs / libraries: vertexai, langchain, langchain-google-vertexai, google-cloud-aiplatform, google-cloud-bigquery, langchain-experimental, wikipedia, bigframes

When to use this

Use this pattern when a Gemini application needs explicit multi-step prompting or LangChain ReAct tool use over external systems such as Wikipedia or BigQuery.

Gotchas & caveats

  • Requires an existing Google Cloud project and Vertex AI API enabled.
  • Colab requires explicit user authentication with google.colab.auth.authenticate_user().
  • PROJECT_ID and LOCATION must be set before vertexai.init().
  • Self-consistency makes multiple LLM calls, increasing cost.
  • The notebook pins specific package versions such as langchain0.3.0 and google-cloud-aiplatform1.67.1.
  • The custom BigQuery agent validates exactly six tools by name.

Best practices

  • Use one-shot exemplars to show the model the desired reasoning format.
  • Append “Let’s think step by step.” for zero-shot chain-of-thought reasoning.
  • Use self-consistency by generating multiple candidate answers and selecting the most popular result.
  • Avoid nested ReAct tool calls; parse work into separate actions.
  • Use external tools when the model lacks current or external information.