原文:
towardsdatascience.com/introducing-the-new-anthropic-token-counting-api-5afd58bad5ff
Anthropic 在过去几天发布了几个令人兴奋的测试版功能,这些功能在很大程度上被忽视了。其中之一是使用他们的模型处理 PDF 的能力,现在可以理解 PDF 文档中的文本和视觉内容。我可能会在以后写一些关于这个话题的内容。
另一个令人兴奋的测试版功能,也是本文的主题,是标记计数的引入。关键的是,您可以在将消息发送给 Claude 之前,对用户消息、PDF 和图片中的标记进行计数。这对那些喜欢密切关注其标记使用成本的人来说是个好消息。
根据 Anthropic 的官方公告(链接这里),
“标记计数端点接受创建消息时相同的结构化输入列表,包括对系统提示、工具、图像和 PDF 的支持。响应包含输入标记的总数。”
它支持以下模型,
“Claude 3.5 Sonnet Claude 3.5 Haiku Claude 3 Haiku Claude 3 Opus”
好消息是,标记计数是免费使用的,但根据您的使用层级,受到每分钟请求数量的限制。
在本文的剩余部分,我们将通过一些示例来展示如何使用标记计数 API 来计算用户/系统消息、PDF 和图片中的标记数。
为了使操作更加互动,一旦我们开发出代码的基础,我们将使用 Gradio 应用封装功能,该应用将展示一个友好的用户界面,用于输入用户文本或上传 PDF 和图片,然后计算标记数。它看起来可能像这样,
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/ce8084e01864fc5276000d64d7291afd.png
作者图片
好的,让我们开始吧。首先,我正在使用 Windows WSL2 Ubuntu 进行开发。如果您是 Windows 用户,我有一份关于安装 WSL2 的全面指南,您可以在这里找到。
设置开发环境
在我们开始编码之前,让我们设置一个独立的发展环境。这样,我们的所有项目都将被隔离,不会相互干扰。我使用 conda 来做这件事,但请使用您熟悉的任何工具。
(base)$ conda create-n token_count python=3.10-y(base)$ conda activate token_count# Install required Libraries(token_count)pip install anthropic jupyter获取 Anthropic API 密钥
您可以从Anthropic 控制台获取。注册或登录后,您将看到如下界面,
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/bf103ad748a5b987748699df02b4de18.png
Anthropic 网站图片
点击**获取 API 密钥**按钮,然后按照说明操作。注意您的密钥,并将环境变量 ANTHROPIC_API_KEY 设置为该密钥。
代码
示例 1 – 在用户和系统提示中计数标记。
importanthropicimportos client=anthropic.Anthropic()response=client.beta.messages.count_tokens(betas=["token-counting-2024-11-01"],model="claude-3-5-sonnet-20241022",system=""" You are a helpful assistant and will respond to users's queries in a polite, friendly and knowledgable manner """,messages=[{"role":"user","content":"What is the capital city of France"}],)print(response.json())## Output#{"input_tokens":41}示例 2— 在 PDF 中计数标记
对于我的输入 PDF,我将使用特斯拉 2023 年 9 月季度提交给证券交易委员会的 Q10 副本。这份文件包含 51 页的混合文本和表格数据。您可以通过点击这里在线查看其外观。
importbase64importanthropic client=anthropic.Anthropic()withopen("/mnt/d/tesla/tesla_q10_sept_23.pdf","rb")aspdf_file:pdf_base64=base64.standard_b64encode(pdf_file.read()).decode("utf-8")response=client.beta.messages.count_tokens(betas=["token-counting-2024-11-01","pdfs-2024-09-25"],model="claude-3-5-sonnet-20241022",messages=[{"role":"user","content":[{"type":"document","source":{"type":"base64","media_type":"application/pdf","data":pdf_base64}},{"type":"text","text":"Please summarize this document."}]}])print(response.json())## Output#{"input_tokens":118967}示例 3 – 在图片中计数令牌
这是我将要使用的图片。
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/08216e2070837ae0745e005ec87be5fd.png
AI(Dalle-3)的图片
它是一个 PNG 文件,大小约为 2.6MB。
importanthropicimportbase64importhttpx image_url="/mnt/d/images/android.png"image_media_type="image/png"# Read the image file and encode it to base64withopen(image_path,"rb")asimage_file:image_data=base64.standard_b64encode(image_file.read()).decode("utf-8")client=anthropic.Anthropic()# Create the request using the locally stored imageresponse=client.beta.messages.count_tokens(betas=["token-counting-2024-11-01"],model="claude-3-5-sonnet-20241022",messages=[{"role":"user","content":[{"type":"image","source":{"type":"base64","media_type":image_media_type,"data":image_data,},},{"type":"text","text":"Describe this image"}],}],)print(response.json())## Output#{"input_tokens":1575}注意,在所有上述示例中,都没有向 LLM 设置任何请求来回答任何用户问题。这只是计数令牌。
将所有内容整合到一个 Gradio 应用程序中。
现在我们有了所有需要的代码,让我们使用 Gradio 为其设计一个用户界面。
我们需要两个输入文本框,一个用于可选的系统提示,一个用于可选的用户提示。
接下来,我们需要一个输入字段,用户可以在此选择要上传的 PDF 或图片文件。在此字段下方,将有一个**添加**按钮,允许用户添加上面选择的文件。所选文件或图片的名称将在消息框中显示。
最后,将有一个按钮调用代码来计算令牌成本,还有一个按钮清除所有输入和输出字段。
我们可以使用 LLM 来完成这部分工作。与 LLM 进行了一些来回的交流,但最终在 GPT4-o 的帮助下,我开发了这段代码。代码注释很多,因此应该相对容易理解。
# Import Gradio for building the web app interfaceimportgradioasgr# Import Anthrop client for token counting APIimportanthropic# Import base64 for encoding files in base64 formatimportbase64# Import os for interacting with the file system (though not used in this script)importos# Initialize the Anthropic client to access the API functions# need to have your ANTHROPIC_API_KEY environment variable setclient=anthropic.Anthropic()# Define a function to handle file uploads incrementally, allowing files to be added without overwriting previous uploadsdefadd_files(uploaded_files,current_files):# Initialize the current_files list if it's emptyifcurrent_filesisNone:current_files=[]# Append any newly uploaded files to the current list of filesifuploaded_files:current_files.extend(uploaded_files)# Create a list of file names for display purposesfile_names=[file.nameforfileincurrent_files]# Return the updated file list, the display names, and clear the uploaded_files inputreturncurrent_files,file_names,None# Define a function to count tokens in system and user prompts, as well as in uploaded filesdefcount_tokens(system_prompt,user_prompt,all_files):# Check if all inputs are empty or cleared; if so, return 0ifnotsystem_promptandnotuser_promptandnotall_files:return0# Initialize an empty list to store the message objects for the API requestmessages=[]# Add the user prompt to the messages list if it's providedifuser_prompt:messages.append({"role":"user","content":user_prompt})# Process each uploaded file, determining whether it's a PDF or an imageifall_files:forfileinall_files:# Get the file type by extracting and converting the file extension to lowercasefile_type=file.name.split(".")[-1].lower()# If the file is a PDF, encode it in base64 and prepare a document messageiffile_type=="pdf":withopen(file.name,"rb")asf:pdf_base64=base64.standard_b64encode(f.read()).decode("utf-8")pdf_content={"type":"document","source":{"type":"base64","media_type":"application/pdf","data":pdf_base64}}# Add the PDF message to the messages list with a prompt for summarizationmessages.append({"role":"user","content":[pdf_content,{"type":"text","text":"Please summarize this document."}]})# If the file is an image (JPEG or PNG), encode it in base64 and prepare an image messageeliffile_typein["jpg","jpeg","png"]:media_type=f"image/{file_type}"withopen(file.name,"rb")asf:image_base64=base64.standard_b64encode(f.read()).decode("utf-8")image_content={"type":"image","source":{"type":"base64","media_type":media_type,"data":image_base64,}}# Add the image message to the messages list with a prompt to describe itmessages.append({"role":"user","content":[image_content,{"type":"text","text":"Describe this image"}]})# If no prompts or files are provided, add a placeholder messageifnotmessages:messages.append({"role":"user","content":""})# Call the Anthrop API to count tokens, using system prompt and messages as inputresponse=client.beta.messages.count_tokens(betas=["token-counting-2024-11-01","pdfs-2024-09-25"],model="claude-3-5-sonnet-20241022",system=system_prompt,messages=messages,)# Return the total number of tokens countedreturnresponse.input_tokens# Define a function to clear all input fields in the Gradio appdefclear_inputs():return"","",[],"",""# Build the Gradio interfacewithgr.Blocks(theme="huggingface")asapp:# Display a title for the appgr.Markdown("<h1 style='text-align: center;'>Anthropic Token Counter</h1>")# Create input fields for system and user promptswithgr.Row():system_prompt=gr.Textbox(label="System Prompt",placeholder="Enter the system prompt here...",lines=3)user_prompt=gr.Textbox(label="User Prompt",placeholder="Enter the user prompt here...",lines=3)# Create an upload field for multiple PDF or image filesuploaded_files=gr.File(label="Upload PDF(s) or Image(s)",file_count="multiple",file_types=[".pdf",".jpg",".jpeg",".png"])# Create a state variable to hold the list of currently uploaded filescurrent_files=gr.State([])# Display a text box to show the names of uploaded filesfile_display=gr.Textbox(label="Uploaded Files",interactive=False)# Define buttons for adding files, counting tokens, and clearing inputsadd_files_button=gr.Button("Add Files")withgr.Row():count_button=gr.Button("Count Tokens",size="small")clear_button=gr.Button("Clear",size="small")# Display the token count result in a text boxresult=gr.Textbox(label="Token Count",interactive=False)# Configure the "Add Files" button to append files to the current file listadd_files_button.click(fn=add_files,inputs=[uploaded_files,current_files],outputs=[current_files,file_display,uploaded_files])# Configure the "Count Tokens" button to process the prompts and files, displaying the token countcount_button.click(fn=count_tokens,inputs=[system_prompt,user_prompt,current_files],outputs=result)# Configure the "Clear" button to reset all inputs and the token count displayclear_button.click(fn=clear_inputs,outputs=[system_prompt,user_prompt,current_files,file_display,result])# Launch the Gradio appapp.launch()要使用此应用程序,请按照以下步骤操作。
如果需要,请输入系统提示和/或用户提示。如果您想留空,可以这样做。
要上传一个或多个文件,请将文件拖放到文件上传框中,或点击它并选择文件。之后,点击添加按钮,您选择的文件应出现在已上传文件列表框中。
如果您想添加更多文件,请重复上述步骤。
点击计数令牌按钮以显示所有上传的文件和/或用户或系统提示中输入的文本的令牌计数。
点击清除按钮重置所有内容并从头开始
这里有一个示例运行,我上传了 2 个 PDF 文件和一个图片,以及一个用户提示。
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/ab0923f001a652381ec4ea19616af70d.png
作者的图片
摘要
在这篇文章中,我写了一个关于 Anthropic 宣布发布的新令牌计数 API 的公告。然后我继续使用该 API 开发代码,用于计算用户和系统提示、上传的图片以及 PDF 文档的令牌。
我展示了如何使用 Gradio 开发代码的用户界面,将我们开发的代码捆绑到应用程序中。
最后,我展示了应用程序的外观,并提供了其使用的一个工作示例。
_ 好的,现在就到这里吧。希望你觉得这篇文章有用。如果你觉得有用,请访问我的个人资料页面这个链接。从那里,你可以看到我其他发布的文章并订阅,以便在我发布新内容时收到通知。_
我知道现在经济困难,钱包紧张,但如果你觉得这篇文章真的有价值,请考虑买给我一小杯威士忌。
如果你喜欢这个内容,我认为你也会对以下这些文章感兴趣。
开发并部署一个 WEBP 到 PNG 图像转换器 Taipy App 到网络 – 第一部分
使用克劳德的新计算机使用模型进行 C 编程