Skip to content

Commit ab07edf

Browse files
hanouticelinaSBrandeisjulien-c
authored
[Inference Providers] Show how to bill to orgs using OpenAI Python/JS clients (#1821)
* update billing snippets * nit * fix snippet * extra space * better openai js snippet * Apply suggestions from code review Co-authored-by: Simon Brandeis <[email protected]> * Apply suggestions from code review Co-authored-by: Julien Chaumond <[email protected]> --------- Co-authored-by: Simon Brandeis <[email protected]> Co-authored-by: Julien Chaumond <[email protected]>
1 parent 4fe884b commit ab07edf

File tree

1 file changed

+168
-16
lines changed

1 file changed

+168
-16
lines changed

docs/inference-providers/pricing.md

Lines changed: 168 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,182 @@ Enterprise Hub organizations receive a pool of free usage credits based on the n
9494
<img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/inference-providers/enterprise-org-settings-dark.png"/>
9595
</div>
9696

97-
If you are using the JavaScript `InferenceClient`, you can set the `billTo` attribute at a client level:
97+
98+
99+
<hfoptions id="python-clients">
100+
101+
<hfoption id="huggingface_hub">
102+
103+
To bill your organization, use the `bill_to` parameter when initializing the client.
104+
105+
```python
106+
from huggingface_hub import InferenceClient
107+
108+
client = InferenceClient(bill_to="my-org-name")
109+
110+
completion = client.chat.completions.create(
111+
model="deepseek-ai/DeepSeek-V3-0324",
112+
messages=[
113+
{
114+
"role": "user",
115+
"content": "How many 'G's in 'huggingface'?"
116+
}
117+
],
118+
)
119+
120+
print(completion.choices[0].message)
121+
```
122+
123+
</hfoption>
124+
125+
<hfoption id="openai">
126+
127+
To bill your organization when using OpenAI's Python client, set the `X-HF-Bill-To` header using `extra_headers` on the `completions.create` method call.
128+
129+
```python
130+
import os
131+
from openai import OpenAI
132+
133+
client = OpenAI(
134+
base_url="https://router.huggingface.co/v1",
135+
api_key=os.environ["HF_TOKEN"],
136+
)
137+
138+
completion = client.chat.completions.create(
139+
model="deepseek-ai/DeepSeek-V3-0324",
140+
messages=[
141+
{
142+
"role": "user",
143+
"content": "How many 'G's in 'huggingface'?"
144+
}
145+
],
146+
extra_headers={"X-HF-Bill-To": "my-org-name"},
147+
)
148+
149+
print(completion.choices[0].message)
150+
```
151+
152+
</hfoption>
153+
154+
<hfoption id="requests">
155+
156+
To bill your organization when making direct HTTP requests, include the `X-HF-Bill-To` header.
157+
158+
```python
159+
import os
160+
import requests
161+
162+
API_URL = "https://router.huggingface.co/v1/chat/completions"
163+
headers = {"Authorization": f"Bearer {os.environ['HF_TOKEN']}", "X-HF-Bill-To": "my-org-name"}
164+
payload = {
165+
"messages": [
166+
{
167+
"role": "user",
168+
"content": "How many 'G's in 'huggingface'?"
169+
}
170+
],
171+
"model": "deepseek-ai/DeepSeek-V3-0324",
172+
}
173+
174+
response = requests.post(API_URL, headers=headers, json=payload)
175+
print(response.json()["choices"][0]["message"])
176+
```
177+
178+
</hfoption>
179+
180+
</hfoptions>
181+
182+
Similarily in JavaScript:
183+
184+
<hfoptions id="javascript-clients">
185+
186+
<hfoption id="huggingface.js">
187+
188+
If you are using the JavaScript `InferenceClient`, you can set the `billTo` attribute at a client level to bill your organization.
98189

99190
```js
100191
import { InferenceClient } from "@huggingface/inference";
101192

102-
const client = new InferenceClient("hf_token", { billTo: "my-org-name" });
193+
const client = new InferenceClient(process.env.HF_TOKEN, { billTo: "my-org-name" });
103194

104-
const image = await client.textToImage({
105-
model: "black-forest-labs/FLUX.1-schnell",
106-
inputs: "A majestic lion in a fantasy forest",
107-
provider: "fal-ai",
195+
const completion = await client.chat.completions.create({
196+
model: "deepseek-ai/DeepSeek-V3-0324",
197+
messages: [
198+
{
199+
role: "user",
200+
content: "How many 'G's in 'huggingface'?",
201+
},
202+
],
108203
});
109-
/// Use the generated image (it's a Blob)
204+
205+
console.log(completion.choices[0].message.content);
110206
```
111207

112-
And similarly in Python:
208+
</hfoption>
113209

114-
```py
115-
from huggingface_hub import InferenceClient
116-
client = InferenceClient(provider="fal-ai", bill_to="my-org-name")
117-
image = client.text_to_image(
118-
"A majestic lion in a fantasy forest",
119-
model="black-forest-labs/FLUX.1-schnell",
120-
)
121-
image.save("lion.png")
210+
<hfoption id="openai">
211+
212+
To bill your organization with the OpenAI JavaScript client, set the `X-HF-Bill-To` header using the `defaultHeaders` option on the `completions.create` method call.
213+
214+
```javascript
215+
import OpenAI from "openai";
216+
217+
const client = new OpenAI({
218+
baseURL: "https://router.huggingface.co/v1",
219+
apiKey: process.env.HF_TOKEN,
220+
});
221+
222+
const completion = await client.chat.completions.create(
223+
{
224+
model: "deepseek-ai/DeepSeek-V3-0324",
225+
messages: [
226+
{
227+
role: "user",
228+
content: "How many 'G's in 'huggingface'?",
229+
},
230+
],
231+
},
232+
{
233+
headers: {
234+
"X-HF-Bill-To": "my-org-name",
235+
},
236+
}
237+
);
238+
239+
console.log(completion.choices[0].message.content);
240+
```
241+
242+
</hfoption>
243+
244+
<hfoption id="fetch">
245+
246+
When using `fetch`, include the `X-HF-Bill-To` header to bill your organization.
247+
248+
```js
249+
import fetch from "node-fetch";
250+
251+
const response = await fetch(
252+
"https://router.huggingface.co/v1/chat/completions",
253+
{
254+
method: "POST",
255+
headers: {
256+
Authorization: `Bearer ${process.env.HF_TOKEN}`,
257+
"Content-Type": "application/json",
258+
"X-HF-Bill-To": "my-org-name",
259+
},
260+
body: JSON.stringify({
261+
model: "deepseek-ai/DeepSeek-V3-0324",
262+
messages: [
263+
{
264+
role: "user",
265+
content: "How many 'G's in 'huggingface'?",
266+
},
267+
],
268+
}),
269+
}
270+
);
271+
console.log(await response.json());
122272
```
273+
</hfoption>
123274

275+
</hfoptions>

0 commit comments

Comments
 (0)