{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "!pip install -q -U google-generativeai" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dotenv import load_dotenv\n", "import os\n", "load_dotenv()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/vasim/.local/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "import pathlib\n", "import textwrap\n", "\n", "import google.generativeai as genai\n", "\n", "from IPython.display import display\n", "from IPython.display import Markdown\n", "\n", "\n", "def to_markdown(text):\n", " text = text.replace('•', ' *')\n", " return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "genai.configure(api_key=os.getenv(\"GOOGLE_API_KEY\"))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "models/gemini-pro\n", "models/gemini-pro-vision\n" ] } ], "source": [ "for m in genai.list_models():\n", " if 'generateContent' in m.supported_generation_methods:\n", " print(m.name)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "model = genai.GenerativeModel('gemini-pro')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 44.3 ms, sys: 615 µs, total: 44.9 ms\n", "Wall time: 9.75 s\n" ] } ], "source": [ "%%time\n", "response = model.generate_content(\"What is the meaning of life?\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "> The meaning of life is a philosophical question that has been pondered by people for thousands of years. There is no one answer that is universally agreed upon, as the meaning of life is subjective and varies from person to person. However, some common themes that emerge when people discuss the meaning of life include:\n", "> \n", "> * **Happiness and fulfillment:** Many people believe that the meaning of life is to be happy and fulfilled. This can involve pursuing activities that bring you joy, such as spending time with loved ones, pursuing your hobbies, or making a difference in the world.\n", "> * **Purpose and meaning:** Some people believe that the meaning of life is to find a purpose or calling that gives their life meaning. This could involve pursuing a career that you are passionate about, volunteering your time to help others, or raising a family.\n", "> * **Connection and belonging:** Many people believe that the meaning of life is to connect with others and feel a sense of belonging. This can involve spending time with friends and family, participating in community activities, or contributing to society in a meaningful way.\n", "> * **Growth and learning:** Some people believe that the meaning of life is to grow and learn as a person. This can involve challenging yourself to try new things, stepping outside of your comfort zone, and expanding your knowledge and understanding of the world.\n", "> \n", "> Ultimately, the meaning of life is something that each person must decide for themselves. There is no right or wrong answer, and the meaning of life can change over time as you grow and learn. The important thing is to find a meaning that resonates with you and that gives your life purpose and direction.\n", "> \n", "> Here are some additional thoughts on the meaning of life from a variety of sources:\n", "> \n", "> * \"The meaning of life is to give life meaning.\" - Viktor Frankl\n", "> * \"The purpose of life is not to be happy. It is to be useful, to be honorable, to be compassionate, to have it make some difference that you have lived and lived well.\" - Ralph Waldo Emerson\n", "> * \"When you discover your purpose, you will feel driven to pursue it. Your whole life will change as you make that pursuit your top priority.\" - John Wooden\n", "> * \"The meaning of life is whatever you assign to it, and the value of life is whatever you choose to make it.\" - John C. Maxwell\n", "> * \"Life is not a problem to be solved, but a gift to be enjoyed.\" - Dalai Lama" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "to_markdown(response.text)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "safety_ratings {\n", " category: HARM_CATEGORY_SEXUALLY_EXPLICIT\n", " probability: NEGLIGIBLE\n", "}\n", "safety_ratings {\n", " category: HARM_CATEGORY_HATE_SPEECH\n", " probability: NEGLIGIBLE\n", "}\n", "safety_ratings {\n", " category: HARM_CATEGORY_HARASSMENT\n", " probability: NEGLIGIBLE\n", "}\n", "safety_ratings {\n", " category: HARM_CATEGORY_DANGEROUS_CONTENT\n", " probability: NEGLIGIBLE\n", "}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response.prompt_feedback" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[index: 0\n", "content {\n", " parts {\n", " text: \"The meaning of life is a philosophical question that has been pondered by people for thousands of years. There is no one answer that is universally agreed upon, as the meaning of life is subjective and varies from person to person. However, some common themes that emerge when people discuss the meaning of life include:\\n\\n* **Happiness and fulfillment:** Many people believe that the meaning of life is to be happy and fulfilled. This can involve pursuing activities that bring you joy, such as spending time with loved ones, pursuing your hobbies, or making a difference in the world.\\n* **Purpose and meaning:** Some people believe that the meaning of life is to find a purpose or calling that gives their life meaning. This could involve pursuing a career that you are passionate about, volunteering your time to help others, or raising a family.\\n* **Connection and belonging:** Many people believe that the meaning of life is to connect with others and feel a sense of belonging. This can involve spending time with friends and family, participating in community activities, or contributing to society in a meaningful way.\\n* **Growth and learning:** Some people believe that the meaning of life is to grow and learn as a person. This can involve challenging yourself to try new things, stepping outside of your comfort zone, and expanding your knowledge and understanding of the world.\\n\\nUltimately, the meaning of life is something that each person must decide for themselves. There is no right or wrong answer, and the meaning of life can change over time as you grow and learn. The important thing is to find a meaning that resonates with you and that gives your life purpose and direction.\\n\\nHere are some additional thoughts on the meaning of life from a variety of sources:\\n\\n* \\\"The meaning of life is to give life meaning.\\\" - Viktor Frankl\\n* \\\"The purpose of life is not to be happy. It is to be useful, to be honorable, to be compassionate, to have it make some difference that you have lived and lived well.\\\" - Ralph Waldo Emerson\\n* \\\"When you discover your purpose, you will feel driven to pursue it. Your whole life will change as you make that pursuit your top priority.\\\" - John Wooden\\n* \\\"The meaning of life is whatever you assign to it, and the value of life is whatever you choose to make it.\\\" - John C. Maxwell\\n* \\\"Life is not a problem to be solved, but a gift to be enjoyed.\\\" - Dalai Lama\"\n", " }\n", " role: \"model\"\n", "}\n", "finish_reason: STOP\n", "safety_ratings {\n", " category: HARM_CATEGORY_SEXUALLY_EXPLICIT\n", " probability: NEGLIGIBLE\n", "}\n", "safety_ratings {\n", " category: HARM_CATEGORY_HATE_SPEECH\n", " probability: NEGLIGIBLE\n", "}\n", "safety_ratings {\n", " category: HARM_CATEGORY_HARASSMENT\n", " probability: NEGLIGIBLE\n", "}\n", "safety_ratings {\n", " category: HARM_CATEGORY_DANGEROUS_CONTENT\n", " probability: NEGLIGIBLE\n", "}\n", "citation_metadata {\n", " citation_sources {\n", " start_index: 1850\n", " end_index: 2013\n", " uri: \"https://labor.alaska.gov/dvr/newsletter/dvr-newsletter-2020-03-director.htm\"\n", " license_: \"\"\n", " }\n", " citation_sources {\n", " start_index: 1916\n", " end_index: 2045\n", " uri: \"http://www.marksoffroad.net/Quotes.html\"\n", " license_: \"\"\n", " }\n", "}\n", "]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response.candidates" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }