Skip to content

Commit

Permalink
Move counter outside of onFinish()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rindrics committed Nov 1, 2024
1 parent 349eb8a commit e4d79a5
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions app/(playground)/p/[agentId]/beta-proto/artifact/server-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ${sourcesToText(sources)}
input: params.userPrompt,
model,
});
const { partialObjectStream, object } = await streamObject({
const { partialObjectStream, object, usage } = await streamObject({
model: openai(model),
system,
prompt: params.userPrompt,
Expand All @@ -62,17 +62,6 @@ ${sourcesToText(sources)}
isEnabled: true,
},
onFinish: async (result) => {
console.log("onFinish() envirenment: ", process.env.NEXT_RUNTIME);
const meter = metrics.getMeter("OpenAI");
const tokenCounter = meter.createCounter("token_consumed", {
description: "Number of OpenAI API tokens consumed by each request",
});
const subscriptionId = await getUserSubscriptionId();
const isR06User = await isRoute06User();
tokenCounter.add(result.usage.totalTokens, {
subscriptionId,
isR06User,
});
generation.end({
output: result,
});
Expand All @@ -87,6 +76,18 @@ ${sourcesToText(sources)}

const result = await object;

console.log("await object envirenment: ", process.env.NEXT_RUNTIME);
const meter = metrics.getMeter("OpenAI");
const tokenCounter = meter.createCounter("token_consumed", {
description: "Number of OpenAI API tokens consumed by each request",
});
const subscriptionId = await getUserSubscriptionId();
const isR06User = await isRoute06User();
tokenCounter.add(await usage.totalTokens, {
subscriptionId,
isR06User,
});

stream.done();
})();

Expand Down

0 comments on commit e4d79a5

Please sign in to comment.