Javascript版Langchain入門(mén)教程解析
引言
我是AI小火箭的HB,我探索和寫(xiě)作人工智能和語(yǔ)言交叉點(diǎn)的所有事物,范圍從LLM,聊天機(jī)器人,語(yǔ)音機(jī)器人,開(kāi)發(fā)框架,以數(shù)據(jù)為中心的潛在空間等。
介紹
LangChain是一個(gè)開(kāi)源Python庫(kù),用于構(gòu)建由大型語(yǔ)言模型(LLM)支持的應(yīng)用程序。它提供了一個(gè)框架,將LLM與其他數(shù)據(jù)源(如互聯(lián)網(wǎng)或個(gè)人文件)連接起來(lái),允許開(kāi)發(fā)人員將多個(gè)命令鏈接在一起,以創(chuàng)建更復(fù)雜的應(yīng)用程序。
LangChain創(chuàng)建于2022年10月,是圍繞LLMs(大語(yǔ)言模型)建立的一個(gè)框架,LLMs使用機(jī)器學(xué)習(xí)算法和海量數(shù)據(jù)來(lái)分析和理解自然語(yǔ)言。LangChain自身并不開(kāi)發(fā)LLMs,它的核心理念是為各種LLMs實(shí)現(xiàn)通用的接口,把LLMs相關(guān)的組件“鏈接”在一起,簡(jiǎn)化LLMs應(yīng)用的開(kāi)發(fā)難度,方便開(kāi)發(fā)者快速地開(kāi)發(fā)復(fù)雜的LLMs應(yīng)用。
支持的語(yǔ)言
LangChain目前有兩個(gè)語(yǔ)言的實(shí)現(xiàn):Python和Node.js。
組件
LangChain的組件包括:
- Models:模型,各種類型的模型和模型集成,比如GPT-4。
- Prompts:提示,包括提示管理、提示優(yōu)化和提示序列化。
- Memory:記憶,用來(lái)保存和模型交互時(shí)的上下文狀態(tài)。
- Indexes:索引,用來(lái)結(jié)構(gòu)化文檔,以便和模型交互。
- Chains:鏈,一系列對(duì)各種組件的調(diào)用。
- Agents:代理,決定模型采取哪些行動(dòng),執(zhí)行并且觀察流程,直到完成為止。
使用場(chǎng)景
LangChain的使用場(chǎng)景包括:構(gòu)建聊天機(jī)器人、文本生成、文本分類、問(wèn)答系統(tǒng)、語(yǔ)言翻譯、語(yǔ)言模型微調(diào)等。
安裝依賴庫(kù)
npm install -S langchain
Hello World
首先,使用Langchain來(lái)調(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(
"寫(xiě)一首詩(shī),限制20個(gè)字"
);
console.log(res);輸出
春風(fēng)迎新年,喜氣繞家園。
祝福短信語(yǔ),友誼永綿長(zhǎng)。
替換提示語(yǔ)中的參數(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);開(kāi)始見(jiàn)識(shí)Langchain的強(qiáng)大
截止上個(gè)實(shí)例,你還沒(méi)見(jiàn)識(shí)到Langchain的強(qiáng)大。
接下來(lái),你先注冊(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 =
"誰(shuí)是周杰倫的老婆?" +
"她的年紀(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 "誰(shuí)是周杰倫的老婆?她的年紀(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入門(mén)教程解析的詳細(xì)內(nèi)容,更多關(guān)于Javascript版Langchain入門(mén)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序之頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序之頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞的實(shí)現(xiàn)的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
微信小程序 網(wǎng)絡(luò)請(qǐng)求API詳解
這篇文章主要介紹了微信小程序 網(wǎng)絡(luò)請(qǐng)求API詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10
Wireshark基本介紹和學(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)資料,希望通過(guò)本文能幫助到大家掌握這方法,需要的朋友可以參考下2017-09-09
JS圖形編輯器場(chǎng)景坐標(biāo)視口坐標(biāo)的相互轉(zhuǎn)換
這篇文章主要為大家介紹了JS圖形編輯器之場(chǎng)景坐標(biāo)視口坐標(biāo)的相互轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

