diff --git a/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/gemini_langchain_helloworld.ipynb b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/gemini_langchain_helloworld.ipynb new file mode 100644 index 0000000..b187e7f --- /dev/null +++ b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/gemini_langchain_helloworld.ipynb @@ -0,0 +1,96 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "source": [ + "# First, install necessary packages for LangChain and Google Generative AI\n", + "!pip install langchain_core\n", + "!pip install langchain_google_genai" + ], + "metadata": { + "collapsed": true, + "id": "2GYbrBvnvEQh" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "\n", + "import os\n", + "from google.colab import userdata\n", + "\n", + "# Retrieve your Google API key from a secure location (Colab secret storage)\n", + "# Replace 'YOUR_SECRET_NAME' with the name of the environment variable storing your API key\n", + "google_api_key: str = userdata.get('GOOGLE_API_KEY')" + ], + "metadata": { + "id": "wFSgVJ4dvNxB" + }, + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "from langchain_google_genai import ChatGoogleGenerativeAI\n", + "\n", + "# Create an instance of the Google Generative AI model (LLM)\n", + "# - 'api_key' is required to authenticate with Google's API\n", + "# - 'model' specifies the version of the AI model to use (gemini-1.5-flash in this case)\n", + "# - 'temperature' controls the randomness of the responses (lower values = more focused responses)\n", + "llm: ChatGoogleGenerativeAI = ChatGoogleGenerativeAI(\n", + " api_key=google_api_key,\n", + " model=\"gemini-1.5-flash\",\n", + " temperature=0.2,\n", + ")" + ], + "metadata": { + "id": "KLWNhgjw1zrk" + }, + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Test the model by sending a simple \"hi\" message and print the AI's response\n", + "# The 'invoke' method is used to query the model and get a response\n", + "response: str = llm.invoke(\"hi\")\n", + "print(response)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "NkORDhrU__e3", + "outputId": "00a39bc2-778f-445a-f1e2-7bf41c3d4f2e" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "content='Hi there! How can I help you today? \\n' additional_kwargs={} response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'safety_ratings': [{'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability': 'NEGLIGIBLE', 'blocked': False}]} id='run-89af641a-5e89-42e3-b0a8-648d0ee614d1-0' usage_metadata={'input_tokens': 2, 'output_tokens': 11, 'total_tokens': 13}\n" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_key_14.jpg b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_key_14.jpg new file mode 100644 index 0000000..961554f Binary files /dev/null and b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_key_14.jpg differ diff --git a/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_key_15.jpg b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_key_15.jpg new file mode 100644 index 0000000..0e6b60e Binary files /dev/null and b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_key_15.jpg differ diff --git a/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_project.jpg b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_project.jpg new file mode 100644 index 0000000..9049182 Binary files /dev/null and b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_project.jpg differ diff --git a/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_project_1.jpg b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_project_1.jpg new file mode 100644 index 0000000..8799624 Binary files /dev/null and b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_project_1.jpg differ diff --git a/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_project_2.jpg b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_project_2.jpg new file mode 100644 index 0000000..f859b6a Binary files /dev/null and b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/google_project_2.jpg differ diff --git a/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/readme.md b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/readme.md new file mode 100644 index 0000000..1717b8c --- /dev/null +++ b/21_langchain_ecosystem/langchain/02_gemini_colab_helloworld/readme.md @@ -0,0 +1,56 @@ +# Google Gemini API with LangChain and Google Colab: + +**Prerequisites:** + +- A Google Cloud Platform account with a project enabled for the Gemini API. +- A Google Colab notebook. +- Python 3.6 or later installed. +- The following Python libraries installed: + - `langchain_core` + - `langchain_google_genai` + +**Steps:** + +1. **Create a Google Colab Notebook:** + - Open Google Colab (colab.research.google.com) and create a new notebook. + +2. **Install Required Libraries:** + - In the first cell of the notebook, run the following code to install the necessary libraries: + + ```python + !pip install langchain_core + !pip install langchain_google_genai + ``` + +3. **Create project with Google Cloud Platform:** + - Visit your google cloud account and create new project or use an existing one: [GOOGLE CLOUD ACCOUNT](https://console.cloud.google.com/projectcreate) + + - ![Create Project](google_project.jpg) + - ![Select Project](google_project_1.jpg) + - ![Check Project](google_project_2.jpg) + + +4. **Create Google API key in Google AI Studio:** + - Visit your google AI Studio account and create API KEY : [GOOGLE AI STUDIO](https://aistudio.google.com/app/apikey) + + - ![Create API KEY](google_key_14.jpg) + - ![Create API KEY](google_key_15.jpg) + +5. **Example code:** + + ```python + import os + from google.colab import userdata + from langchain_google_genai import ChatGoogleGenerativeAI + + # Replace 'YOUR_SECRET_NAME' with the actual name of your secret + google_api_key = userdata.get('GOOGLE_API_KEY') + + llm = ChatGoogleGenerativeAI( + api_key=google_api_key, + model="gemini-1.5-flash", + temperature=0.2, + ) + + print(llm.invoke("hi")) + ```