Skip to content

Commit

Permalink
usage handler calc for azure openai
Browse files Browse the repository at this point in the history
  • Loading branch information
niztal committed Aug 4, 2024
1 parent acbf37b commit a919aa4
Show file tree
Hide file tree
Showing 6 changed files with 9,854 additions and 7,983 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ConversationalRetrievalToolAgent_Agents implements INode {
if (isStreamable) {
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
const customerId = nodeData.inputs?.vars?.customerId ?? '';

Check failure on line 131 in packages/components/nodes/agents/ConversationalRetrievalToolAgent/ConversationalRetrievalToolAgent.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.15.0)

Delete `;`
const usageHandler = new UsageHandler(customerId);
const usageHandler = new UsageHandler(customerId, nodeData?.inputs?.model?.modelName);

Check failure on line 132 in packages/components/nodes/agents/ConversationalRetrievalToolAgent/ConversationalRetrievalToolAgent.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.15.0)

Delete `;`
res = await executor.invoke({ input }, { callbacks: [loggerHandler, handler, usageHandler, ...callbacks] })
if (res.sourceDocuments) {
options.socketIO.to(options.socketIOClientId).emit('sourceDocuments', flatten(res.sourceDocuments))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class ChatOpenAI_ChatModels implements INode {
modelName,
openAIApiKey,
streaming: streaming ?? true,

Check failure on line 184 in packages/components/nodes/chatmodels/ChatOpenAI/ChatOpenAI.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.15.0)

Delete `,`
stream_options: { include_usage: true }
// stream_options: { include_usage: true }
}

if (maxTokens) obj.maxTokens = parseInt(maxTokens, 10)
Expand Down
2 changes: 1 addition & 1 deletion packages/components/nodes/vectorstores/Milvus/Milvus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ const similaritySearchVectorWithScore = async (query: number[], k: number, vecto
anns_field: vectorStore.vectorField,
topk: k.toString(),
metric_type: vectorStore.indexCreateParams.metric_type,
params: vectorStore.indexSearchParams
params: '' + vectorStore.indexSearchParams
},
output_fields: outputFields,
vector_type: DataType.FloatVector,
Expand Down
2 changes: 2 additions & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@google/generative-ai": "^0.15.0",
"@huggingface/inference": "^2.6.1",
"@langchain/anthropic": "^0.2.1",
"@langchain/aws": "^0.0.6",
"@langchain/cohere": "^0.0.7",
"@langchain/community": "^0.2.17",
"@langchain/exa": "^0.0.5",
Expand Down Expand Up @@ -80,6 +81,7 @@
"graphql": "^16.6.0",
"html-to-text": "^9.0.5",
"ioredis": "^5.3.2",
"js-tiktoken": "^1.0.12",
"jsdom": "^22.1.0",
"jsonpointer": "^5.0.1",
"langchain": "^0.2.11",
Expand Down
59 changes: 38 additions & 21 deletions packages/components/src/usageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";

Check failure on line 1 in packages/components/src/usageHandler.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.15.0)

Replace `"@langchain/core/callbacks/base";` with `'@langchain/core/callbacks/base'`
import { Run } from '@langchain/core/tracers/base';
import { LLMResult } from "@langchain/core/outputs";

Check failure on line 2 in packages/components/src/usageHandler.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.15.0)

Replace `"@langchain/core/outputs";` with `'@langchain/core/outputs'`
import { Serialized } from "@langchain/core/load/serializable";

Check failure on line 3 in packages/components/src/usageHandler.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.15.0)

Replace `"@langchain/core/load/serializable";` with `'@langchain/core/load/serializable'`
import { encodingForModel, Tiktoken } from "js-tiktoken";

Check failure on line 4 in packages/components/src/usageHandler.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.15.0)

Replace `"js-tiktoken";` with `'js-tiktoken'`
import axios from "axios";

Check failure on line 5 in packages/components/src/usageHandler.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.15.0)

Replace `"axios";` with `'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 {

Check failure on line 8 in packages/components/src/usageHandler.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.15.0)

Replace `⏎····name·=·"usage_handler"` with `····name·=·'usage_handler'`
name = "usage_handler" as const
customerId: string;

Check failure on line 10 in packages/components/src/usageHandler.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.15.0)

Delete `;`
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);
}
}
}
Loading

0 comments on commit a919aa4

Please sign in to comment.