GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||
GeezerVison is showing doc.agentscope.io — Token - AgentScope Note Go to the end to download the full example code. Token¶AgentScope provides a token counter module under Specifically, the following token counters are available:
Tip The formatter module has integrated the token counters to support prompt truncation. Refer to the Prompt Formatter section for more details. Note For DashScope models, the dashscope library doesn’t provide a token counting API. So we recommend using the HuggingFace token counter instead. We show an example of using the OpenAI token counter to count the number of tokens: import asyncio from agentscope.token import OpenAITokenCounter async def example_token_counting(): # Example messages messages = [ {"role": "user", "content": "Hello!"}, {"role": "assistant", "content": "Hi, how can I help you?"}, ] # OpenAI token counting openai_counter = OpenAITokenCounter(model_name="gpt-4.1") n_tokens = await openai_counter.count(messages) print(f"Number of tokens: {n_tokens}") asyncio.run(example_token_counting()) Number of tokens: 21 Further Reading¶Total running time of the script: (0 minutes 1.579 seconds) | ||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |