如何使用LangChain调用Google的大模型Gemini

当今,大型语言模型(LLM)已经成为人工智能领域的热门话题。近期,Gemini发布了一款新的大模型,而LangChain也发布了调用接口。在这篇文章中,我们将介绍如何使用LangChain调用Google的大模型Gemini。

通过 ChatGoogleGenerativeAI 和 langchain-google-genai 集成包中的类可以访问 Google gemini 和 gemini-vision 模型。

安装开发包

%pip install -U --quiet langchain-google-genai pillow

引入类和配置GOOGLE_API_KEY

import getpass
import os

if "GOOGLE_API_KEY" not in os.environ:
    os.environ["GOOGLE_API_KEY"] = getpass("Provide your Google API Key")

调用Gemini模型

from langchain_google_genai import ChatGoogleGenerativeAI

llm = ChatGoogleGenerativeAI(model="gemini-pro")
result = llm.invoke("Write a ballad about LangChain")
print(result.content)
In realms where data streams like fervent tides,
Where algorithms dance and knowledge abides,
A tale unfolds of LangChain, grand and bold,
A ballad sung in bits and bytes untold.

Amidst the codes and circuits' hum,
A spark ignited, a vision would come.
From minds of brilliance, a tapestry formed,
A model to learn, to comprehend, to transform.

In layers deep, its architecture wove,
A neural network, ever-growing, in love.
With language's essence, it sought to entwine,
To unlock the secrets, each word's design.

From texts vast and varied, it feasted and learned,
Its grasp on meaning, swiftly discerned.
Context and syntax, it embraced with grace,
Unraveling stories, at an astonishing pace.

Translations sprang forth, with seamless art,
Bridging tongues and weaving hearts apart.
From English to French, Chinese to Spanish, behold,
LangChain's prowess, like a language untold.

It summarized texts, with insights profound,
Extracting knowledge, without a sound.
Questions it answered, with eloquence rare,
A digital sage, beyond compare.

Yet, its journey faced trials and tribulations,
Obstacles that tested its dedication.
Biases and errors, it sought to transcend,
For fairness and accuracy, it would contend.

With every challenge, it emerged more strong,
Adaptive and resilient, all along.
For LangChain's purpose was etched in its core,
To empower humans, forevermore.

In classrooms and workplaces, it lent its hand,
A tireless assistant, at every demand.
It aided students, in their quests for knowledge,
And professionals thrived, with its guidance and homage.

As years unfurled, its fame grew wide,
A testament to its unwavering stride.
Researchers and scholars, they all took heed,
Of LangChain's brilliance, a groundbreaking deed.

And so, the ballad of LangChain resounds,
A tribute to progress, where innovation abounds.
In the annals of AI, its name shall be etched,
A pioneer, forever in our hearts sketched.

流式输出方式

ChatGoogleGenerativeAI 本机支持流式输出。

for chunk in llm.stream("Write a limerick about LLMs."):
    print(chunk.content)
    print("---")
#Note that each chunk may contain more than one "token"
There once was an AI named Bert,
Whose language skills were quite expert.
---

With a vast dataset,
It could chat, even bet,
And write limericks, for what it's worth.
---
results = llm.batch(
    [
        "What's 2+2?",
        "What's 3+5?",
    ]
)
for res in results:
    print(res.content)
4
8

支持上传图片

要提供图像,请传递内容为 类型的 List[dict] 人工消息,其中每个字典都包含图像值(type of )或文本(type of image_url text )值。的值 image_url 可以是以下任何值:

  • 公开可访问的图片URL

  • 可访问的 gcs 文件(例如,“gcs://path/to/file.png”)

  • 本地文件路径

  • base64 编码的图像(例如, data:image/png;base64,abcd124

  • PIL 图像

import requests
from IPython.display import Image

image_url = "https://picsum.photos/seed/picsum/300/300"
content = requests.get(image_url).content
Image(content)
from langchain_core.messages import HumanMessage
from langchain_google_genai import ChatGoogleGenerativeAI

llm = ChatGoogleGenerativeAI(model="gemini-pro-vision")
example
message = HumanMessage(
    content=[
        {
            "type": "text",
            "text": "What's in this image?",
        },  You can optionally provide text parts
        {"type": "image_url", "image_url": image_url},
    ]
)
llm.invoke([message])
AIMessage(content=' The image contains a snow-capped mountain peak.')

Gemini提示词常见问题

截至本文档撰写之时(2024 年 12 月 12 日),Gemini 对其接受的提示类型和结构有一些限制。具体说来:

  1. 提供多模态(图像)输入时,您最多只能收到 1 条“人类”(用户)类型的消息。您不能传递多条消息(尽管单个人工消息可能有多个内容条目)

  2. 不接受系统消息。

  3. 对于常规聊天对话,消息必须遵循 human/ai/human/ai 交替模式。您不能按顺序提供 2 条 AI 或人工消息。

  4. 如果消息违反了 LLM 的安全检查,则可能会被阻止。在这种情况下,模型将返回一个空响应。

### LangChain Gemini代理的使用方法 LangChain提供了一种机制,使得开发人员能够利用大型语言模型(LLM)作为推理引擎来决定执行的操作及其参数。对于Gemini代理而言,这种能力被进一步扩展以适应特定的任务需求。 #### 安装依赖库 为了开始使用LangChain Gemini代理,在项目环境中安装必要的Python包是必需的第一步操作。通常情况下,这涉及到`langchain`以及其他可能的支持库。 ```bash pip install langchain ``` #### 初始化Gemini代理实例 创建并初始化一个新的Gemini代理对象涉及指定目标搜索引擎或其他服务接口。此过程允许开发者定义如何与外部资源互动的具体细节[^2]。 ```python from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI from langchain.utilities import GoogleSearchAPIWrapper # 假设已经设置好了环境变量GOOGLE_CSE_ID和GOOGLE_API_KEY search = GoogleSearchAPIWrapper() tools = [ Tool( name="Google Search", func=search.run, description="Useful for when you need to search the web." ) ] llm = OpenAI(temperature=0) gemini_agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True) ``` #### 使用Gemini代理查询数据 一旦成功设置了代理实例,就可以向其发送请求来进行实际的数据检索工作。下面的例子展示了怎样询问有关技术主题的问题,并观察代理自动调用相应的搜索功能获取答案。 ```python response = gemini_agent.run("What is the latest version of Python?") print(response) ``` 上述代码片段会触发一次网络搜索活动,旨在找到关于最新版Python的相关信息。最终的结果会被打印出来供用户查看。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我叫秋水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值