Add LangWatch integration (#2677)

Add langwatch integration
This commit is contained in:
Rogério Chaves
2024-06-21 17:26:55 +02:00
committed by GitHub
parent aec9e7a3b7
commit 0fc5e3d0c5
9 changed files with 1014 additions and 218 deletions
+130 -1
View File
@@ -16,6 +16,7 @@ import { LunaryHandler } from '@langchain/community/callbacks/handlers/lunary'
import { getCredentialData, getCredentialParam, getEnvironmentVariable } from './utils'
import { ICommonObject, INodeData } from './Interface'
import { LangWatch, LangWatchSpan, LangWatchTrace, autoconvertTypedValues } from 'langwatch'
interface AgentRun extends Run {
actions: AgentAction[]
@@ -293,6 +294,17 @@ export const additionalCallbacks = async (nodeData: INodeData, options: ICommonO
const handler = new LunaryHandler(lunaryFields)
callbacks.push(handler)
} else if (provider === 'langWatch') {
const langWatchApiKey = getCredentialParam('langWatchApiKey', credentialData, nodeData)
const langWatchEndpoint = getCredentialParam('langWatchEndpoint', credentialData, nodeData)
const langwatch = new LangWatch({
apiKey: langWatchApiKey,
endpoint: langWatchEndpoint
})
const trace = langwatch.getTrace()
callbacks.push(trace.getLangChainCallback())
}
}
}
@@ -360,6 +372,16 @@ export class AnalyticHandler {
})
this.handlers['lunary'] = { client: lunary }
} else if (provider === 'langWatch') {
const langWatchApiKey = getCredentialParam('langWatchApiKey', credentialData, this.nodeData)
const langWatchEndpoint = getCredentialParam('langWatchEndpoint', credentialData, this.nodeData)
const langwatch = new LangWatch({
apiKey: langWatchApiKey,
endpoint: langWatchEndpoint
})
this.handlers['langWatch'] = { client: langwatch }
}
}
}
@@ -372,7 +394,8 @@ export class AnalyticHandler {
const returnIds: ICommonObject = {
langSmith: {},
langFuse: {},
lunary: {}
lunary: {},
langWatch: {}
}
if (Object.prototype.hasOwnProperty.call(this.handlers, 'langSmith')) {
@@ -460,6 +483,33 @@ export class AnalyticHandler {
}
}
if (Object.prototype.hasOwnProperty.call(this.handlers, 'langWatch')) {
let langwatchTrace: LangWatchTrace
if (!parentIds || !Object.keys(parentIds).length) {
const langwatch: LangWatch = this.handlers['langWatch'].client
langwatchTrace = langwatch.getTrace({
name,
metadata: { tags: ['openai-assistant'], threadId: this.options.chatId },
...this.nodeData?.inputs?.analytics?.langWatch
})
} else {
langwatchTrace = this.handlers['langWatch'].trace[parentIds['langWatch']]
}
if (langwatchTrace) {
const span = langwatchTrace.startSpan({
name,
type: 'chain',
input: autoconvertTypedValues(input)
})
this.handlers['langWatch'].trace = { [langwatchTrace.traceId]: langwatchTrace }
this.handlers['langWatch'].span = { [span.spanId]: span }
returnIds['langWatch'].trace = langwatchTrace.traceId
returnIds['langWatch'].span = span.spanId
}
}
return returnIds
}
@@ -508,6 +558,15 @@ export class AnalyticHandler {
})
}
}
if (Object.prototype.hasOwnProperty.call(this.handlers, 'langWatch')) {
const span: LangWatchSpan | undefined = this.handlers['langWatch'].span[returnIds['langWatch'].span]
if (span) {
span.end({
output: autoconvertTypedValues(output)
})
}
}
}
async onChainError(returnIds: ICommonObject, error: string | object, shutdown = false) {
@@ -557,6 +616,15 @@ export class AnalyticHandler {
})
}
}
if (Object.prototype.hasOwnProperty.call(this.handlers, 'langWatch')) {
const span: LangWatchSpan | undefined = this.handlers['langWatch'].span[returnIds['langWatch'].span]
if (span) {
span.end({
error
})
}
}
}
async onLLMStart(name: string, input: string, parentIds: ICommonObject) {
@@ -612,6 +680,18 @@ export class AnalyticHandler {
}
}
if (Object.prototype.hasOwnProperty.call(this.handlers, 'langWatch')) {
const trace: LangWatchTrace | undefined = this.handlers['langWatch'].trace[parentIds['langWatch'].trace]
if (trace) {
const span = trace.startLLMSpan({
name,
input: autoconvertTypedValues(input)
})
this.handlers['langWatch'].span = { [span.spanId]: span }
returnIds['langWatch'].span = span.spanId
}
}
return returnIds
}
@@ -648,6 +728,15 @@ export class AnalyticHandler {
})
}
}
if (Object.prototype.hasOwnProperty.call(this.handlers, 'langWatch')) {
const span: LangWatchSpan | undefined = this.handlers['langWatch'].span[returnIds['langWatch'].span]
if (span) {
span.end({
output: autoconvertTypedValues(output)
})
}
}
}
async onLLMError(returnIds: ICommonObject, error: string | object) {
@@ -683,6 +772,15 @@ export class AnalyticHandler {
})
}
}
if (Object.prototype.hasOwnProperty.call(this.handlers, 'langWatch')) {
const span: LangWatchSpan | undefined = this.handlers['langWatch'].span[returnIds['langWatch'].span]
if (span) {
span.end({
error
})
}
}
}
async onToolStart(name: string, input: string | object, parentIds: ICommonObject) {
@@ -738,6 +836,19 @@ export class AnalyticHandler {
}
}
if (Object.prototype.hasOwnProperty.call(this.handlers, 'langWatch')) {
const trace: LangWatchTrace | undefined = this.handlers['langWatch'].trace[parentIds['langWatch'].trace]
if (trace) {
const span = trace.startSpan({
name,
type: 'tool',
input: autoconvertTypedValues(input)
})
this.handlers['langWatch'].span = { [span.spanId]: span }
returnIds['langWatch'].span = span.spanId
}
}
return returnIds
}
@@ -774,6 +885,15 @@ export class AnalyticHandler {
})
}
}
if (Object.prototype.hasOwnProperty.call(this.handlers, 'langWatch')) {
const span: LangWatchSpan | undefined = this.handlers['langWatch'].span[returnIds['langWatch'].span]
if (span) {
span.end({
output: autoconvertTypedValues(output)
})
}
}
}
async onToolError(returnIds: ICommonObject, error: string | object) {
@@ -809,5 +929,14 @@ export class AnalyticHandler {
})
}
}
if (Object.prototype.hasOwnProperty.call(this.handlers, 'langWatch')) {
const span: LangWatchSpan | undefined = this.handlers['langWatch'].span[returnIds['langWatch'].span]
if (span) {
span.end({
error
})
}
}
}
}