Javascript版Langchain入門教程解析
引言
我是AI小火箭的HB,我探索和寫作人工智能和語言交叉點(diǎn)的所有事物,范圍從LLM,聊天機(jī)器人,語音機(jī)器人,開發(fā)框架,以數(shù)據(jù)為中心的潛在空間等。
介紹
LangChain是一個(gè)開源Python庫,用于構(gòu)建由大型語言模型(LLM)支持的應(yīng)用程序。它提供了一個(gè)框架,將LLM與其他數(shù)據(jù)源(如互聯(lián)網(wǎng)或個(gè)人文件)連接起來,允許開發(fā)人員將多個(gè)命令鏈接在一起,以創(chuàng)建更復(fù)雜的應(yīng)用程序。
LangChain創(chuàng)建于2022年10月,是圍繞LLMs(大語言模型)建立的一個(gè)框架,LLMs使用機(jī)器學(xué)習(xí)算法和海量數(shù)據(jù)來分析和理解自然語言。LangChain自身并不開發(fā)LLMs,它的核心理念是為各種LLMs實(shí)現(xiàn)通用的接口,把LLMs相關(guān)的組件“鏈接”在一起,簡化LLMs應(yīng)用的開發(fā)難度,方便開發(fā)者快速地開發(fā)復(fù)雜的LLMs應(yīng)用。
支持的語言
LangChain目前有兩個(gè)語言的實(shí)現(xiàn):Python和Node.js。
組件
LangChain的組件包括:
- Models:模型,各種類型的模型和模型集成,比如GPT-4。
- Prompts:提示,包括提示管理、提示優(yōu)化和提示序列化。
- Memory:記憶,用來保存和模型交互時(shí)的上下文狀態(tài)。
- Indexes:索引,用來結(jié)構(gòu)化文檔,以便和模型交互。
- Chains:鏈,一系列對(duì)各種組件的調(diào)用。
- Agents:代理,決定模型采取哪些行動(dòng),執(zhí)行并且觀察流程,直到完成為止。
使用場景
LangChain的使用場景包括:構(gòu)建聊天機(jī)器人、文本生成、文本分類、問答系統(tǒng)、語言翻譯、語言模型微調(diào)等。
安裝依賴庫
npm install -S langchain
Hello World
首先,使用Langchain來調(diào)用OpenAI模型。
import { OpenAI } from "langchain/llms/openai"; const model = new OpenAI({ openAIApiKey: 'sk-xxxx',//你的OpenAI API Key temperature: 0.9 }); const res = await model.call( "寫一首詩,限制20個(gè)字" ); console.log(res);
輸出
春風(fēng)迎新年,喜氣繞家園。
祝福短信語,友誼永綿長。
替換提示語中的參數(shù)
import { OpenAI } from "langchain/llms/openai"; import { PromptTemplate } from "langchain/prompts"; import { LLMChain } from "langchain/chains"; const model = new OpenAI({ openAIApiKey: 'sk-xxxx',//你的OpenAI API Key temperature: 0.9 }); const template = "What is a good name for a company that makes {product}?"; const prompt = new PromptTemplate({ template: template, inputVariables: ["product"], }); const chain = new LLMChain({ llm: model, prompt: prompt }); const res = await chain.call({ product: "colorful socks" }); console.log(res);
開始見識(shí)Langchain的強(qiáng)大
截止上個(gè)實(shí)例,你還沒見識(shí)到Langchain的強(qiáng)大。
接下來,你先注冊(cè)一個(gè)SerpApi
帳號(hào),獲取api key
。
點(diǎn)擊這里注冊(cè)
然后執(zhí)行以下的代碼,
import { OpenAI } from "langchain/llms/openai"; import { initializeAgentExecutorWithOptions } from "langchain/agents"; import { SerpAPI } from "langchain/tools"; import { Calculator } from "langchain/tools/calculator"; const model = new OpenAI({ streaming: true, openAIApiKey: 'sk-xxxx',//你的OpenAI API Key temperature: 0.9 }); const tools = [ new SerpAPI('你的SerpAPI的key', { location: "Austin,Texas,United States", hl: "en", gl: "us", }), new Calculator(), ]; const executor = await initializeAgentExecutorWithOptions(tools, model, { agentType: "zero-shot-react-description", }); console.log("Loaded agent."); const input = "誰是周杰倫的老婆?" + "她的年紀(jì)加上10是多少?" console.log(`Executing with input "${input}"...`); const result = await executor.call({ input }); console.log(`Got output ${result.output}`);
輸出:
Loaded agent.
Executing with input "誰是周杰倫的老婆?她的年紀(jì)加上10是多少?"...
Got output Hannah Quinlivan is Zhou Jielun's wife and she is 39 years old.
執(zhí)行結(jié)果做了兩件事,
- 使用
SerpAPI工具
獲取周杰倫的老婆的名字
:Quinlivan - 然后獲取
她的年齡
:29歲 - 最后使用
Calculator
工具加上10
:最終得到39歲的結(jié)果
這里引進(jìn)了Langchain
的agents
概念:代理。
決定模型采取哪些行動(dòng),執(zhí)行并且觀察流程,直到完成為止。
代碼中引進(jìn)了兩個(gè)工具:SerpAPI
和Calculator
:
const tools = [ new SerpAPI('你的SerpAPI的key', { location: "Austin,Texas,United States", hl: "en", gl: "us", }), new Calculator(), ];
以上就是Javascript版Langchain入門教程解析的詳細(xì)內(nèi)容,更多關(guān)于Javascript版Langchain入門的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序之頁面跳轉(zhuǎn)和參數(shù)傳遞的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序之頁面跳轉(zhuǎn)和參數(shù)傳遞的實(shí)現(xiàn)的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09微信小程序 網(wǎng)絡(luò)請(qǐng)求API詳解
這篇文章主要介紹了微信小程序 網(wǎng)絡(luò)請(qǐng)求API詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10Wireshark基本介紹和學(xué)習(xí)TCP三次握手
本文主要介紹Wireshark基本介紹和學(xué)習(xí)TCP三次握手,這里詳細(xì)整理了相關(guān)資料,并給出詳細(xì)流程,有需要的小伙伴可以參考下2016-08-08微信小程序之?dāng)?shù)據(jù)雙向綁定與數(shù)據(jù)操作
這篇文章主要介紹了微信小程序之?dāng)?shù)據(jù)雙向綁定與數(shù)據(jù)操作的相關(guān)資料,需要的朋友可以參考下2017-05-05自行實(shí)現(xiàn)Promise.allSettled的Polyfill處理
這篇文章主要為大家介紹了自行實(shí)現(xiàn)Promise.allSettled?的?Polyfill處理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08詳解微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用
這篇文章主要介紹了詳解微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用的相關(guān)資料,希望通過本文能幫助到大家掌握這方法,需要的朋友可以參考下2017-09-09JS圖形編輯器場景坐標(biāo)視口坐標(biāo)的相互轉(zhuǎn)換
這篇文章主要為大家介紹了JS圖形編輯器之場景坐標(biāo)視口坐標(biāo)的相互轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01