LangChain

LangChain 详解 什么是 LangChain LangChain 是一个用于构建基于大语言模型(LLM)应用的开源框架。它提供了一套标准化的组件和工具,简化了 AI Agent、聊天机器人、问答系统等应用的开发过程。 核心理念:通过"链(Chain)“将多个组件连接起来,构建复杂的 LLM 应用。 官方仓库: Python:langchain-ai/langchain JavaScript/TypeScript:langchain-ai/langchainjs 核心组件 1. Models(模型) 支持多种 LLM 提供商: from langchain.llms import OpenAI, Anthropic, HuggingFaceHub from langchain.chat_models import ChatOpenAI, ChatAnthropic # OpenAI llm = OpenAI(temperature=0.7) # Claude chat = ChatAnthropic(model="claude-3-sonnet-20240229") # 本地模型 from langchain.llms import Ollama local_llm = Ollama(model="llama2") 2. Prompts(提示模板) 管理和复用提示词: from langchain.prompts import PromptTemplate, ChatPromptTemplate # 简单模板 prompt = PromptTemplate( input_variables=["product"], template="给{product}写一个广告语" ) # 聊天模板 chat_prompt = ChatPromptTemplate.from_messages([ ("system", "你是一个{role}"), ("human", "{user_input}") ]) # 使用 formatted = prompt.format(product="智能手表") result = llm(formatted) 3. Chains(链) 将多个组件串联: from langchain.chains import LLMChain, SimpleSequentialChain # 单个链 chain = LLMChain(llm=llm, prompt=prompt) result = chain.run(product="智能手表") # 顺序链 chain1 = LLMChain(llm=llm, prompt=prompt1) # 生成产品描述 chain2 = LLMChain(llm=llm, prompt=prompt2) # 生成广告语 overall_chain = SimpleSequentialChain( chains=[chain1, chain2], verbose=True ) result = overall_chain.run("智能手表") 4. Agents(代理) 动态决策和工具调用: ...

Created 26266-04-26 · Updated 26266-07-26 · 5 min · 901 words · -

LlamaIndex

LlamaIndex 详解 什么是 LlamaIndex LlamaIndex(原名 GPT Index)是一个专注于数据连接和检索的开源框架,主要用于构建 RAG(检索增强生成)应用。与 LangChain 相比,LlamaIndex 更专注于数据索引、检索和查询,是构建知识库和文档问答系统的首选工具。 核心定位:数据框架(Data Framework for LLM Applications) 官方仓库:run-llama/llama_index 设计哲学: LangChain:通用的 LLM 应用框架(Chain、Agent、Memory 等) LlamaIndex:专注于数据摄入、索引和检索 ⚠️ 名称说明:LlamaIndex vs Llama2 重要澄清:LlamaIndex 与 Meta 的 Llama2/Llama3 开源模型没有任何关系,它们是完全独立的项目: 项目 类型 开发者 功能 LlamaIndex 开发框架 LlamaIndex 团队(原 run-llama) 数据索引和检索框架 Llama2/Llama3 大语言模型 Meta(Facebook) 开源 LLM 模型 为什么叫 “Llama”? LlamaIndex 的命名来自"Large Language Model Applications"的缩写 与 Meta 的 Llama 模型纯属巧合(命名冲突) 实际关系: LlamaIndex 框架可以使用 Llama2/Llama3 作为底层 LLM 但 LlamaIndex 也支持 OpenAI、Claude、Gemini 等任何 LLM 两者是"框架"与"模型"的关系,而非从属关系 示例: ...

Created 26266-04-26 · Updated 23236-07-23 · 5 min · 1040 words · -