-
-
Notifications
You must be signed in to change notification settings - Fork 16.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
44adebe
commit c7306c9
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
packages/components/nodes/prompts/PromptLangfuse/PromptLangfuse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.