Skip to content

Commit

Permalink
Added langfuse-langchain and langfuse int as options
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Kais committed Dec 20, 2024
1 parent 0ba4c6e commit 0f14f47
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getConnectionHintNoticeField } from '@utils/sharedFields';
import { ollamaModel, ollamaOptions, ollamaDescription } from '../LMOllama/description';
import { makeN8nLlmFailedAttemptHandler } from '../n8nLlmFailedAttemptHandler';
import { N8nLlmTracing } from '../N8nLlmTracing';
import { CallbackHandler } from 'langfuse-langchain';

export class LmChatOllama implements INodeType {
description: INodeTypeDescription = {
Expand Down Expand Up @@ -61,12 +62,27 @@ export class LmChatOllama implements INodeType {
const modelName = this.getNodeParameter('model', itemIndex) as string;
const options = this.getNodeParameter('options', itemIndex, {}) as ChatOllamaInput;

const { langfuse } = options as {
langfuse?: {
baseUrl: string;
publicKey: string;
secretKey: string;
};
};

const langfuseHandler = new CallbackHandler({
sessionId: this.getExecutionId(),
publicKey: langfuse?.publicKey ?? process.env.LANGFUSE_PUBLIC_KEY,
secretKey: langfuse?.secretKey ?? process.env.LANGFUSE_SECRET_KEY,
baseUrl: langfuse?.baseUrl ?? process.env.LANGFUSE_HOST,
});

const model = new ChatOllama({
...options,
baseUrl: credentials.baseUrl as string,
model: modelName,
format: options.format === 'default' ? undefined : options.format,
callbacks: [new N8nLlmTracing(this)],
callbacks: [new N8nLlmTracing(this), langfuseHandler],
onFailedAttempt: makeN8nLlmFailedAttemptHandler(this),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getConnectionHintNoticeField } from '@utils/sharedFields';
import { ollamaDescription, ollamaModel, ollamaOptions } from './description';
import { makeN8nLlmFailedAttemptHandler } from '../n8nLlmFailedAttemptHandler';
import { N8nLlmTracing } from '../N8nLlmTracing';
import { CallbackHandler } from 'langfuse-langchain';

export class LmOllama implements INodeType {
description: INodeTypeDescription = {
Expand Down Expand Up @@ -60,11 +61,26 @@ export class LmOllama implements INodeType {
const modelName = this.getNodeParameter('model', itemIndex) as string;
const options = this.getNodeParameter('options', itemIndex, {}) as object;

const { langfuse } = options as {
langfuse?: {
baseUrl: string;
publicKey: string;
secretKey: string;
};
};

const langfuseHandler = new CallbackHandler({
sessionId: this.getExecutionId(),
publicKey: langfuse?.publicKey ?? process.env.LANGFUSE_PUBLIC_KEY,
secretKey: langfuse?.secretKey ?? process.env.LANGFUSE_SECRET_KEY,
baseUrl: langfuse?.baseUrl ?? process.env.LANGFUSE_HOST,
});

const model = new Ollama({
baseUrl: credentials.baseUrl as string,
model: modelName,
...options,
callbacks: [new N8nLlmTracing(this)],
callbacks: [new N8nLlmTracing(this), langfuseHandler],
onFailedAttempt: makeN8nLlmFailedAttemptHandler(this),
});

Expand Down
35 changes: 35 additions & 0 deletions packages/@n8n/nodes-langchain/nodes/llms/LMOllama/description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,41 @@ export const ollamaOptions: INodeProperties = {
description:
'Specifies the duration to keep the loaded model in memory after use. Useful for frequently used models. Format: 1h30m (1 hour 30 minutes).',
},
{
displayName: 'Langfuse',
name: 'langfuse',
placeholder: 'Add Langfuse Options',
description: 'Options for Langfuse tracing',
type: 'collection',
default: {},
options: [
{
displayName: 'Public Key',
name: 'publicKey',
type: 'string',
default: '',
description:
'Your Langfuse public key. Falls back to LANGFUSE_PUBLIC_KEY environment variable if not set.',
},
{
displayName: 'Secret Key',
name: 'secretKey',
type: 'string',
typeOptions: { password: true },
default: '',
description:
'Your Langfuse secret key. Falls back to LANGFUSE_SECRET_KEY environment variable if not set.',
},
{
displayName: 'Host',
name: 'baseUrl',
type: 'string',
default: '',
description:
'Your Langfuse host URL. Falls back to LANGFUSE_HOST environment variable if not set.',
},
],
},
{
displayName: 'Low VRAM Mode',
name: 'lowVram',
Expand Down
1 change: 1 addition & 0 deletions packages/@n8n/nodes-langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"html-to-text": "9.0.5",
"jsdom": "23.0.1",
"langchain": "0.3.6",
"langfuse-langchain": "^3.32.0",
"lodash": "catalog:",
"mammoth": "1.7.2",
"mime-types": "2.1.35",
Expand Down
Loading

0 comments on commit 0f14f47

Please sign in to comment.