Skip to content

Commit 5ed2855

Browse files
authored
Add new chat and generate_content templates.
1 parent 8f08c6e commit 5ed2855

File tree

2 files changed

+592
-0
lines changed

2 files changed

+592
-0
lines changed
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "Tce3stUlHN0L"
7+
},
8+
"source": [
9+
"##### Copyright 2023 Google LLC"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {
16+
"id": "tuOe1ymfHZPu"
17+
},
18+
"outputs": [],
19+
"source": [
20+
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
21+
"# you may not use this file except in compliance with the License.\n",
22+
"# You may obtain a copy of the License at\n",
23+
"#\n",
24+
"# https://www.apache.org/licenses/LICENSE-2.0\n",
25+
"#\n",
26+
"# Unless required by applicable law or agreed to in writing, software\n",
27+
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
28+
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
29+
"# See the License for the specific language governing permissions and\n",
30+
"# limitations under the License."
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"source": [
36+
"## Setup"
37+
],
38+
"metadata": {
39+
"id": "FKwyTRdwB8aW"
40+
}
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"source": [
45+
"### Install & import\n"
46+
],
47+
"metadata": {
48+
"id": "rlE8UqxrDIez"
49+
}
50+
},
51+
{
52+
"cell_type": "code",
53+
"source": [
54+
"!pip install google-generativelanguage"
55+
],
56+
"metadata": {
57+
"id": "cZiU4TKzznh9"
58+
},
59+
"execution_count": null,
60+
"outputs": []
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"metadata": {
66+
"id": "kWIuwKG2_oWE"
67+
},
68+
"outputs": [],
69+
"source": [
70+
"# Install the client library and import necessary modules.\n",
71+
"#!pip install google-generativeai\n",
72+
"import google.generativeai as genai\n",
73+
"import json\n",
74+
"import pathlib\n",
75+
"import pprint\n",
76+
"import requests\n",
77+
"import mimetypes\n",
78+
"from IPython.display import Markdown"
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"source": [
84+
"\n",
85+
"\n",
86+
"### Mount Google Drive"
87+
],
88+
"metadata": {
89+
"id": "qZsRPVv1ITbh"
90+
}
91+
},
92+
{
93+
"cell_type": "code",
94+
"source": [
95+
"from google.colab import drive\n",
96+
"drive.mount('/gdrive')"
97+
],
98+
"metadata": {
99+
"id": "d9-t_OkGoLIP"
100+
},
101+
"execution_count": null,
102+
"outputs": []
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"source": [
107+
"## Set the API key"
108+
],
109+
"metadata": {
110+
"id": "Fet3lFjdKHEM"
111+
}
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"source": [
116+
"Add your API_KEY to the secrets manager in the left pannel \"🔑\"."
117+
],
118+
"metadata": {
119+
"id": "ZoRWILAtCzBE"
120+
}
121+
},
122+
{
123+
"cell_type": "code",
124+
"source": [
125+
"from google.colab import userdata\n",
126+
"\n",
127+
"API_KEY=userdata.get('API_KEY')"
128+
],
129+
"metadata": {
130+
"id": "LaLCwNlkCyQd"
131+
},
132+
"execution_count": null,
133+
"outputs": []
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": null,
138+
"metadata": {
139+
"id": "_SvYoR3WCeKr"
140+
},
141+
"outputs": [],
142+
"source": [
143+
"# Configure the client library by providing your API key.\n",
144+
"genai.configure(api_key=API_KEY)"
145+
]
146+
},
147+
{
148+
"cell_type": "markdown",
149+
"source": [
150+
"### Parse the arguments"
151+
],
152+
"metadata": {
153+
"id": "weo-o73WDpdm"
154+
}
155+
},
156+
{
157+
"cell_type": "code",
158+
"source": [
159+
"import json\n",
160+
"\n",
161+
"model = \"gemini-pro\" # @param {isTemplate: true}\n",
162+
"contents = '[{\"role\":\"user\", \"parts\" : [{\"text\": \"hello\"}]}, {\"role\": \"model\", \"parts\": [{\"text\": \"Hello! How can I assist you today?\"}]}]' # @param {isTemplate: true}\n",
163+
"generation_config = \"{}\" # @param {isTemplate: true}\n",
164+
"safety_settings = \"{}\" # @param {isTemplate: true}\n",
165+
"user_input = 'How does electricity work?' #@param {isTemplate: true}\n",
166+
"\n",
167+
"contents = json.loads(contents)\n",
168+
"generation_config = json.loads(generation_config)\n",
169+
"safety_settings = json.loads(safety_settings)\n",
170+
"\n",
171+
"\n",
172+
"stream = False"
173+
],
174+
"metadata": {
175+
"id": "uIog-0SyDuIF"
176+
},
177+
"execution_count": null,
178+
"outputs": []
179+
},
180+
{
181+
"cell_type": "code",
182+
"source": [
183+
"contents"
184+
],
185+
"metadata": {
186+
"id": "wBS8xNhN0x62"
187+
},
188+
"execution_count": null,
189+
"outputs": []
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"source": [
194+
"### Call the API"
195+
],
196+
"metadata": {
197+
"id": "E7zAD69vE92b"
198+
}
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": null,
203+
"metadata": {
204+
"id": "LB2LxPmAB95V"
205+
},
206+
"outputs": [],
207+
"source": [
208+
"# Call the model and print the response.\n",
209+
"gemini = genai.GenerativeModel(model_name=model)\n",
210+
"\n",
211+
"chat = gemini.start_chat(history=contents)\n",
212+
"\n",
213+
"response = chat.send_message(\n",
214+
" user_input,\n",
215+
" stream=False)"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"source": [
221+
"display(Markdown(response.text))"
222+
],
223+
"metadata": {
224+
"id": "Lm3RXwYuGtZK"
225+
},
226+
"execution_count": null,
227+
"outputs": []
228+
},
229+
{
230+
"cell_type": "code",
231+
"source": [
232+
"response.prompt_feedback"
233+
],
234+
"metadata": {
235+
"id": "JbKuUc3NGxYD"
236+
},
237+
"execution_count": null,
238+
"outputs": []
239+
},
240+
{
241+
"cell_type": "code",
242+
"source": [
243+
"response.candidates"
244+
],
245+
"metadata": {
246+
"id": "SLAaIq3kgwwJ"
247+
},
248+
"execution_count": null,
249+
"outputs": []
250+
},
251+
{
252+
"cell_type": "code",
253+
"source": [],
254+
"metadata": {
255+
"id": "8kS1DntQ2TKM"
256+
},
257+
"execution_count": null,
258+
"outputs": []
259+
}
260+
],
261+
"metadata": {
262+
"colab": {
263+
"provenance": [],
264+
"private_outputs": true,
265+
"collapsed_sections": [
266+
"Tce3stUlHN0L"
267+
]
268+
},
269+
"kernelspec": {
270+
"display_name": "Python 3",
271+
"name": "python3"
272+
}
273+
},
274+
"nbformat": 4,
275+
"nbformat_minor": 0
276+
}

0 commit comments

Comments
 (0)