Skip to content

[example] Add conversational snippets for requests #1282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/inference/src/snippets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ interface TemplateParams {
baseUrl?: string;
fullUrl?: string;
inputs?: object;
providerInputs?: object;
model?: ModelDataMinimal;
provider?: InferenceProvider;
providerModelId?: string;
methodName?: string; // specific to snippetBasic
importBase64?: boolean; // specific to snippetImportRequests
importJson?: boolean; // specific to snippetImportRequests
}

// Helpers to find + load templates
Expand Down Expand Up @@ -114,6 +116,18 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
{ chatCompletion: templateName.includes("conversational"), task: model.pipeline_tag as InferenceTask }
);

/// Parse request.info.body if not a binary.
/// This is the body sent to the provider. Important for snippets with raw payload (e.g curl, requests, etc.)
let providerInputs = inputs;
const bodyAsObj = request.info.body;
if (typeof bodyAsObj === "string") {
try {
providerInputs = JSON.parse(bodyAsObj);
} catch (e) {
console.error("Failed to parse body as JSON", e);
}
}

/// Prepare template injection data
const params: TemplateParams = {
accessToken,
Expand All @@ -125,6 +139,11 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
asJsonString: formatBody(inputs, "json"),
asPythonString: indentString(formatBody(inputs, "python"), 4),
},
providerInputs: {
asObj: providerInputs,
asJsonString: formatBody(providerInputs, "json"),
asPythonString: indentString(formatBody(providerInputs, "python"), 4),
},
model,
provider,
providerModelId: providerModelId ?? model.id,
Expand Down Expand Up @@ -157,6 +176,7 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
const importSection = snippetImportRequests({
...params,
importBase64: snippet.includes("base64"),
importJson: snippet.includes("json."),
});
snippet = `${importSection}\n\n${snippet}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ stream = client.chat.completions.create(
)

for chunk in stream:
print(chunk.choices[0].delta.content, end="")
print(chunk.choices[0].delta.content, end="")
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ def query(filename):
response = requests.post(API_URL, headers=headers, data=data)
return response.json()

output = query({{ inputs.asObj.inputs }})
output = query({{ providerInputs.asObj.inputs }})
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ def query(payload):
return response.json()

output = query({
"inputs": {{ inputs.asObj.inputs }},
"inputs": {{ providerInputs.asObj.inputs }},
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ def query(filename):
response = requests.post(API_URL, headers=headers, data=data)
return response.json()

output = query({{ inputs.asObj.inputs }})
output = query({{ providerInputs.asObj.inputs }})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()

response = query({
{{ providerInputs.asJsonString }}
})

print(response["choices"][0]["message"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
for line in response.iter_lines():
if not line.startswith(b"data:"):
continue
if line.strip() == b"data: [DONE]":
return
yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n"))

chunks = query({
{{ providerInputs.asJsonString }},
"stream": True,
})

for chunk in chunks:
print(chunk["choices"][0]["delta"]["content"], end="")
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ def query(payload):
return response.json()

output = query({
{{ inputs.asJsonString }},
{{ providerInputs.asJsonString }},
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def query(payload):
return response.content

image_bytes = query({
{{ inputs.asJsonString }}
{{ providerInputs.asJsonString }}
})

# You can access the image with PIL.Image for example
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{% if importBase64 %}
import base64
{% endif %}
{% if importJson %}
import json
{% endif %}
import requests

API_URL = "{{ fullUrl }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ def query(payload):

response = query({
"inputs": {
"data": {{ inputs.asObj.inputs }}
"data": {{ providerInputs.asObj.inputs }}
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def query(payload):
return response.content

audio_bytes = query({
"inputs": {{ inputs.asObj.inputs }},
"inputs": {{ providerInputs.asObj.inputs }},
})
# You can access the audio with IPython.display for example
from IPython.display import Audio
Expand All @@ -15,7 +15,7 @@ def query(payload):
return response.json()

audio, sampling_rate = query({
"inputs": {{ inputs.asObj.inputs }},
"inputs": {{ providerInputs.asObj.inputs }},
})
# You can access the audio with IPython.display for example
from IPython.display import Audio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def query(payload):
return response.content

image_bytes = query({
"inputs": {{ inputs.asObj.inputs }},
"inputs": {{ providerInputs.asObj.inputs }},
})

# You can access the image with PIL.Image for example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ def query(payload):
return response.json()

output = query({
"inputs": {{ inputs.asObj.inputs }},
"inputs": {{ providerInputs.asObj.inputs }},
"parameters": {"candidate_labels": ["refund", "legal", "faq"]},
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ def query(data):
return response.json()

output = query({
"image_path": {{ inputs.asObj.inputs }},
"image_path": {{ providerInputs.asObj.inputs }},
"parameters": {"candidate_labels": ["cat", "dog", "llama"]},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import requests

API_URL = "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.1-8B-Instruct/v1/chat/completions"
headers = {"Authorization": "Bearer api_token"}

def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()

response = query({
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
],
"max_tokens": 500,
"model": "meta-llama/Llama-3.1-8B-Instruct"
})

print(response["choices"][0]["message"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import requests

API_URL = "https://api.together.xyz/v1/chat/completions"
headers = {"Authorization": "Bearer api_token"}

def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()

response = query({
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
],
"max_tokens": 500,
"model": "<together alias for meta-llama/Llama-3.1-8B-Instruct>"
})

print(response["choices"][0]["message"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import json
import requests

API_URL = "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.1-8B-Instruct/v1/chat/completions"
headers = {"Authorization": "Bearer api_token"}

def query(payload):
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
for line in response.iter_lines():
if not line.startswith(b"data:"):
continue
if line.strip() == b"data: [DONE]":
return
yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n"))

chunks = query({
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
],
"max_tokens": 500,
"model": "meta-llama/Llama-3.1-8B-Instruct",
"stream": True,
})

for chunk in chunks:
print(chunk["choices"][0]["delta"]["content"], end="")
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import json
import requests

API_URL = "https://api.together.xyz/v1/chat/completions"
headers = {"Authorization": "Bearer api_token"}

def query(payload):
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
for line in response.iter_lines():
if not line.startswith(b"data:"):
continue
if line.strip() == b"data: [DONE]":
return
yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n"))

chunks = query({
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
],
"max_tokens": 500,
"model": "<together alias for meta-llama/Llama-3.1-8B-Instruct>",
"stream": True,
})

for chunk in chunks:
print(chunk["choices"][0]["delta"]["content"], end="")
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import requests

API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
headers = {"Authorization": "Bearer api_token"}

def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()

response = query({
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in one sentence."
},
{
"type": "image_url",
"image_url": {
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
}
}
]
}
],
"max_tokens": 500,
"model": "<fireworks-ai alias for meta-llama/Llama-3.2-11B-Vision-Instruct>"
})

print(response["choices"][0]["message"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import requests

API_URL = "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.2-11B-Vision-Instruct/v1/chat/completions"
headers = {"Authorization": "Bearer api_token"}

def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()

response = query({
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in one sentence."
},
{
"type": "image_url",
"image_url": {
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
}
}
]
}
],
"max_tokens": 500,
"model": "meta-llama/Llama-3.2-11B-Vision-Instruct"
})

print(response["choices"][0]["message"])
Loading