LangChain 是一个用于开发大语言模型(LLM)应用的框架,它通过模块化设计简化了与LLM的交互、数据整合和流程管理。以下是其核心要点:
from langchain_community.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
# 初始化LLM
llm = OpenAI(model_name="gpt-3.5-turbo")
# 创建提示模板
prompt = PromptTemplate(
input_variables=["product"],
template="为{product}写3个广告标语:"
)
# 构建链
chain = LLMChain(llm=llm, prompt=prompt)
# 执行
print(chain.run("智能手表"))
该框架适合需要快速构建复杂LLM应用的开发者,但需注意其抽象层可能带来性能开销,简单场景可直接调用原生API。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号