Skip to content

Commit

Permalink
feat: Add langfuse-langchain and langfuse int as options (#5)
Browse files Browse the repository at this point in the history
* Added langfuse-langchain and langfuse int as options

* empty commit

* empty commit

* sanity test

* another try

* revert

* point to bitovi test-workflow repo now

* point to bitovi n8n-test-workflow repo now

* Try adding key to release-publish env

* Return repo path to test-workflows

* try putting encryption key in env for run-test-workflows

* try putting encryption key in env for run-test-workflows

* Do not run test-workflow

* trigger

---------

Co-authored-by: Emil Kais <ekais@apple.com>
  • Loading branch information
roy-coder and Emil Kais authored Jan 6, 2025
1 parent b32efbd commit 2a44866
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 114 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/release-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ name: 'Build and Publish Docker Image'
on:
push:
branches:
- 'feat/*'
- master
pull_request:
branches:
- master
types:
- closed

jobs:
publish-to-npm:
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/test-workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ jobs:
build:
name: Install & Build
runs-on: ubuntu-latest
if: github.event_name != 'pull_request_review' || startsWith(github.event.pull_request.base.ref, 'release/')
# if: github.event_name != 'pull_request_review' || startsWith(github.event.pull_request.base.ref, 'release/')
if: false
steps:
- uses: actions/checkout@v4.1.1
- run: corepack enable
Expand All @@ -44,6 +45,8 @@ jobs:
runs-on: ubuntu-latest
needs: build
timeout-minutes: 10
env:
N8N_ENCRYPTION_KEY: ${{secrets.ENCRYPTION_KEY}}
steps:
- uses: actions/checkout@v4.1.1
- run: corepack enable
Expand Down Expand Up @@ -72,7 +75,7 @@ jobs:
- name: Checkout workflows repo
uses: actions/checkout@v4.1.1
with:
repository: n8n-io/test-workflows
repository: bitovi/n8n-test-workflows
path: test-workflows

- name: Import credentials
Expand Down
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 2a44866

Please sign in to comment.