@terranlabs/appflow-langchain
Version:
Use langchain in appflow
103 lines (90 loc) • 4.36 kB
JavaScript
const { OpenAI } = require("langchain/llms/openai");
const { SimpleSequentialChain } = require("langchain/chains");
const DEV_LLM_SERVICE_URL = "http://10.22.0.38:8686/v1";
const DEV_ACCESS_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY0ZGIwMDAxOWFmMTA4ZTk5MDQ3NjIwNSIsImVtYWlsIjoic3lzdGVtX2FkbWluQGdtYWlsLmNvbSIsInVzZXJuYW1lIjoic3lzdGVtX2FkbWluIiwicm9sZSI6IlN5c3RlbSBBZG1pbiIsImlhdCI6MTY5NjkxODc0OSwiZXhwIjoxNzI4NDU0NzQ5fQ.PZEhH5fYJ9VfRYr-w-M1nmpg0s9UkQkERya5tKrf9lY";
const createLlm = (maxTokens = 16, temperature = 0.0, serviceUrl = DEV_LLM_SERVICE_URL, accessToken = DEV_ACCESS_TOKEN) => {
return new OpenAI({ temperature, openAIApiKey: accessToken, maxTokens, stop: ["<|im_end|>"] }, { basePath: serviceUrl })
}
const createSearchChain = () => {
const { BaseChain } = require("langchain/chains");
const { customSearch } = require("./agent")
class SearchChain extends BaseChain {
static lc_name() {
return "SearchChain";
}
_chainType() {
return "search";
}
get inputKeys() {
return this.inputVariables;
}
get outputKeys() {
return this.outputVariables;
}
constructor(fields) {
super(fields);
Object.defineProperty(this, "searchFunc", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "inputVariables", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "outputVariables", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.searchFunc = fields.search;
this.inputVariables = fields.inputVariables;
this.outputVariables = fields.outputVariables;
}
async _call(values, runManager) {
return { text: await this.searchFunc(values[this.inputVariables[0]], "google", runManager?.getChild("search")) };
}
}
const searchChain = new SearchChain({ search: customSearch, inputVariables: ["text"], outputVariables: ["text"] })
return searchChain
}
const createGenLeadChain = () => {
const { PromptTemplate } = require("langchain/prompts");
const { LLMChain } = require("langchain/chains");
const llm = createLlm(maxTokens = 1024);
const chat_format_prompt_for_extract_name_position = `<|im_start|>system
Assistant find information of Board of Director in user's input. Let's do step by step:
1. Read the input carefully. Focus on human name and position.
1. Find all people name who has one of these position 代表取締役社長 or 専務取締役 or 取締役 or 執行役員 or 社外取締役 or 社外監査役 or 社外役員 監査役 appear in the input
2. Combine names and position.
3. Output format: - position: name<|im_end|>
<|im_start|>user
Context: {text}<|im_end|>
<|im_start|>assistant
`
const chat_format_prompt_for_JSON_format = `<|im_start|>system
Assistant help format user's input into JSON format. Let's do step by step:
1. Read the input carefully. Input format is: - <position>: <name>, ..
2. Find name and postion in user's input.
3. Ignore all rows don't have any name
4. Output Format: {{"<position>": ["<name>"]}}.<|im_end|>
<|im_start|>user
{text}<|im_end|>
<|im_start|>assistant
`
const prompt_for_extract_name_position = PromptTemplate.fromTemplate(chat_format_prompt_for_extract_name_position)
const extract_name_position_chain = new LLMChain({ llm, prompt:prompt_for_extract_name_position })
const prompt_for_JSON_format = PromptTemplate.fromTemplate(chat_format_prompt_for_JSON_format)
const JSON_format_chain = new LLMChain({ llm, prompt: prompt_for_JSON_format })
return new SimpleSequentialChain({ chains: [extract_name_position_chain, JSON_format_chain] })
}
const createFullChain = () => {
const finalChain = new SimpleSequentialChain({ chains: [createSearchChain(), createGenLeadChain()], verbose: true })
return finalChain
}
const chain = createFullChain()
chain.run("大阪市高速電気軌道株式会社").then(d => console.log(d))