add format prompt values to prompt template

This commit is contained in:
Henry
2023-04-18 23:27:05 +01:00
parent cd8a5b96eb
commit 17207e01db
13 changed files with 675 additions and 599 deletions
@@ -1,8 +1,7 @@
import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { LLMChain } from 'langchain/chains'
import { BaseLanguageModel } from 'langchain/base_language'
import { BasePromptTemplate } from 'langchain/prompts'
class LLMChain_Chains implements INode {
label: string
@@ -38,21 +37,8 @@ class LLMChain_Chains implements INode {
label: 'Chain Name',
name: 'chainName',
type: 'string',
placeholder: 'Task Creation Chain',
placeholder: 'Name Your Chain',
optional: true
},
{
label: 'Format Prompt Values',
name: 'promptValues',
type: 'string',
rows: 5,
placeholder: `{
"input_language": "English",
"output_language": "French"
}`,
optional: true,
acceptVariable: true,
list: true
}
]
this.outputs = [
@@ -71,9 +57,9 @@ class LLMChain_Chains implements INode {
async init(nodeData: INodeData, input: string): Promise<any> {
const model = nodeData.inputs?.model as BaseLanguageModel
const prompt = nodeData.inputs?.prompt as BasePromptTemplate
const prompt = nodeData.inputs?.prompt
const output = nodeData.outputs?.output as string
const promptValuesStr = nodeData.inputs?.promptValues as string
const promptValues = prompt.promptValues as ICommonObject
if (output === this.name) {
const chain = new LLMChain({ llm: model, prompt })
@@ -81,7 +67,7 @@ class LLMChain_Chains implements INode {
} else if (output === 'outputPrediction') {
const chain = new LLMChain({ llm: model, prompt })
const inputVariables = chain.prompt.inputVariables as string[] // ["product"]
const res = await runPrediction(inputVariables, chain, input, promptValuesStr)
const res = await runPrediction(inputVariables, chain, input, promptValues)
// eslint-disable-next-line no-console
console.log('\x1b[92m\x1b[1m\n*****OUTPUT PREDICTION*****\n\x1b[0m\x1b[0m')
// eslint-disable-next-line no-console
@@ -93,8 +79,9 @@ class LLMChain_Chains implements INode {
async run(nodeData: INodeData, input: string): Promise<string> {
const inputVariables = nodeData.instance.prompt.inputVariables as string[] // ["product"]
const chain = nodeData.instance as LLMChain
const promptValuesStr = nodeData.inputs?.promptValues as string
const res = await runPrediction(inputVariables, chain, input, promptValuesStr)
const promptValues = nodeData.inputs?.prompt.promptValues as ICommonObject
const res = await runPrediction(inputVariables, chain, input, promptValues)
// eslint-disable-next-line no-console
console.log('\x1b[93m\x1b[1m\n*****FINAL RESULT*****\n\x1b[0m\x1b[0m')
// eslint-disable-next-line no-console
@@ -103,14 +90,11 @@ class LLMChain_Chains implements INode {
}
}
const runPrediction = async (inputVariables: string[], chain: LLMChain, input: string, promptValuesStr: string) => {
const runPrediction = async (inputVariables: string[], chain: LLMChain, input: string, promptValues: ICommonObject) => {
if (inputVariables.length === 1) {
const res = await chain.run(input)
return res
} else if (inputVariables.length > 1) {
if (!promptValuesStr) throw new Error('Please provide Prompt Values')
const promptValues = JSON.parse(promptValuesStr.replace(/\s/g, ''))
let seen: string[] = []
for (const variable of inputVariables) {
@@ -1,4 +1,4 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate } from 'langchain/prompts'
@@ -25,15 +25,28 @@ class ChatPromptTemplate_Prompts implements INode {
label: 'System Message',
name: 'systemMessagePrompt',
type: 'string',
rows: 3,
rows: 4,
placeholder: `You are a helpful assistant that translates {input_language} to {output_language}.`
},
{
label: 'Human Message',
name: 'humanMessagePrompt',
type: 'string',
rows: 3,
rows: 4,
placeholder: `{text}`
},
{
label: 'Format Prompt Values',
name: 'promptValues',
type: 'string',
rows: 4,
placeholder: `{
"input_language": "English",
"output_language": "French"
}`,
optional: true,
acceptVariable: true,
list: true
}
]
}
@@ -41,11 +54,20 @@ class ChatPromptTemplate_Prompts implements INode {
async init(nodeData: INodeData): Promise<any> {
const systemMessagePrompt = nodeData.inputs?.systemMessagePrompt as string
const humanMessagePrompt = nodeData.inputs?.humanMessagePrompt as string
const promptValuesStr = nodeData.inputs?.promptValues as string
const prompt = ChatPromptTemplate.fromPromptMessages([
SystemMessagePromptTemplate.fromTemplate(systemMessagePrompt),
HumanMessagePromptTemplate.fromTemplate(humanMessagePrompt)
])
let promptValues: ICommonObject = {}
if (promptValuesStr) {
promptValues = JSON.parse(promptValuesStr.replace(/\s/g, ''))
}
// @ts-ignore
prompt.promptValues = promptValues
return prompt
}
}
@@ -27,7 +27,7 @@ class FewShotPromptTemplate_Prompts implements INode {
label: 'Examples',
name: 'examples',
type: 'string',
rows: 5,
rows: 4,
placeholder: `[
{ "word": "happy", "antonym": "sad" },
{ "word": "tall", "antonym": "short" },
@@ -42,14 +42,14 @@ class FewShotPromptTemplate_Prompts implements INode {
label: 'Prefix',
name: 'prefix',
type: 'string',
rows: 3,
rows: 4,
placeholder: `Give the antonym of every input`
},
{
label: 'Suffix',
name: 'suffix',
type: 'string',
rows: 3,
rows: 4,
placeholder: `Word: {input}\nAntonym:`
},
{
@@ -1,6 +1,6 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { ICommonObject, INode, INodeData, INodeParams, PromptTemplate } from '../../../src/Interface'
import { getBaseClasses, getInputVariables } from '../../../src/utils'
import { PromptTemplate, PromptTemplateInput } from 'langchain/prompts'
import { PromptTemplateInput } from 'langchain/prompts'
class PromptTemplate_Prompts implements INode {
label: string
@@ -19,20 +19,40 @@ class PromptTemplate_Prompts implements INode {
this.icon = 'prompt.svg'
this.category = 'Prompts'
this.description = 'Schema to represent a basic prompt for an LLM'
this.baseClasses = [this.type, ...getBaseClasses(PromptTemplate)]
this.baseClasses = [...getBaseClasses(PromptTemplate)]
this.inputs = [
{
label: 'Template',
name: 'template',
type: 'string',
rows: 5,
rows: 4,
placeholder: `What is a good name for a company that makes {product}?`
},
{
label: 'Format Prompt Values',
name: 'promptValues',
type: 'string',
rows: 4,
placeholder: `{
"input_language": "English",
"output_language": "French"
}`,
optional: true,
acceptVariable: true,
list: true
}
]
}
async init(nodeData: INodeData): Promise<any> {
const template = nodeData.inputs?.template as string
const promptValuesStr = nodeData.inputs?.promptValues as string
let promptValues: ICommonObject = {}
if (promptValuesStr) {
promptValues = JSON.parse(promptValuesStr.replace(/\s/g, ''))
}
const inputVariables = getInputVariables(template)
try {
@@ -41,6 +61,7 @@ class PromptTemplate_Prompts implements INode {
inputVariables
}
const prompt = new PromptTemplate(options)
prompt.promptValues = promptValues
return prompt
} catch (e) {
throw new Error(e)
+14
View File
@@ -88,3 +88,17 @@ export interface IMessage {
message: string
type: MessageType
}
/**
* Classes
*/
import { PromptTemplate as LangchainPromptTemplate, PromptTemplateInput } from 'langchain/prompts'
export class PromptTemplate extends LangchainPromptTemplate {
promptValues: ICommonObject
constructor(input: PromptTemplateInput) {
super(input)
}
}