Skip to content

Commit

Permalink
Merge pull request #1345 from FlowiseAI/feature/ManualAnalytics
Browse files Browse the repository at this point in the history
Feature/Add custom analytics
  • Loading branch information
HenryHengZJ authored Dec 8, 2023
2 parents aaf4aee + da2fe78 commit 2373ed0
Show file tree
Hide file tree
Showing 4 changed files with 522 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as path from 'node:path'
import fetch from 'node-fetch'
import { flatten, uniqWith, isEqual } from 'lodash'
import { zodToJsonSchema } from 'zod-to-json-schema'
import { AnalyticHandler } from '../../../src/handler'

class OpenAIAssistant_Agents implements INode {
label: string
Expand Down Expand Up @@ -149,6 +150,11 @@ class OpenAIAssistant_Agents implements INode {

const openai = new OpenAI({ apiKey: openAIApiKey })

// Start analytics
const analyticHandlers = new AnalyticHandler(nodeData, options)
await analyticHandlers.init()
const parentIds = await analyticHandlers.onChainStart('OpenAIAssistant', input)

try {
const assistantDetails = JSON.parse(assistant.details)
const openAIAssistantId = assistantDetails.id
Expand All @@ -171,7 +177,8 @@ class OpenAIAssistant_Agents implements INode {
}

const chatmessage = await appDataSource.getRepository(databaseEntities['ChatMessage']).findOneBy({
chatId: options.chatId
chatId: options.chatId,
chatflowid: options.chatflowid
})

let threadId = ''
Expand All @@ -185,7 +192,7 @@ class OpenAIAssistant_Agents implements INode {
threadId = thread.id
}

// List all runs
// List all runs, in case existing thread is still running
if (!isNewThread) {
const promise = (threadId: string) => {
return new Promise<void>((resolve) => {
Expand Down Expand Up @@ -221,6 +228,7 @@ class OpenAIAssistant_Agents implements INode {
})

// Run assistant thread
const llmIds = await analyticHandlers.onLLMStart('ChatOpenAI', input, parentIds)
const runThread = await openai.beta.threads.runs.create(threadId, {
assistant_id: retrievedAssistant.id
})
Expand Down Expand Up @@ -253,7 +261,15 @@ class OpenAIAssistant_Agents implements INode {
for (let i = 0; i < actions.length; i += 1) {
const tool = tools.find((tool: any) => tool.name === actions[i].tool)
if (!tool) continue

// Start tool analytics
const toolIds = await analyticHandlers.onToolStart(tool.name, actions[i].toolInput, parentIds)

const toolOutput = await tool.call(actions[i].toolInput)

// End tool analytics
await analyticHandlers.onToolEnd(toolIds, toolOutput)

submitToolOutputs.push({
tool_call_id: actions[i].toolCallId,
output: toolOutput
Expand Down Expand Up @@ -302,7 +318,9 @@ class OpenAIAssistant_Agents implements INode {
runThreadId = newRunThread.id
state = await promise(threadId, newRunThread.id)
} else {
throw new Error(`Error processing thread: ${state}, Thread ID: ${threadId}`)
const errMsg = `Error processing thread: ${state}, Thread ID: ${threadId}`
await analyticHandlers.onChainError(parentIds, errMsg)
throw new Error(errMsg)
}
}

Expand Down Expand Up @@ -387,18 +405,26 @@ class OpenAIAssistant_Agents implements INode {
const bitmap = fsDefault.readFileSync(filePath)
const base64String = Buffer.from(bitmap).toString('base64')

// TODO: Use a file path and retrieve image on the fly. Storing as base64 to localStorage and database will easily hit limits
const imgHTML = `<img src="data:image/png;base64,${base64String}" width="100%" height="max-content" alt="${fileObj.filename}" /><br/>`
returnVal += imgHTML
}
}

const imageRegex = /<img[^>]*\/>/g
let llmOutput = returnVal.replace(imageRegex, '')
llmOutput = llmOutput.replace('<br/>', '')
await analyticHandlers.onLLMEnd(llmIds, llmOutput)
await analyticHandlers.onChainEnd(parentIds, messageData, true)

return {
text: returnVal,
usedTools,
fileAnnotations,
assistant: { assistantId: openAIAssistantId, threadId, runId: runThreadId, messages: messageData }
}
} catch (error) {
await analyticHandlers.onChainError(parentIds, error, true)
throw new Error(error)
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
"husky": "^8.0.3",
"ioredis": "^5.3.2",
"langchain": "^0.0.196",
"langfuse": "^1.2.0",
"langfuse-langchain": "^1.0.31",
"langsmith": "^0.0.32",
"langsmith": "^0.0.49",
"linkifyjs": "^4.1.1",
"llmonitor": "^0.5.5",
"mammoth": "^1.5.1",
Expand Down
Loading

0 comments on commit 2373ed0

Please sign in to comment.