Javascript版Langchain入門教程解析
引言
我是AI小火箭的HB,我探索和寫作人工智能和語言交叉點的所有事物,范圍從LLM,聊天機器人,語音機器人,開發(fā)框架,以數(shù)據(jù)為中心的潛在空間等。
介紹
LangChain是一個開源Python庫,用于構(gòu)建由大型語言模型(LLM)支持的應用程序。它提供了一個框架,將LLM與其他數(shù)據(jù)源(如互聯(lián)網(wǎng)或個人文件)連接起來,允許開發(fā)人員將多個命令鏈接在一起,以創(chuàng)建更復雜的應用程序。
LangChain創(chuàng)建于2022年10月,是圍繞LLMs(大語言模型)建立的一個框架,LLMs使用機器學習算法和海量數(shù)據(jù)來分析和理解自然語言。LangChain自身并不開發(fā)LLMs,它的核心理念是為各種LLMs實現(xiàn)通用的接口,把LLMs相關的組件“鏈接”在一起,簡化LLMs應用的開發(fā)難度,方便開發(fā)者快速地開發(fā)復雜的LLMs應用。
支持的語言
LangChain目前有兩個語言的實現(xiàn):Python和Node.js。
組件
LangChain的組件包括:
- Models:模型,各種類型的模型和模型集成,比如GPT-4。
- Prompts:提示,包括提示管理、提示優(yōu)化和提示序列化。
- Memory:記憶,用來保存和模型交互時的上下文狀態(tài)。
- Indexes:索引,用來結(jié)構(gòu)化文檔,以便和模型交互。
- Chains:鏈,一系列對各種組件的調(diào)用。
- Agents:代理,決定模型采取哪些行動,執(zhí)行并且觀察流程,直到完成為止。
使用場景
LangChain的使用場景包括:構(gòu)建聊天機器人、文本生成、文本分類、問答系統(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個字"
);
console.log(res);輸出
春風迎新年,喜氣繞家園。
祝福短信語,友誼永綿長。
替換提示語中的參數(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);開始見識Langchain的強大
截止上個實例,你還沒見識到Langchain的強大。
接下來,你先注冊一個SerpApi帳號,獲取api key。
點擊這里注冊
然后執(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 =
"誰是周杰倫的老婆?" +
"她的年紀加上10是多少?"
console.log(`Executing with input "${input}"...`);
const result = await executor.call({ input });
console.log(`Got output ${result.output}`);輸出:
Loaded agent.
Executing with input "誰是周杰倫的老婆?她的年紀加上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é)果
這里引進了Langchain的agents概念:代理。
決定模型采取哪些行動,執(zhí)行并且觀察流程,直到完成為止。
代碼中引進了兩個工具:SerpAPI和Calculator:
const tools = [
new SerpAPI('你的SerpAPI的key', {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];以上就是Javascript版Langchain入門教程解析的詳細內(nèi)容,更多關于Javascript版Langchain入門的資料請關注腳本之家其它相關文章!
相關文章
微信小程序之頁面跳轉(zhuǎn)和參數(shù)傳遞的實現(xiàn)
這篇文章主要介紹了微信小程序之頁面跳轉(zhuǎn)和參數(shù)傳遞的實現(xiàn)的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09

