Skip to content

Latest commit

 

History

History
81 lines (60 loc) · 2.71 KB

Readme.md

File metadata and controls

81 lines (60 loc) · 2.71 KB

Universal Tool Schema Generation and Gemini Assistant Example

This repo attempts at exploring function calls and paralell tool usage for the new gemini-2.0-flash-exp model as well as automate tool creation and schema convertion for other llm apis This repository demonstrates how to:

  1. Convert Python functions into schemas compatible with various AI APIs (e.g., OpenAI, Anthropic, and Gemini).
  2. Use these schemas to initialize a Gemini assistant that can handle function calls in a conversation.
  3. Showcase parallel function calling and schema transformations from a single list of Python functions.

Overview

tool_converters/tool_converter.py

Uses Google Gemini models to generate OpenAI-like schemas from Python functions.

tool_converters/schema_converter.py

Converts the OpenAI-like schemas generated by tool_converter.py into Anthropic and Gemini formats.
Also provides a single generate_all_schemas(functions) method that returns all formats at once:

{
  "openai": [...],
  "anthropic": [...],
  "gemini": [...]
}

tool_generators.py

Demonstrates generating all schemas from a list of Python functions.
Prints Anthropic, Gemini, and OpenAI schemas.

gemini_assistant.py

Provides GeminiAssistant, a class that:

  • Accepts either a functions map, a list of functions, or a provided OpenAI schema.
  • If given functions, it uses ToolConverter internally to create an OpenAI-like schema, then uses schema_converter to convert to Gemini tools.
  • Initializes a Gemini model using the new Gemini 2.0 Flash experimental release.
  • Handles parallel function calling automatically, making it simple to integrate and manage multiple tool invocations.
  • Runs queries and automatically executes functions upon the model’s requests.

main.py

Example of using GeminiAssistant with a list of Python functions.
Runs a user query that triggers function calls and prints the final answer.

Requirements

  • Python 3.9+
  • google-generativeai Python package
  • Set the GEMINI_API_KEY environment variable to your Google Gemini API key.

Usage

  1. Install dependencies:

    pip install google-generativeai
  2. Set your API key:

    export GEMINI_API_KEY=your_api_key_here
  3. Run tool_generators.py to see the generated schemas:

    python tool_generators.py
  4. Run main.py to see the Gemini assistant in action:

    python main.py

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Google Generative AI team for the Gemini API
  • Contributors and maintainers