Lang Anchor provides a structured way to define AI prompts, manage missions, enforce rules, and format output results.
Install Lang Anchor via npm or yarn:
npm i @knfs-tech/lang-anchor
#or
yarn add @knfs-tech/lang-anchor
- Prompting: Helps you build standard reminder forms, optimizing your reminder process by including usage role, AI role, tasks, rules, output data, and reinforcement data
- Rule Constants: Includes built-in rule set to increase prompt accuracy and efficiency.
- Language Constants: A set of available languages, convenient to use
- Roles Constants: A set of user and AI roles are available for ease of use
- Chain Construction: Build a list of steps to manipulate axiomatic data for RAG and AI Agents models
const { Mission, ResultForm, PromptTemplate } = require("@knfs-tech/lang-anchor")
const { rules, roles, languages } = require("@knfs-tech/lang-anchor").constants
const variableInputs = [
{ type: "string", name: "text" },
]
const tasks = [
"Summarize the given text into key points: {text} ",
"Translate the give text to Vietnamese: {text}",
]
const mission = new Mission(tasks, variableInputs)
const resultForm = new ResultForm("json", {
summary: "string",
keyPoints: "string",
translation: "string"
})
const modelRole = roles.modelRole.TRANSLATOR
const ruleApplies = [
rules.noMoreCreativity,
rules.onlyFormatResult,
rules.noBias,
rules.factualOnly
]
const prompt = new PromptTemplate(
mission,
resultForm,
{
modelRole,
rules: ruleApplies,
}
)
const inputData = {text: "Lang Anchor is powerful LLM AI framework..."}
prompt.addEnhancedData("keyPoints should be defined by ,")
//get prompted data
const query = prompt.get(inputData)
const { Mission, ResultForm, PromptTemplate, Chain } = require("@knfs-tech/lang-anchor")
const { rules, roles, languages } = require("@knfs-tech/lang-anchor").constants
const processChain = new Chain()
const inputData = {text: "Lang Anchor is powerful LLM AI framework..."}
processChain
.addStep(
async() => {
const variableInputs = [
{ type: "string", name: "text" },
]
const tasks = [
"Summarize the given text into key points: {text} ",
"Translate the give text to Vietnamese: {text}",
]
const mission = new Mission(tasks, variableInputs)
const resultForm = new ResultForm("json", {
summary: "string",
keyPoints: "string",
translation: "string"
})
const modelRole = roles.modelRole.TRANSLATOR
const ruleApplies = [
rules.noMoreCreativity,
rules.onlyFormatResult,
rules.noBias,
rules.factualOnly
]
const prompt = new PromptTemplate(
mission,
resultForm,
{
modelRole,
rules: ruleApplies,
}
)
prompt.addEnhancedData("keyPoints should be defined by ,")
return prompt
}
)
.addStep(
async() => {
// Add more context data, by getting more information from vectorDatabase
const prompt = processChain.results[0]
const vectorDBResults = getDataFromVectorDB(inputData.text)
for (const result of vectorDBResults) {
prompt.addEnhancedData(result)
}
}
)
.addStep(
async() => {
// Add more context data, by getting more information from vectorDatabase
const prompt = processChain.results[0]
const query = prompt.get(inputData)
return await callLLMMode(query);
}
)
const result = await processChain.run();
Create mission of AI
Parameter | Type | Default | Description | Support Version |
---|---|---|---|---|
tasks |
Array string | None | AI task work list. | >= 0.1.1 |
variableInputs |
Array ({type: , name: }) | None | Variable input of the task. | >= 0.1.1 |
const { Mission } = require("@knfs-tech/lang-anchor")
const variableInputs = [
{ type: "string", name: "mainKeyWords" },
{ type: "string", name: "secondaryKeywords"}
]
const tasks = [
"Write me an SEO standard article with main keyword: {mainKeyWords} and secondary keywords: {secondaryKeywords} ",
]
const mission = new Mission(tasks, variableInputs)
Get mission prompt with value inputs
Parameter | Type | Default | Description | Support Version |
---|---|---|---|---|
valueInputs |
Array ({ : <value of variable input}) | None | Value of variable input. | >= 0.1.1 |
Returns: String
- The prompted string
const inputData = {
mainKeyWords: "Logitech wireless mouse",
secondaryKeywords: "Logitech GHUB, Logitech mouse",
}
const missionPrompt = mission.get(inputData)
Create result form of AI
Parameter | Type | Default | Description | Support Version |
---|---|---|---|---|
type |
String | "string" | Type of response received when communicating with LLM. | >= 0.1.1 |
structure |
String | "" | Structure of the returned response. | >= 0.1.1 |
const { ResultForm } = require("@knfs-tech/lang-anchor")
const resultForm = new ResultForm("json", {
content: "string",
keywords: "string",
})
Get result prompt with value inputs
Returns: String
- The prompted string
const resultFormPrompt = resultForm.get()
Create prompt tempt of AI
Parameter | Type | Default | Description | Support Version |
---|---|---|---|---|
mission |
Mission | Mission of AI | >= 0.1.1 | |
resultForm |
ResultForm | Result form of response AI | >= 0.1.1 | |
options.modelRole |
string | roleCons.modelRole.PERSONAL_ASSISTANT | The role of the AI. | >= 0.1.1 |
options.userRole |
string | roleCons.userRole.NORMAL_USER | The role of user. | >= 0.1.1 |
options.language |
string | languageCons.en | The language for the result. | >= 0.1.1 |
options.rules |
Array | [] | A set of rules the AI must follow. | >= 0.1.1 |
const { PromptTemplate } = require("@knfs-tech/lang-anchor");
const { rules, roles, languages } = require("@knfs-tech/lang-anchor").constants;
const modelRole = roles.modelRole.MARKETING_EXPERT
const ruleApplies = [
rules.onlyFormatResult,
rules.noBias,
rules.factualOnly,
rules.useFormalTone,
rules.followInstructions
]
const prompt = new PromptTemplate(
mission,
resultForm,
{
modelRole,
rules: ruleApplies,
language: languages.vi
}
)
Get prompt with value inputs
Parameter | Type | Default | Description | Support Version |
---|---|---|---|---|
enhanceData |
string | None | The enhanced contextual data to increase accuracy. | >= 0.1.1 |
prompt.addEnhancedData("The article has 3H2.")
prompt.addEnhancedData("Articles need to be at least 1000 words.")
Get prompt with value inputs
Parameter | Type | Default | Description | Support Version |
---|---|---|---|---|
valueInputs |
Array ({ : <value of variable input}) | None | Value of variable input. | >= 0.1.1 |
Returns: String
- The prompted string
const inputData = {
mainKeyWords: "Logitech wireless mouse",
secondaryKeywords: "Logitech GHUB, Logitech mouse",
}
const query = prompt.get(inputData)
Create step-by-step AI processing.
Returns: Chain
- This chain
Get prompt with value inputs
Parameter | Type | Default | Description | Support Version |
---|---|---|---|---|
step |
function | None | The function handle in step. | >= 0.1.1 |
Returns: Chain
- This chain
const { Chain } = require("@knfs-tech/lang-anchor")
const processChain = new Chain()
processChain
.addStep(
async () => console.log("a")
)
.addStep(
async () => console.log("b")
)
.addStep(
async () => {
return "c"
}
)
Run process
Returns: Promise<*>
- Final result in result list
const reulst = await processChain.run()
Lang Anchor is open-sourced software licensed under the MIT license.