LangChain在链上#2:快速入门

查询OpenAI,HuggingFace和Gemini

以前:

在本章中,我们深入探讨了使用LangChain实现先进语言模型的实际方面。

我们将进行一次实践之旅,重点关注三种不同模型的直接集成:OpenAI、来自Hugging Face的多功能模型以及Gemini模型。

OpenAI (开放人工智能)

pip安装openai

去 OPENAI 网站获取你的 API 密钥。

https://openai.com/ -> 登录 -> API -> API密钥

首先,将OPENAI_API_KEY环境变量的值设置为您实际的OpenAI API密钥。将API密钥设置为环境变量是一种常见做法,用于安全地管理凭据。这样可以将密钥置于源代码之外,降低意外暴露的风险。

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

在LangChain库的langchain.llms模块中,OpenAI类被设计用来与OpenAI的语言模型进行交互。

from langchain.llms import OpenAI

我们可以使用OpenAI提供的任何模型。您可以在这里找到这些模型。

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 安装 huggingface-hub

到https://huggingface.co/settings/tokens获取您的密钥。

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.'
"""

双子座

pip安装langchain-google-genai,google-generativeai

从以下网址获取您的密钥:https://cloud.google.com/vertex-ai?hl=zh-CN#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."
"""

阅读更多

来源

请访问以下链接查看已保留HTML结构的文本的简体中文翻译:https://python.langchain.com/docs/integrations/llms/huggingface_hub

LangChain简介

欢迎来到LangChain系列的简介页面。

LangChain是一家领先的语言创新公司,专注于提供跨语言沟通解决方案。

我们的目标是帮助用户打破语言壁垒,实现无缝的全球交流。

在这个系列中,我们将深入介绍LangChain的核心技术和产品。

请继续浏览以下链接了解更多:

LangChain简介

使用LangChain实现HuggingFace模型

使用LangChain实现HuggingFace模型

在本文中,我们将探讨如何使用LangChain将HuggingFace模型应用到实际项目中。

LangChain是一种强大的工具,可帮助改进自然语言处理,并在各种 NLP 任务中实现最新的HuggingFace模型。

如果你对自然语言处理和机器学习感兴趣,那么这篇博客对你来说将是一个宝藏。继续阅读,了解如何开始使用LangChain!

此博客由Analytics Vidhya团队提供。你可以在Analytics Vidhya网站上找到更多相关资源和文章。

2024-01-05 04:23:09 AI中文站翻译自原文