日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

把LangChain跑起來的三個方法

來源: 責編: 時間:2023-08-05 11:44:41 4682觀看
導讀使用LangChain開發LLM應用時,需要機器進行GLM部署,好多同學第一步就被勸退了,那么如何繞過這個步驟先學習LLM模型的應用,對Langchain進行快速上手?本片講解3個把LangChain跑起來的方法,如有錯誤歡迎糾正。Langchain官方文檔

使用LangChain開發LLM應用時,需要機器進行GLM部署,好多同學第一步就被勸退了,那么如何繞過這個步驟先學習LLM模型的應用,對Langchain進行快速上手?本片講解3個把LangChain跑起來的方法,如有錯誤歡迎糾正。yO728資訊網——每日最新資訊28at.com

Langchain官方文檔地址:https://python.langchain.com/yO728資訊網——每日最新資訊28at.com

基礎功能

LLM 調用yO728資訊網——每日最新資訊28at.com

  • 支持多種模型接口,比如 OpenAI、HuggingFace、AzureOpenAI …
  • Fake LLM,用于測試
  • 緩存的支持,比如 in-mem(內存)、SQLite、Redis、SQL
  • 用量記錄
  • 支持流模式(就是一個字一個字的返回,類似打字效果)

Prompt管理,支持各種自定義模板yO728資訊網——每日最新資訊28at.com

擁有大量的文檔加載器,比如 Email、Markdown、PDF、Youtube …yO728資訊網——每日最新資訊28at.com

對索引的支持yO728資訊網——每日最新資訊28at.com

  • 文檔分割器
  • 向量化
  • 對接向量存儲與搜索,比如 Chroma、Pinecone、Qdrand

ChainsyO728資訊網——每日最新資訊28at.com

  • LLMChain
  • 各種工具Chain
  • LangChainHub

詳細地址可參考:https://www.langchain.cn/t/topic/35yO728資訊網——每日最新資訊28at.com

測試Langchain工程的3個方法:

1 使用Langchian提供的FakeListLLM

為了節約時間,直接上代碼yO728資訊網——每日最新資訊28at.com

yO728資訊網——每日最新資訊28at.com

import osfrom decouple import configfrom langchain.agents import initialize_agentfrom langchain.agents import AgentTypefrom langchain.agents import load_tools

這里mock下ChatGPT,使用mockLLmyO728資訊網——每日最新資訊28at.com

#from langchain.llms import OpenAIfrom langchain.llms.fake import FakeListLLMos.environ["OPENAI_API_KEY"] = config('OPENAI_API_KEY')

REPL 是 “Read–Eval–Print Loop”(讀取-求值-打印-循環)的縮寫,它是一種簡單的、交互式的編程環境。yO728資訊網——每日最新資訊28at.com

在 REPL 環境中,用戶可以輸入一條或多條編程語句,系統會立即執行這些語句并輸出結果。這種方式非常適合進行快速的代碼試驗和調試。yO728資訊網——每日最新資訊28at.com

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,訪問維基百科查詢

yO728資訊網——每日最新資訊28at.com

from langchain.llms.human import HumanInputLLMfrom langchain.agents import load_toolsfrom langchain.agents import initialize_agentfrom langchain.agents import AgentTypefrom wikipedia import set_lang

使用維基百科工具yO728資訊網——每日最新資訊28at.com

tools = load_tools(["wikipedia"])

這里必須要設置為中文url前綴,不然訪問不了yO728資訊網——每日最新資訊28at.com

set_lang("zh")

初始化LLMyO728資訊網——每日最新資訊28at.com

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

初始化agentyO728資訊網——每日最新資訊28at.com

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

使用huggingfacehttps://huggingface.co/docs

1)注冊賬號yO728資訊網——每日最新資訊28at.com

2)創建Access TokensyO728資訊網——每日最新資訊28at.com

yO728資訊網——每日最新資訊28at.com

Demo: 使用模型對文檔進行摘要yO728資訊網——每日最新資訊28at.com

yO728資訊網——每日最新資訊28at.com

from langchain.document_loaders import UnstructuredFileLoaderfrom langchain.chains.summarize import load_summarize_chainfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain import HuggingFaceHubimport osfrom decouple import configfrom langchain.agents import load_tools

這里mock下ChatGPT,使用HUGGINGFACEHUByO728資訊網——每日最新資訊28at.com

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

導入文本yO728資訊網——每日最新資訊28at.com

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

將文本轉成 Document 對象yO728資訊網——每日最新資訊28at.com

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

初始化文本分割器yO728資訊網——每日最新資訊28at.com

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

切分文本yO728資訊網——每日最新資訊28at.com

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

加載 LLM 模型yO728資訊網——每日最新資訊28at.com

overal_temperature = 0.1flan_t5xxl = HuggingFaceHub(repo_id="google/flan-t5-xxl",                          model_kwargs={"temperature":overal_temperature,                                        "max_new_tokens":200}                         ) llm = flan_t5xxltools = load_tools(["llm-math"], llm=llm)

創建總結鏈yO728資訊網——每日最新資訊28at.com

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

執行總結鏈yO728資訊網——每日最新資訊28at.com

chain.run(split_documents)

作者:京東科技 楊建yO728資訊網——每日最新資訊28at.com

來源:京東云開發者社區yO728資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-84-0.html把LangChain跑起來的三個方法

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Automa-通過連接塊來自動化你的瀏覽器

下一篇: 一文看懂為蘋果Vision Pro開發應用程序

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 武鸣县| 泾阳县| 新乡县| 肥城市| 余庆县| 边坝县| 安达市| 普格县| 梧州市| 沿河| 营山县| 岑巩县| 双柏县| 积石山| 井冈山市| 祁门县| 朝阳区| 海伦市| 泸溪县| 霍城县| 正安县| 安丘市| 武胜县| 新丰县| 大竹县| 濮阳县| 阆中市| 枝江市| 庆城县| 普宁市| 桃江县| 余干县| 高雄市| 诸暨市| 永顺县| 嵩明县| 故城县| 枣阳市| 桐柏县| 海盐县| 南雄市|