Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using gemini in colab with langchain #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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"))
```