-
Context / ScenarioI'm using Kernel memory as a plugin with Semantic Kernel. QuestionI would like to know how can I get the count of the used tokens by the kernel memory plugin? |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments
-
The number of tokens is logged in the application logs. If you need to count upfront, you can use the provided tokenizers, e.g. we just added tiktoken counters that covers GPT2/GPT3/GPT4 models, and the repo contains also a LLama tokenizer for llama compatible models. |
Beta Was this translation helpful? Give feedback.
-
@dluc where can I find an example? |
Beta Was this translation helpful? Give feedback.
-
@rborosak for which model and/or tokenizer? |
Beta Was this translation helpful? Give feedback.
-
@dluc For my implementation, I need to track the used tokens by the LLM. It's a web application, I need to record user info and tokens used to bill them after a period. Reviewing the code the only approach is to use the Logger which counts the tokens with the Tokenizer, could you implement a Meter and trigger a metric or invoking a Event maybe? |
Beta Was this translation helpful? Give feedback.
-
I'll see if there's an easy approach, not too expensive to develop. Can't promise though. We always welcome PRs, or draft PRs to kickstart the process if that might help. |
Beta Was this translation helpful? Give feedback.
-
SemanticKernel has the concepts of FunctionFilters, where you can do things like that, so I guess KM should follow the same approach... which actually makes me wonder in KernelMemory should drop the AskAsync functionality, being "just" a Memory plugin (SearchAsync) for SK + all the Indexing capabilities... (don't kill me, just thinking out loud 😄 ) |
Beta Was this translation helpful? Give feedback.
-
it's actually the other way around. SK memoy plugins need DB connectors to talk to storage. AskAsync is built on those connectors. KM connectors are an evolution of SK connectors, out of necessity of
KM paved the way on the research side, so that these features can land also in SK ;-) Anyway, if you're looking for a plugin here's KM plugin for SK: https://github.com/microsoft/kernel-memory/tree/main/clients/dotnet/SemanticKernelPlugin |
Beta Was this translation helpful? Give feedback.
-
yeah, don't get me wrong, I love KM and we're using it in PROD (would be interesting for a case-study??). That said, the AskAsync method offers less possibilities than if the call to the model is done by SK: calling other plugins, handlebars templates, streaming... |
Beta Was this translation helpful? Give feedback.
-
(feedback appreciated, in both ways, no worries :-)) I wouldn't compare KM with an agent or a planner, or similar features based on function calling, if that makes sense. For instance, when using "function" calling, there are multiple functions that the LLM can choose from, and "Ask" is one of those functions. Looking at SK memory classes, you can see there's a "Search" function, which can be used to put relevant information into a planner/agent context. Then one needs to create another function to leverage the context to answer questions or execute some actions, which IMO are out of "memory" scope. Looking at KM, we could (would like to) extend the Ask method to do intent detection and decide how to process the user question, e.g. whether to search for relevant records or to process an entire document without the need for "search". However, in terms of "Memory" the scope of actions should be limited to retrieving information -- at least that's the idea of the primary API, and one can leverage the underlying orchestration to do more :-) |
Beta Was this translation helpful? Give feedback.
-
Please feel free to use #532 to vote for this feature |
Beta Was this translation helpful? Give feedback.
-
Update: starting from version 0.96, KM Ask API returns a Token Usage report. Hope this helps! |
Beta Was this translation helpful? Give feedback.
Update: starting from version 0.96, KM Ask API returns a Token Usage report. Hope this helps!