Exploring Gen AI — A Guide to Model Parameters for Better Content Creation (Part 2)

Satyam Gondhale
3 min readDec 24, 2024

--

Source From Google

We are already aware of the Firebase Genkit & How to create a basic Firebase Genkit App & How to model the parameters to control the length of response we are getting from our model.

Build your First AI application with Firebase Genkit 🚀(Part 1)
https://shorturl.at/891Js

Build your First AI application with Firebase Genkit 🚀(Part 2)
https://shorturl.at/zPMCf

Exploring Gen AI — A Guide to Model Parameters for Better Content Creation (Part 1)
https://shorturl.at/BHA9u

Let’s check how to model parameters that control creativity.

Parameters that controls Creativity

  1. temperature
    -
    This parameter is critical component in AI generated text that determines how random & creative the output is by controlling the probability distribution of model’s token predictions
    - In general, models predict the next word or token in a sequence by assigning probabilities to each possible next token
    - A low temperature makes the model focus on the most likely tokens, leading to deterministic & predictable output
    - A high temperature makes the model more likely to choose less probable tokens, resulting in creative & diverse outputs
    - Example Prompt: Describe a calm evening by the lake.
    The model predicts the following token probabilities:
    "peaceful": 0.5
    "tranquil": 0.3
    "quiet": 0.1
    "stormy": 0.05
    "noisy": 0.05
    - With Low Temperature (0.2) Result, The calm evening by the lake was peaceful and serene.
    - With High Temperature Result, The calm evening by the lake was tranquil, with whispers of an enigmatic breeze
  2. topP
    -
    This parameter used in AI models to control randomness and creativity in output
    - It works dynamically selecting tokens based on their cumulative probabilities, ensuring a balance between creativity
    - While generating the next word or token LLM’s assign probabilities to each possible next token
    - Then, Top-P limits the token selection to only those whose cumulative probabilities add up to a specific threshold like 0.8 or 0.9
    - This ensures that model focuses on subset of likely tokens while ignoring highly improbable ones creating contextually appropriate responses
    - Example Prompt: Describe a magical forest.
    The model predicts the probabilities of the next tokens:
    "enchanted": 0.4
    "mystical": 0.3
    "dark": 0.2
    "haunted": 0.05
    "ancient": 0.05
    Then, Cumulative Probability:
    "enchanted": 0.4 (40%)
    "mystical": 0.4 + 0.3 = 0.7 (70%)
    "dark": 0.7 + 0.2 = 0.9 (90%)
    "haunted": 0.9 + 0.05 = 0.95 (95%)
    "ancient": 0.95 + 0.05 = 1.0 (100%)
    When top-P is applied:
    Top-P = 0.5: Only tokens contributing to the first 50% of the probability are considered ("enchanted", "mystical").
    Top-P = 0.9: Tokens contributing to the first 90% are considered ("enchanted", "mystical", "dark").
  3. topK
    - Now we know language models assign probabilities to each possible next token in sequence
    - Top-K limits the selection to the K most probable tokens from the model’s predictions
    - From this Top-K tokens one is selected either deterministically or through sampling
    - Exactly K tokens are selected regardless of their cumulative probabilities
    - Example Prompt: Describe a magical forest. The model predicts
    "bright": 0.5
    "golden": 0.3
    "red": 0.1
    "cloudy": 0.05
    "dark": 0.05
    For top-K= 1, The sunrise was bright and warm.
    For top-K=3, The sunrise was bright and glowing

To adjust the parameters, update the code as given below

// import the Genkit and Google AI plugin libraries
import { gemini15Flash, googleAI } from '@genkit-ai/googleai';
import { genkit } from 'genkit';
import dotenv from 'dotenv';

// Load the API key from environment variables
dotenv.config();

// configure a Genkit instance
const ai = genkit({
plugins: [googleAI({ apiKey: process.env.GOOGLE_GENAI_API_KEY })], // Pass the API key
model: gemini15Flash, // Set the model
});


(async () => {
// make a generation request
const { text } = await ai.generate({
prompt: 'Write a description of Generative AI',
config: {
temperature: 1.5, // 0-2
topP: 0.9, // Consider tokens with cumulative probability of 90%
topK: 10 // Consider the top 10 most likely tokens
}
});
console.log(text);
})();

Feel free to change the parameters and see the amazing results for getting the most creative and diverse output by keeping the balance in response.

🎉🎆🎇🎉🎆🎇🎉🎆🎇🎉🎆🎇🎉🎆🎇🎉🎆🎇🎉🎆🎇

What Next ?
Learn how to use special Genkit functions, called flows.

Stay Tuned and Keep Watch for Next Part, Happy Learning.
Feel Free to Clap
👏 and Keep me Motivated 👏 to come up with Next Parts.

--

--

Satyam Gondhale
Satyam Gondhale

Written by Satyam Gondhale

Associate Consultant (Globallogic)

No responses yet