Skip to content

Commit c26b0bb

Browse files
committed
Add conversational snippets for requests
1 parent 52d3245 commit c26b0bb

File tree

13 files changed

+152
-2
lines changed

13 files changed

+152
-2
lines changed

packages/inference/src/snippets/python.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface TemplateParams {
2222
providerModelId?: string;
2323
methodName?: string; // specific to snippetBasic
2424
importBase64?: boolean; // specific to snippetImportRequests
25+
importJson?: boolean; // specific to snippetImportRequests
2526
}
2627

2728
// Helpers to find + load templates
@@ -157,6 +158,7 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
157158
const importSection = snippetImportRequests({
158159
...params,
159160
importBase64: snippet.includes("base64"),
161+
importJson: snippet.includes("json."),
160162
});
161163
snippet = `${importSection}\n\n${snippet}`;
162164
}

packages/inference/src/snippets/templates/python/openai/conversationalStream.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ stream = client.chat.completions.create(
1212
)
1313

1414
for chunk in stream:
15-
print(chunk.choices[0].delta.content, end="")
15+
print(chunk.choices[0].delta.content, end="")

packages/inference/src/snippets/templates/python/requests/conversational.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ response = query({
77
{{ inputs.asJsonString }}
88
})
99

10-
print(response["choices"][0]["message"])
10+
print(response["choices"][0]["message"])
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def query(payload):
2+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
3+
for line in response.iter_lines():
4+
if not line.startswith(b"data:"):
5+
continue
6+
if line.strip() == b"data: [DONE]":
7+
return
8+
yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n"))
9+
10+
for chunk in query({
11+
"model": "{{ providerModelId }}",
12+
{{ inputs.asJsonString }},
13+
"stream": True,
14+
}):
15+
print(chunk["choices"][0]["delta"]["content"], end="")

packages/inference/src/snippets/templates/python/requests/importRequests.jinja

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{% if importBase64 %}
22
import base64
33
{% endif %}
4+
{% if importJson %}
5+
import json
6+
{% endif %}
47
import requests
58

69
API_URL = "{{ fullUrl }}"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import json
2+
import requests
3+
4+
API_URL = "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.1-8B-Instruct/v1/chat/completions"
5+
headers = {"Authorization": "Bearer api_token"}
6+
7+
def query(payload):
8+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
9+
for line in response.iter_lines():
10+
if not line.startswith(b"data:"):
11+
continue
12+
if line.strip() == b"data: [DONE]":
13+
return
14+
yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n"))
15+
16+
for chunk in query({
17+
"model": "meta-llama/Llama-3.1-8B-Instruct",
18+
"messages": [
19+
{
20+
"role": "user",
21+
"content": "What is the capital of France?"
22+
}
23+
],
24+
"max_tokens": 500,
25+
"stream": True,
26+
}):
27+
print(chunk["choices"][0]["delta"]["content"], end="")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import json
2+
import requests
3+
4+
API_URL = "https://api.together.xyz/v1/chat/completions"
5+
headers = {"Authorization": "Bearer api_token"}
6+
7+
def query(payload):
8+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
9+
for line in response.iter_lines():
10+
if not line.startswith(b"data:"):
11+
continue
12+
if line.strip() == b"data: [DONE]":
13+
return
14+
yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n"))
15+
16+
for chunk in query({
17+
"model": "<together alias for meta-llama/Llama-3.1-8B-Instruct>",
18+
"messages": [
19+
{
20+
"role": "user",
21+
"content": "What is the capital of France?"
22+
}
23+
],
24+
"max_tokens": 500,
25+
"stream": True,
26+
}):
27+
print(chunk["choices"][0]["delta"]["content"], end="")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import json
2+
import requests
3+
4+
API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
5+
headers = {"Authorization": "Bearer api_token"}
6+
7+
def query(payload):
8+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
9+
for line in response.iter_lines():
10+
if not line.startswith(b"data:"):
11+
continue
12+
if line.strip() == b"data: [DONE]":
13+
return
14+
yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n"))
15+
16+
for chunk in query({
17+
"model": "<fireworks-ai alias for meta-llama/Llama-3.2-11B-Vision-Instruct>",
18+
"messages": [
19+
{
20+
"role": "user",
21+
"content": [
22+
{
23+
"type": "text",
24+
"text": "Describe this image in one sentence."
25+
},
26+
{
27+
"type": "image_url",
28+
"image_url": {
29+
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
30+
}
31+
}
32+
]
33+
}
34+
],
35+
"max_tokens": 500,
36+
"stream": True,
37+
}):
38+
print(chunk["choices"][0]["delta"]["content"], end="")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import json
2+
import requests
3+
4+
API_URL = "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.2-11B-Vision-Instruct/v1/chat/completions"
5+
headers = {"Authorization": "Bearer api_token"}
6+
7+
def query(payload):
8+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
9+
for line in response.iter_lines():
10+
if not line.startswith(b"data:"):
11+
continue
12+
if line.strip() == b"data: [DONE]":
13+
return
14+
yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n"))
15+
16+
for chunk in query({
17+
"model": "meta-llama/Llama-3.2-11B-Vision-Instruct",
18+
"messages": [
19+
{
20+
"role": "user",
21+
"content": [
22+
{
23+
"type": "text",
24+
"text": "Describe this image in one sentence."
25+
},
26+
{
27+
"type": "image_url",
28+
"image_url": {
29+
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
30+
}
31+
}
32+
]
33+
}
34+
],
35+
"max_tokens": 500,
36+
"stream": True,
37+
}):
38+
print(chunk["choices"][0]["delta"]["content"], end="")

0 commit comments

Comments
 (0)