基于 Gemini 的 Langchain:组装 LLM 应用的多个部分
到目前为止,我们所了解的关键组件是LangChain框架的基本构建块。
这就引出了一个关键问题:我们如何有效地结合它们来构建LLM应用程序?
调试步骤
python
import getpass
import os
if "GOOGLE_API_KEY" not in os.environ:
os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter your Google AI API key: ")
python
import os
import requests
os.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890'
os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:7890'
r = requests.get("https://www.google.com")
print(r.status_code) # 能返回 200 就说明代理成功了
python
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash-001", # 或其他可用模型
)
print(llm.invoke("你好呀!你现在通了吗?").content)
输出为:
你好!我一直都在运行,可以随时为你提供帮助。所以,是的,可以说我"通了"。有什么我可以为你做的吗?
Using the Runnable Interface:使用Runnable接口
在 LangChain 中,无论是模型、提示模板,还是输出解析器,它们都遵循一个统一的接口 ------ Runnable 接口。这使得调用方式非常一致。
Runnable 接口支持的三种方法:
- invoke()
功能: 接收一个输入,返回一个输出。
用途: 处理单个输入,例如一次问答。
- batch()
功能: 批量处理多个输入,返回多个输出。
用途: 高效处理多个任务,比如批量问题回答。
- stream()
功能: 流式返回结果,一边生成一边输出。
用途: 用于实时显示长文本(像 ChatGPT 一样一字一字显示)。
额外功能支持:
-
重试机制(retries)
-
备用模型(fallbacks)
-
自动格式校验(schemas)
-
运行时可配置(runtime configurability)
Python 中的异步支持:
这三种方法都有对应的异步版本(asyncio),适合构建高性能、并发的应用程序。
只要你学会了如何使用 invoke、batch 或 stream 中的任意一个,就可以通用于所有 LangChain 的组件,因为它们都实现了相同的接口。
python
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash-001", # 或其他可用模型
)
completion_1 = llm.invoke('Hi there!')
# Hi!
print(completion_1.content)
输出为:
Hi! How can I help you today?
python
completion_2 = llm.batch(['Hi there!', 'Bye!'])
# ['Hi!', 'See you!']
# 因为结果是一个列表,所以我们这样打印查看
for msg in completion_2:
print(msg.content)
输出为:
Hi! How can I help you today?
Goodbye! Have a great day!
而llm.stream() 是一个生成器(generator),会"边生成边输出",输出将会是流式的文本。
python
for token in llm.stream("Bye!"):
# print(token)
print(token.content, end="")
# Good
# bye
# !
输出为:
Okay, bye! Have a great day! Let me know if you need anything else in the future.
注意:如果底层组件不支持 stream(流式输出),那返回的也会是普通完整内容。
你可以通过两种方式将这些组件组合在一起:
- Imperative(命令式)方式
➡️ 直接写代码调用,比如:
python
model.invoke("your input")
- Declarative(声明式)方式
➡️ 使用 LangChain 的 LCEL(LangChain Expression Language),是一种更高级、可组合的写法(后面章节会讲)
命令式 vs 声明式的区别
特性 | 命令式(Imperative) | 声明式(Declarative) |
---|---|---|
语法 | Python / JavaScript | LCEL |
并发处理 | 手动写线程 / Promise | 自动并发 |
流式输出 | 用 yield 关键字 |
自动支持 |
异步处理 | 需要写 async 函数 |
自动支持 |
Imperative Composition:命令式组合
命令式组合只是一个花哨的名字,用于编写你习惯编写的代码,将这些组件组合成函数和类。以下是将prompts、models和output parsers组合在一起的例子:
python
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import chain
# the building blocks
template = ChatPromptTemplate.from_messages([
('system', 'You are a helpful assistant.'),
('human', '{question}'),
])
model = ChatGoogleGenerativeAI(
model="gemini-2.0-flash-001", # 或其他可用模型
)
# combine them in a function
# @chain decorator adds the same Runnable interface for any function you write
@chain
def chatbot(values):
prompt = template.invoke(values)
return model.invoke(prompt)
# use it
result = chatbot.invoke({
"question": "Which model providers offer LLMs?"
})
print(result.content)
输出为:
Okay, here's a breakdown of prominent model providers offering Large Language Models (LLMs):
**Major Players & Cloud Providers:**
* **Google:**
* **Models:** PaLM 2, Gemini (various sizes), BERT, LaMDA, T5.
* **Platforms/Access:** Google Cloud Platform (Vertex AI), Google AI Studio (MakerSuite), PaLM API, Gemini API.
* **Notes:** Google has a strong research background and is rapidly developing and deploying new models. Gemini is their most advanced and multimodal model.
......
I hope this comprehensive overview is helpful! Let me know if you have any other questions.
这段代码中使用了 @chain 装饰器,用于把自定义的函数变成一个"Runnable 组件",也就是可以像模型一样调用 .invoke()、.batch()、.stream() 等方法。
这一句:
python
@chain
def chatbot(values):
...
相当于做了这些事:
-
让 chatbot() 函数拥有 .invoke()、.batch()、.stream() 等通用接口;
-
你就可以像用 LangChain 模型一样去用它;
-
使得它可以被其他 chain 组合、复用。
另一方面,如果您想启用流式传输或异步支持,则必须修改函数以支持它。例如,可以在 Python 中首先添加流式传输支持:
python
@chain
def chatbot(values):
prompt = template.invoke(values)
for token in model.invoke(prompt):
yield token
for part in chatbot.stream({
"question": "Which model providers offer LLMs?"
}):
print(part)
输出为:
('content', "Okay, here's a breakdown of prominent model providers offering Large Language Models (LLMs), categorized for clarity:\n\n**Major Cloud Providers (Offering a Wide Range of Models & Services):**\n\n* **Google:**\n * **Models:** Gemini (Pro, Ultra, Nano), PaLM 2, various smaller models.\n * **Platforms/Services:** Vertex AI (for model deployment, tuning, and management), Google AI Studio (for prototyping), Colab (for experimentation).\n* **Microsoft:**\n * **Models:** GPT-4 (via partnership with OpenAI), various smaller models.\n * **Platforms/Services:** Azure AI (including Azure OpenAI Service for accessing OpenAI models), Microsoft Copilot (integrates LLMs into productivity tools).\n* **Amazon Web Services (AWS):**\n * **Models:** Titan family of models, access to models from other providers (AI21 Labs, Anthropic, Cohere, Meta, Stability AI) through Amazon Bedrock.\n * **Platforms/Services:** Amazon Bedrock (managed service for accessing and using various LLMs), SageMaker (for model building, training, and deployment).\n\n**Leading AI Companies (Developing and Offering Cutting-Edge Models):**\n\n* **OpenAI:**\n * **Models:** GPT-4, GPT-3.5, DALL-E (image generation), Whisper (speech-to-text), etc.\n * **Platform/Service:** OpenAI API (for accessing their models).\n* **Anthropic:**\n * **Models:** Claude (Claude 3 Opus, Claude 3 Sonnet, Claude 3 Haiku).\n * **Platform/Service:** Anthropic API.\n* **AI21 Labs:**\n * **Models:** Jurassic-2.\n * **Platform/Service:** AI21 Studio.\n* **Cohere:**\n * **Models:** Command, Generate.\n * **Platform/Service:** Cohere API.\n\n**Open Source Focused Providers:**\n\n* **Meta:**\n * **Models:** Llama (Llama 3).\n * **Availability:** Models are released under open-source licenses (check specific license terms).\n* **Hugging Face:**\n * **Models:** A vast collection of open-source models from various creators, including models based on Llama, BERT, T5, etc. They don't create *the* foundational models themselves in most cases, but they are the central hub.\n * **Platform/Service:** Hugging Face Hub (repository of models), Transformers library (for using and fine-tuning models), Inference API.\n* **Mistral AI**\n * **Models:** Mistral 7B, Mistral 8x7B, Mistral Large\n * **Availability:** Some models available under open-source licenses, others through API.\n\n**Other Notable Providers:**\n\n* **NVIDIA:** Focuses on hardware and software optimization for AI, but also provides tools and platforms that support LLM development and deployment. They don't directly offer general-purpose LLM APIs in the same way as the above, but their technology is crucial.\n* **Cerebras:** Develops specialized hardware (wafer-scale engines) optimized for training large AI models.\n\n**Important Considerations When Choosing a Provider:**\n\n* **Model Capabilities:** What tasks do you need the model to perform? (Text generation, translation, code completion, etc.)\n* **Pricing:** Pricing models vary widely (pay-per-token, subscription, etc.).\n* **Performance:** Speed, latency, and accuracy are critical.\n* **Scalability:** Can the provider handle your expected usage volume?\n* **Customization:** Do you need to fine-tune the model for your specific use case?\n* **Data Privacy and Security:** How does the provider handle your data?\n* **Support:** What level of support is offered?\n* **Open Source vs. Proprietary:** Open-source models offer more flexibility and control, but may require more technical expertise.\n\nThis list is not exhaustive, but it covers the major players in the LLM space. The field is rapidly evolving, so new providers and models are constantly emerging. Remember to research and compare options carefully to find the best fit for your needs.")
('additional_kwargs', {})
('response_metadata', {'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'model_name': 'gemini-2.0-flash-001', 'safety_ratings': []})
('type', 'ai')
('name', None)
('id', 'run-25aeb0c1-7209-4fbc-9541-5dfc19298f89-0')
('example', False)
('tool_calls', [])
('invalid_tool_calls', [])
('usage_metadata', {'input_tokens': 13, 'output_tokens': 940, 'total_tokens': 953, 'input_token_details': {'cache_read': 0}})
上面的代码中展示了Python 中,可以通过 yield 返回要流式传输的值来启用自定义函数的流式传输,然后使用 stream 调用它。
对于异步执行,可以在 Python 中这样重写你的函数:
python
@chain
async def chatbot(values):
prompt = await template.ainvoke(values)
return await model.ainvoke(prompt)
result = await chatbot.ainvoke({
"question": "Which model providers offer LLMs?"
})
print(result)
输出为:
content="Okay, here's a breakdown of prominent model providers offering Large Language Models (LLMs), categorized for clarity:\n\n**Major Cloud Providers (Offering a Wide Range of Models & Services):**\n\n* **Google:**\n * **Models:** Gemini (various sizes and capabilities), PaLM 2, LaMDA.\n * **Platforms/Services:** Vertex AI (for model deployment and management), Google AI Studio (for prototyping and experimentation), Gemini API.\n* **Microsoft:**\n * **Models:** GPT-4 (via partnership with OpenAI), various other Azure OpenAI Service models.\n * **Platforms/Services:** Azure OpenAI Service (access to OpenAI models, fine-tuning, enterprise features), Azure Machine Learning.\n* **Amazon (AWS):**\n * **Models:** Titan (internally developed), access to models from AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and others.\n * **Platforms/Services:** Amazon Bedrock (unified access to various LLMs), Amazon SageMaker (for model building, training, and deployment).\n* **IBM:**\n * **Models:** Granite.\n * **Platforms/Services:** Watson AI.\n\n**Leading AI Companies (Focused Primarily on AI):**\n\n* **OpenAI:**\n * **Models:** GPT-4, GPT-3.5 Turbo, DALL-E, Whisper (speech-to-text), and others.\n * **Platforms/Services:** OpenAI API (for accessing models), Playground (interactive environment).\n* **Anthropic:**\n * **Models:** Claude (Claude 2, Claude Instant).\n * **Platforms/Services:** Anthropic API.\n* **AI21 Labs:**\n * **Models:** Jurassic-2.\n * **Platforms/Services:** AI21 Studio.\n* **Cohere:**\n * **Models:** Command, Generate, Embed.\n * **Platforms/Services:** Cohere API.\n\n**Open Source Focused Providers:**\n\n* **Meta:**\n * **Models:** Llama (Llama 2, Llama 3).\n * **Availability:** Llama models are available for download and use, subject to licensing terms.\n* **Hugging Face:**\n * **Models:** A vast collection of open-source models from various creators, including fine-tuned versions of popular models.\n * **Platforms/Services:** Hugging Face Hub (repository of models, datasets, and Spaces), Transformers library (for model usage), Inference API.\n* **Stability AI:**\n * **Models:** StableLM.\n\n**Other Notable Providers:**\n\n* **NVIDIA:**\n * **Models:** Nemo.\n * **Platforms/Services:** NVIDIA AI Enterprise.\n* **MosaicML (Databricks):**\n * Now part of Databricks, offering tools and infrastructure for training and deploying LLMs.\n\n**Important Considerations:**\n\n* **Model Availability:** Access to specific models may vary depending on the provider, your subscription level, and other factors.\n* **Pricing:** Pricing models differ significantly. Some providers charge per token (unit of text), while others offer subscription-based access.\n* **Capabilities:** Models vary in their strengths. Some are better at creative writing, while others excel at code generation or question answering.\n* **Open Source vs. Proprietary:** Open-source models offer greater flexibility and control, but may require more technical expertise to deploy and manage. Proprietary models are typically easier to use but come with licensing restrictions.\n* **Fine-tuning:** Many providers allow you to fine-tune their models on your own data to improve performance for specific tasks.\n\nThis list is not exhaustive, as the LLM landscape is rapidly evolving. New providers and models are constantly emerging. Always research and compare options to find the best fit for your specific needs." additional_kwargs={} response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'model_name': 'gemini-2.0-flash-001', 'safety_ratings': []} id='run-652aae17-909c-4fb3-926a-a01b9acadfe8-0' usage_metadata={'input_tokens': 13, 'output_tokens': 848, 'total_tokens': 861, 'input_token_details': {'cache_read': 0}}
但是为什么 Jupyter 看不到流式输出效果?
因为 Jupyter Notebook 的 print() 默认是有"缓冲"的,也就是说,它会等到一大段文本输出后才一起显示,而不是像终端或命令行那样"实时一行一行刷出来"。所以即使你用了 yield 和 stream(),输出也可能是一次性跳出来的,看起来没"流"的效果。
Declarative Composition:声明性组合
LangChain 表达式语言(LCEL)是一种用于组合 LangChain 组件的声明性语言。LangChain 将 LCEL 组合编译为优化的执行计划,具有自动并行化、流式、跟踪和异步支持。
让我们看看使用 LCEL 的相同示例,首先在 Python 中:
python
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
# the building blocks
template = ChatPromptTemplate.from_messages([
('system', 'You are a helpful assistant.'),
('human', '{question}'),
])
model = ChatGoogleGenerativeAI(
model="gemini-2.0-flash-001", # 或其他可用模型
)
# combine them with the | operator
chatbot = template | model
# use it
result = chatbot.invoke({
"question": "Which model providers offer LLMs?"
})
print(result.content)
Okay, here's a breakdown of prominent model providers offering Large Language Models (LLMs), categorized for clarity:
**Major Cloud Providers (Offering a wide range of models and services):**
* **Google:**
* **Models:** Gemini (various sizes and capabilities, including Gemini Pro, Gemini 1.5 Pro, Gemini Ultra), PaLM 2, Med-PaLM 2, Codey (for code generation), Imagen (for image generation).
* **Platforms/Services:** Vertex AI (their primary AI platform), Google AI Studio, Cloud AI Platform.
* **Notes:** Google is a leader in AI research and development, offering cutting-edge models and strong integration with their cloud infrastructure.
......
I hope this comprehensive overview is helpful! Let me know if you have any other questions.
LangChain 表达式语言(LCEL) 是一种 声明式语言,用来像"搭积木"一样组合 LangChain 中的各种组件(如提示模板、模型、输出解析器等)。
LCEL 的关键特性
特性 | 说明 |
---|---|
声明式写法 | 像数学公式一样组合组件,而不是写一堆控制流程 |
自动并行 | 多个组件能同时运行(自动并发) |
支持流式输出 | 输出内容可以边生成边使用 |
支持异步 | 可以用 async/await 直接调用 |
支持追踪 | 自动记录每一步调用结果(方便调试、分析) |
示例:用 LCEL 组合一个链
python
chatbot = template | model | parser
你可以理解为:
先用 template 生成提示 → 然后送入 model → 再用 parser 解析结果
每个步骤都是 LangChain 的组件(都实现了 Runnable 接口)
在这个版本中,你无需执行任何其他操作即可使用流式处理:
python
chatbot = template | model
for part in chatbot.stream({
"question": "Which model providers offer LLMs?"
}):
print(part)
而且,对于 Python 来说,使用异步方法也无需执行任何其他操作:
python
chatbot = template | model
result = await chatbot.ainvoke({
"question": "Which model providers offer LLMs?"
})
print(result)
使用 LCEL(LangChain Expression Language)后,很多流式和异步功能可以自动支持,你不需要自己手动去写 async、yield 或控制流代码,这正是 LCEL 的强大之处。
因为 LCEL 构建出来的"链"本质上是一个 Runnable 对象,它自动实现了这些方法:
-
.invoke() → 普通同步调用
-
.stream() → 自动启用流式输出
-
.ainvoke() → 异步调用
-
.astream() → 异步 + 流式输出
-
.batch() / .abatch() → 批处理支持
下一节,我们将要讲如何将外部数据作为上下文提供给你的 AI 聊天机器人,以便你可以构建一个LLM 应用程序,使你能够与你的数据进行"聊天"。
欢迎点赞收藏!