File size: 4,357 Bytes
bd6ff35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import gradio as gr
import tiktoken
from decimal import getcontext

getcontext().prec = 2

def num_tokens_from_messages(messages, model):
    """Return the number of tokens used by a list of messages."""
    encoding = tiktoken.encoding_for_model(model)
    if model in {
        "gpt-3.5-turbo-1106",
        "gpt-4-1106-preview",
        "gpt-4",
        }:
        tokens_per_message = 3
        tokens_per_name = 1
    else:
        raise NotImplementedError(
            f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens."""
        )
    num_tokens = 0
    for message in messages:
        num_tokens += tokens_per_message
        for key, value in message.items():
            num_tokens += len(encoding.encode(value))
            if key == "name":
                num_tokens += tokens_per_name
    num_tokens += 3  # every reply is primed with <|start|>assistant<|message|>
    return num_tokens

# https://openai.com/pricing
def calc_input_cost(tokens, model):
    """Return the input cost of tokens"""
    cost = 0
    if model == "gpt-3.5-turbo-1106":
      # input $0.001 / 1K tokens, output $0.002 / 1K tokens
      cost = 0.001 * tokens / 1000
    elif model == "gpt-4-1106-preview":
      # input $0.01 / 1K tokens, output $0.03 / 1K tokens
      cost = 0.01 * tokens / 1000
    elif model == "gpt-4":
      # input $0.03 / 1K tokens, output $0.06 / 1K tokens
      cost = 0.03 * tokens / 1000
    else:
      raise NotImplementedError(
            f"""calc_input_cost() is not implemented for model {model}."""
        )
    return float(cost)

# https://openai.com/pricing
def calc_output_cost(tokens, model="gpt-3.5-turbo-1106"):
    """Return the output cost of tokens"""
    cost = 0
    if model == "gpt-3.5-turbo-1106":
      # input $0.001 / 1K tokens, output $0.002 / 1K tokens
      cost = 0.002 * tokens / 1000
    elif model == "gpt-4-1106-preview":
      # input $0.03 / 1K tokens, output $0.03 / 1K tokens
      cost = 0.03 * tokens / 1000
    elif model == "gpt-4":
      # input $0.03 / 1K tokens, output $0.06 / 1K tokens
      cost = 0.06 * tokens / 1000
    else:
      raise NotImplementedError(
            f"""calc_input_cost() is not implemented for model {model}."""
        )
    return float(cost)

def make_input_messages(system, user):
    return [
        {
            "role": "system",
            "content": system,
        },
        {
            "role": "user",
            "content": user,
        }
    ]

def make_output_messages(output):
    return [
        {
            "role": "assistant",
            "content": output,
        }
    ]

def calc(request_count, system_prompt, user_prompt, output):
    result = ""
    input_messages = make_input_messages(system_prompt, user_prompt)
    output_messages = make_output_messages(output)
    for model in [
        "gpt-3.5-turbo-1106",
        "gpt-4-1106-preview",
        "gpt-4"
        ]:

        # example token count from the function defined above
        input_token = num_tokens_from_messages(input_messages, model)
        input_cost = calc_input_cost(input_token, model)
        output_token = num_tokens_from_messages(output_messages, model)
        output_cost = calc_output_cost(output_token, model)
        total_cost = input_cost + output_cost

        result += f"""{model}
        input token:{input_token}, output token: {output_token} tokens counted by tiktoken.
        input cost: ${format(input_cost, 'f')}, output cost: ${format(output_cost, 'f')}, total cost: ${format(total_cost, 'f')} per 1 request
        {int(request_count)} request cost is ${format(total_cost * request_count, 'f')}\n"""
    return result

iface = gr.Interface(
    fn=calc,
    inputs=[
        gr.Number(label="Request Count", value=500),
        gr.Textbox(label="System Prompt", value="System Prompt"),
        gr.Textbox(label="User Prompt", value="User Prompt"),
        gr.Textbox(label="Output", value="Output Text"),
    ],
    outputs=[ 
        gr.Textbox(label="Result")
    ],
    title="ChatGPT Token and Cost Calculator",
    description=f"Enter each form value, then click the button to see the results.\nThe results of this calculation should be considered for reference only."
)

iface.launch()