Operations
Sample
Submits a sample operation that will asynchronously generate text completions with logprobs.
POST
/
v1
/
rl
/
training-sessions
/
{session_id}
/
operations
/
sample
Together AI SDK (Python)
import uuid
from together import Together
client = Together()
# Token IDs come from the base model's tokenizer.
operation = client.post_training.operations.sample(
"<SESSION_ID>",
idempotency_key=str(uuid.uuid4()),
model_inputs=[
{"chunks": [{"encoded_text": {"tokens": [791, 6864, 315, 9822]}}]}
],
num_samples=4,
sampling_params={"max_tokens": 256, "temperature": 1.0},
)
print(operation.id, operation.status)
curl --request POST \
--url https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--header 'Idempotency-Key: <idempotency-key>' \
--data '
{
"model_inputs": [
{
"chunks": [
{
"encoded_text": {
"tokens": [
123,
456,
789
]
}
}
]
}
],
"sampling_params": {
"max_tokens": 512,
"temperature": 1,
"top_p": 1,
"top_k": -1,
"stop": [
"\n",
"END"
],
"seed": "42"
},
"num_samples": 1,
"prompt_logprobs": false,
"topk_prompt_logprobs": 0,
"return_routed_experts": false
}
'const options = {
method: 'POST',
headers: {
'Idempotency-Key': '<idempotency-key>',
Authorization: 'Bearer <token>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model_inputs: [{chunks: [{encoded_text: {tokens: [123, 456, 789]}}]}],
sampling_params: {
max_tokens: 512,
temperature: 1,
top_p: 1,
top_k: -1,
stop: ['\n', 'END'],
seed: '42'
},
num_samples: 1,
prompt_logprobs: false,
topk_prompt_logprobs: 0,
return_routed_experts: false
})
};
fetch('https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model_inputs' => [
[
'chunks' => [
[
'encoded_text' => [
'tokens' => [
123,
456,
789
]
]
]
]
]
],
'sampling_params' => [
'max_tokens' => 512,
'temperature' => 1,
'top_p' => 1,
'top_k' => -1,
'stop' => [
'
',
'END'
],
'seed' => '42'
],
'num_samples' => 1,
'prompt_logprobs' => false,
'topk_prompt_logprobs' => 0,
'return_routed_experts' => false
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json",
"Idempotency-Key: <idempotency-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample"
payload := strings.NewReader("{\n \"model_inputs\": [\n {\n \"chunks\": [\n {\n \"encoded_text\": {\n \"tokens\": [\n 123,\n 456,\n 789\n ]\n }\n }\n ]\n }\n ],\n \"sampling_params\": {\n \"max_tokens\": 512,\n \"temperature\": 1,\n \"top_p\": 1,\n \"top_k\": -1,\n \"stop\": [\n \"\\n\",\n \"END\"\n ],\n \"seed\": \"42\"\n },\n \"num_samples\": 1,\n \"prompt_logprobs\": false,\n \"topk_prompt_logprobs\": 0,\n \"return_routed_experts\": false\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Idempotency-Key", "<idempotency-key>")
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample")
.header("Idempotency-Key", "<idempotency-key>")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model_inputs\": [\n {\n \"chunks\": [\n {\n \"encoded_text\": {\n \"tokens\": [\n 123,\n 456,\n 789\n ]\n }\n }\n ]\n }\n ],\n \"sampling_params\": {\n \"max_tokens\": 512,\n \"temperature\": 1,\n \"top_p\": 1,\n \"top_k\": -1,\n \"stop\": [\n \"\\n\",\n \"END\"\n ],\n \"seed\": \"42\"\n },\n \"num_samples\": 1,\n \"prompt_logprobs\": false,\n \"topk_prompt_logprobs\": 0,\n \"return_routed_experts\": false\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Idempotency-Key"] = '<idempotency-key>'
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model_inputs\": [\n {\n \"chunks\": [\n {\n \"encoded_text\": {\n \"tokens\": [\n 123,\n 456,\n 789\n ]\n }\n }\n ]\n }\n ],\n \"sampling_params\": {\n \"max_tokens\": 512,\n \"temperature\": 1,\n \"top_p\": 1,\n \"top_k\": -1,\n \"stop\": [\n \"\\n\",\n \"END\"\n ],\n \"seed\": \"42\"\n },\n \"num_samples\": 1,\n \"prompt_logprobs\": false,\n \"topk_prompt_logprobs\": 0,\n \"return_routed_experts\": false\n}"
response = http.request(request)
puts response.read_body{
"id": "550e8400-e29b-41d4-a716-446655440000",
"status": "TRAINING_OPERATION_STATUS_PENDING",
"output": {
"results": [
{
"sequences": [
{
"tokens": [
"123",
"456",
"789"
],
"stop_reason": "STOP_REASON_LENGTH",
"prompt_cache_hit_tokens": 1024,
"logprobs": [
-0.5,
-1.2,
-0.3
],
"routed_experts_key": "<string>"
}
],
"policy_segments": [
{
"version": 5,
"start_token": 0
}
],
"prompt_logprobs": [
0,
-0.5,
-1.2
],
"topk_prompt_logprobs": [
{
"token_ids": [
123,
456
],
"logprobs": [
-0.5,
-2.1
]
}
]
}
]
},
"error": {
"code": "TRAINING_OPERATION_ERROR_CODE_TIMEOUT",
"message": "Operation timed out"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"param": null,
"code": null
}
}Authorizations
Headers
Body
application/json
Response
Async sample operation
Operation status
Available options:
TRAINING_OPERATION_STATUS_UNSPECIFIED, TRAINING_OPERATION_STATUS_PENDING, TRAINING_OPERATION_STATUS_RUNNING, TRAINING_OPERATION_STATUS_COMPLETED, TRAINING_OPERATION_STATUS_FAILED Example:
"TRAINING_OPERATION_STATUS_PENDING"
Was this page helpful?
⌘I
Together AI SDK (Python)
import uuid
from together import Together
client = Together()
# Token IDs come from the base model's tokenizer.
operation = client.post_training.operations.sample(
"<SESSION_ID>",
idempotency_key=str(uuid.uuid4()),
model_inputs=[
{"chunks": [{"encoded_text": {"tokens": [791, 6864, 315, 9822]}}]}
],
num_samples=4,
sampling_params={"max_tokens": 256, "temperature": 1.0},
)
print(operation.id, operation.status)
curl --request POST \
--url https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--header 'Idempotency-Key: <idempotency-key>' \
--data '
{
"model_inputs": [
{
"chunks": [
{
"encoded_text": {
"tokens": [
123,
456,
789
]
}
}
]
}
],
"sampling_params": {
"max_tokens": 512,
"temperature": 1,
"top_p": 1,
"top_k": -1,
"stop": [
"\n",
"END"
],
"seed": "42"
},
"num_samples": 1,
"prompt_logprobs": false,
"topk_prompt_logprobs": 0,
"return_routed_experts": false
}
'const options = {
method: 'POST',
headers: {
'Idempotency-Key': '<idempotency-key>',
Authorization: 'Bearer <token>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model_inputs: [{chunks: [{encoded_text: {tokens: [123, 456, 789]}}]}],
sampling_params: {
max_tokens: 512,
temperature: 1,
top_p: 1,
top_k: -1,
stop: ['\n', 'END'],
seed: '42'
},
num_samples: 1,
prompt_logprobs: false,
topk_prompt_logprobs: 0,
return_routed_experts: false
})
};
fetch('https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model_inputs' => [
[
'chunks' => [
[
'encoded_text' => [
'tokens' => [
123,
456,
789
]
]
]
]
]
],
'sampling_params' => [
'max_tokens' => 512,
'temperature' => 1,
'top_p' => 1,
'top_k' => -1,
'stop' => [
'
',
'END'
],
'seed' => '42'
],
'num_samples' => 1,
'prompt_logprobs' => false,
'topk_prompt_logprobs' => 0,
'return_routed_experts' => false
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json",
"Idempotency-Key: <idempotency-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample"
payload := strings.NewReader("{\n \"model_inputs\": [\n {\n \"chunks\": [\n {\n \"encoded_text\": {\n \"tokens\": [\n 123,\n 456,\n 789\n ]\n }\n }\n ]\n }\n ],\n \"sampling_params\": {\n \"max_tokens\": 512,\n \"temperature\": 1,\n \"top_p\": 1,\n \"top_k\": -1,\n \"stop\": [\n \"\\n\",\n \"END\"\n ],\n \"seed\": \"42\"\n },\n \"num_samples\": 1,\n \"prompt_logprobs\": false,\n \"topk_prompt_logprobs\": 0,\n \"return_routed_experts\": false\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Idempotency-Key", "<idempotency-key>")
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample")
.header("Idempotency-Key", "<idempotency-key>")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model_inputs\": [\n {\n \"chunks\": [\n {\n \"encoded_text\": {\n \"tokens\": [\n 123,\n 456,\n 789\n ]\n }\n }\n ]\n }\n ],\n \"sampling_params\": {\n \"max_tokens\": 512,\n \"temperature\": 1,\n \"top_p\": 1,\n \"top_k\": -1,\n \"stop\": [\n \"\\n\",\n \"END\"\n ],\n \"seed\": \"42\"\n },\n \"num_samples\": 1,\n \"prompt_logprobs\": false,\n \"topk_prompt_logprobs\": 0,\n \"return_routed_experts\": false\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/sample")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Idempotency-Key"] = '<idempotency-key>'
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model_inputs\": [\n {\n \"chunks\": [\n {\n \"encoded_text\": {\n \"tokens\": [\n 123,\n 456,\n 789\n ]\n }\n }\n ]\n }\n ],\n \"sampling_params\": {\n \"max_tokens\": 512,\n \"temperature\": 1,\n \"top_p\": 1,\n \"top_k\": -1,\n \"stop\": [\n \"\\n\",\n \"END\"\n ],\n \"seed\": \"42\"\n },\n \"num_samples\": 1,\n \"prompt_logprobs\": false,\n \"topk_prompt_logprobs\": 0,\n \"return_routed_experts\": false\n}"
response = http.request(request)
puts response.read_body{
"id": "550e8400-e29b-41d4-a716-446655440000",
"status": "TRAINING_OPERATION_STATUS_PENDING",
"output": {
"results": [
{
"sequences": [
{
"tokens": [
"123",
"456",
"789"
],
"stop_reason": "STOP_REASON_LENGTH",
"prompt_cache_hit_tokens": 1024,
"logprobs": [
-0.5,
-1.2,
-0.3
],
"routed_experts_key": "<string>"
}
],
"policy_segments": [
{
"version": 5,
"start_token": 0
}
],
"prompt_logprobs": [
0,
-0.5,
-1.2
],
"topk_prompt_logprobs": [
{
"token_ids": [
123,
456
],
"logprobs": [
-0.5,
-2.1
]
}
]
}
]
},
"error": {
"code": "TRAINING_OPERATION_ERROR_CODE_TIMEOUT",
"message": "Operation timed out"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"param": null,
"code": null
}
}