Skip to content

Commit 4ba3121

Browse files
authored
Use genai.protos to access the raw protos. (google#446)
* replace glm reference with genai.protos * fix links * Fix more glm references * genai.protos * Generative Language API -> Gemini API
1 parent 8bcdbeb commit 4ba3121

File tree

10 files changed

+51
-81
lines changed

10 files changed

+51
-81
lines changed

examples/gemini/python/vectordb_with_chroma/vectordb_with_chroma.ipynb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
"import pandas as pd\n",
139139
"\n",
140140
"import google.generativeai as genai\n",
141-
"import google.ai.generativelanguage as glm\n",
142141
"\n",
143142
"# Used to securely store your API key\n",
144143
"from google.colab import userdata\n",

site/en/docs/search_reranking_using_embeddings.ipynb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@
184184
"import textwrap\n",
185185
"\n",
186186
"import google.generativeai as genai\n",
187-
"import google.ai.generativelanguage as glm\n",
188187
"\n",
189188
"import wikipedia\n",
190189
"from wikipedia.exceptions import DisambiguationError, PageError\n",
@@ -821,7 +820,7 @@
821820
"In the chat history you can see all 4 steps:\n",
822821
"\n",
823822
"1. The user sent the query.\n",
824-
"2. The model replied with a `glm.FunctionCall` calling the `wikipedia_search` with a number of relevant searches.\n",
823+
"2. The model replied with a `genai.protos.FunctionCall` calling the `wikipedia_search` with a number of relevant searches.\n",
825824
"3. Because you set `enable_automatic_function_calling=True` when creating the `genai.ChatSession`, it executed the search function and returned the list of article summaries to the model.\n",
826825
"4. Folliwing the instructions in the prompt, the model generated a final answer based on those summaries.\n"
827826
]
@@ -1044,9 +1043,9 @@
10441043
],
10451044
"source": [
10461045
"response = chat.send_message(\n",
1047-
" glm.Content(\n",
1048-
" parts=[glm.Part(\n",
1049-
" function_response = glm.FunctionResponse(\n",
1046+
" genai.protos.Content(\n",
1047+
" parts=[genai.protos.Part(\n",
1048+
" function_response = genai.protos.FunctionResponse(\n",
10501049
" name='wikipedia_search',\n",
10511050
" response={'result': summaries}\n",
10521051
" )\n",

site/en/gemini-api/docs/function-calling/python.ipynb

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
"import time\n",
132132
"\n",
133133
"import google.generativeai as genai\n",
134-
"import google.ai.generativelanguage as glm\n",
135134
"\n",
136135
"from IPython import display\n",
137136
"from IPython.display import Markdown\n",
@@ -206,7 +205,7 @@
206205
"\n",
207206
"To use function calling, pass a list of functions to the `tools` parameter when creating a [`GenerativeModel`](https://ai.google.dev/api/python/google/generativeai/GenerativeModel). The model uses the function name, docstring, parameters, and parameter type annotations to decide if it needs the function to best answer a prompt.\n",
208207
"\n",
209-
"> Important: The SDK converts function parameter type annotations to a format the API understands (`glm.FunctionDeclaration`). The API only supports a limited selection of parameter types, and the Python SDK's automatic conversion only supports a subset of that: `AllowedTypes = int | float | bool | str | list['AllowedTypes'] | dict`"
208+
"> Important: The SDK converts function parameter type annotations to a format the API understands (`genai.protos.FunctionDeclaration`). The API only supports a limited selection of parameter types, and the Python SDK's automatic conversion only supports a subset of that: `AllowedTypes = int | float | bool | str | list['AllowedTypes'] | dict`"
210209
]
211210
},
212211
{
@@ -327,13 +326,13 @@
327326
"source": [
328327
"Examine the chat history to see the flow of the conversation and how function calls are integrated within it.\n",
329328
"\n",
330-
"The `ChatSession.history` property stores a chronological record of the conversation between the user and the Gemini model. Each turn in the conversation is represented by a [`glm.Content`](https://ai.google.dev/api/python/google/ai/generativelanguage/Content) object, which contains the following information:\n",
329+
"The `ChatSession.history` property stores a chronological record of the conversation between the user and the Gemini model. Each turn in the conversation is represented by a [`genai.protos.Content`](https://ai.google.dev/api/python/google/generativeai/protos/Content) object, which contains the following information:\n",
331330
"\n",
332331
"* **Role**: Identifies whether the content originated from the \"user\" or the \"model\".\n",
333-
"* **Parts**: A list of [`glm.Part`](https://ai.google.dev/api/python/google/ai/generativelanguage/Part) objects that represent individual components of the message. With a text-only model, these parts can be:\n",
332+
"* **Parts**: A list of [`genai.protos.Part`](https://ai.google.dev/api/python/google/generativeai/protos/Part) objects that represent individual components of the message. With a text-only model, these parts can be:\n",
334333
" * **Text**: Plain text messages.\n",
335-
" * **Function Call** ([`glm.FunctionCall`](https://ai.google.dev/api/python/google/ai/generativelanguage/FunctionCall)): A request from the model to execute a specific function with provided arguments.\n",
336-
" * **Function Response** ([`glm.FunctionResponse`](https://ai.google.dev/api/python/google/ai/generativelanguage/FunctionResponse)): The result returned by the user after executing the requested function.\n",
334+
" * **Function Call** ([`genai.protos.FunctionCall`](https://ai.google.dev/api/python/google/generativeai/protos/FunctionCall)): A request from the model to execute a specific function with provided arguments.\n",
335+
" * **Function Response** ([`genai.protos.FunctionResponse`](https://ai.google.dev/api/python/google/generativeai/protos/FunctionResponse)): The result returned by the user after executing the requested function.\n",
337336
"\n",
338337
" In the previous example with the mittens calculation, the history shows the following sequence:\n",
339338
"\n",
@@ -400,7 +399,7 @@
400399
"source": [
401400
"While this was all handled automatically, if you need more control, you can:\n",
402401
"\n",
403-
"- Leave the default `enable_automatic_function_calling=False` and process the `glm.FunctionCall` responses yourself.\n",
402+
"- Leave the default `enable_automatic_function_calling=False` and process the `genai.protos.FunctionCall` responses yourself.\n",
404403
"- Or use `GenerativeModel.generate_content`, where you also need to manage the chat history."
405404
]
406405
},
@@ -541,7 +540,7 @@
541540
"\n",
542541
"# Build the response parts.\n",
543542
"response_parts = [\n",
544-
" glm.Part(function_response=glm.FunctionResponse(name=fn, response={\"result\": val}))\n",
543+
" genai.protos.Part(function_response=genai.protos.FunctionResponse(name=fn, response={\"result\": val}))\n",
545544
" for fn, val in responses.items()\n",
546545
"]\n",
547546
"\n",
@@ -570,18 +569,7 @@
570569
"AllowedType = (int | float | bool | str | list['AllowedType'] | dict[str, AllowedType]\n",
571570
"```\n",
572571
"\n",
573-
"The `google.ai.generativelanguage` client library provides access to the low level types giving you full control."
574-
]
575-
},
576-
{
577-
"cell_type": "code",
578-
"execution_count": null,
579-
"metadata": {
580-
"id": "S53E0EE8TBUF"
581-
},
582-
"outputs": [],
583-
"source": [
584-
"import google.ai.generativelanguage as glm"
572+
"The `google.generativeai.protos` submodule provides access to the low level types giving you full control."
585573
]
586574
},
587575
{
@@ -648,7 +636,7 @@
648636
"id": "qFD4U7ym04F5"
649637
},
650638
"source": [
651-
"This returns the list of `glm.Tool` objects that would be sent to the API. If the printed format is not familiar, it's because these are Google protobuf classes. Each `glm.Tool` (1 in this case) contains a list of `glm.FunctionDeclarations`, which describe a function and its arguments."
639+
"This returns the list of `genai.protos.Tool` objects that would be sent to the API. If the printed format is not familiar, it's because these are Google protobuf classes. Each `genai.protos.Tool` (1 in this case) contains a list of `genai.protos.FunctionDeclarations`, which describe a function and its arguments."
652640
]
653641
},
654642
{
@@ -657,7 +645,7 @@
657645
"id": "eY6RmFQ76FVu"
658646
},
659647
"source": [
660-
"Here is a declaration for the same multiply function written using the `glm` classes.\n",
648+
"Here is a declaration for the same multiply function written using the `genai.protos` classes.\n",
661649
"\n",
662650
"Note that these classes just describe the function for the API, they don't include an implementation of it. So using this doesn't work with automatic function calling, but functions don't always need an implementation."
663651
]
@@ -670,16 +658,16 @@
670658
},
671659
"outputs": [],
672660
"source": [
673-
"calculator = glm.Tool(\n",
661+
"calculator = genai.protos.Tool(\n",
674662
" function_declarations=[\n",
675-
" glm.FunctionDeclaration(\n",
663+
" genai.protos.FunctionDeclaration(\n",
676664
" name='multiply',\n",
677665
" description=\"Returns the product of two numbers.\",\n",
678-
" parameters=glm.Schema(\n",
679-
" type=glm.Type.OBJECT,\n",
666+
" parameters=genai.protos.Schema(\n",
667+
" type=genai.protos.Type.OBJECT,\n",
680668
" properties={\n",
681-
" 'a':glm.Schema(type=glm.Type.NUMBER),\n",
682-
" 'b':glm.Schema(type=glm.Type.NUMBER)\n",
669+
" 'a':genai.protos.Schema(type=genai.protos.Type.NUMBER),\n",
670+
" 'b':genai.protos.Schema(type=genai.protos.Type.NUMBER)\n",
683671
" },\n",
684672
" required=['a','b']\n",
685673
" )\n",
@@ -753,7 +741,7 @@
753741
}
754742
],
755743
"source": [
756-
"glm.Tool(calculator)"
744+
"genai.protos.Tool(calculator)"
757745
]
758746
},
759747
{
@@ -762,7 +750,7 @@
762750
"id": "jS6ruiTp6VBf"
763751
},
764752
"source": [
765-
"Either way, you pass a representation of a `glm.Tool` or list of tools to"
753+
"Either way, you pass a representation of a `genai.protos.Tool` or list of tools to"
766754
]
767755
},
768756
{
@@ -787,7 +775,7 @@
787775
"id": "517ca06297bb"
788776
},
789777
"source": [
790-
"Like before the model returns a `glm.FunctionCall` invoking the calculator's `multiply` function:"
778+
"Like before the model returns a `genai.protos.FunctionCall` invoking the calculator's `multiply` function:"
791779
]
792780
},
793781
{
@@ -889,9 +877,9 @@
889877
"outputs": [],
890878
"source": [
891879
"response = chat.send_message(\n",
892-
" glm.Content(\n",
893-
" parts=[glm.Part(\n",
894-
" function_response = glm.FunctionResponse(\n",
880+
" genai.protos.Content(\n",
881+
" parts=[genai.protos.Part(\n",
882+
" function_response = genai.protos.FunctionResponse(\n",
895883
" name='multiply',\n",
896884
" response={'result': result}))]))"
897885
]

site/en/gemini-api/docs/get-started/python.ipynb

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@
419419
"source": [
420420
"Gemini can generate multiple possible responses for a single prompt. These possible responses are called `candidates`, and you can review them to select the most suitable one as the response.\n",
421421
"\n",
422-
"View the response candidates with <a href=\"https://ai.google.dev/api/python/google/ai/generativelanguage/GenerateContentResponse#candidates\"><code>GenerateContentResponse.candidates</code></a>:"
422+
"View the response candidates with <a href=\"https://ai.google.dev/api/python/google/generativeai/protos/GenerateContentResponse#candidates\"><code>GenerateContentResponse.candidates</code></a>:"
423423
]
424424
},
425425
{
@@ -957,7 +957,7 @@
957957
"id": "AwCqtZ6D4kvk"
958958
},
959959
"source": [
960-
"`glm.Content` objects contain a list of `glm.Part` objects that each contain either a text (string) or inline_data (`glm.Blob`), where a blob contains binary data and a `mime_type`. The chat history is available as a list of `glm.Content` objects in `ChatSession.history`:"
960+
"`genai.protos.Content` objects contain a list of `genai.protos.Part` objects that each contain either a text (string) or inline_data (`genai.protos.Blob`), where a blob contains binary data and a `mime_type`. The chat history is available as a list of `genai.protos.Content` objects in `ChatSession.history`:"
961961
]
962962
},
963963
{
@@ -1033,7 +1033,7 @@
10331033
"source": [
10341034
"## Count tokens\n",
10351035
"\n",
1036-
"Large language models have a context window, and the context length is often measured in terms of the **number of tokens**. With the Gemini API, you can determine the number of tokens per any `glm.Content` object. In the simplest case, you can pass a query string to the `GenerativeModel.count_tokens` method as follows:"
1036+
"Large language models have a context window, and the context length is often measured in terms of the **number of tokens**. With the Gemini API, you can determine the number of tokens per any `genai.protos.Content` object. In the simplest case, you can pass a query string to the `GenerativeModel.count_tokens` method as follows:"
10371037
]
10381038
},
10391039
{
@@ -1188,9 +1188,9 @@
11881188
"id": "zBg0eNeml3d4"
11891189
},
11901190
"source": [
1191-
"While the `genai.embed_content` function accepts simple strings or lists of strings, it is actually built around the `glm.Content` type (like <a href=\"https://ai.google.dev/api/python/google/generativeai/GenerativeModel#generate_content\"><code>GenerativeModel.generate_content</code></a>). `glm.Content` objects are the primary units of conversation in the API.\n",
1191+
"While the `genai.embed_content` function accepts simple strings or lists of strings, it is actually built around the `genai.protos.Content` type (like <a href=\"https://ai.google.dev/api/python/google/generativeai/GenerativeModel#generate_content\"><code>GenerativeModel.generate_content</code></a>). `genai.protos.Content` objects are the primary units of conversation in the API.\n",
11921192
"\n",
1193-
"While the `glm.Content` object is multimodal, the `embed_content` method only supports text embeddings. This design gives the API the *possibility* to expand to multimodal embeddings."
1193+
"While the `genai.protos.Content` object is multimodal, the `embed_content` method only supports text embeddings. This design gives the API the *possibility* to expand to multimodal embeddings."
11941194
]
11951195
},
11961196
{
@@ -1248,7 +1248,7 @@
12481248
"id": "jU8juHCxoUKG"
12491249
},
12501250
"source": [
1251-
"Similarly, the chat history contains a list of `glm.Content` objects, which you can pass directly to the `embed_content` function:"
1251+
"Similarly, the chat history contains a list of `genai.protos.Content` objects, which you can pass directly to the `embed_content` function:"
12521252
]
12531253
},
12541254
{
@@ -1488,18 +1488,7 @@
14881488
"id": "-fthdIItnqki"
14891489
},
14901490
"source": [
1491-
"Underlying the Python SDK is the <a href=\"https://ai.google.dev/api/python/google/ai/generativelanguage\"><code>google.ai.generativelanguage</code></a> client library:"
1492-
]
1493-
},
1494-
{
1495-
"cell_type": "code",
1496-
"execution_count": null,
1497-
"metadata": {
1498-
"id": "l6aafWECnpX6"
1499-
},
1500-
"outputs": [],
1501-
"source": [
1502-
"import google.ai.generativelanguage as glm"
1491+
"The [`google.generativeai.protos`](https://ai.google.dev/api/python/google/generativeai/protos) submodule provides access to the low level classes used by the API behind the scenes:"
15031492
]
15041493
},
15051494
{
@@ -1508,10 +1497,10 @@
15081497
"id": "gm1RWcB3n_n0"
15091498
},
15101499
"source": [
1511-
"The SDK attempts to convert your message to a `glm.Content` object, which contains a list of `glm.Part` objects that each contain either:\n",
1500+
"The SDK attempts to convert your message to a `genai.protos.Content` object, which contains a list of `genai.protos.Part` objects that each contain either:\n",
15121501
"\n",
15131502
"1. a <a href=\"https://www.tensorflow.org/text/api_docs/python/text\"><code>text</code></a> (string)\n",
1514-
"2. `inline_data` (`glm.Blob`), where a blob contains binary `data` and a `mime_type`.\n",
1503+
"2. `inline_data` (`genai.protos.Blob`), where a blob contains binary `data` and a `mime_type`.\n",
15151504
"\n",
15161505
"You can also pass any of these classes as an equivalent dictionary.\n",
15171506
"\n",
@@ -1530,11 +1519,11 @@
15301519
"source": [
15311520
"model = genai.GenerativeModel('gemini-1.5-flash')\n",
15321521
"response = model.generate_content(\n",
1533-
" glm.Content(\n",
1522+
" genai.protos.Content(\n",
15341523
" parts = [\n",
1535-
" glm.Part(text=\"Write a short, engaging blog post based on this picture.\"),\n",
1536-
" glm.Part(\n",
1537-
" inline_data=glm.Blob(\n",
1524+
" genai.protos.Part(text=\"Write a short, engaging blog post based on this picture.\"),\n",
1525+
" genai.protos.Part(\n",
1526+
" inline_data=genai.protos.Blob(\n",
15381527
" mime_type='image/jpeg',\n",
15391528
" data=pathlib.Path('image.jpg').read_bytes()\n",
15401529
" )\n",
@@ -1581,9 +1570,9 @@
15811570
"\n",
15821571
"While the `genai.ChatSession` class shown earlier can handle many use cases, it does make some assumptions. If your use case doesn't fit into this chat implementation it's good to remember that `genai.ChatSession` is just a wrapper around <a href=\"https://ai.google.dev/api/python/google/generativeai/GenerativeModel#generate_content\"><code>GenerativeModel.generate_content</code></a>. In addition to single requests, it can handle multi-turn conversations.\n",
15831572
"\n",
1584-
"The individual messages are `glm.Content` objects or compatible dictionaries, as seen in previous sections. As a dictionary, the message requires `role` and `parts` keys. The `role` in a conversation can either be the `user`, which provides the prompts, or `model`, which provides the responses.\n",
1573+
"The individual messages are `genai.protos.Content` objects or compatible dictionaries, as seen in previous sections. As a dictionary, the message requires `role` and `parts` keys. The `role` in a conversation can either be the `user`, which provides the prompts, or `model`, which provides the responses.\n",
15851574
"\n",
1586-
"Pass a list of `glm.Content` objects and it will be treated as multi-turn chat:"
1575+
"Pass a list of `genai.protos.Content` objects and it will be treated as multi-turn chat:"
15871576
]
15881577
},
15891578
{

0 commit comments

Comments
 (0)