forked from FlowiseAI/Flowise
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
9,854 additions
and
7,983 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,56 @@ | ||
import { BaseCallbackHandler } from "@langchain/core/callbacks/base"; | ||
import { Run } from '@langchain/core/tracers/base'; | ||
import { LLMResult } from "@langchain/core/outputs"; | ||
import { Serialized } from "@langchain/core/load/serializable"; | ||
import { encodingForModel, Tiktoken } from "js-tiktoken"; | ||
import axios from "axios"; | ||
import { BaseMessage } from "@langchain/core/messages"; | ||
import { LLMResult, Generation } from "@langchain/core/outputs"; | ||
import { AIMessageChunk } from "@langchain/core/messages"; | ||
type Message = BaseMessage | Generation | string; | ||
|
||
|
||
export class UsageHandler extends BaseCallbackHandler { | ||
|
||
name = "usage_handler" as const | ||
customerId: string; | ||
encoding: Tiktoken; | ||
|
||
constructor(customerId: string) { | ||
constructor(customerId: string, model: string) { | ||
super(); | ||
this.customerId = customerId; | ||
//@ts-ignore | ||
this.encoding = encodingForModel(model); | ||
} | ||
|
||
handleLLMStart(_llm: Serialized, prompts: string[]) { | ||
try { | ||
const prompt = prompts[0]; | ||
const tokens = this.encoding?.encode(prompt); | ||
if (tokens) { | ||
const url = `${process.env.USAGE_URL}/usage`; | ||
axios.post(url, { | ||
customerId: this.customerId, | ||
usage: { | ||
input_tokens: tokens.length | ||
} | ||
}); | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
|
||
handleLLMEnd(output: LLMResult, runId: string) { | ||
handleLLMEnd(output: LLMResult) { | ||
try { | ||
//@ts-ignore | ||
const message: AIMessageChunk = output?.generations[0][0].message; | ||
const { usage_metadata } = message?.lc_kwargs || {}; | ||
if (usage_metadata && this.customerId) { | ||
const prompt = output?.generations[0][0].text; | ||
const tokens = this.encoding?.encode(prompt); | ||
|
||
if (tokens) { | ||
const url = `${process.env.USAGE_URL}/usage`; | ||
console.log('calling usage: ', url); | ||
if (url) { | ||
axios.post(url, { | ||
customerId: this.customerId, | ||
usage: usage_metadata | ||
}); | ||
} | ||
axios.post(url, { | ||
customerId: this.customerId, | ||
usage: { | ||
output_tokens: tokens.length | ||
} | ||
}); | ||
} | ||
} catch (err) { | ||
console.log("usage handler error:" + err); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.