Why are largelanguage
models different?
LLMs are characterized by emergent
abilities, or the ability to perform tasks
that were not present in smaller models.
LLMs contextual understanding of human
language changes how we interact with
data and intelligent systems.
LLMs can find patterns and connections in
massive, disparate data corpora.
Search
Conversation
Content generation
11.
The Gemini Ecosystem
Themost advanced AI from Google
For Developers
For Consumers
For Business and Enterprise
Models
Gemini API
(in Google AI Studio + ai.google.dev)
Gemini for Google Workspace
Gemini for Google Cloud
Gemini in Vertex AI
Gemini | app and web
Gemini in the Google App
Gemini in Gmails, Docs…
this deck
is about
ai.google.dev
REST API +Client libraries for Python, Node, Java, and Swift
Libraries
Libraries
SDKs
17.
Vertex AI
Enterprise gradesupport.
Full MLOps (Examples: Model
evaluation, monitoring, registry)
Vertex AI
Check it out when you're
ready for production
Gemini API and Vertex AI
Both give access Gemini family
models.
Vertex AI
Cloud Production
ai.google.dev/docs/migrate_to_cloud
PlatfoEndpoints
rms
Platforms
Platforms
18.
Vertex AI isthe Google Cloud
product group for ML
End-to-end ML Platform
LLMs Explained
Elements of aFunction call
● Function Declaration (name, description, parameters, required)
● Tools (group functions into tools)
● Attach (tools to Gemini initialization)
● Response Parts (parts that are returned to Gemini for response
generation)
Function Calling
Gemini Function callingalso supports
● Parallel function processing
● Nested parameters and complex schemas
● Function calling modes:
○ AUTO, The model decides whether to predict a function call or a
natural language response
○ ANY mode forces the model to predict a function call
○ NONE mode instructs the model to not predict function calls
Function Calling
32.
Nested Params
Let's considera scenario where we want
to generate detailed product
descriptions for an e-commerce
website.
Prompt:
Generate a detailed product description based on
the provided information. Highlight the key
features and benefits of the product.
Function Calling
33.
Gemini Function callingalso supports
● Parallel function processing
● Nested parameters and complex schemas
● Function calling modes:
○ AUTO, The model decides whether to predict a function call or a
natural language response
○ ANY mode forces the model to predict a function call
○ NONE mode instructs the model to not predict function calls
Function Calling
34.
Gemini Function callingbenefits
● Ability to feed in real time results from own app/database
● Adheres output to a specific schema - receiving consistently formatted
responses.
● Ability to work on multimodal inputs
● Agent and workflow processing
● Reasoning engine, and orchestrator jobs STEPS to simulate compile/run of
output
Function Calling
35.
Function Calling
Function Calling
●Describe external functions to the model.
● The model may ask you to call the function to help it
respond to your queries.
● Endless possibilities for integrating external tools.
36.
Function calling -Basics
Function Calling
● How?
The google.generativeai SDK will inspect the function's
type hints to determine the schema.
● Allowed types are limited:
AllowedTypes = (
int | float | str | bool | list | dict )
https://ai.google.dev/tutorials/function_calling_python_quickstart
37.
def multiply(a: float,b: float):
"""Returns a * b."""
return a*b
model = genai.GenerativeModel(
model_name='gemini-1.0-pro',
tools=[multiply])
38.
Function calling -Basics
Function Calling
● Because function calling requires alternating turns, it's
easiest to use through chat.
● Enable "automatic function calling" when you start a chat,
and the ChatSession will call the function(s) for you.
○ You don't have to use automatic function calling, it just
makes simple cases easier.
https://ai.google.dev/tutorials/function_calling_python_quickstart
Function calling -Basics
Function Calling
● What happened? Use the chat history to find out.
● The chat history collects all the function calls and responses
that took place.
https://ai.google.dev/tutorials/function_calling_python_quickstart
42.
for content inchat.history:
part = content.parts[0]
print(content.role, "->", type(part).to_dict(part))
43.
for content inchat.history:
part = content.parts[0]
print(content.role, "->", type(part).to_dict(part))
# user -> {'text': 'I have 57 cats, each owns 44 mittens, '
# 'how many mittens is that in total?'}
# model -> {'function_call': {'name': 'multiply',
# 'args': {'a': 57.0, 'b': 44.0}}}
# user -> {'function_response': {'name': 'multiply',
# 'response': {'result': 2508.0}}}
# model -> {'text': ' The number of mittens in total is 2508.'}
44.
Contents
Tools
Text
Function Declaration
Function Declaration
Text
FunctionCalling nteraction
Function Call
Function Response
Text
model may predict a function call
based on user content
model can understand the function
response
and generate text
OR another function call
if one or more function declarations are
provided, function calling feature will
turn on
45.
Function calling -More Examples
Function Calling
● Wikipedia research aid
○ Integrates a search tool.
○ Uses the Gemini API inside the function call to summarize pages.
def wikipedia_search(queries:list[str]) -> list[str]:
...
https://ai.google.dev/docs/search_reranking_using_embeddings/
46.
model = genai.GenerativeModel('gemini-pro',tools=[wikipedia_search])
chat = model.start_chat(enable_automatic_function_calling=True)
query = "Explain how deep-sea life survives."
res = chat.send_message(instructions.format(query=query))
47.
model = genai.GenerativeModel('gemini-pro',tools=[wikipedia_search])
chat = model.start_chat(enable_automatic_function_calling=True)
query = "Explain how deep-sea life survives."
res = chat.send_message(instructions.format(query=query))
# Searching for "How do deep-sea creatures survive the extreme pressure?"
# Related search terms: ['Deep sea', 'Deep-sea community', 'Deep-sea fish']
# Fetching page: "Deep sea"
# Information Source: https://en.wikipedia.org/wiki/Deep_sea
# Fetching page: "Deep-sea community"
# Information Source: https://en.wikipedia.org/wiki/Deep-sea_community
# Fetching page: "Deep-sea fish"
# Information Source: https://en.wikipedia.org/wiki/Deep-sea_fish
# Searching for "How do deep-sea creatures survive the cold temperatures?"
# Related search terms: ['Deep-sea community', 'Deep sea', 'Deep-water coral']
# Fetching page: "Deep-water coral"
48.
Function calling -More Examples
Function Calling
● SQL Talk
○ Use function calling to talk to a database.
○ Live example: https://goo.gle/gemini-fn-call-sql
https://github.com/GoogleCloudPlatform/generative-ai/tree/main/gemini/function-calling/sql-talk-app
Function calling -Schema
Function Calling
● Automatically building the schema from the type hints doesn't
[currently 02/2024] work for everything.
● The allowed types are actually:
AllowedType= (
int | float | str | bool |
list['AllowedType'] |
dict[str, 'AllowedType']
)
https://ai.google.dev/tutorials/function_calling_python_quickstart
52.
Function calling -Schema
Function Calling
● Let's look at how the schema is constructed.
https://ai.google.dev/tutorials/function_calling_python_quickstart
[function_declarations {
name: "multiply"
description:"returns a * b."
parameters {
type_: OBJECT
properties {
key: "b" value { type_: NUMBER }}
properties {
key: "a" value { type_: NUMBER }}
required: "a" required: "b" }}]
55.
Function calling -Schema
Function Calling
● It's an OpenAPI schema, written as a protobuf.
● The protobuf-classes are available in the google.ai.generativelanguage
client library.
● Reference docs:
https://ai.google.dev/api/python/google/ai/generativelanguage/FunctionDeclar
ation
https://ai.google.dev/tutorials/function_calling_python_quickstart
56.
import google.ai.generativelanguage asglm
calculator = glm.Tool(
function_declarations=[
glm.FunctionDeclaration(
name='multiply',
description="Returns the product of two numbers.",
parameters=glm.Schema(
type=glm.Type.OBJECT,
properties={
'a': glm.Schema(type=glm.Type.NUMBER),
'b': glm.Schema(type=glm.Type.NUMBER)},
required=['a','b']))])
57.
Function calling -Schema
Function Calling
● They can be written out as JSON-compatible objects as well.
https://ai.google.dev/tutorials/function_calling_python_quickstart
58.
calculator = {
'function_declarations':[{
'name': 'multiply',
'description': 'Returns the product of two numbers.',
'parameters': {
'type': 'OBJECT',
'properties': {
'a': {'type': 'NUMBER'},
'b': {'type': 'NUMBER'}},
'required': ['a', 'b']}}]}
Function calling -Structured data
Function Calling
● Structured data extraction.
● You can just ask the model to do it and return JSON.
https://ai.google.dev/tutorials/structured_data_extraction
61.
response = model.generate_content(textwrap.dedent("""
Pleasereturn JSON describing the the people, places, things and relationships from this
story using the following schema:
{"people": list[PERSON], "places":list[PLACE], "things":list[THING], "relationships": list[RELATIONSHIP]}
PERSON = {"name": str, "description": str, "start_place_name": str, "end_place_name": str}
PLACE = {"name": str, "description": str}
THING = {"name": str, "description": str, "start_place_name": str, "end_place_name": str}
RELATIONSHIP = {"person_1_name": str, "person_2_name": str, "relationship": str}
Here is the story:
""") + story)
62.
Function calling -Structured data
Function Calling
● Asking for JSON often works.
● Function calling lets you strictly describe the schema.
● With a strict description, we can strictly enforce that that's
what gets returned.
https://ai.google.dev/tutorials/structured_data_extraction
#3 Large language models (LLMs) predict the next word in a sequence by:
Learning from patterns
LLMs learn to predict the next word by analyzing patterns in large amounts of text data. This process is called self-supervised pre-training.
Using a statistical model
LLMs use a statistical model of language to estimate the probability of the next word in a sequence.
Comparing predictions to actual words
LLMs compare their predictions to the actual next word in the data.
Adjusting parameters
LLMs use a mathematical process called backpropagation to adjust their parameters so they're more likely to predict the correct next word.
Analyzing the prompt
LLMs analyze the prompt and the predicted first word of the response to generate the second word.
Using decoding strategies
LLMs use decoding strategies to extract coherent text strings from their probability estimates.
#31 Parallel - For prompts such as "Get weather details in New Delhi and San Francisco?", the model may propose several parallel function calls. For a list of models that support parallel function calling, see Supported models.
#32 Parallel - For prompts such as "Get weather details in New Delhi and San Francisco?", the model may propose several parallel function calls. For a list of models that support parallel function calling, see Supported models.
#33 Parallel - For prompts such as "Get weather details in New Delhi and San Francisco?", the model may propose several parallel function calls. For a list of models that support parallel function calling, see Supported models.