Skip to content

Commit 324e59e

Browse files
committed
Update gemini_api_walkthrough.ipynb
1 parent a346d45 commit 324e59e

File tree

1 file changed

+284
-2
lines changed

1 file changed

+284
-2
lines changed

Module 9 - GenAI (LLMs and Prompt Engineering)/4. Google Gemini Walkthrough/gemini_api_walkthrough.ipynb

Lines changed: 284 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,301 @@
141141
"print(response.text)"
142142
]
143143
},
144+
{
145+
"cell_type": "markdown",
146+
"id": "59aba55e-c0a3-416e-8c94-4ac1ffeae39c",
147+
"metadata": {},
148+
"source": [
149+
"## **Adding a System Prompt**\n",
150+
"\n",
151+
"**Important Note:** System Prompt can be specified using `system_instruction`. `system_instruction` is not enabled for models/gemini-pro."
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": 11,
157+
"id": "9b18e562-75f5-4eb0-a312-f7a30fccbec7",
158+
"metadata": {},
159+
"outputs": [
160+
{
161+
"name": "stdout",
162+
"output_type": "stream",
163+
"text": [
164+
"terrestrial planet, the third planet from the Sun, and the only astronomical object known to harbor life. \n",
165+
"\n"
166+
]
167+
}
168+
],
169+
"source": [
170+
"model = genai.GenerativeModel(model_name=\"models/gemini-1.5-pro-latest\", \n",
171+
" system_instruction=\"\"\"Generate some factual information to complete the user input. \n",
172+
" Completion must have maximum 2-3 lines.\"\"\")\n",
173+
"\n",
174+
"user_prompt = \"\"\"In our solar system, Earth is a \"\"\"\n",
175+
"\n",
176+
"response = model.generate_content(user_prompt)\n",
177+
"\n",
178+
"print(response.text)"
179+
]
180+
},
144181
{
145182
"cell_type": "markdown",
146183
"id": "27c53a90-0a98-4404-87b2-1d17e5bca01f",
147184
"metadata": {},
148185
"source": [
149-
"## **Converstation AI using Gemini AI**"
186+
"## **Conversation AI using Gemini AI**"
150187
]
151188
},
152189
{
153190
"cell_type": "code",
154-
"execution_count": null,
191+
"execution_count": 12,
155192
"id": "513a14c4-0211-473c-80d8-90a918376cac",
156193
"metadata": {},
194+
"outputs": [
195+
{
196+
"data": {
197+
"text/plain": [
198+
"ChatSession(\n",
199+
" model=genai.GenerativeModel(\n",
200+
" model_name='models/gemini-pro',\n",
201+
" generation_config={},\n",
202+
" safety_settings={},\n",
203+
" tools=None,\n",
204+
" system_instruction=None,\n",
205+
" ),\n",
206+
" history=[]\n",
207+
")"
208+
]
209+
},
210+
"execution_count": 12,
211+
"metadata": {},
212+
"output_type": "execute_result"
213+
}
214+
],
215+
"source": [
216+
"model = genai.GenerativeModel('gemini-pro')\n",
217+
"\n",
218+
"chat = model.start_chat(history=[])\n",
219+
"\n",
220+
"chat"
221+
]
222+
},
223+
{
224+
"cell_type": "markdown",
225+
"id": "233ea38d-c1a5-4b0f-b8ce-ca080f0d0d9b",
226+
"metadata": {},
227+
"source": [
228+
"Gemini enables you to have freeform conversations across multiple turns. The `ChatSession` class simplifies the process by managing the state of the conversation, so unlike with `generate_content`, you do not have to store the conversation history as a list."
229+
]
230+
},
231+
{
232+
"cell_type": "code",
233+
"execution_count": 14,
234+
"id": "b146e8d1-ecb6-438e-ad05-cc191d04f817",
235+
"metadata": {},
236+
"outputs": [
237+
{
238+
"data": {
239+
"text/plain": [
240+
"[]"
241+
]
242+
},
243+
"execution_count": 14,
244+
"metadata": {},
245+
"output_type": "execute_result"
246+
}
247+
],
248+
"source": [
249+
"chat.history"
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"execution_count": 15,
255+
"id": "59c37c2b-4fdb-4400-82d6-b227614ed1a5",
256+
"metadata": {},
257+
"outputs": [
258+
{
259+
"name": "stdout",
260+
"output_type": "stream",
261+
"text": [
262+
"**Logistic Regression**\n",
263+
"\n",
264+
"Logistic regression is a statistical model used to predict the probability of an event occurring, typically used for binary classification problems (where the output is either 0 or 1). It uses a logistic function to model the relationship between independent variables (predictors) and the probability of the dependent variable (target).\n",
265+
"\n",
266+
"**Logistic Function:**\n",
267+
"\n",
268+
"The logistic function, also known as the sigmoid function, is a sigmoidal curve that maps a real number input to a probability between 0 and 1. It is defined as:\n",
269+
"\n",
270+
"```\n",
271+
"f(x) = 1 / (1 + e^(-x))\n",
272+
"```\n",
273+
"\n",
274+
"**Model:**\n",
275+
"\n",
276+
"The logistic regression model takes the form:\n",
277+
"\n",
278+
"```\n",
279+
"P(y = 1 | x) = 1 / (1 + e^(-(b + w1x1 + w2x2 + ... + wnxn)))\n",
280+
"```\n",
281+
"\n",
282+
"where:\n",
283+
"\n",
284+
"* P(y = 1 | x) is the probability of the event occurring given the input data vector x\n",
285+
"* b is the intercept\n",
286+
"* w1, w2, ..., wn are the coefficients (weights) for each independent variable xi\n",
287+
"\n",
288+
"**Interpretation:**\n",
289+
"\n",
290+
"The coefficients (weights) in the logistic regression model represent the relative importance of each independent variable in predicting the probability of the event occurring. A positive coefficient indicates that the variable increases the probability of the event, while a negative coefficient indicates a decrease.\n",
291+
"\n",
292+
"**Assumptions:**\n",
293+
"\n",
294+
"Logistic regression makes the following assumptions:\n",
295+
"\n",
296+
"* The independent variables are linearly related to the log-odds of the event occurring.\n",
297+
"* The observations are independent.\n",
298+
"\n",
299+
"**Applications:**\n",
300+
"\n",
301+
"Logistic regression is widely used in various fields, including:\n",
302+
"\n",
303+
"* Medical diagnosis\n",
304+
"* Credit risk assessment\n",
305+
"* Fraud detection\n",
306+
"* Marketing segmentation\n",
307+
"* Customer churn prediction\n",
308+
"\n",
309+
"**Advantages:**\n",
310+
"\n",
311+
"* Easy to interpret and implement\n",
312+
"* Robust to outliers\n",
313+
"* Suitable for binary classification problems\n",
314+
"\n",
315+
"**Disadvantages:**\n",
316+
"\n",
317+
"* Not suitable for multi-class classification problems\n",
318+
"* Requires large sample sizes\n"
319+
]
320+
}
321+
],
322+
"source": [
323+
"user_input = \"Explain the concept of Logistic Regression.\"\n",
324+
"\n",
325+
"response = chat.send_message(user_input)\n",
326+
"\n",
327+
"print(response.text)"
328+
]
329+
},
330+
{
331+
"cell_type": "code",
332+
"execution_count": 16,
333+
"id": "50808f14-194d-4229-8013-4510ccf06ae1",
334+
"metadata": {},
335+
"outputs": [
336+
{
337+
"data": {
338+
"text/plain": [
339+
"[parts {\n",
340+
" text: \"Explain the concept of Logistic Regression.\"\n",
341+
" }\n",
342+
" role: \"user\",\n",
343+
" parts {\n",
344+
" text: \"**Logistic Regression**\\n\\nLogistic regression is a statistical model used to predict the probability of an event occurring, typically used for binary classification problems (where the output is either 0 or 1). It uses a logistic function to model the relationship between independent variables (predictors) and the probability of the dependent variable (target).\\n\\n**Logistic Function:**\\n\\nThe logistic function, also known as the sigmoid function, is a sigmoidal curve that maps a real number input to a probability between 0 and 1. It is defined as:\\n\\n```\\nf(x) = 1 / (1 + e^(-x))\\n```\\n\\n**Model:**\\n\\nThe logistic regression model takes the form:\\n\\n```\\nP(y = 1 | x) = 1 / (1 + e^(-(b + w1x1 + w2x2 + ... + wnxn)))\\n```\\n\\nwhere:\\n\\n* P(y = 1 | x) is the probability of the event occurring given the input data vector x\\n* b is the intercept\\n* w1, w2, ..., wn are the coefficients (weights) for each independent variable xi\\n\\n**Interpretation:**\\n\\nThe coefficients (weights) in the logistic regression model represent the relative importance of each independent variable in predicting the probability of the event occurring. A positive coefficient indicates that the variable increases the probability of the event, while a negative coefficient indicates a decrease.\\n\\n**Assumptions:**\\n\\nLogistic regression makes the following assumptions:\\n\\n* The independent variables are linearly related to the log-odds of the event occurring.\\n* The observations are independent.\\n\\n**Applications:**\\n\\nLogistic regression is widely used in various fields, including:\\n\\n* Medical diagnosis\\n* Credit risk assessment\\n* Fraud detection\\n* Marketing segmentation\\n* Customer churn prediction\\n\\n**Advantages:**\\n\\n* Easy to interpret and implement\\n* Robust to outliers\\n* Suitable for binary classification problems\\n\\n**Disadvantages:**\\n\\n* Not suitable for multi-class classification problems\\n* Requires large sample sizes\"\n",
345+
" }\n",
346+
" role: \"model\"]"
347+
]
348+
},
349+
"execution_count": 16,
350+
"metadata": {},
351+
"output_type": "execute_result"
352+
}
353+
],
354+
"source": [
355+
"chat.history"
356+
]
357+
},
358+
{
359+
"cell_type": "code",
360+
"execution_count": 18,
361+
"id": "eaae305c-b6eb-42b5-8a4e-6b61612e1e45",
362+
"metadata": {},
363+
"outputs": [
364+
{
365+
"name": "stdout",
366+
"output_type": "stream",
367+
"text": [
368+
">> user: Explain the concept of Logistic Regression.\n",
369+
">> model: **Logistic Regression**\n",
370+
"\n",
371+
"Logistic regression is a statistical model used to predict the probability of an event occurring, typically used for binary classification problems (where the output is either 0 or 1). It uses a logistic function to model the relationship between independent variables (predictors) and the probability of the dependent variable (target).\n",
372+
"\n",
373+
"**Logistic Function:**\n",
374+
"\n",
375+
"The logistic function, also known as the sigmoid function, is a sigmoidal curve that maps a real number input to a probability between 0 and 1. It is defined as:\n",
376+
"\n",
377+
"```\n",
378+
"f(x) = 1 / (1 + e^(-x))\n",
379+
"```\n",
380+
"\n",
381+
"**Model:**\n",
382+
"\n",
383+
"The logistic regression model takes the form:\n",
384+
"\n",
385+
"```\n",
386+
"P(y = 1 | x) = 1 / (1 + e^(-(b + w1x1 + w2x2 + ... + wnxn)))\n",
387+
"```\n",
388+
"\n",
389+
"where:\n",
390+
"\n",
391+
"* P(y = 1 | x) is the probability of the event occurring given the input data vector x\n",
392+
"* b is the intercept\n",
393+
"* w1, w2, ..., wn are the coefficients (weights) for each independent variable xi\n",
394+
"\n",
395+
"**Interpretation:**\n",
396+
"\n",
397+
"The coefficients (weights) in the logistic regression model represent the relative importance of each independent variable in predicting the probability of the event occurring. A positive coefficient indicates that the variable increases the probability of the event, while a negative coefficient indicates a decrease.\n",
398+
"\n",
399+
"**Assumptions:**\n",
400+
"\n",
401+
"Logistic regression makes the following assumptions:\n",
402+
"\n",
403+
"* The independent variables are linearly related to the log-odds of the event occurring.\n",
404+
"* The observations are independent.\n",
405+
"\n",
406+
"**Applications:**\n",
407+
"\n",
408+
"Logistic regression is widely used in various fields, including:\n",
409+
"\n",
410+
"* Medical diagnosis\n",
411+
"* Credit risk assessment\n",
412+
"* Fraud detection\n",
413+
"* Marketing segmentation\n",
414+
"* Customer churn prediction\n",
415+
"\n",
416+
"**Advantages:**\n",
417+
"\n",
418+
"* Easy to interpret and implement\n",
419+
"* Robust to outliers\n",
420+
"* Suitable for binary classification problems\n",
421+
"\n",
422+
"**Disadvantages:**\n",
423+
"\n",
424+
"* Not suitable for multi-class classification problems\n",
425+
"* Requires large sample sizes\n"
426+
]
427+
}
428+
],
429+
"source": [
430+
"for message in chat.history:\n",
431+
" print(f\">> {message.role}: {message.parts[0].text}\" )"
432+
]
433+
},
434+
{
435+
"cell_type": "code",
436+
"execution_count": null,
437+
"id": "8ce35971-c8b9-4c9b-96ea-cc77b0f019f9",
438+
"metadata": {},
157439
"outputs": [],
158440
"source": []
159441
}

0 commit comments

Comments
 (0)