Skip to main content

A tool-aware agent powered by Gemini for structured query handling

Project description

Gemini Tool Agent

A lightweight, tool-aware Gemini agent to handle structured prompts and tool usage in conversations.

Overview

Gemini Tool Agent is a Python library that provides a simple interface for creating tool-aware agents powered by Google's Gemini AI models. It enables developers to define custom tools with structured input schemas and seamlessly integrate them into conversational flows.

Features

  • Tool-aware conversation handling
  • Structured prompt processing
  • Automatic context management
  • JSON response parsing
  • Conversation history tracking

Installation

pip install gemini-tool-agent

Requirements

  • Python 3.8 or higher
  • Google Generative AI Python SDK (google-genai >= 0.3.2)

Usage

from gemini_tool_agent.agent import Agent

# Initialize the agent with your API key
agent = Agent(key="your-api-key")

# Define your tools
agent.tools = [
    {
        "name": "save_note",
        "description": "Save a note to the database",
        "input_schema": {
            "title": "string",
            "content": "string"
        }
    }
]

# Process a query that might use tools
response = agent.process_query("Save a note about AI agents")
print(response)

Response Format

The agent returns a structured response in JSON format:

{
  "needs_tool": true,
  "tool_name": "save_note",
  "needs_direct_response": true,
  "direct_response_first": false,
  "reasoning": "The query explicitly asks to save a note, which requires the save_note tool",
  "direct_response": "AI agents are software entities that can perform tasks autonomously..."
}

Advanced Usage

You can access the conversation history:

# Get the conversation history
history = agent.history

1### Tool Parameter Extraction

After identifying that a tool needs to be used, you can extract parameters from the conversation:

# First process the query to determine if a tool is needed
response = agent.process_query("Save a note titled 'AI Agents' with content about machine learning")

# If a tool is needed, extract the parameters
if response.get("needs_tool", False):
    tool_name = response.get("tool_name")
    tool_params = agent.process_use_tool(tool_name)
    
    # Now you can use the extracted parameters to execute the tool
    print(tool_params)
    # Output: {'tool_name': 'save_note', 'input': {'title': 'AI Agents', 'content': '...'}}  

Optimized Response Generation

The agent automatically handles large prompts for memory efficiency:

# For direct usage (normally used internally by the agent)
response_text = agent.generate_response(large_prompt)

# The method automatically optimizes prompts over 10,000 characters by:
# - Trimming conversation history to the most recent 15 lines when needed
# - Truncating large direct responses while preserving start and end content

License

MIT

Author

Paul Fruitful (fruitful2007@outlook.com)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gemini_tool_agent-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gemini_tool_agent-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file gemini_tool_agent-0.1.1.tar.gz.

File metadata

  • Download URL: gemini_tool_agent-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for gemini_tool_agent-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c1ec51855a824e7ca4da231422d3b9ad8863a708d323850d3fa8afb552c2305d
MD5 7a548eb7b4092e1a459a1cfd627ba51c
BLAKE2b-256 8ef854228721ac173fe4efb5f7982e47a9839b1367d065f95cbf565c2d8b6714

See more details on using hashes here.

File details

Details for the file gemini_tool_agent-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for gemini_tool_agent-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 119587ff35dfc181c0cc3c11431f7163c5bfad5562be08f9751e5bf3f669aed1
MD5 c8673f4d5c4f17ef109a2a0b47e4001c
BLAKE2b-256 a819f44b826edfc5fed08e6e61c5c8d6ed0b8eb85dd30b368fd9d3c3ed10a428

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page