Skip to content

Commit 3e281e7

Browse files
Add sample for JSON controlled generation (#398)
* Add sample for JSON controlled generation * Update embeddings and JSON samples * Move import statement inside function * Remove import statement from inside function
1 parent 8e13711 commit 3e281e7

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

samples/embed.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,36 @@
1414
# limitations under the License.
1515
from absl.testing import absltest
1616

17-
import google
17+
1818
import google.generativeai as genai
19-
import pathlib
19+
2020

2121
class UnitTests(absltest.TestCase):
2222
def test_embed_content(self):
2323
# [START embed_content]
24+
2425
text = "Hello World!"
25-
result = genai.embed_content(model="models/text-embedding-004",
26-
content=text)
27-
print(result['embedding'])
26+
result = genai.embed_content(
27+
model="models/text-embedding-004", content=text, output_dimensionality=10
28+
)
29+
print(result["embedding"])
2830
print()
2931
# [END embed_content]
3032

3133
def batch_embed_content(self):
3234
# [START batch_embed_content]
33-
texts = ["What is the meaning of life?",
34-
"How much wood would a woodchuck chuck?",
35-
"How does the brain work?"]
36-
result = genai.embed_content(model="models/text-embedding-004",
37-
content=texts)
35+
texts = [
36+
"What is the meaning of life?",
37+
"How much wood would a woodchuck chuck?",
38+
"How does the brain work?",
39+
]
40+
result = genai.embed_content(
41+
model="models/text-embedding-004", content=texts, output_dimensionality=10
42+
)
3843
print(result)
3944
print()
4045
# [END batch_embed_content]
4146

47+
4248
if __name__ == "__main__":
43-
absltest.main()
49+
absltest.main()

samples/json_mode.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
from absl.testing import absltest
14+
15+
import google.generativeai as genai
16+
import typing_extensions as typing
17+
18+
19+
class UnitTests(absltest.TestCase):
20+
def test_controlled_generation(self):
21+
# [START controlled_generation]
22+
class Recipe(typing.TypedDict):
23+
recipe_name: str
24+
25+
model = genai.GenerativeModel("gemini-1.5-pro-latest")
26+
result = model.generate_content(
27+
"List a few popular cookie recipes.",
28+
generation_config=genai.GenerationConfig(
29+
response_mime_type="application/json", response_schema=list([Recipe])
30+
),
31+
)
32+
print(result)
33+
print()
34+
# [END controlled_generation]
35+
36+
37+
if __name__ == "__main__":
38+
absltest.main()

0 commit comments

Comments
 (0)