azure - 当调用部署在 Azure 实例中的同一个 OpenAI GPT 模型时,如何单独跟踪每个程序的费用?

标签 azure azure-cognitive-services cost-management azure-openai

我在属于我的 Azure 订阅中的资源的实例中部署了 OpenAI GPT 模型。我有两个使用此 OpenAI GPT 模型的程序。如何分别跟踪每个项目的费用?

<小时/>

示例:我将 OpenAI GPT 模型“GPT 4 32k”部署为 gpt-4-32k-viet。程序A和程序B使用这个模型。我如何分别追踪项目A和项目B产生的费用?

enter image description here

我使用 Azure OpenAI tutorial 中的代码:

import tiktoken
import openai
import os
openai.api_type = "azure"
openai.api_version = "2023-03-15-preview"
openai.api_base = "https://[resourcename].openai.azure.com/" # Your Azure OpenAI resource's endpoint value .
openai.api_key = "[my instance key]"


system_message = {"role": "system", "content": "You are a helpful assistant."}
max_response_tokens = 250
token_limit= 4096
conversation=[]
conversation.append(system_message)


def num_tokens_from_messages(messages, model="gpt-4-32k"):
    encoding = tiktoken.encoding_for_model(model)
    num_tokens = 0
    for message in messages:
        num_tokens += 4  # every message follows <im_start>{role/name}\n{content}<im_end>\n
        for key, value in message.items():
            num_tokens += len(encoding.encode(value))
            if key == "name":  # if there's a name, the role is omitted
                num_tokens += -1  # role is always required and always 1 token
    num_tokens += 2  # every reply is primed with <im_start>assistant
    return num_tokens


user_input = 'Hi there. What is the difference between Facebook and TikTok?'
conversation.append({"role": "user", "content": user_input})
conv_history_tokens = num_tokens_from_messages(conversation)

while (conv_history_tokens + max_response_tokens >= token_limit):
    del conversation[1]
    conv_history_tokens = num_tokens_from_messages(conversation)

response = openai.ChatCompletion.create(
    engine="gpt-4-32k-viet",  # The deployment name you chose when you deployed the ChatGPT or GPT-4 model.
    messages=conversation,
    temperature=.7,
    max_tokens=max_response_tokens,
)

conversation.append({"role": "assistant", "content": response['choices'][0]['message']['content']})
print("\n" + response['choices'][0]['message']['content'] + "\n")

最佳答案

您必须将它们包含在不同的资源组中。

如果您想获得更精细的 View ,则可以按服务名称定位该资源组和组:

enter image description here

我们很难理解 GPT 的真正成本,我建议您的唯一方法就是不要疯狂测试。

如果您向该语言模型提供 500 个字符,则会产生成本。

但是,如果您为其提供 5,000 个字符,则不要期望其成本为 10 倍。

很难预测,因此我建议您按资源组进行容器化。该技术并非设计为 Multi-Tenancy 的,您将损失成本。如果您想知道客户消费了多少,唯一的方法就是选择单租户。

否则,您必须为每个客户创建一个 ID,并将每个 token 链接到该 ID。祝你好运。

关于azure - 当调用部署在 Azure 实例中的同一个 OpenAI GPT 模型时,如何单独跟踪每个程序的费用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76186539/

相关文章:

azure - 如何从Azure表存储迁移到SQL Azure?

azure - 有没有办法使用 Powershell 获取 Azure 资源属于哪个 Log Analytics 工作区?

azure - 无法让 Microsoft Translator API 字典查找工作

android - 图像尺寸太小 Azure Face API Android

azure - Power BI Desktop 无法使用 Azure 成本管理连接器导入数据

azure - 是否可以在 Azure DevOps 中对每个项目进行成本分析?

sql-server - Azure SQL 数据库审计/管理

azure - 对 Azure 存储的只写访问(不可读取、不可列表、不可删除)

c# - Azure 应用程序配置重新加载失败时如何记录错误

c# - 文档提取认知技能的使用