6658 workflows add a first twenty piece email sender (#6965)

This commit is contained in:
martmull
2024-09-12 11:00:25 +02:00
committed by GitHub
parent f8e5b333d9
commit 3190f4a87b
397 changed files with 1143 additions and 1037 deletions

View File

@ -0,0 +1,26 @@
/* eslint-disable no-console */
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
import { Run } from '@langchain/core/tracers/base';
import { ConsoleCallbackHandler } from '@langchain/core/tracers/console';
import { LLMTracingDriver } from 'src/engine/core-modules/llm-tracing/drivers/interfaces/llm-tracing-driver.interface';
class WithMetadataConsoleCallbackHandler extends ConsoleCallbackHandler {
private metadata: Record<string, unknown>;
constructor(metadata: Record<string, unknown>) {
super();
this.metadata = metadata;
}
onChainStart(run: Run) {
console.log(`Chain metadata: ${JSON.stringify(this.metadata)}`);
super.onChainStart(run);
}
}
export class ConsoleDriver implements LLMTracingDriver {
getCallbackHandler(metadata: Record<string, unknown>): BaseCallbackHandler {
return new WithMetadataConsoleCallbackHandler(metadata);
}
}

View File

@ -0,0 +1,5 @@
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
export interface LLMTracingDriver {
getCallbackHandler(metadata: Record<string, unknown>): BaseCallbackHandler;
}

View File

@ -0,0 +1,26 @@
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
import CallbackHandler from 'langfuse-langchain';
import { LLMTracingDriver } from 'src/engine/core-modules/llm-tracing/drivers/interfaces/llm-tracing-driver.interface';
export interface LangfuseDriverOptions {
secretKey: string;
publicKey: string;
}
export class LangfuseDriver implements LLMTracingDriver {
private options: LangfuseDriverOptions;
constructor(options: LangfuseDriverOptions) {
this.options = options;
}
getCallbackHandler(metadata: Record<string, unknown>): BaseCallbackHandler {
return new CallbackHandler({
secretKey: this.options.secretKey,
publicKey: this.options.publicKey,
baseUrl: 'https://cloud.langfuse.com',
metadata: metadata,
});
}
}

View File

@ -0,0 +1,26 @@
import { ModuleMetadata, FactoryProvider } from '@nestjs/common';
import { LangfuseDriverOptions } from 'src/engine/core-modules/llm-tracing/drivers/langfuse.driver';
export enum LLMTracingDriver {
Langfuse = 'langfuse',
Console = 'console',
}
export interface LangfuseDriverFactoryOptions {
type: LLMTracingDriver.Langfuse;
options: LangfuseDriverOptions;
}
export interface ConsoleDriverFactoryOptions {
type: LLMTracingDriver.Console;
}
export type LLMTracingModuleOptions =
| LangfuseDriverFactoryOptions
| ConsoleDriverFactoryOptions;
export type LLMTracingModuleAsyncOptions = {
useFactory: (...args: any[]) => LLMTracingModuleOptions;
} & Pick<ModuleMetadata, 'imports'> &
Pick<FactoryProvider, 'inject'>;

View File

@ -0,0 +1 @@
export const LLM_TRACING_DRIVER = Symbol('LLM_TRACING_DRIVER');

View File

@ -0,0 +1,34 @@
import { LLMTracingDriver } from 'src/engine/core-modules/llm-tracing/interfaces/llm-tracing.interface';
import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';
export const llmTracingModuleFactory = (
environmentService: EnvironmentService,
) => {
const driver = environmentService.get('LLM_TRACING_DRIVER');
switch (driver) {
case LLMTracingDriver.Console: {
return { type: LLMTracingDriver.Console as const };
}
case LLMTracingDriver.Langfuse: {
const secretKey = environmentService.get('LANGFUSE_SECRET_KEY');
const publicKey = environmentService.get('LANGFUSE_PUBLIC_KEY');
if (!(secretKey && publicKey)) {
throw new Error(
`${driver} LLM tracing driver requires LANGFUSE_SECRET_KEY and LANGFUSE_PUBLIC_KEY to be defined, check your .env file`,
);
}
return {
type: LLMTracingDriver.Langfuse as const,
options: { secretKey, publicKey },
};
}
default:
throw new Error(
`Invalid LLM tracing driver (${driver}), check your .env file`,
);
}
};

View File

@ -0,0 +1,39 @@
import { Global, DynamicModule } from '@nestjs/common';
import {
LLMTracingModuleAsyncOptions,
LLMTracingDriver,
} from 'src/engine/core-modules/llm-tracing/interfaces/llm-tracing.interface';
import { LangfuseDriver } from 'src/engine/core-modules/llm-tracing/drivers/langfuse.driver';
import { ConsoleDriver } from 'src/engine/core-modules/llm-tracing/drivers/console.driver';
import { LLMTracingService } from 'src/engine/core-modules/llm-tracing/llm-tracing.service';
import { LLM_TRACING_DRIVER } from 'src/engine/core-modules/llm-tracing/llm-tracing.constants';
@Global()
export class LLMTracingModule {
static forRoot(options: LLMTracingModuleAsyncOptions): DynamicModule {
const provider = {
provide: LLM_TRACING_DRIVER,
useFactory: (...args: any[]) => {
const config = options.useFactory(...args);
switch (config.type) {
case LLMTracingDriver.Langfuse: {
return new LangfuseDriver(config.options);
}
case LLMTracingDriver.Console: {
return new ConsoleDriver();
}
}
},
inject: options.inject || [],
};
return {
module: LLMTracingModule,
providers: [LLMTracingService, provider],
exports: [LLMTracingService],
};
}
}

View File

@ -0,0 +1,16 @@
import { Injectable, Inject } from '@nestjs/common';
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
import { LLMTracingDriver } from 'src/engine/core-modules/llm-tracing/drivers/interfaces/llm-tracing-driver.interface';
import { LLM_TRACING_DRIVER } from 'src/engine/core-modules/llm-tracing/llm-tracing.constants';
@Injectable()
export class LLMTracingService {
constructor(@Inject(LLM_TRACING_DRIVER) private driver: LLMTracingDriver) {}
getCallbackHandler(metadata: Record<string, unknown>): BaseCallbackHandler {
return this.driver.getCallbackHandler(metadata);
}
}