HomeSample Page

Sample Page Title


On this tutorial, we shift from conventional immediate crafting to a extra systematic, programmable strategy by treating prompts as tunable parameters fairly than static textual content. As an alternative of guessing which instruction or instance works finest, we construct an optimization loop round Gemini 2.0 Flash that experiments, evaluates, and routinely selects the strongest immediate configuration. On this implementation, we watch our mannequin enhance step-by-step, demonstrating how immediate engineering turns into way more highly effective after we orchestrate it with data-driven search fairly than instinct. Try the Full Codes right here.

import google.generativeai as genai
import json
import random
from typing import Record, Dict, Tuple, Non-obligatory
from dataclasses import dataclass
import numpy as np
from collections import Counter


def setup_gemini(api_key: str = None):
   if api_key is None:
       api_key = enter("Enter your Gemini API key: ").strip()
   genai.configure(api_key=api_key)
   mannequin = genai.GenerativeModel('gemini-2.0-flash-exp')
   print("✓ Gemini 2.0 Flash configured")
   return mannequin


@dataclass
class Instance:
   textual content: str
   sentiment: str
   def to_dict(self):
       return {"textual content": self.textual content, "sentiment": self.sentiment}


@dataclass
class Prediction:
   sentiment: str
   reasoning: str = ""
   confidence: float = 1.0

We import all required libraries and outline the setup_gemini helper to configure Gemini 2.0 Flash. We additionally create the Instance and Prediction knowledge courses to signify dataset entries and mannequin outputs in a clear, structured method. Try the Full Codes right here.

def create_dataset() -> Tuple[List[Example], Record[Example]]:
   train_data = [
       Example("This movie was absolutely fantastic! Best film of the year.", "positive"),
       Example("Terrible experience, waste of time and money.", "negative"),
       Example("The product works as expected, nothing special.", "neutral"),
       Example("I'm blown away by the quality and attention to detail!", "positive"),
       Example("Disappointing and overpriced. Would not recommend.", "negative"),
       Example("It's okay, does the job but could be better.", "neutral"),
       Example("Incredible customer service and amazing results!", "positive"),
       Example("Complete garbage, broke after one use.", "negative"),
       Example("Average product, met my basic expectations.", "neutral"),
       Example("Revolutionary! This changed everything for me.", "positive"),
       Example("Frustrating bugs and poor design choices.", "negative"),
       Example("Decent quality for the price point.", "neutral"),
       Example("Exceeded all my expectations, truly remarkable!", "positive"),
       Example("Worst purchase I've ever made, avoid at all costs.", "negative"),
       Example("It's fine, nothing to complain about really.", "neutral"),
       Example("Absolutely stellar performance, 5 stars!", "positive"),
       Example("Broken and unusable, total disaster.", "negative"),
       Example("Meets requirements, standard quality.", "neutral"),
   ]
   val_data = [
       Example("Absolutely love it, couldn't be happier!", "positive"),
       Example("Broken on arrival, very upset.", "negative"),
       Example("Works fine, no major issues.", "neutral"),
       Example("Outstanding performance and great value!", "positive"),
       Example("Regret buying this, total letdown.", "negative"),
       Example("Adequate for basic use.", "neutral"),
   ]
   return train_data, val_data


class PromptTemplate:
   def __init__(self, instruction: str = "", examples: Record[Example] = None):
       self.instruction = instruction
       self.examples = examples or []
   def format(self, textual content: str) -> str:
       prompt_parts = []
       if self.instruction:
           prompt_parts.append(self.instruction)
       if self.examples:
           prompt_parts.append("nExamples:")
           for ex in self.examples:
               prompt_parts.append(f"nText: {ex.textual content}")
               prompt_parts.append(f"Sentiment: {ex.sentiment}")
       prompt_parts.append(f"nText: {textual content}")
       prompt_parts.append("Sentiment:")
       return "n".be part of(prompt_parts)
   def clone(self):
       return PromptTemplate(self.instruction, self.examples.copy())

We generate a small however various sentiment dataset for coaching and validation utilizing the create_dataset perform. We then outline PromptTemplate, which lets us assemble directions, a few-shot examples, and a present question right into a single immediate string. We deal with the template as a programmable object so we are able to swap directions and examples throughout optimization. Try the Full Codes right here.

class SentimentModel:
   def __init__(self, mannequin, prompt_template: PromptTemplate):
       self.mannequin = mannequin
       self.prompt_template = prompt_template


   def predict(self, textual content: str) -> Prediction:
       immediate = self.prompt_template.format(textual content)
       strive:
           response = self.mannequin.generate_content(immediate)
           consequence = response.textual content.strip().decrease()
           for sentiment in ['positive', 'negative', 'neutral']:
               if sentiment in consequence:
                   return Prediction(sentiment=sentiment, reasoning=consequence)
           return Prediction(sentiment="impartial", reasoning=consequence)
       besides Exception as e:
           return Prediction(sentiment="impartial", reasoning=str(e))


   def consider(self, dataset: Record[Example]) -> float:
       appropriate = 0
       for instance in dataset:
           pred = self.predict(instance.textual content)
           if pred.sentiment == instance.sentiment:
               appropriate += 1
       return (appropriate / len(dataset)) * 100

We wrap Gemini within the SentimentModel class so we are able to name it like a daily classifier. We format prompts by way of the template, name generate_content, and post-process the textual content to extract considered one of three sentiments. We additionally add an consider technique so we are able to measure accuracy over any dataset with a single name. Try the Full Codes right here.

class PromptOptimizer:
   def __init__(self, mannequin):
       self.mannequin = mannequin
       self.instruction_candidates = [
           "Analyze the sentiment of the following text. Classify as positive, negative, or neutral.",
           "Classify the sentiment: positive, negative, or neutral.",
           "Determine if this text expresses positive, negative, or neutral sentiment.",
           "What is the emotional tone? Answer: positive, negative, or neutral.",
           "Sentiment classification (positive/negative/neutral):",
           "Evaluate sentiment and respond with exactly one word: positive, negative, or neutral.",
       ]


   def select_best_examples(self, train_data: Record[Example], val_data: Record[Example], n_examples: int = 3) -> Record[Example]:
       best_examples = None
       best_score = 0
       for _ in vary(10):
           examples_by_sentiment = {
               'optimistic': [e for e in train_data if e.sentiment == 'positive'],
               'damaging': [e for e in train_data if e.sentiment == 'negative'],
               'impartial': [e for e in train_data if e.sentiment == 'neutral']
           }
           chosen = []
           for sentiment in ['positive', 'negative', 'neutral']:
               if examples_by_sentiment[sentiment]:
                   chosen.append(random.alternative(examples_by_sentiment[sentiment]))
           remaining = [e for e in train_data if e not in selected]
           whereas len(chosen) < n_examples and remaining:
               chosen.append(random.alternative(remaining))
               remaining.take away(chosen[-1])
           template = PromptTemplate(instruction=self.instruction_candidates[0], examples=chosen)
           test_model = SentimentModel(self.mannequin, template)
           rating = test_model.consider(val_data[:3])
           if rating > best_score:
               best_score = rating
               best_examples = chosen
       return best_examples


   def optimize_instruction(self, examples: Record[Example], val_data: Record[Example]) -> str:
       best_instruction = self.instruction_candidates[0]
       best_score = 0
       for instruction in self.instruction_candidates:
           template = PromptTemplate(instruction=instruction, examples=examples)
           test_model = SentimentModel(self.mannequin, template)
           rating = test_model.consider(val_data)
           if rating > best_score:
               best_score = rating
               best_instruction = instruction
       return best_instruction

We introduce the PromptOptimizer class and outline a pool of candidate directions to check. We implement select_best_examples to seek for a small, various set of few-shot examples and optimize_instruction to attain every instruction variant on validation knowledge. We’re successfully turning immediate design into a light-weight search drawback over examples and directions. Try the Full Codes right here.

  def compile(self, train_data: Record[Example], val_data: Record[Example], n_examples: int = 3) -> PromptTemplate:
       best_examples = self.select_best_examples(train_data, val_data, n_examples)
       best_instruction = self.optimize_instruction(best_examples, val_data)
       optimized_template = PromptTemplate(instruction=best_instruction, examples=best_examples)
       return optimized_template


def fundamental():
   print("="*70)
   print("Immediate Optimization Tutorial")
   print("Cease Writing Prompts, Begin Programming Them!")
   print("="*70)


   mannequin = setup_gemini()
   train_data, val_data = create_dataset()
   print(f"✓ {len(train_data)} coaching examples, {len(val_data)} validation examples")


   baseline_template = PromptTemplate(
       instruction="Classify sentiment as optimistic, damaging, or impartial.",
       examples=[]
   )
   baseline_model = SentimentModel(mannequin, baseline_template)
   baseline_score = baseline_model.consider(val_data)


   manual_examples = train_data[:3]
   manual_template = PromptTemplate(
       instruction="Classify sentiment as optimistic, damaging, or impartial.",
       examples=manual_examples
   )
   manual_model = SentimentModel(mannequin, manual_template)
   manual_score = manual_model.consider(val_data)


   optimizer = PromptOptimizer(mannequin)
   optimized_template = optimizer.compile(train_data, val_data, n_examples=4)

We add the compile technique to mix the most effective examples and finest directions right into a closing optimized PromptTemplate. Inside fundamental, we configure Gemini, construct the dataset, and consider each a zero-shot baseline and a easy handbook few-shot immediate. We then name the optimizer to provide our compiled, optimized immediate for sentiment evaluation. Try the Full Codes right here.

optimized_model = SentimentModel(mannequin, optimized_template)
   optimized_score = optimized_model.consider(val_data)


   print(f"Baseline (zero-shot):     {baseline_score:.1f}%")
   print(f"Guide few-shot:          {manual_score:.1f}%")
   print(f"Optimized (compiled):     {optimized_score:.1f}%")


   print(f"nInstruction: {optimized_template.instruction}")
   print(f"nSelected Examples ({len(optimized_template.examples)}):")
   for i, ex in enumerate(optimized_template.examples, 1):
       print(f"n{i}. Textual content: {ex.textual content}")
       print(f"   Sentiment: {ex.sentiment}")


   test_cases = [
       "This is absolutely amazing, I love it!",
       "Completely broken and unusable.",
       "It works as advertised, no complaints."
   ]


   for test_text in test_cases:
       print(f"nInput: {test_text}")
       pred = optimized_model.predict(test_text)
       print(f"Predicted: {pred.sentiment}")


   print("✓ Tutorial Full!")


if __name__ == "__main__":
   fundamental()

We consider the optimized mannequin and evaluate its accuracy in opposition to the baseline and handbook few-shot setups. We print the chosen instruction and the chosen examples so we are able to examine what the optimizer discovers, after which we run a couple of reside take a look at sentences to see predictions in motion. We end by summarizing the enhancements and reinforcing the concept prompts could be tuned programmatically fairly than written by hand.

In conclusion, we applied how programmatic immediate optimization offers a repeatable, evidence-driven workflow for designing high-performing prompts. We started with a fragile baseline, then iteratively examined directions, chosen various examples, and compiled an optimized template that outperforms handbook makes an attempt. This course of exhibits that we now not depend on trial-and-error prompting; as a substitute, we orchestrated a managed optimization cycle. Additionally, we are able to lengthen this pipeline to new duties, richer datasets, and extra superior scoring strategies, permitting us to engineer prompts with precision, confidence, and scalability.


Try the Full Codes right here. Be at liberty to take a look at our GitHub Web page for Tutorials, Codes and Notebooks. Additionally, be happy to observe us on Twitter and don’t neglect to affix our 100k+ ML SubReddit and Subscribe to our E-newsletter. Wait! are you on telegram? now you possibly can be part of us on telegram as properly.


Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is dedicated to harnessing the potential of Synthetic Intelligence for social good. His most up-to-date endeavor is the launch of an Synthetic Intelligence Media Platform, Marktechpost, which stands out for its in-depth protection of machine studying and deep studying information that’s each technically sound and simply comprehensible by a large viewers. The platform boasts of over 2 million month-to-month views, illustrating its reputation amongst audiences.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles