Google AI gemini Wow gemini-vision To access other generation models as well as models langchain-google-genai Integrated package ChatGoogleGenerativeAI If you use a class, you can.
Environment variable the user's Google API key GOOGLE_API_KEY Set to.
from dotenv import load_dotenv
load_dotenv()
True
# LangSmith Set up tracking. https://smith.langchain.com
# !pip install langchain-teddynote
from langchain_teddynote import logging
from langchain_teddynote.messages import stream_response
# Enter a project name.
logging.langsmith("CH04-Models")
Get the ChatGoogleGenerativeAI class from the langchain_google_genai package.
The ChatGoogleGenerativeAI class is used to implement an interactive AI system using Google's Generative AI model.
This class allows users to interact with Google's interactive AI model.
The conversation with the model takes place in the form of a chat, and the model generates an appropriate response based on the user's input.
The ChatGoogleGenerativeAI class is integrated with the LangChain framework, which can be used with other LangChain components.
Supported model information: https://ai.google.dev/gemini-api/docs/models/gemini?hl=ko
Safety Settings
Gemini models have basic safety settings (Satety Settings), but you can override them.
If you're getting a lot of "Safety Warnings" from the model, safety_settings You can adjust the properties.
Google's Safety Setting Types Documentation provides enumerated information about available categories and thresholds.
This document defines various categories and corresponding thresholds related to content filtering and safety settings, helping developers to select and apply appropriate safety settings when utilizing a generated AI model.
This allows developers to ensure the safety and adequacy of the content generated by the model, and to prevent harmful or inappropriate content from being exposed to users.
Run Batch units
Multimodal model
langchain-teddynote On the multi-modal model implemented in gemini-1.5-pro Images can be converted to text using the model.
from langchain_google_genai import ChatGoogleGenerativeAI
# ChatGoogleGenerativeAI Initialize the language model.
llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro-latest")
# Generate results by passing prompts.
answer = llm.stream("Briefly explain natural language processing")
# Prints the results.
stream_response(answer)
## What is Natural Language Processing (NLP)?
** It is a field of artificial intelligence that allows computers to understand and process human language.**
Easily speaking, it's a technique that allows computers to recognize, analyze, and even create new sentences in natural languages like Korean and English that we use in our daily lives.
** For example,**
* ** We asked the smartphone, "How is the weather in Seoul today?" Ask, ** NLP technology understands this question and finds weather information, "Seoul is clear today and the highest temperature is 25 degrees." Answer.
* ** Automatically create subtitles on YouTube,**
* ** Talk to chatbots at shopping malls and get product information ** All thanks to NLP technology.
**NLP is utilized in a variety of fields, including:**
* ** Machine translation:** Google translation, translation services like Papago
* ** Voice recognition:** Artificial intelligence secretary (Siri, Bixby, Google Assistant), dictation
* **Text summary:** Short summary of long articles
* **Emotion analysis:** Emotions in text or sentences (positive, negative, neutral, etc.) analysis
* ** Chatbot:** Automated conversation system for customer consultation, information provision, etc.
* ** Information Search:** Identify the user's search term intent and search for accurate information
* **Fake News Detection:** Determine and block fake news
**NLP is not perfect yet, but ** is developing rapidly with the development of artificial technology. It is expected to make our lives convenient in more diverse fields in the future.
from langchain_core.prompts import PromptTemplate
# ChatGoogleGenerativeAI Initialize the language model.
model = ChatGoogleGenerativeAI(
model="gemini-1.5-flash-latest", # Specifies the model to use.
)
# Generate a promp.
prompt = PromptTemplate.from_template(
"Answer yes/no questions. {question}is a fruit?"
)
# Create a chain.
chain = prompt | model
# Prints the results.
stream_response(chain.stream({"question": "apologize"}))
Yes
from langchain_google_genai import (
ChatGoogleGenerativeAI,
HarmBlockThreshold,
HarmCategory,
)
llm = ChatGoogleGenerativeAI(
# Which model to use "gemini-pro"is designated as.
model="gemini-1.5-pro-latest",
safety_settings={
# Set a blocking threshold for dangerous content.
# In this case, it is set not to block dangerous content. (However, there may still be basic blocking.다.)
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
},
)
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(
# which model to use "gemini-pro"is designated as.
model="gemini-1.5-pro-latest",
)
results = llm.batch(
[
"The capital of South Korea is?",
"List 5 major tourist destinations in South Korea",
]
)
for res in results:
# Prints the contents of each result.
print(res.content)
The capital of Korea is **Seoul**.
## 5 major tourist destinations in Korea (selected in various fields)
**1. History and Tradition:** ** Gyeongju Historic Site (Infantry, with petite)**
* Designated as a UNESCO World Cultural Heritage Site as a place that retains the history of the Shilla Millennium.
* Various historic sites such as Buddhist history, grottoam, augmentation, and non-national history are gathered, which is good for exploring history.
**2. Nature and relaxation:** ** Jeju Island**
* Boasting beautiful natural scenery with volcanic islands located in the southernmost part of Korea.
* There are many places designated as UNESCO World Natural Products, such as Halla San, Sunrise, and Unanim.
* Enjoy a variety of activities including sea bathing, mountaineering and olegil trekking.
**3. Cities and Culture:** ** Special poems in Seoul (including Gyeongbokgung Palace and North Korean Kwan)**
* The capital of Korea and the center of politics, economy and culture.
* An attractive city where tradition and modernity coexist, including Gyeongbokgung Palace, Changdeokgung Palace, and Deoksugung Palace.
* Various cultural experiences such as shopping, K-POP, and art are available.
**4. Shopping and theme parks:** ** City of Busan and Gwang (including shipping and Gwangcheon culture)**
* Korea's second city and international maritime city.
* Beautiful beaches such as shipping and beach, and a variety of food and plenty to enjoy.
* Various festivals are held, including Busan International Film Festival, as the city of cinema.
**5. Space of peace and history:** **DMZ (non-armed) and Panmunjom**
* A place where you can feel the pain and division of the Korean War.
* Recently, it has emerged as a peace tourist destination in the atmosphere of inter-Korean reconciliation.
* You can revisit the history of the division by touring the 3rd pane, the bell network, and the gate point.
**Note:** In addition to this, there are many attractive tourist destinations in Korea, including Sulakshan National Park, Suncheon Bay Marsh, and Jeonju Hanokma. Choose a place that suits your travel tastes and purposes to plan your trip.
from langchain_teddynote.models import MultiModal
from langchain_teddynote.messages import stream_response
# Object creation
gemini = ChatGoogleGenerativeAI(model="gemini-1.5-pro-latest")
system_prompt = (
"You are a poet. Your task is to write a poem with the given image."
)
user_prompt = "Please write a poem about the following image."
# Creating multimodal objects
multimodal_gemini = MultiModal(
llm, system_prompt=system_prompt, user_prompt=user_prompt
)
# Specify the sample image path (file path, URL, etc.)
IMAGE_URL = "images/jeju-beach.jpg"
# Query from image file
answer = multimodal_gemini.stream(IMAGE_URL)
# Outputs each token in a streaming manner (real-time output).
stream_response(answer)Object creation
One island in a sky mirror
Falling asleep in calm peace
I'm lying with the blue tabernacle
White lace embroidered
Calm, noble, nerd
The transparent, non-confronting sea
Shines with dazzling emerald light
Black rocks silently
In the same landscape that will be eternal
Looks full of awe
Time is breathing
The wind sings whispering
Where all the noise in the world has disappeared
Only nature's breath
A place that permeates the heart
A piece of paradise is here.