[5-Day Gen AI Intensive Course] Day 1: Prompting
Published:
What are the interesting things in the first day of 5-Day Gen AI Intensive Course?
Disclaimer: This write-up’s content is mainly about what I feel interesting in the Day 1 of 5-Day Gen AI Intensive Course, so it will not contain every that is mentioned in the course. To have more information, please access the course’s contents:
- Foundational Large Language Models & Text Generation
- Whitepaper Companion Podcast - Foundational LLMs & Text Generation
- Prompt Engineering
- Whitepaper Companion Podcast - Prompt Engineering
First of all, Import & Config
%pip install -U -q "google-generativeai>=0.8.3"
import google.generativeai as genai
from IPython.display import HTML, Markdown, display
GOOGLE_API_KEY = 'YOUR_SECRET_KEY'
genai.configure(api_key=GOOGLE_API_KEY)
Chat History
I have worked with Gemini API since August of 2024, but up to now, I don’t know that we can send the chat history for the LLM. Let’s see how they set it up.
flash = genai.GenerativeModel('gemini-1.5-flash')
chat = flash.start_chat(history=[])
response = chat.send_message('Hello! My name is Zlork.')
print(response.text)
# Hello Zlork! It's nice to meet you. 😊 What can I do for you today?
response = chat.send_message('Can you tell something interesting about dinosaurs?')
print(response.text)
# ... Response
response = chat.send_message('Do you remember what my name is?')
print(response.text)
# Yes, I remember! You said your name is Zlork. 😊 ...
Enum Mode
This is absolutely new to me! Let’s see how to write.
import enum
class Sentiment(enum.Enum):
POSITIVE = "positive"
NEUTRAL = "neutral"
NEGATIVE = "negative"
model = genai.GenerativeModel(
'gemini-1.5-flash-001',
generation_config=genai.GenerationConfig(
response_mime_type="text/x.enum",
response_schema=Sentiment
))
response = model.generate_content(zero_shot_prompt)
print(response.text)
# positive
I think this will be the best for ones who want to try classification tasks.
JSON mode
I know about JSON mode, but I’ve never think it could be this way. All of my past experience is nothing.
import typing_extensions as typing
class PizzaOrder(typing.TypedDict):
size: str
ingredients: list[str]
type: str
model = genai.GenerativeModel(
'gemini-1.5-pro-002',
generation_config=genai.GenerationConfig(
temperature=0.1,
response_mime_type="application/json",
response_schema=PizzaOrder,
))
response = model.generate_content("Can I have a large dessert pizza with apple and chocolate")
print(response.text)
# {"ingredients": ["apple", "chocolate"], "size": "large", "type": "dessert"}
Or even combine them together!
import enum
from typing_extensions import TypedDict
class Grade(enum.Enum):
A_PLUS = "a+"
A = "a"
B = "b"
C = "c"
D = "d"
F = "f"
class Recipe(TypedDict):
recipe_name: str
grade: Grade
model = genai.GenerativeModel("gemini-1.5-pro-latest")
result = model.generate_content(
"List about 10 cookie recipes, grade them based on popularity",
generation_config=genai.GenerationConfig(
response_mime_type="application/json", response_schema=list[Recipe]
),
)
print(result) # [{"grade": "a+", "recipe_name": "Chocolate Chip Cookies"}, ...]