Add prompt template from langfuse (#2883)

* feat: add langfuse prompt template

* style: change component label

* style: adjust some semi-colons and add author field

* add credential to PromptLangfuse.ts

* fix linting issue

---------

Co-authored-by: Henry Heng <henryheng@flowiseai.com>
This commit is contained in:
Lucas Cruz 2024-08-07 08:08:31 -03:00 committed by GitHub
parent 44adebe953
commit c7306c93d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,96 @@
import { ICommonObject, INode, INodeData, INodeParams, PromptTemplate } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam, getInputVariables } from '../../../src/utils'
import { PromptTemplateInput } from '@langchain/core/prompts'
import { Langfuse } from 'langfuse'
class PromptLangfuse_Prompts implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
author: string
baseClasses: string[]
credential: INodeParams
inputs: INodeParams[]
constructor() {
this.label = 'LangFuse Prompt Template'
this.name = 'promptLangFuse'
this.version = 1.0
this.type = 'PromptTemplate'
this.icon = 'prompt.svg'
this.category = 'Prompts'
this.author = 'Lucas Cruz'
this.description = 'Fetch schema from LangFuse to represent a prompt for an LLM'
this.baseClasses = [...getBaseClasses(PromptTemplate)]
this.credential = {
label: 'Langfuse Credential',
name: 'credential',
type: 'credential',
credentialNames: ['langfuseApi']
}
this.inputs = [
{
label: 'Prompt Name',
name: 'template',
type: 'string',
placeholder: `Name of the template`
},
{
label: 'Format Prompt Values',
name: 'promptValues',
type: 'json',
optional: true,
acceptVariable: true,
list: true
}
]
}
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const langFuseSecretKey = getCredentialParam('langFuseSecretKey', credentialData, nodeData)
const langFusePublicKey = getCredentialParam('langFusePublicKey', credentialData, nodeData)
const langFuseEndpoint = getCredentialParam('langFuseEndpoint', credentialData, nodeData)
const langfuse = new Langfuse({
secretKey: langFuseSecretKey,
publicKey: langFusePublicKey,
baseUrl: langFuseEndpoint ?? 'https://cloud.langfuse.com',
sdkIntegration: 'Flowise'
})
const langfusePrompt = await langfuse.getPrompt(nodeData.inputs?.template as string)
const template = langfusePrompt.getLangchainPrompt()
const promptValuesStr = nodeData.inputs?.promptValues
let promptValues: ICommonObject = {}
if (promptValuesStr) {
try {
promptValues = typeof promptValuesStr === 'object' ? promptValuesStr : JSON.parse(promptValuesStr)
} catch (exception) {
throw new Error("Invalid JSON in the PromptTemplate's promptValues: " + exception)
}
}
const inputVariables = getInputVariables(template)
try {
const options: PromptTemplateInput = {
template,
inputVariables
}
const prompt = new PromptTemplate(options)
prompt.promptValues = promptValues
return prompt
} catch (e) {
throw new Error(e)
}
}
}
module.exports = { nodeClass: PromptLangfuse_Prompts }

View File

@ -0,0 +1,5 @@
<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="5" y="7" width="22" height="18" rx="2" stroke="black" stroke-width="2"/>
<path d="M11 12L15 16L11 20" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M17 20H21" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

After

Width:  |  Height:  |  Size: 396 B