LangChain 开发 LLM 应用,跑起来 3 个方法

2023-07-1110:46:41人工智能与大数据Comments1,236 views字数 2832阅读模式

LangChain 开发 LLM 应用时,需要机器进行 GLM 部署,好多同学第一步就被劝退了,那么如何绕过这个步骤先学习 LLM 模型的应用,对 Langchain 进行快速上手?本片讲解 3 个把 LangChain 跑起来的方法,如有错误欢迎纠正。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

Langchain 官方文档地址: https://python.langchain.com/文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

基础功能

LLM 调用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

  • 支持多种模型接口,比如 OpenAI、HuggingFace、AzureOpenAI …
  • Fake LLM,用于测试
  • 缓存的支持,比如 in-mem(内存)、SQLite、Redis、SQL
  • 用量记录
  • 支持流模式(就是一个字一个字的返回,类似打字效果)

Prompt 管理,支持各种自定义模板文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

拥有大量的文档加载器,比如 Email、Markdown、PDF、Youtube …文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

对索引的支持文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

  • 文档分割器
  • 向量化
  • 对接向量存储与搜索,比如 Chroma、Pinecone、Qdrand

Chains文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

  • LLMChain
  • 各种工具 Chain
  • LangChainHub

详细地址可参考:
https://www.langchain.cn/t/topic/35文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

测试 Langchain 工程的 3 个方法:

1 使用 Langchian 提供的 FakeListLLM文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

为了节约时间,直接上代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

LangChain 开发 LLM 应用,跑起来 3 个方法
import os
from decouple import config
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.agents import load_tools

这里 mock 下 ChatGPT, 使用 mockLLm文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

#from langchain.llms import OpenAI
from langchain.llms.fake import FakeListLLM
os.environ["OPENAI_API_KEY"] = config('OPENAI_API_KEY')

REPL 是 “Read–Eval–Print Loop”(读取 - 求值 - 打印 - 循环)的缩写,它是一种简单的、交互式的编程环境。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

在 REPL 环境中,用户可以输入一条或多条编程语句,系统会立即执行这些语句并输出结果。这种方式非常适合进行快速的代码试验和调试。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

tools = load_tools(["python_repl"])
responses=[
    "Action: Python REPL\nAction Input: chatGpt原理",
    "Final Answer: mock答案"
]
llm = FakeListLLM(responses=responses)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run("chatGpt原理2")

2 使用 Langchian 提供的 HumanInputLLM,访问维基百科查询文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

LangChain 开发 LLM 应用,跑起来 3 个方法
from langchain.llms.human import HumanInputLLM
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from wikipedia import set_lang

使用维基百科工具文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

tools = load_tools(["wikipedia"])

这里必须要设置为中文 url 前缀,不然访问不了文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

set_lang("zh")

初始化 LLM文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

llm = HumanInputLLM(prompt_func=lambda prompt: print(f"\n===PROMPT====\n{prompt}\n=====END OF PROMPT======"))

初始化 agent文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run("喜羊羊")

使用 huggingface文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

https://huggingface.co/docs文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

1. 注册账号文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

2. 创建 Access Tokens文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

LangChain 开发 LLM 应用,跑起来 3 个方法

Demo: 使用模型对文档进行摘要文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

LangChain 开发 LLM 应用,跑起来 3 个方法
from langchain.document_loaders import UnstructuredFileLoader
from langchain.chains.summarize import load_summarize_chain
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain import HuggingFaceHub
import os
from decouple import config

from langchain.agents import load_tools

这里 mock 下 ChatGPT, 使用 HUGGINGFACEHUB文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

os.environ["HUGGINGFACEHUB_API_TOKEN"] = config('HUGGINGFACEHUB_API_TOKEN')

导入文本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

loader = UnstructuredFileLoader("docment_store\helloLangChain.txt")

将文本转成 Document 对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

document = loader.load()
print(f'documents:{len(document)}')

初始化文本分割器文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size = 500,
    chunk_overlap = 0
)

切分文本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

split_documents = text_splitter.split_documents(document)
print(f'documents:{len(split_documents)}')

加载 LLM 模型文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

overal_temperature = 0.1
flan_t5xxl = HuggingFaceHub(repo_id="google/flan-t5-xxl", 
                         model_kwargs={"temperature":overal_temperature, 
                                       "max_new_tokens":200}
                         ) 

llm = flan_t5xxl
tools = load_tools(["llm-math"], llm=llm)

创建总结链文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

chain = load_summarize_chain(llm, chain_type="refine", verbose=True)

执行总结链文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

chain.run(split_documents)

作者:京东科技 杨建
来源:京东云开发者社区文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/51302.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ai/51302.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定