main.go
Overview
The main.go file serves as the entry point for an AI agent application focused on answering questions specifically about the time and weather in a given city. It initializes and configures a Gemini-based language model, constructs an LLM agent with a defined purpose and tools, and then launches the agent within a command-line interface framework. The file integrates several core components from the AI agent framework and tool system, orchestrating their collaboration to provide a focused question-answering agent.
Detailed Explanation
Package and Imports
The package is
main, indicating this file is an executable entrypoint.Imports include standard libraries (context, log, os) and multiple modules from Google’s AI Development Kit (ADK) and Gemini model ecosystem:
genaifor general AI client configurations.agentand llmagent for agent creation and lifecycle management.launcherand full for command-line launching infrastructure.model/gemini for Gemini model instantiation.
tooland geminitool for integrating tools like Google Search into the agent.
Function: main
The main function orchestrates the full lifecycle from model creation through agent setup to launch execution.
Steps:
Context Creation
ctx := context.Background()Creates a base context for managing lifecycle and cancellation signals.
Model Initialization
Calls gemini.NewModel with:
Context
Model name "gemini-2.5-flash"
genai.ClientConfigconfigured with an API key from environment variable GOOGLE_API_KEY.
Returns a
modelinstance or logs a fatal error if failed.
Purpose: Connects to the Gemini LLM backend with authentication and model selection.
Agent Creation
Uses llmagent.New with llmagent.Config:
Model: the previously created Gemini modelDescription: Explains agent’s focus on time and weather questions.
Instruction: Defines a strict rule that the agent only answers time and weather-related queries, refusing others.
Tools: Initializes with the geminitool.GoogleSearch tool, enabling web search capability.
Returns an agent instance
aor logs a fatal error.
Key Points:
The agent is a specialized LLM agent configured to interact with a single Gemini model.
The instruction acts as a guardrail to constrain agent behavior.
Integration of
GoogleSearchtool allows the agent to augment model knowledge with real-time search results.
Launcher Configuration
Defines a launcher.Config with an AgentLoader that loads the single created agent.
This config prepares the launcher to run the agent in an interactive or scripted CLI environment.
Launcher Invocation
Creates a new launcher instance using full.NewLauncher().
Executes the launcher with the context, config, and command-line arguments (os.Args[1:]).
Logs fatal error with command-line usage details if launcher execution fails.
Important Implementation Details
Agent Instruction Enforcement:
The Instruction in the agent config ensures the agent’s behavior is explicitly limited to answering only time and weather questions. This leverages the instruction injection and prompt engineering features of the LLM agent system to restrict unintended responses. (Related to Instruction Template Processing and Instruction Injection)Tool Integration:
The agent includes theGoogleSearchtool (geminitool.GoogleSearch), a Gemini-native tool enabling web search. This augments the agent’s ability to retrieve up-to-date information beyond the static model knowledge. This integration is a practical example of the Tooling System and specifically the Google Search Tool.Launcher Usage:
The launcher pattern abstracts the runtime environment setup, command-line parsing, and agent invocation. full.NewLauncher() and launcher.Config facilitate running the agent in various CLI contexts, referencing the REST API and Web Launchers topic for broader launcher patterns.Model Configuration:
The Gemini model is instantiated with credentials and a specific version, highlighting secure and flexible model selection through environment variables and configuration objects.
Interactions with Other System Components
Gemini Model (Model Layer): The Gemini model provides LLM capabilities. The model instance is wrapped and managed within the agent lifecycle. (See LLM Integration and Agents)
LLM Agent Framework: The agent created using llmagent.New encapsulates the model, instructions, and tools, managing execution and response generation. (See LLM Agent Configuration)
Tool System: The Google Search tool is integrated as part of the agent’s toolkit to retrieve external information dynamically. (See Agent Tools, Tooling System)
Launcher Framework: The launcher executes the agent interactively or via scripts, handling CLI argument parsing, lifecycle, and error reporting. (See REST API and Web Launchers)
Environment Configuration: The API key is sourced from environment variables, connecting runtime configuration to secure credential management.
Usage Example
Assuming the environment variable GOOGLE_API_KEY is set with a valid API key, running the compiled binary starts the agent launcher. The user can then input queries related to city weather or time, and the agent will respond accordingly, refusing unrelated questions.
Example CLI interaction:
$ ./weather_time_agent
> What is the current time in New York?
[Agent responds with accurate time]
> What's the capital of France?
[Agent refuses to answer as it is unrelated to time or weather]
Mermaid Diagram
flowchart TD
main["main() Function"]
ctx["Create Context"]
model["Initialize Gemini Model"]
agent["Create LLM Agent"]
config["Prepare Launcher Config"]
launcher["Instantiate Launcher"]
execute["Execute Launcher"]
main --> ctx
main --> model
main --> agent
main --> config
main --> launcher
main --> execute
model -->|Uses| genai["genai.ClientConfig"]
agent -->|Uses| model
agent -->|Includes| tools["GoogleSearch Tool"]
config -->|Loads| agent
launcher --> config
execute --> launcher
Components Summary
Component | Description |
|---|---|
Creates a Gemini LLM model instance with API key config. | |
Constructs an LLM agent with model, instructions, and tools. | |
Tool enabling Google web search integrated into the agent. | |
Configuration for launching agents with loaders. | |
Creates a full-featured CLI launcher for the agent. | |
Runs the launcher with context, config, and CLI args. |
This file exemplifies the orchestration of an LLM-based AI agent specialized in answering domain-specific queries by combining model instantiation, strict instruction enforcement, tool integration, and runtime launching infrastructure. It leverages the modular architecture of the AI agent framework to achieve a focused, interactive application.