LangChain #2: Quick Start

Querying OpenAI, HuggingFace, And Gemini

In this chapter, we delve into the practical aspects of implementing cutting-edge language models using LangChain.

We’ll embark on a hands-on journey, focusing on the straightforward integration of three distinct models: OpenAI, a versatile model from Hugging Face, and the Gemini model.

OpenAI

pip install openai

Go and get your API key from the OPENAI web-site.

https://openai.com/ -> login -> API -> API KEYS

Firstly, set the value of the OPENAI_API_KEY environment variable to your actual OpenAI API key. Setting the API key as an environment variable is a common practice to manage credentials securely(not for production). It keeps the key out of your source code, reducing the risk of accidentally exposing it.

import os
os.environ["OPENAI_API_KEY"] = "your-api-key"        

The OpenAI class from the langchain.llms module in the LangChain library is designed to interface with OpenAI's language models.

from langchain.llms import OpenAI        

We can use any models provided by OpenAI. You can reach the models here.

llm = OpenAI(model="text-davinci-003")

query = "Explain the process of photosynthesis in detail."
answer = llm(query)
answer

"""
"\n\nPhotosynthesis is the process by which plants, algae, and some bacteria 
convert light energy from the sun into chemical energy stored in the form of 
glucose. This process is essential for all plant life and is responsible for 
the presence of oxygen in the Earth's atmosphere.\n\nThe process of 
photosynthesis requires three primary components: light, carbon dioxide, 
and water. When these three components are present, the plant absorbs light
 energy through the chloroplasts in its leaves. This energy is then used to 
convert carbon dioxide and water into oxygen and glucose (a type of sugar).
\n\nThe light energy activates the chlorophyll molecules in the chloroplasts,
 which then use the energy to break down the carbon dioxide and water
 molecules into their component parts. The oxygen is then released into the 
atmosphere, while the glucose molecules are used to fuel the plant's growth 
and development.\n\nTo begin the photosynthesis process, the plant absorbs 
light energy through the chloroplasts in its leaves. This light energy is 
converted into chemical energy in the form of ATP and NADPH. The ATP and 
NADPH are then used in a series of reactions known as the light-dependent 
reactions, which release oxygen as a by-product.\n\nNext, the light-dependent 
reactions produce"
"""        

HuggingFace

pip install huggingface-hub

Go to https://huggingface.co/settings/tokens to get your key.

os.environ["HUGGINGFACEHUB_API_TOKEN"] = "your-huggingface-api-token"        
from langchain.llms import HuggingFaceHub

llm = HuggingFaceHub(repo_id="google/flan-t5-xxl")

query = "Explain the process of photosynthesis in detail."
answer = llm(query)
answer

"""
'The process of photosynthesis is the conversion of light energy into 
chemical energy.'
"""
        

Gemini

pip install langchain-google-genai, google-generativeai

Get your key from: https://cloud.google.com/vertex-ai?hl=en#build-with-gemini

os.environ["GOOGLE_API_KEY"] = "your-google-key"        
import google.generativeai as genai

genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))        
from langchain_google_genai import ChatGoogleGenerativeAI

llm = ChatGoogleGenerativeAI(model="gemini-pro")
result = llm.invoke("Explain the process of photosynthesis in detail.")
result.content

"""
"Photosynthesis is the process by which plants and other organisms use sunlight to convert carbon dioxide and water into oxygen and energy in the form of sugar. It takes place in chloroplasts, which are organelles found in plant cells.\n\nThe overall equation for photosynthesis is:\n\n6CO2 + 6H2O + light energy → C6H12O6 + 6O2\n\n**Step 1: Light Absorption**\n\nPhotosynthesis begins with the absorption of light energy by chlorophyll, a green pigment found in chloroplasts. Chlorophyll molecules are located in structures called thylakoids, which are stacked together to form grana. When light strikes a chlorophyll molecule, an electron is excited and moves to a higher energy level.\n\n**Step 2: Electron Transport**\n\nThe excited electron is then passed along a series of electron carriers in the thylakoid membrane. This movement of electrons is called the electron transport chain. As the electrons move through the electron transport chain, they lose energy. This energy is used to pump hydrogen ions (H+) across the thylakoid membrane.\n\n**Step 3: Chemiosmosis**\n\nThe hydrogen ions that were pumped across the thylakoid membrane create a gradient, with a higher concentration of hydrogen ions inside the thylakoid than outside. This gradient drives the synthesis of ATP, a molecule that provides energy for the cell. As the hydrogen ions flow back across the thylakoid membrane through an enzyme called ATP synthase, they cause the enzyme to change shape. This shape change drives the synthesis of ATP.\n\n**Step 4: Carbon Fixation**\n\nWhile the electron transport chain and chemiosmosis are taking place, another process is occurring in the stroma of the chloroplast. This process is called carbon fixation. In carbon fixation, carbon dioxide from the atmosphere is combined with a molecule called ribulose 1,5-bisphosphate (RuBP) to form two molecules of 3-phosphoglycerate (3-PGA).\n\n**Step 5: Reduction**\n\nThe 3-PGA molecules are then reduced using ATP and NADPH, which were produced in the electron transport chain and chemiosmosis. This reduction converts the 3-PGA molecules into two molecules of glyceraldehyde 3-phosphate (G3P).\n\n**Step 6: Regeneration of RuBP**\n\nOne of the G3P molecules is used to regenerate RuBP, which is needed for the next round of carbon fixation. The other G3P molecule is used to produce glucose and other organic molecules.\n\n**Step 7: Release of Oxygen**\n\nThe oxygen that is produced during photosynthesis is a byproduct of the splitting of water molecules. The water molecules are split into hydrogen and oxygen atoms. The hydrogen atoms are used in the reduction of 3-PGA to G3P. The oxygen atoms are released into the atmosphere.\n\nPhotosynthesis is a complex process that is essential for life on Earth. It provides the oxygen we breathe and the food we eat. It also helps to regulate the Earth's climate by absorbing carbon dioxide from the atmosphere."
"""        


要查看或添加评论,请登录

Sushant S.的更多文章

社区洞察

其他会员也浏览了