machine-learningscikit-learnartificial-intelligencegoogle-gemini

Function Calling Google GEMINI AI- TypeError: Invalid input type. Expected an instance of `genai.FunctionDeclarationType`


I am trying to run a simple Random Forest Classification Model using the iris dataset and integrate it into Gemini AI

Here is my code:

import google.generativeai as genai
from vertexai.preview.generative_models import (
   

 FunctionDeclaration,
    GenerativeModel,
    Part,
    Tool,
)


genai.configure(api_key="API KEY")

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

# Load and train the model
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)

def predict_iris_species(sepal_length, sepal_width, petal_length, petal_width):
    """
    Predicts the iris species based on sepal and petal measurements.

    Args:
        sepal_length (float): Length of the sepal in cm.
        sepal_width (float): Width of the sepal in cm.
        petal_length (float): Length of the petal in cm.
        petal_width (float): Width of the petal in cm.

    Returns:
            str: The predicted iris species.
    """
    input_data = [[sepal_length, sepal_width, petal_length, petal_width]]
    prediction = model.predict(input_data)
    return str(iris.target_names[prediction[0]])

tools = Tool(
    function_declarations=[
        FunctionDeclaration(
            name="predict_iris_species",
            description="predicts the iris species based on sepal and petal measurements",
            parameters={
                "type": "object",
                "properties": {
                    "sepal_length": {"type": "number", "description": "Length of the sepal in cm."},
                    "sepal_width": {"type": "number", "description": "Width of the sepal in cm."},
                    "petal_length": {"type": "number", "description": "Length of the petal in cm."},
                    "petal_width": {"type": "number", "description": "Width of the petal in cm."}
                },
                "required": ["sepal_length", "sepal_width", "petal_length", "petal_width"]
            }
        )
    ]
)

llm = genai.GenerativeModel(model_name='gemini-1.5-flash',
                              tools=[tools])

chat = llm.start_chat()
response = chat.send_message("what is the  species of the iris flower with sepal length 5.1, sepal width 3.5, petal length 1.4, and petal width 0.2?")
response.text

I get an error saying:

TypeError: Invalid input type. Expected an instance of `genai.FunctionDeclarationType`.
However, received an object of type: <class 'vertexai.generative_models._generative_models.Tool'>.
Object Value: function_declarations {
...
    property_ordering: "petal_length"
    property_ordering: "petal_width"
  }
}

What does it mean? I thought that's how you format the JSON data? Could it be that my function from predict_iris_species need to return something else instead of a string?

Would it be the fact that it needs to output a JSON dictionary?


Solution

  • Okay, I figured it out.

    It seems that since I'm only testing one function, I can skip the whole function_declaration thing. (You can do it but not necessary).

    The important thing is to include enable_automatic_function_calling on the llm.start_chat() so basically:

    llm_model.start_chat(enable_automatic_function_calling=True)

    So in the end, the whole code comes to this:

    def predict_iris_species(sepal_length:float, sepal_width:float, petal_length:float, petal_width:float):
    
         #nothing changes...
    
    llm_model = genai.GenerativeModel(model_name='gemini-1.5-flash', tools=[predict_iris_species])
    
    chat = llm_model.start_chat(enable_automatic_function_calling=True)
    
    prompt= """
    
    insert prompt here
    
    """
    chat.send_message(prompt).text
    

    This will show the output. Hopes this helps to the people experiencing the same issue