jadehardouin commited on
Commit
37bd313
1 Parent(s): 468deee

Update How_to_contribute.md

Browse files
Files changed (1) hide show
  1. How_to_contribute.md +107 -20
How_to_contribute.md CHANGED
@@ -1,8 +1,10 @@
1
  # Contribute to our TCO Calculator
2
 
3
- ## What can you contribute with
4
 
5
- The TCO Calculator’s purpose is to assist users in comparing the deployment [Total Cost of Ownership](https://www.techtarget.com/searchdatacenter/definition/TCO?Offer=abt_pubpro_AI-Insider) (TCO) of various AI model services. To do so, it computes the cost/request of said service and adds a labor cost to get a comprehensive estimate of how much the set-up of these services would cost.
 
 
6
 
7
  Here is the formula used to compute the cost/request of an AI model service:
8
 
@@ -14,6 +16,15 @@ with:
14
  - IT = Input Tokens
15
  - OT = Output Tokens
16
 
 
 
 
 
 
 
 
 
 
17
  To contribute, you’ll have to provide the value of the input and output cost/token.
18
 
19
  If you want to add your own service to this [Gradio](https://www.gradio.app/) application, you’ll have to follow two main steps:
@@ -22,7 +33,7 @@ If you want to add your own service to this [Gradio](https://www.gradio.app/) ap
22
 
23
  ## Create a class for your model
24
 
25
- Step by step, we’ll see how you can create a model class for your own service that can later be an option of our TCO Calculator.
26
 
27
  First, you need to create a class for your model and set basic information such as the name of your service and the latency of your model.
28
 
@@ -42,17 +53,18 @@ It can be a Dropdown with multiple choices for the user to make or a Textbox wit
42
 
43
  ```python
44
  def render(self):
45
- #Create as many Gradio components as you want to provide information or customization to the user
46
- #Put all their visibility to False
47
- #Don't forget to put the interactive parameter of the component to False if the value is fixed
48
  self.model_parameter = gr.Dropdown(["Option 1", "Option 2"], value="Option 1", visible=False, interactive=True, label="Title for this parameter", info="Add some information to clarify specific aspects of your parameter")
49
  ```
50
  Then, still in the `render` function, you must instantiate your input and output cost/token. They are the key values needed to compute the cost/request of your AI model service.
51
  Note that the user can’t interact with these since they are the values you’ll have to provide from benchmark tests on your model.
 
52
  ```python
53
- #Put the values of the input and output cost per token
54
- #These values can be updated using a function above that is triggered by a change in the parameters
55
- #Put default values accordingly to the default parameters
56
 
57
  self.input_cost_per_token = gr.Number(0.1, visible=False, label="($) Price/1K input prompt tokens", interactive=False)
58
 
@@ -61,28 +73,35 @@ self.output_cost_per_token = gr.Number(0.2, visible=False, label="($) Price/1K o
61
  Then, if the user can modify some parameters using the Gradio components mentioned above, you’ll have to update the values influenced by this.
62
  This is why you need to create an update function that has the changing parameter(s) for input(s) and outputs the correct value.
63
  In the test example, the parameter only influences the cost/token but it could be another parameter whose choices depend on the value of the former one.
64
- ```python
65
 
 
66
  def on_model_parameter_change(model_parameter):
67
- if model_parameter == "Option 1":
68
- input_tokens_cost_per_token = 0.1
69
- output_tokens_cost_per_token = 0.2
70
- else:
71
- input_tokens_cost_per_token = 0.2
72
- output_tokens_cost_per_token = 0.4
73
- return input_tokens_cost_per_token, output_tokens_cost_per_token
74
  ```
75
 
76
  Don’t forget to add the triggering event that calls the update function when a Gradio parameter is changed.
77
  Note that the inputs and outputs can vary depending on the update function.
 
78
  ```python
79
  self.model_parameter.change(on_model_parameter_change, inputs=self.model_parameter, outputs=[self.input_cost_per_token, self.output_cost_per_token])
80
  ```
81
- The last element of the render function you have to implement is the labor cost parameter. It provides an estimation of how much it would cost to have engineers deploy the model. Note that for a SaaS solution, this cost is 0 when considering the set-up of the service.
 
 
82
  ```python
83
  self.labor = gr.Number(0, visible=False, label="($) Labor cost per month", info="This is an estimate of the labor cost of the AI engineer in charge of deploying the model", interactive=True)
84
  ```
85
- Eventually, you need to create another function that will be necessary to compute the cost/request of your AI model service. You must have the same inputs and outputs as below and this function can be used to convert values.
 
 
 
 
86
  ```python
87
  def compute_cost_per_token(self, input_cost_per_token, output_cost_per_token, labor):
88
  #Additional computation on your cost_per_token values
@@ -91,10 +110,78 @@ return input_cost_per_token, output_cost_per_token, labor
91
  ```
92
 
93
  Once your model class is ready, you’ll have to **add it to the `models.py` file** in our TCO Calculator’s [Hugging Face repository](https://huggingface.co/spaces/mithril-security/TCO_calculator/tree/main).
 
94
  ## Update the app.py file
95
  For the user to be able to select your AI model service option in the Calculator, you still have one step to go.
96
 
97
  In the following code line of the `app.py` file (line 93), you’ll have to **add the name of your model class** as follows:
98
  ```python
99
  Models: list[models.BaseTCOModel] = [models.OpenAIModelGPT4,..., models.NewModel]
100
- ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Contribute to our TCO Calculator
2
 
3
+ ## Presentation
4
 
5
+ The TCO Calculator’s purpose is to assist users in comparing the deployment [Total Cost of Ownership](https://www.techtarget.com/searchdatacenter/definition/TCO?Offer=abt_pubpro_AI-Insider) (TCO) of different AI consumption, from deploying an open-source model like Llama 2 on one's internal infrastructure, to consuming a SaaS AI like OpenAI.
6
+
7
+ To do so, it computes the cost/request of said service and adds a labor cost to get a comprehensive estimate of how much the set-up of these services would cost.
8
 
9
  Here is the formula used to compute the cost/request of an AI model service:
10
 
 
16
  - IT = Input Tokens
17
  - OT = Output Tokens
18
 
19
+ For instance, imagine we want to evaluate the cost of a summarization request. We will assume here that the number of input tokens is `500`, and that the output is `200` tokens.
20
+
21
+ If we use OpenAI GPT3.5 pricing, prices are `$0.0015` per 1k input tokens, and `$0.002` per 1k output tokens.
22
+
23
+ Therefore the formula gives us: Cost per request = (500 * 0.0015 + 0.002 * 200) / 1000 = $0.00115, aka `$1.15` per `1000` requests.
24
+
25
+
26
+ ## Contributing
27
+
28
  To contribute, you’ll have to provide the value of the input and output cost/token.
29
 
30
  If you want to add your own service to this [Gradio](https://www.gradio.app/) application, you’ll have to follow two main steps:
 
33
 
34
  ## Create a class for your model
35
 
36
+ Step by step, we’ll see how you can create a model class for your own service that can later be an option of our TCO Calculator. We’ll use a basic frame that you can find [here](https://huggingface.co/spaces/mithril-security/TCO_calculator/blob/main/contribution_example.py) in our calculator’s repository.
37
 
38
  First, you need to create a class for your model and set basic information such as the name of your service and the latency of your model.
39
 
 
53
 
54
  ```python
55
  def render(self):
56
+ # Create as many Gradio components as you want to provide information or customization to the user
57
+ # Put all their visibility to False at the beginning
58
+ # Don't forget to put the interactive parameter of the component to False if the value is fixed
59
  self.model_parameter = gr.Dropdown(["Option 1", "Option 2"], value="Option 1", visible=False, interactive=True, label="Title for this parameter", info="Add some information to clarify specific aspects of your parameter")
60
  ```
61
  Then, still in the `render` function, you must instantiate your input and output cost/token. They are the key values needed to compute the cost/request of your AI model service.
62
  Note that the user can’t interact with these since they are the values you’ll have to provide from benchmark tests on your model.
63
+
64
  ```python
65
+ # Put the values of the input and output cost per token
66
+ # These values can be updated using a function above that is triggered by a change in the parameters
67
+ # Put default values accordingly to the default parameters
68
 
69
  self.input_cost_per_token = gr.Number(0.1, visible=False, label="($) Price/1K input prompt tokens", interactive=False)
70
 
 
73
  Then, if the user can modify some parameters using the Gradio components mentioned above, you’ll have to update the values influenced by this.
74
  This is why you need to create an update function that has the changing parameter(s) for input(s) and outputs the correct value.
75
  In the test example, the parameter only influences the cost/token but it could be another parameter whose choices depend on the value of the former one.
 
76
 
77
+ ```python
78
  def on_model_parameter_change(model_parameter):
79
+ if model_parameter == "Option 1":
80
+ input_tokens_cost_per_token = 0.1
81
+ output_tokens_cost_per_token = 0.2
82
+ else:
83
+ input_tokens_cost_per_token = 0.2
84
+ output_tokens_cost_per_token = 0.4
85
+ return input_tokens_cost_per_token, output_tokens_cost_per_token
86
  ```
87
 
88
  Don’t forget to add the triggering event that calls the update function when a Gradio parameter is changed.
89
  Note that the inputs and outputs can vary depending on the update function.
90
+
91
  ```python
92
  self.model_parameter.change(on_model_parameter_change, inputs=self.model_parameter, outputs=[self.input_cost_per_token, self.output_cost_per_token])
93
  ```
94
+
95
+ The last element of the render function you have to implement is the labor cost parameter. It provides an estimation of how much it would cost to have engineers deploy the model. Note that for a SaaS solution, we consider this cost is be 0 as it requires no engineer to deploy and maintain the model.
96
+
97
  ```python
98
  self.labor = gr.Number(0, visible=False, label="($) Labor cost per month", info="This is an estimate of the labor cost of the AI engineer in charge of deploying the model", interactive=True)
99
  ```
100
+
101
+ Lastly, it's important to create a `compute_cost_per_token` function that's essential for computing the cost/request of your AI model service. This function should have the same input and output parameters as in the example below for the whole calculator to work.
102
+
103
+ Additionally, if any conversion is required on the input or output cost/token, you can perform them in this function.
104
+
105
  ```python
106
  def compute_cost_per_token(self, input_cost_per_token, output_cost_per_token, labor):
107
  #Additional computation on your cost_per_token values
 
110
  ```
111
 
112
  Once your model class is ready, you’ll have to **add it to the `models.py` file** in our TCO Calculator’s [Hugging Face repository](https://huggingface.co/spaces/mithril-security/TCO_calculator/tree/main).
113
+
114
  ## Update the app.py file
115
  For the user to be able to select your AI model service option in the Calculator, you still have one step to go.
116
 
117
  In the following code line of the `app.py` file (line 93), you’ll have to **add the name of your model class** as follows:
118
  ```python
119
  Models: list[models.BaseTCOModel] = [models.OpenAIModelGPT4,..., models.NewModel]
120
+ ```
121
+
122
+ ## Example: How we wrote OpenAI GPT3.5 Turbo TCO
123
+
124
+ Let’s break down how we followed the previous steps to implement the GPT3.5 Turbo model class.
125
+
126
+ To get the values for the input and output cost/token, we went to OpenAI’s [pricing web page](https://openai.com/pricing). We spotted which were their options, and which parameters could users change that would influence the costs.
127
+
128
+ ```python
129
+ class OpenAIModelGPT3_5(BaseTCOModel):
130
+ def __init__(self):
131
+ self.set_name("(SaaS) OpenAI GPT3.5 Turbo")
132
+ #Average latency value for GPT3.5 Turbo
133
+ self.set_latency("5s")
134
+ super().__init__()
135
+ ```
136
+ Let’s implement the `render` function.
137
+ For this model, OpenAI offers two different options for the context of the model, as in the amount of tokens considered by the model when processing text. Then, the user can choose between both using the dropdown below.
138
+ Note that the visibility is set to False!
139
+
140
+ ```python
141
+ def render(self):
142
+ self.context_length = gr.Dropdown(choices=["4K", "16K"], value="4K", visible=False, interactive=True, label="Context size", info="Number of tokens the model considers when processing text")
143
+ ```
144
+
145
+ Then, we have to create the input and output cost/token components that will be necessary to compute the cost/request.
146
+ Furthermore, we added a [Markdown](https://www.gradio.app/docs/markdown) component to provide information on the values we put for these cost/token (this Gradio component supports HTML).
147
+
148
+ ```python
149
+ self.input_tokens_cost_per_token = gr.Number(0.0015, visible=False, label="($) Price/1K input prompt tokens", interactive=False)
150
+ self.output_tokens_cost_per_token = gr.Number(0.002, visible=False, label="($) Price/1K output prompt tokens", interactive=False)
151
+ self.info = gr.Markdown("The cost per input and output tokens values are from OpenAI's [pricing web page](https://openai.com/pricing)", interactive=False, visible=False)
152
+ ```
153
+ Then, depending on the user selection for the context, the input and output cost/token are impacted. So we have to create an update function to implement their changes.
154
+
155
+ ```python
156
+ def define_cost_per_token(context_length):
157
+ if context_length == "4K":
158
+ cost_per_1k_input_tokens = 0.0015
159
+ cost_per_1k_output_tokens = 0.002
160
+ else:
161
+ cost_per_1k_input_tokens = 0.003
162
+ cost_per_1k_output_tokens = 0.004
163
+ return cost_per_1k_input_tokens, cost_per_1k_output_tokens
164
+ ```
165
+
166
+ This function is called when the user changes the value of `context_length`.
167
+
168
+ ```python
169
+ self.context_length.change(define_cost_per_token, inputs=self.context_length, outputs=[self.input_tokens_cost_per_token, self.output_tokens_cost_per_token])
170
+ ```
171
+
172
+ Then, the last part of the `render` function is about the labor cost. For a SaaS solution like OpenAI’s GPT3.5 Turbo, no engineer is required to deploy and maintain the model so this cost is null.
173
+
174
+ ```python
175
+ self.labor = gr.Number(0, visible=False, label="($) Labor cost per month", info="This is an estimate of the labor cost of the AI engineer in charge of deploying the model", interactive=True)
176
+ ```
177
+
178
+ Eventually, we have to implement the `compute_cost_per_token` function. Because OpenAI’s pricing is given in dollars per 1000 tokens, a little conversion is needed.
179
+
180
+ ```python
181
+ def compute_cost_per_token(self, input_tokens_cost_per_token, output_tokens_cost_per_token, labor):
182
+ cost_per_input_token = (input_tokens_cost_per_token / 1000)
183
+ cost_per_output_token = (output_tokens_cost_per_token / 1000)
184
+ return cost_per_input_token, cost_per_output_token, labor
185
+ ```
186
+
187
+ Then, we added this model class to the models.py [file](https://huggingface.co/spaces/mithril-security/TCO_calculator/blob/main/models.py) in the TCO Calculator repository, and added its name `OpenAIModelGPT3_5` in the list of models in the app.py [file](https://huggingface.co/spaces/mithril-security/TCO_calculator/blob/main/app.py).