Replies: 1 comment
-
🤖 In LangChainJS, you can access information such as the token remaining and model name from the http response headers of the OpenAI http response using the Here is an example of how you can access the token usage: const data = await this.completionWithRetry(
{
...params,
stream: false,
messages: messagesMapped,
},
{
signal: options?.signal,
...options?.options,
}
);
const {
completion_tokens: completionTokens,
prompt_tokens: promptTokens,
total_tokens: totalTokens,
} = data?.usage ?? {};
if (completionTokens) {
tokenUsage.completionTokens =
(tokenUsage.completionTokens ?? 0) + completionTokens;
}
if (promptTokens) {
tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;
}
if (totalTokens) {
tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;
} In the above code, As for the model name, it is stored in the console.log(this.modelName); // prints the model name If you want to use a callback to handle these values, you would need to pass a callback function to the method where these values are being calculated and call that callback with these values as arguments. Please note that I wasn't able to find a built-in method or function within the LangChainJS framework to directly access HTTP response headers. If you need further assistance or if there's anything else you'd like to know, feel free to ask. SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
There is information like token remaining and modal name, etc. from the http response headers of the OpenAI http response. how to access that information in langchian? especially through callback?
Beta Was this translation helpful? Give feedback.
All reactions