add analytic

This commit is contained in:
Henry
2023-09-11 23:44:20 +01:00
parent 0a5d453338
commit d19f72db6f
42 changed files with 742 additions and 82 deletions
@@ -0,0 +1,39 @@
import { INodeParams, INodeCredential } from '../src/Interface'
class LangfuseApi implements INodeCredential {
label: string
name: string
version: number
description: string
inputs: INodeParams[]
constructor() {
this.label = 'Langfuse API'
this.name = 'langfuseApi'
this.version = 1.0
this.description =
'Refer to <a target="_blank" href="https://langfuse.com/docs/get-started/">official guide</a> on how to get API key on Langfuse'
this.inputs = [
{
label: 'Secret Key',
name: 'langFuseSecretKey',
type: 'password',
placeholder: 'sk-lf-abcdefg'
},
{
label: 'Public Key',
name: 'langFusePublicKey',
type: 'string',
placeholder: 'pk-lf-abcdefg'
},
{
label: 'Endpoint',
name: 'langFuseEndpoint',
type: 'string',
default: 'https://cloud.langfuse.com'
}
]
}
}
module.exports = { credClass: LangfuseApi }
@@ -0,0 +1,33 @@
import { INodeParams, INodeCredential } from '../src/Interface'
class LangsmithApi implements INodeCredential {
label: string
name: string
version: number
description: string
inputs: INodeParams[]
constructor() {
this.label = 'Langsmith API'
this.name = 'langsmithApi'
this.version = 1.0
this.description =
'Refer to <a target="_blank" href="https://docs.smith.langchain.com/">official guide</a> on how to get API key on Langsmith'
this.inputs = [
{
label: 'API Key',
name: 'langSmithApiKey',
type: 'password',
placeholder: '<LANGSMITH_API_KEY>'
},
{
label: 'Endpoint',
name: 'langSmithEndpoint',
type: 'string',
default: 'https://api.smith.langchain.com'
}
]
}
}
module.exports = { credClass: LangsmithApi }
@@ -4,7 +4,7 @@ import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../
import { LoadPyodide, finalSystemPrompt, systemPrompt } from './core'
import { LLMChain } from 'langchain/chains'
import { BaseLanguageModel } from 'langchain/base_language'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
import axios from 'axios'
class Airtable_Agents implements INode {
@@ -102,6 +102,7 @@ class Airtable_Agents implements INode {
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
const callbacks = await additionalCallbacks(nodeData, options)
const pyodide = await LoadPyodide()
@@ -141,7 +142,7 @@ json.dumps(my_dict)`
dict: dataframeColDict,
question: input
}
const res = await chain.call(inputs, [loggerHandler])
const res = await chain.call(inputs, [loggerHandler, ...callbacks])
pythonCode = res?.text
}
@@ -169,10 +170,10 @@ json.dumps(my_dict)`
}
if (options.socketIO && options.socketIOClientId) {
const result = await chain.call(inputs, [loggerHandler, handler])
const result = await chain.call(inputs, [loggerHandler, handler, ...callbacks])
return result?.text
} else {
const result = await chain.call(inputs, [loggerHandler])
const result = await chain.call(inputs, [loggerHandler, ...callbacks])
return result?.text
}
}
@@ -4,7 +4,7 @@ import { getBaseClasses } from '../../../src/utils'
import { LoadPyodide, finalSystemPrompt, systemPrompt } from './core'
import { LLMChain } from 'langchain/chains'
import { BaseLanguageModel } from 'langchain/base_language'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
class CSV_Agents implements INode {
label: string
@@ -63,6 +63,7 @@ class CSV_Agents implements INode {
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
const callbacks = await additionalCallbacks(nodeData, options)
let files: string[] = []
@@ -119,7 +120,7 @@ json.dumps(my_dict)`
dict: dataframeColDict,
question: input
}
const res = await chain.call(inputs, [loggerHandler])
const res = await chain.call(inputs, [loggerHandler, ...callbacks])
pythonCode = res?.text
}
@@ -149,10 +150,10 @@ json.dumps(my_dict)`
}
if (options.socketIO && options.socketIOClientId) {
const result = await chain.call(inputs, [loggerHandler, handler])
const result = await chain.call(inputs, [loggerHandler, handler, ...callbacks])
return result?.text
} else {
const result = await chain.call(inputs, [loggerHandler])
const result = await chain.call(inputs, [loggerHandler, ...callbacks])
return result?.text
}
}
@@ -3,7 +3,7 @@ import { initializeAgentExecutorWithOptions, AgentExecutor } from 'langchain/age
import { getBaseClasses, mapChatHistory } from '../../../src/utils'
import { flatten } from 'lodash'
import { BaseChatMemory } from 'langchain/memory'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
const defaultMessage = `Do your best to answer the questions. Feel free to use any tools available to look up relevant information, only if necessary.`
@@ -86,13 +86,14 @@ class ConversationalRetrievalAgent_Agents implements INode {
}
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
const result = await executor.call({ input }, [loggerHandler, handler])
const result = await executor.call({ input }, [loggerHandler, handler, ...callbacks])
return result?.output
} else {
const result = await executor.call({ input }, [loggerHandler])
const result = await executor.call({ input }, [loggerHandler, ...callbacks])
return result?.output
}
}
@@ -4,7 +4,7 @@ import { getBaseClasses, mapChatHistory } from '../../../src/utils'
import { BaseLanguageModel } from 'langchain/base_language'
import { flatten } from 'lodash'
import { BaseChatMemory } from 'langchain/memory'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
class OpenAIFunctionAgent_Agents implements INode {
label: string
@@ -86,13 +86,14 @@ class OpenAIFunctionAgent_Agents implements INode {
}
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
const result = await executor.run(input, [loggerHandler, handler])
const result = await executor.run(input, [loggerHandler, handler, ...callbacks])
return result
} else {
const result = await executor.run(input, [loggerHandler])
const result = await executor.run(input, [loggerHandler, ...callbacks])
return result
}
}
@@ -0,0 +1,33 @@
import { INode, INodeParams } from '../../../src/Interface'
class LangFuse_Analytic implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs?: INodeParams[]
credential: INodeParams
constructor() {
this.label = 'LangFuse'
this.name = 'langFuse'
this.version = 1.0
this.type = 'LangFuse'
this.icon = 'langfuse.png'
this.category = 'Analytic'
this.baseClasses = [this.type]
this.inputs = []
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['langfuseApi']
}
}
}
module.exports = { nodeClass: LangFuse_Analytic }
Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

@@ -0,0 +1,33 @@
import { INode, INodeParams } from '../../../src/Interface'
class LangSmith_Analytic implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs?: INodeParams[]
credential: INodeParams
constructor() {
this.label = 'LangSmith'
this.name = 'langSmith'
this.version = 1.0
this.type = 'LangSmith'
this.icon = 'langchain.png'
this.category = 'Analytic'
this.baseClasses = [this.type]
this.inputs = []
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['langsmithApi']
}
}
}
module.exports = { nodeClass: LangSmith_Analytic }
Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

@@ -3,7 +3,7 @@ import { APIChain } from 'langchain/chains'
import { getBaseClasses } from '../../../src/utils'
import { BaseLanguageModel } from 'langchain/base_language'
import { PromptTemplate } from 'langchain/prompts'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
export const API_URL_RAW_PROMPT_TEMPLATE = `You are given the below API Documentation:
{api_docs}
@@ -99,13 +99,14 @@ class GETApiChain_Chains implements INode {
const chain = await getAPIChain(apiDocs, model, headers, urlPrompt, ansPrompt)
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2)
const res = await chain.run(input, [loggerHandler, handler])
const res = await chain.run(input, [loggerHandler, handler, ...callbacks])
return res
} else {
const res = await chain.run(input, [loggerHandler])
const res = await chain.run(input, [loggerHandler, ...callbacks])
return res
}
}
@@ -2,7 +2,7 @@ import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Inter
import { APIChain, createOpenAPIChain } from 'langchain/chains'
import { getBaseClasses } from '../../../src/utils'
import { ChatOpenAI } from 'langchain/chat_models/openai'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
class OpenApiChain_Chains implements INode {
label: string
@@ -61,13 +61,14 @@ class OpenApiChain_Chains implements INode {
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
const chain = await initChain(nodeData)
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
const res = await chain.run(input, [loggerHandler, handler])
const res = await chain.run(input, [loggerHandler, handler, ...callbacks])
return res
} else {
const res = await chain.run(input, [loggerHandler])
const res = await chain.run(input, [loggerHandler, ...callbacks])
return res
}
}
@@ -3,7 +3,7 @@ import { getBaseClasses } from '../../../src/utils'
import { BaseLanguageModel } from 'langchain/base_language'
import { PromptTemplate } from 'langchain/prompts'
import { API_RESPONSE_RAW_PROMPT_TEMPLATE, API_URL_RAW_PROMPT_TEMPLATE, APIChain } from './postCore'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
class POSTApiChain_Chains implements INode {
label: string
@@ -88,13 +88,14 @@ class POSTApiChain_Chains implements INode {
const chain = await getAPIChain(apiDocs, model, headers, urlPrompt, ansPrompt)
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2)
const res = await chain.run(input, [loggerHandler, handler])
const res = await chain.run(input, [loggerHandler, handler, ...callbacks])
return res
} else {
const res = await chain.run(input, [loggerHandler])
const res = await chain.run(input, [loggerHandler, ...callbacks])
return res
}
}
@@ -4,7 +4,7 @@ import { getBaseClasses, mapChatHistory } from '../../../src/utils'
import { ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate } from 'langchain/prompts'
import { BufferMemory } from 'langchain/memory'
import { BaseChatModel } from 'langchain/chat_models/base'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
import { flatten } from 'lodash'
import { Document } from 'langchain/document'
@@ -111,13 +111,14 @@ class ConversationChain_Chains implements INode {
}
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
const res = await chain.call({ input }, [loggerHandler, handler])
const res = await chain.call({ input }, [loggerHandler, handler, ...callbacks])
return res?.response
} else {
const res = await chain.call({ input }, [loggerHandler])
const res = await chain.call({ input }, [loggerHandler, ...callbacks])
return res?.response
}
}
@@ -5,7 +5,7 @@ import { ConversationalRetrievalQAChain, QAChainParams } from 'langchain/chains'
import { BaseRetriever } from 'langchain/schema/retriever'
import { BufferMemory, BufferMemoryInput } from 'langchain/memory'
import { PromptTemplate } from 'langchain/prompts'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
import {
default_map_reduce_template,
default_qa_template,
@@ -183,6 +183,7 @@ class ConversationalRetrievalQAChain_Chains implements INode {
}
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(
@@ -191,7 +192,7 @@ class ConversationalRetrievalQAChain_Chains implements INode {
chainOption === 'refine' ? 4 : undefined,
returnSourceDocuments
)
const res = await chain.call(obj, [loggerHandler, handler])
const res = await chain.call(obj, [loggerHandler, handler, ...callbacks])
if (chainOption === 'refine') {
if (res.output_text && res.sourceDocuments) {
return {
@@ -204,7 +205,7 @@ class ConversationalRetrievalQAChain_Chains implements INode {
if (res.text && res.sourceDocuments) return res
return res?.text
} else {
const res = await chain.call(obj, [loggerHandler])
const res = await chain.call(obj, [loggerHandler, ...callbacks])
if (res.text && res.sourceDocuments) return res
return res?.text
}
@@ -2,7 +2,7 @@ import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from
import { getBaseClasses, handleEscapeCharacters } from '../../../src/utils'
import { LLMChain } from 'langchain/chains'
import { BaseLanguageModel } from 'langchain/base_language'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
class LLMChain_Chains implements INode {
label: string
@@ -70,7 +70,7 @@ class LLMChain_Chains implements INode {
} else if (output === 'outputPrediction') {
const chain = new LLMChain({ llm: model, prompt, verbose: process.env.DEBUG === 'true' ? true : false })
const inputVariables = chain.prompt.inputVariables as string[] // ["product"]
const res = await runPrediction(inputVariables, chain, input, promptValues, options)
const res = await runPrediction(inputVariables, chain, input, promptValues, options, nodeData)
// eslint-disable-next-line no-console
console.log('\x1b[92m\x1b[1m\n*****OUTPUT PREDICTION*****\n\x1b[0m\x1b[0m')
// eslint-disable-next-line no-console
@@ -88,7 +88,7 @@ class LLMChain_Chains implements INode {
const inputVariables = nodeData.instance.prompt.inputVariables as string[] // ["product"]
const chain = nodeData.instance as LLMChain
const promptValues = nodeData.inputs?.prompt.promptValues as ICommonObject
const res = await runPrediction(inputVariables, chain, input, promptValues, options)
const res = await runPrediction(inputVariables, chain, input, promptValues, options, nodeData)
// eslint-disable-next-line no-console
console.log('\x1b[93m\x1b[1m\n*****FINAL RESULT*****\n\x1b[0m\x1b[0m')
// eslint-disable-next-line no-console
@@ -102,9 +102,12 @@ const runPrediction = async (
chain: LLMChain,
input: string,
promptValuesRaw: ICommonObject,
options: ICommonObject
options: ICommonObject,
nodeData: INodeData
) => {
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
const isStreaming = options.socketIO && options.socketIOClientId
const socketIO = isStreaming ? options.socketIO : undefined
const socketIOClientId = isStreaming ? options.socketIOClientId : ''
@@ -131,10 +134,10 @@ const runPrediction = async (
const options = { ...promptValues }
if (isStreaming) {
const handler = new CustomChainHandler(socketIO, socketIOClientId)
const res = await chain.call(options, [loggerHandler, handler])
const res = await chain.call(options, [loggerHandler, handler, ...callbacks])
return res?.text
} else {
const res = await chain.call(options, [loggerHandler])
const res = await chain.call(options, [loggerHandler, ...callbacks])
return res?.text
}
} else if (seen.length === 1) {
@@ -147,10 +150,10 @@ const runPrediction = async (
}
if (isStreaming) {
const handler = new CustomChainHandler(socketIO, socketIOClientId)
const res = await chain.call(options, [loggerHandler, handler])
const res = await chain.call(options, [loggerHandler, handler, ...callbacks])
return res?.text
} else {
const res = await chain.call(options, [loggerHandler])
const res = await chain.call(options, [loggerHandler, ...callbacks])
return res?.text
}
} else {
@@ -159,10 +162,10 @@ const runPrediction = async (
} else {
if (isStreaming) {
const handler = new CustomChainHandler(socketIO, socketIOClientId)
const res = await chain.run(input, [loggerHandler, handler])
const res = await chain.run(input, [loggerHandler, handler, ...callbacks])
return res
} else {
const res = await chain.run(input, [loggerHandler])
const res = await chain.run(input, [loggerHandler, ...callbacks])
return res
}
}
@@ -2,7 +2,7 @@ import { BaseLanguageModel } from 'langchain/base_language'
import { ICommonObject, INode, INodeData, INodeParams, PromptRetriever } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { MultiPromptChain } from 'langchain/chains'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
class MultiPromptChain_Chains implements INode {
label: string
@@ -67,13 +67,14 @@ class MultiPromptChain_Chains implements INode {
const obj = { input }
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2)
const res = await chain.call(obj, [loggerHandler, handler])
const res = await chain.call(obj, [loggerHandler, handler, ...callbacks])
return res?.text
} else {
const res = await chain.call(obj, [loggerHandler])
const res = await chain.call(obj, [loggerHandler, ...callbacks])
return res?.text
}
}
@@ -2,7 +2,7 @@ import { BaseLanguageModel } from 'langchain/base_language'
import { ICommonObject, INode, INodeData, INodeParams, VectorStoreRetriever } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { MultiRetrievalQAChain } from 'langchain/chains'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
class MultiRetrievalQAChain_Chains implements INode {
label: string
@@ -75,14 +75,15 @@ class MultiRetrievalQAChain_Chains implements INode {
const obj = { input }
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2, returnSourceDocuments)
const res = await chain.call(obj, [loggerHandler, handler])
const res = await chain.call(obj, [loggerHandler, handler, ...callbacks])
if (res.text && res.sourceDocuments) return res
return res?.text
} else {
const res = await chain.call(obj, [loggerHandler])
const res = await chain.call(obj, [loggerHandler, ...callbacks])
if (res.text && res.sourceDocuments) return res
return res?.text
}
@@ -3,7 +3,7 @@ import { RetrievalQAChain } from 'langchain/chains'
import { BaseRetriever } from 'langchain/schema/retriever'
import { getBaseClasses } from '../../../src/utils'
import { BaseLanguageModel } from 'langchain/base_language'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
class RetrievalQAChain_Chains implements INode {
label: string
@@ -53,13 +53,14 @@ class RetrievalQAChain_Chains implements INode {
query: input
}
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
const res = await chain.call(obj, [loggerHandler, handler])
const res = await chain.call(obj, [loggerHandler, handler, ...callbacks])
return res?.text
} else {
const res = await chain.call(obj, [loggerHandler])
const res = await chain.call(obj, [loggerHandler, ...callbacks])
return res?.text
}
}
@@ -5,7 +5,7 @@ import { DataSource } from 'typeorm'
import { SqlDatabase } from 'langchain/sql_db'
import { BaseLanguageModel } from 'langchain/base_language'
import { PromptTemplate, PromptTemplateInput } from 'langchain/prompts'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
import { DataSourceOptions } from 'typeorm/data-source'
type DatabaseType = 'sqlite' | 'postgres' | 'mssql' | 'mysql'
@@ -119,13 +119,14 @@ class SqlDatabaseChain_Chains implements INode {
const chain = await getSQLDBChain(databaseType, url, model, customPrompt)
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2)
const res = await chain.run(input, [loggerHandler, handler])
const res = await chain.run(input, [loggerHandler, handler, ...callbacks])
return res
} else {
const res = await chain.run(input, [loggerHandler])
const res = await chain.run(input, [loggerHandler, ...callbacks])
return res
}
}
@@ -3,7 +3,7 @@ import { getBaseClasses } from '../../../src/utils'
import { VectorDBQAChain } from 'langchain/chains'
import { BaseLanguageModel } from 'langchain/base_language'
import { VectorStore } from 'langchain/vectorstores'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
class VectorDBQAChain_Chains implements INode {
label: string
@@ -57,13 +57,14 @@ class VectorDBQAChain_Chains implements INode {
}
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
if (options.socketIO && options.socketIOClientId) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
const res = await chain.call(obj, [loggerHandler, handler])
const res = await chain.call(obj, [loggerHandler, handler, ...callbacks])
return res?.text
} else {
const res = await chain.call(obj, [loggerHandler])
const res = await chain.call(obj, [loggerHandler, ...callbacks])
return res?.text
}
}
+3 -1
View File
@@ -42,7 +42,9 @@
"google-auth-library": "^9.0.0",
"graphql": "^16.6.0",
"html-to-text": "^9.0.5",
"langchain": "^0.0.133",
"langchain": "^0.0.145",
"langfuse-langchain": "^1.0.14-alpha.0",
"langsmith": "^0.0.32",
"linkifyjs": "^4.1.1",
"mammoth": "^1.5.1",
"moment": "^2.29.3",
+67
View File
@@ -2,6 +2,11 @@ import { BaseTracer, Run, BaseCallbackHandler } from 'langchain/callbacks'
import { AgentAction, ChainValues } from 'langchain/schema'
import { Logger } from 'winston'
import { Server } from 'socket.io'
import { Client } from 'langsmith'
import { LangChainTracer } from 'langchain/callbacks'
import { getCredentialData, getCredentialParam } from './utils'
import { ICommonObject, INodeData } from './Interface'
import CallbackHandler from 'langfuse-langchain'
interface AgentRun extends Run {
actions: AgentAction[]
@@ -178,3 +183,65 @@ export class CustomChainHandler extends BaseCallbackHandler {
}
}
}
export const additionalCallbacks = async (nodeData: INodeData, options: ICommonObject) => {
try {
if (!options.analytic) return []
const analytic = JSON.parse(options.analytic)
const callbacks: any = []
for (const provider in analytic) {
const providerStatus = analytic[provider].status as boolean
if (providerStatus) {
if (provider === 'langSmith') {
const credentialId = analytic[provider].credentialId as string
const langSmithProject = analytic[provider].projectName as string
const credentialData = await getCredentialData(credentialId ?? '', options)
const langSmithApiKey = getCredentialParam('langSmithApiKey', credentialData, nodeData)
const langSmithEndpoint = getCredentialParam('langSmithEndpoint', credentialData, nodeData)
const client = new Client({
apiUrl: langSmithEndpoint ?? 'https://api.smith.langchain.com',
apiKey: langSmithApiKey
})
const tracer = new LangChainTracer({
projectName: langSmithProject ?? 'default',
//@ts-ignore
client
})
callbacks.push(tracer)
} else if (provider === 'langFuse') {
const credentialId = analytic[provider].credentialId as string
const flushAt = analytic[provider].flushAt as string
const flushInterval = analytic[provider].flushInterval as string
const requestTimeout = analytic[provider].requestTimeout as string
const release = analytic[provider].release as string
const credentialData = await getCredentialData(credentialId ?? '', options)
const langFuseSecretKey = getCredentialParam('langFuseSecretKey', credentialData, nodeData)
const langFusePublicKey = getCredentialParam('langFusePublicKey', credentialData, nodeData)
const langFuseEndpoint = getCredentialParam('langFuseEndpoint', credentialData, nodeData)
const langFuseOptions: ICommonObject = {
secretKey: langFuseSecretKey,
publicKey: langFusePublicKey,
baseUrl: langFuseEndpoint ?? 'https://cloud.langfuse.com'
}
if (flushAt) langFuseOptions.flushAt = parseInt(flushAt, 10)
if (flushInterval) langFuseOptions.flushInterval = parseInt(flushInterval, 10)
if (requestTimeout) langFuseOptions.requestTimeout = parseInt(requestTimeout, 10)
if (release) langFuseOptions.release = release
const handler = new CallbackHandler(langFuseOptions)
callbacks.push(handler)
}
}
}
return callbacks
} catch (e) {
throw new Error(e)
}
}