LangChain - 构建 AI 应用的强大框架

一、什么是 LangChain?

LangChain 是一个用于开发基于大语言模型(LLM)应用程序的框架,它提供了一系列工具和抽象,让开发者能够更容易地构建复杂的 AI 应用。

二、核心概念

1. 基础组件

从示例代码中我们可以看到几个重要的基础组件:

1. 环境配置

js 复制代码
import os
os.environ["OPENAI_API_KEY"] = "your-api-key"
os.environ["OPENAI_BASE_URL"] = "your-base-url"

2. 聊天模型

js 复制代码
from langchain_openai import ChatOpenAI
chat = ChatOpenAI(
    model="gpt-3.5-turbo",
    temperature=0.8,
    max_tokens=60    
)

3. 基础LLM

js 复制代码
from langchain_openai import OpenAI
llm = OpenAI(model_name="gpt-3.5-turbo-instruct")

2. 消息类型

LangChain 支持多种消息类型:

js 复制代码
from langchain.schema import (
    SystemMessage,  # 系统消息
    HumanMessage    # 用户消息
)
messages = [
    SystemMessage(content="你是一个很棒的风水师"),
    HumanMessage(content="请给我的花店起个名")
]

3. 模板系统

LangChain 提供了强大的模板系统:

js 复制代码
from langchain.prompts import PromptTemplate
template = """你是一位专业的鲜花店的文案撰写员。
对于售价为{price} 元的{flower_name},你能提供一个吸引人的简短描述吗?
"""
prompt = PromptTemplate.from_template(template)
input = prompt.format(flower_name="玫瑰",price="99")

三、实际应用示例

1. 简单对话

js 复制代码
chat = ChatOpenAI(    model="gpt-3.5-turbo",   
temperature=0.8,    
max_tokens=60    
)

messages = [    SystemMessage(content="你是一个很棒的风水师"),
HumanMessage(content="请给我的花店起个名")]

response = chat(messages)
print(response.content)

2. 模板化文案生成

js 复制代码
template = """你是一位专业的鲜花店的文案撰写员。
对于售价为{price} 元的{flower_name},你能提供一个吸引人的简短描述吗?
"""

prompt = PromptTemplate.from_template(template)

input = prompt.format(flower_name="玫瑰",price="99")

model = OpenAI(model_name="gpt-3.5-turbo-instruct")output = model.invoke(input)

2. 格式化输出格式

js 复制代码
# 格式化输出
from langchain.output_parsers import StructuredOutputParser, ResponseSchema

response_schemas = [
    ResponseSchema(name="description",description="鲜花的描述文案"),
    ResponseSchema(name="reason",description="为什么要这样写这个文案"),
]

output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
print(output_parser)

format_instruction = output_parser.get_format_instructions()
print(format_instruction)

output = PromptTemplate.from_template(template,
partial_variables={"format_instruction":format_instructions})

input = prompt.format_prompt(
    flower_name="玫瑰",
    price="99",
    format_instruction=format_instruction
)
print(input)

output = model.invoke(input)
print(output)
相关推荐
kailp5 小时前
OpenAI发布AI浏览器Atlas:探索下一代网页交互新可能
人工智能·大模型·云计算·aigc·算力
Mintopia7 小时前
🤖 算法偏见修正:WebAI模型的公平性优化技术
前端·javascript·aigc
墨风如雪18 小时前
OpenAI重磅发布ChatGPT Atlas:告别传统浏览器的AI新纪元!
aigc
win4r21 小时前
🚀DeepSeek又放大招!这个OCR模型让文档识别效率倍增!本地部署+客观实测DeepSeek-OCR!OCR识别准确率97%,支持100+语言,每天处理3
llm·aigc·deepseek
王中阳Go1 天前
挑战一周用 AI 开发商业化项目!4 大痛点反思 + 新手专属提示词分享
后端·aigc·openai
win4r1 天前
🚀彻底改写浏览器自动化!ChatGPT Atlas浏览器深度评测:Agent模式自动操作网页太疯狂!跨网页理解+长文秒懂+图片识别,论文工作流被改写!看完你会
chatgpt·aigc·openai
猫头虎1 天前
DeepSeek刚刚开源了一个3B的 OCR模型:什么是DeepSeek-OCR?单张A100-40G每天可以处理20万+页文档
人工智能·开源·whisper·prompt·aigc·ocr·gpu算力
自由生长20241 天前
使用AI的思考-为什么大模型不会主动提问
aigc·ai编程
Mintopia1 天前
⚖️ AIGC版权确权技术:Web内容的AI生成标识与法律适配
前端·javascript·aigc
用户5191495848452 天前
使用Python ConfigParser解析INI配置文件完全指南
人工智能·aigc