AI DevKit
Glitch9 Inc.Glitch9 DocsDiscordIssues
  • Introduction
    • AI DevKit 3.0
    • Update Logs
    • Troubleshooting
      • ❗Issues After Updating AIDevKit?
      • ❗The type or namespace name 'Newtonsoft' could not be found
      • ❗Build Error: The name 'UnityMenu' does not exist in the current context
      • ❗Model 'modelName' not found
      • ❗The model `model name` does not exist or you do not have access to it
      • ❗The type or namespace name 'AndroidJavaObject' could not be found
      • ❗The type or namaspace name 'Plastic' does not exist
      • ❗Build Error: The name 'Asset Database' does not exist in the current context
      • ❗'ModelData.Create(Provider, string, UnixTime?, string)': not all code paths return a value
      • ⚠️ Timeout Issues
      • ⚠️ Receiving a “HTTP/1.1 400 Bad Request” Error?
    • FAQ
      • My OpenAI API free trial has ended or is inactive.
  • Quick Start
    • Get API Keys
      • OpenAI API Key Guide
      • Google API Key Guide
      • ElevenLabs API Key Guide
    • Text Generation
    • C# Object Generation
    • Image Generation
    • Sound Effect Generation
    • Text to Speech (TTS)
    • Speech to Text (STT)
    • Voice Changer
    • Audio Isolation
  • Pro Features
    • Generation Menu
      • Code Generators
        • C# Script Generator
        • Unity Component Generator
    • Editor Chat
    • Editor Vision (TTI, ITI)
    • Editor Speech (TTS)
    • Management Tools
      • Prompt History Viewer
      • AI Model Manager
      • TTS Voice Manager
      • OpenAI File Manager
      • OpenAI Assistant Manager
      • ElevenLabs Voice Library
  • Assistants API (OpenAI)
    • How it works
    • Creating custom functions
    • Creating assistants API
  • Advanced API Supports
    • OpenAI API
      • 💬Chat completions
      • 🖼️Image operations
      • 🗣️Text to speech
      • 🎙️Speech to text
        • Recording real-time in Unity
      • 💾Files
      • 🔎Embeddings
      • 🛡️Moderations
      • ⚙️Fine-tuning
    • Google API
      • 📝System instructions
      • 💬Text generation
      • ⚙️Fine-tuning
      • ▶️Fucntion calling
      • 🔎Embeddings
      • 🛡️Safety
      • 💻Code execution
    • ElevenLabs API
  • Legacy Documents
    • AI DevKit 1.0 - 2.0
      • AI DevKit 2.0
      • AI DevKit 1.0
      • Preperation
      • Event Handlers
      • Scriptable Toolkits
        • Chat Streamer
        • Image Generator
        • Voice Transcriber
        • Voice Generator
      • Editor Tools
Powered by GitBook
On this page
  • Create tuned model
  • Evaluate your model
  • Update the description
  • Delete the model
  1. Advanced API Supports
  2. Google API

Fine-tuning

Create tuned model

To create a tuned model, you need to pass your dataset to the model in the genai.create_tuned_model method. You can do this by directly defining the input and output values in the call or importing from a file into a dataframe to pass to the method.

For this example, you will tune a model to generate the next number in the sequence. For example, if the input is 1, the model should output 2. If the input is one hundred, the output should be one hundred one.

var baseModel = (await GenerativeAI.DefaultInstance.Models.List())
    .FirstOrDefault(m => m.SupportedGenerationMethods.Contains("createTunedModel"));

Debug.Log(baseModel.ToString());

/* log
Model(name='models/gemini-1.0-pro-001',
      base_model_id='',
      version='001',
      display_name='Gemini 1.0 Pro',
      description=('The best model for scaling across a wide range of tasks. This is a stable '
                   'model that supports tuning.'),
      input_token_limit=30720,
      output_token_limit=2048,
      supported_generation_methods=['generateContent', 'countTokens', 'createTunedModel'],
      temperature=0.9,
      top_p=1.0,
      top_k=1)
*/
base_model = [
    m for m in genai.list_models()
    if "createTunedModel" in m.supported_generation_methods][0]
base_model
var name = $"generate-num-{UnityEngine.Random.Range(0, 10000)}";

var tunedModelRequest = new TunedModelRequest.Builder()
    .SetSourceModel(baseModel.Name)
    .SetTrainingData(
        new TuningExample("1", "2"),
        new TuningExample("3", "4"),
        new TuningExample("-3", "-2"),
        new TuningExample("twenty two", "twenty three"),
        new TuningExample("two hundred", "two hundred one"),
        new TuningExample("ninety nine", "one hundred"),
        new TuningExample("8", "9"),
        new TuningExample("-98", "-97"),
        new TuningExample("1,000", "1,001"),
        new TuningExample("10,100,000", "10,100,001"),
        new TuningExample("thirteen", "fourteen"),
        new TuningExample("eighty", "eighty one"),
        new TuningExample("one", "two"),
        new TuningExample("three", "four"),
        new TuningExample("seven", "eight"))
    .SetName(name)
    .SetEpochCount(100)
    .SetBatchSize(4)
    .SetLearningRate(0.001f)
    .Build();

var operation = 
    await GenerativeAI.DefaultInstance.TunedModels.Create(tunedModelRequest);
import random

name = f'generate-num-{random.randint(0,10000)}'
operation = genai.create_tuned_model(
    # You can use a tuned model here too. Set `source_model="tunedModels/..."`
    source_model=base_model.name,
    training_data=[
        {
             'text_input': '1',
             'output': '2',
        },{
             'text_input': '3',
             'output': '4',
        },{
             'text_input': '-3',
             'output': '-2',
        },{
             'text_input': 'twenty two',
             'output': 'twenty three',
        },{
             'text_input': 'two hundred',
             'output': 'two hundred one',
        },{
             'text_input': 'ninety nine',
             'output': 'one hundred',
        },{
             'text_input': '8',
             'output': '9',
        },{
             'text_input': '-98',
             'output': '-97',
        },{
             'text_input': '1,000',
             'output': '1,001',
        },{
             'text_input': '10,100,000',
             'output': '10,100,001',
        },{
             'text_input': 'thirteen',
             'output': 'fourteen',
        },{
             'text_input': 'eighty',
             'output': 'eighty one',
        },{
             'text_input': 'one',
             'output': 'two',
        },{
             'text_input': 'three',
             'output': 'four',
        },{
             'text_input': 'seven',
             'output': 'eight',
        }
    ],
    id = name,
    epoch_count = 100,
    batch_size=4,
    learning_rate=0.001,
)

Your tuned model is immediately added to the list of tuned models, but its status is set to "creating" while the model is tuned.

var model = 
    await GenerativeAI.DefaultInstance.TunedModels.Get(name);

Debug.Log(model.ToString());

/* log
TunedModel(name='tunedModels/generate-num-2946',
   source_model='models/gemini-1.0-pro-001',
   base_model='models/gemini-1.0-pro-001',
   display_name='',
   description='',
   temperature=0.9,
   top_p=1.0,
   top_k=1,
   state=<State.CREATING: 1>,
   create_time=datetime.datetime(2024, 2, 21, 20, 4, 16, 448050, tzinfo=datetime.timezone.utc),
   update_time=datetime.datetime(2024, 2, 21, 20, 4, 16, 448050, tzinfo=datetime.timezone.utc),
   tuning_task=TuningTask(start_time=datetime.datetime(2024, 2, 21, 20, 4, 16, 890698, tzinfo=datetime.timezone.utc),
                          complete_time=None,
                          snapshots=[],
                          hyperparameters=Hyperparameters(epoch_count=100,
                                                          batch_size=4,
                                                          learning_rate=0.001)))
 */
base_model = [

model = genai.get_tuned_model(f'tunedModels/{name}')

model

TunedModel(name='tunedModels/generate-num-2946',
           source_model='models/gemini-1.0-pro-001',
           base_model='models/gemini-1.0-pro-001',
           display_name='',
           description='',
           temperature=0.9,
           top_p=1.0,
           top_k=1,
           state=<State.CREATING: 1>,
           create_time=datetime.datetime(2024, 2, 21, 20, 4, 16, 448050, tzinfo=datetime.timezone.utc),
           update_time=datetime.datetime(2024, 2, 21, 20, 4, 16, 448050, tzinfo=datetime.timezone.utc),
           tuning_task=TuningTask(start_time=datetime.datetime(2024, 2, 21, 20, 4, 16, 890698, tzinfo=datetime.timezone.utc),
                                  complete_time=None,
                                  snapshots=[],
                                  hyperparameters=Hyperparameters(epoch_count=100,
                                                                  batch_size=4,
                                                                  learning_rate=0.001)))

Evaluate your model

You can use the GenerativeAI.DefaultInstance.TunedModels.GenerateText method and specify the name of your model to test your model performance.

var model = new GenerativeModel("name");

var result = await model.GenerateContentAsync("55");
Debug.Log(result.Text);

// '56'

result = await model.GenerateContentAsync("123455");
Debug.Log(result.Text);

// '123456'

result = await model.GenerateContentAsync("four");
Debug.Log(result.Text);

// 'five'

result = await model.GenerateContentAsync("quatre"); // French 4
Debug.Log(result.Text); // French 5 is "cinq"

// 'cinq'

result = await model.GenerateContentAsync("III"); // Roman numeral 3
Debug.Log(result.Text); // Roman numeral 4 is IV

// 'IV'

result = await model.GenerateContentAsync("七"); // Japanese 7
Debug.Log(result.Text); // Japanese 8 is 八!

// '八'
base_model = [

model = genai.GenerativeModel(model_name=f'tunedModels/{name}')

result = model.generate_content('55')
result.text

'56'

result = model.generate_content('123455')
result.text

'123456'

result = model.generate_content('four')
result.text

'five'

result = model.generate_content('quatre') # French 4
result.text                               # French 5 is "cinq"

'cinq'

result = model.generate_content('III')    # Roman numeral 3
result.text                               # Roman numeral 4 is IV

'IV'

result = model.generate_content('七')  # Japanese 7
result.text                            # Japanese 8 is 八!

'八'

Update the description

You can update the description of your tuned model any time using the GenerativeAI.DefaultInstance.TunedModels.Patch method.

await GenerativeAI.DefaultInstance.TunedModels.Patch(
    "name", 
    new UpdateMask("description", "This is my model."));

var model = await GenerativeAI.DefaultInstance.TunedModels.Get("name");

Debug.Log(model.Data.Description);

// log: "This is my model."
genai.update_tuned_model(f'tunedModels/{name}', {"description":"This is my model."});

model = genai.get_tuned_model(f'tunedModels/{name}')

model.description

'This is my model.'

Delete the model

You can clean up your tuned model list by deleting models you no longer need. Use the GenerativeAI.DefaultInstance.TunedModels.Delete method to delete a model. If you canceled any tuning jobs, you may want to delete those as their performance may be unpredictable.

await GenerativeAI.DefaultInstance.TunedModels.Delete("name");
genai.delete_tuned_model(f'tunedModels/{name}')

If the model no longer exists, it will return an error:

<class 'google.api_core.exceptions.NotFound'>: 404 Tuned model tunedModels/generate-num-2946 does not exist.

PreviousText generationNextFucntion calling

Last updated 10 months ago

It really seems to have picked up the task despite the limited examples, but "next" is a relatively simple concept, see the for more guidance on improving performance.

⚙️
tuning guide
LogoFine-tuning with the Gemini API  |  Google for DevelopersGoogle for Developers
Google official document