google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.62k stars 322 forks source link

ImportError: cannot import name 'create_tuned_model' from 'genai' in VSCode #623

Closed elifbeyzatok00 closed 1 week ago

elifbeyzatok00 commented 1 week ago

Description of the bug:

I want to fine tune "models/gemini-1.5-flash-001-tuning" but i took an ImportError in VSCode.

!pip install -q -U google-generativeai

import pandas as pd
import time
import seaborn as sns
from genai import create_tuned_model, GenerativeModel

# File path to the dataset
file_path = "datasets/Question-Types.csv"  # Adjust this if needed

# Read the file, skipping the first row
df = pd.read_csv(file_path, header=None, names=["RawData"], skiprows=1)

# Split the data into 'Question' and 'Question Type' columns based on the semicolon delimiter
df[['Question', 'Question Type']] = df['RawData'].str.split(';', n=1, expand=True)

# Drop the original 'RawData' column and inspect the result
df = df.drop(columns=['RawData'])
df.head()

# Use the dataset loaded in the previous step
training_data = df 

# Initialize the base model
base_model = "models/gemini-1.5-flash-001-tuning"

# Set up the model tuning
operation = create_tuned_model(
    display_name="increment",
    source_model=base_model,
    epoch_count=20,
    batch_size=4,
    learning_rate=0.001,
    training_data=training_data,
)

# Monitor training status
for status in operation.wait_bar():
    time.sleep(10)

# Retrieve tuning results
result = operation.result()
print(result)

# Plot the loss curve
snapshots = pd.DataFrame(result.tuning_task.snapshots)
sns.lineplot(data=snapshots, x='epoch', y='mean_loss')

# Initialize the tuned model for generation
types2question_model = GenerativeModel(model_name=result.name)
generated_result = types2question_model.generate_content("Negative Factual Information Question")
print(generated_result.text)

csv file: Question-Types.csv

Error Stack:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[46], [line 4](vscode-notebook-cell:?execution_count=46&line=4)
      [2](vscode-notebook-cell:?execution_count=46&line=2) import time
      [3](vscode-notebook-cell:?execution_count=46&line=3) import seaborn as sns
----> [4](vscode-notebook-cell:?execution_count=46&line=4) from genai import create_tuned_model, GenerativeModel
      [6](vscode-notebook-cell:?execution_count=46&line=6) # Use the dataset loaded in the previous step
      [7](vscode-notebook-cell:?execution_count=46&line=7) training_data = df 

ImportError: cannot import name 'create_tuned_model' from 'genai' (c:\Users\tokel\OneDrive\Masaüstü\Learnify\.conda\Lib\site-packages\genai\__init__.py)

Actual vs expected behavior:

It must generate a Negative Factual Information Question

Any other information you'd like to share?

System Info

genai info: Name: genai Version: 2.1.0 Summary: Generative AI for IPython (enhance your code cells) Home-page: Author: Kyle Kelley Author-email: rgbkrk@gmail.com License: BSD-3-Clause Location: c:\users\tokel\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages Requires: ipython, openai, tabulate, tiktoken Required-by:

Hamza-nabil commented 1 week ago

Hi @elifbeyzatok00 , The problem is in the import , genai is an alias for the google.generativeai module . it imported like this :

import google.generativeai as genai

You first need to configure the api (Set up authentication)

import os

genai.configure(api_key=os.environ["API_KEY"])

Then you can set up the model tuning:

operation = genai. create_tuned_model(
    display_name="increment",
    source_model=base_model,
    epoch_count=20,
    batch_size=4,
    learning_rate=0.001,
    training_data=training_data,
) 

then Initialize the tuned model for generation

types2question_model = genai.GenerativeModel(model_name=result.name)

Take a look at : Gemini API : Fine-tuning tutorial

MarkDaoust commented 1 week ago
from genai import create_tuned_model, GenerativeModel

Hi, careful, the package is google.generativeai try:

from google.generativeai import create_tuned_model, GenerativeModel