Why V2? Completions V2 offers auto-routing for optimal model selection, model family preferences, and tradition-aware responses—all while maintaining compatibility with the standard chat completions format.
Prerequisites
Before starting, ensure you have:- A Gloo AI Studio account
- Your API key from the API Credentials page
- Authentication setup - Complete the Authentication Tutorial first
Choose Your Routing Strategy
Completions V2 offers three routing modes:| Mode | Use Case | Parameter |
|---|---|---|
| AI Core (Recommended) | Let Gloo AI automatically select the best model | "auto_routing": true |
| AI Core Select | Choose a provider family, let Gloo pick the model | "model_family": "anthropic" |
| AI Select | Specify an exact model | "model": "gloo-openai-gpt-5-mini" |
Example 1: Auto-Routing (Recommended)
Let Gloo AI analyze your query and automatically select the optimal model:import requests
def make_v2_completion_auto(api_key):
"""Makes a V2 completion request with auto-routing."""
api_url = "https://platform.ai.gloo.com/ai/v2/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"messages": [
{"role": "user", "content": "How does the Old Testament connect to the New Testament?"}
],
"auto_routing": True,
"tradition": "evangelical" # Optional: evangelical, catholic, or mainline
}
response = requests.post(api_url, headers=headers, json=payload)
response.raise_for_status()
return response.json()
async function makeV2CompletionAuto() {
const apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
const payload = {
messages: [
{ role: "user", content: "How does the Old Testament connect to the New Testament?" }
],
auto_routing: true,
tradition: "evangelical" // Optional: evangelical, catholic, or mainline
};
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
return response.json();
}
async function makeV2CompletionAuto(): Promise<any> {
const apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
const payload = {
messages: [
{ role: "user", content: "How does the Old Testament connect to the New Testament?" }
],
auto_routing: true,
tradition: "evangelical" // Optional: evangelical, catholic, or mainline
};
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
return response.json();
}
<?php
function makeV2CompletionAuto() {
$apiUrl = 'https://platform.ai.gloo.com/ai/v2/chat/completions';
$apiKey = $_ENV['GLOO_API_KEY'] ?? 'YOUR_API_KEY';
$payload = json_encode([
'messages' => [
['role' => 'user', 'content' => 'How does the Old Testament connect to the New Testament?']
],
'auto_routing' => true,
'tradition' => 'evangelical' // Optional: evangelical, catholic, or mainline
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
]);
$result = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
return json_decode($result, true);
}
?>
func makeV2CompletionAuto() (map[string]interface{}, error) {
apiUrl := "https://platform.ai.gloo.com/ai/v2/chat/completions"
payload := map[string]interface{}{
"messages": []map[string]string{
{"role": "user", "content": "How does the Old Testament connect to the New Testament?"},
},
"auto_routing": true,
"tradition": "evangelical", // Optional: evangelical, catholic, or mainline
}
jsonPayload, _ := json.Marshal(payload)
req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+apiKey)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
return result, nil
}
public String makeV2CompletionAuto() throws IOException, InterruptedException {
String apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
String payload = """
{
"messages": [
{"role": "user", "content": "How does the Old Testament connect to the New Testament?"}
],
"auto_routing": true,
"tradition": "evangelical"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + API_KEY)
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
}
Example 2: Model Family Selection
Specify a provider family and let Gloo AI pick the best model within that family:def make_v2_completion_family(token_info):
"""Makes a V2 completion request with model family selection."""
api_url = "https://platform.ai.gloo.com/ai/v2/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"messages": [
{"role": "user", "content": "Draft a short sermon outline on forgiveness."}
],
"model_family": "anthropic", # Options: openai, anthropic, google, open source
"stream": False
}
response = requests.post(api_url, headers=headers, json=payload)
response.raise_for_status()
return response.json()
async function makeV2CompletionFamily() {
const apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
const payload = {
messages: [
{ role: "user", content: "Draft a short sermon outline on forgiveness." }
],
model_family: "anthropic", // Options: openai, anthropic, google, open source
stream: false
};
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
return response.json();
}
async function makeV2CompletionFamily(): Promise<any> {
const apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
const payload = {
messages: [
{ role: "user", content: "Draft a short sermon outline on forgiveness." }
],
model_family: "anthropic", // Options: openai, anthropic, google, open source
stream: false
};
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
return response.json();
}
<?php
function makeV2CompletionFamily() {
$apiUrl = 'https://platform.ai.gloo.com/ai/v2/chat/completions';
$apiKey = $_ENV['GLOO_API_KEY'] ?? 'YOUR_API_KEY';
$payload = json_encode([
'messages' => [
['role' => 'user', 'content' => 'Draft a short sermon outline on forgiveness.']
],
'model_family' => 'anthropic', // Options: openai, anthropic, google, open source
'stream' => false
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
]);
$result = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
return json_decode($result, true);
}
?>
func makeV2CompletionFamily() (map[string]interface{}, error) {
apiUrl := "https://platform.ai.gloo.com/ai/v2/chat/completions"
payload := map[string]interface{}{
"messages": []map[string]string{
{"role": "user", "content": "Draft a short sermon outline on forgiveness."},
},
"model_family": "anthropic", // Options: openai, anthropic, google, open source
"stream": false,
}
jsonPayload, _ := json.Marshal(payload)
req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+apiKey)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
return result, nil
}
public String makeV2CompletionFamily() throws IOException, InterruptedException {
String apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
String payload = """
{
"messages": [
{"role": "user", "content": "Draft a short sermon outline on forgiveness."}
],
"model_family": "anthropic",
"stream": false
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + API_KEY)
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
}
openai, anthropic, google, open source
Example 3: Direct Model Selection
Choose a specific model for full control:def make_v2_completion_direct(token_info):
"""Makes a V2 completion request with direct model selection."""
api_url = "https://platform.ai.gloo.com/ai/v2/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"messages": [
{"role": "user", "content": "Summarize the book of Romans in 3 sentences."}
],
"model": "gloo-anthropic-claude-sonnet-4.5",
"temperature": 0.7,
"max_tokens": 500
}
response = requests.post(api_url, headers=headers, json=payload)
response.raise_for_status()
return response.json()
async function makeV2CompletionDirect() {
const apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
const payload = {
messages: [
{ role: "user", content: "Summarize the book of Romans in 3 sentences." }
],
model: "gloo-anthropic-claude-sonnet-4.5",
temperature: 0.7,
max_tokens: 500
};
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
return response.json();
}
async function makeV2CompletionDirect(): Promise<any> {
const apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
const payload = {
messages: [
{ role: "user", content: "Summarize the book of Romans in 3 sentences." }
],
model: "gloo-anthropic-claude-sonnet-4.5",
temperature: 0.7,
max_tokens: 500
};
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
return response.json();
}
<?php
function makeV2CompletionDirect() {
$apiUrl = 'https://platform.ai.gloo.com/ai/v2/chat/completions';
$apiKey = $_ENV['GLOO_API_KEY'] ?? 'YOUR_API_KEY';
$payload = json_encode([
'messages' => [
['role' => 'user', 'content' => 'Summarize the book of Romans in 3 sentences.']
],
'model' => 'gloo-anthropic-claude-sonnet-4.5',
'temperature' => 0.7,
'max_tokens' => 500
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
]);
$result = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
return json_decode($result, true);
}
?>
func makeV2CompletionDirect() (map[string]interface{}, error) {
apiUrl := "https://platform.ai.gloo.com/ai/v2/chat/completions"
payload := map[string]interface{}{
"messages": []map[string]string{
{"role": "user", "content": "Summarize the book of Romans in 3 sentences."},
},
"model": "gloo-anthropic-claude-sonnet-4.5",
"temperature": 0.7,
"max_tokens": 500,
}
jsonPayload, _ := json.Marshal(payload)
req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+apiKey)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
return result, nil
}
public String makeV2CompletionDirect() throws IOException, InterruptedException {
String apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
String payload = """
{
"messages": [
{"role": "user", "content": "Summarize the book of Romans in 3 sentences."}
],
"model": "gloo-anthropic-claude-sonnet-4.5",
"temperature": 0.7,
"max_tokens": 500
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + API_KEY)
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
}
Understanding the Response
V2 responses include additional routing metadata:{
"id": "chatcmpl-xyz",
"object": "chat.completion",
"created": 1733184562,
"model": "gloo-anthropic-claude-sonnet-4.5",
"routing_mechanism": "auto_routing",
"routing_tier": "tier_2",
"routing_confidence": 0.87,
"tradition": "evangelical",
"provider": "Anthropic",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The response content..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 125,
"completion_tokens": 78,
"total_tokens": 203
}
}
routing_mechanism: How the model was selected (auto_routing,model_family, ordirect_model_selection)routing_tier: The complexity tier determined by auto-routing (tier_1,tier_2,tier_3)routing_confidence: Confidence score for the routing decision (0.0-1.0)tradition: The theological perspective applied (if specified)
Streaming Responses
Enable streaming for real-time responses:import requests
def make_v2_completion_streaming(token_info):
"""Makes a streaming V2 completion request."""
api_url = "https://platform.ai.gloo.com/ai/v2/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"messages": [
{"role": "user", "content": "Explain the significance of the resurrection."}
],
"auto_routing": True,
"stream": True
}
with requests.post(api_url, headers=headers, json=payload, stream=True) as response:
response.raise_for_status()
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
async function makeV2CompletionStreaming() {
const apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
const payload = {
messages: [
{ role: "user", content: "Explain the significance of the resurrection." }
],
auto_routing: true,
stream: true
};
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log(decoder.decode(value));
}
}
async function makeV2CompletionStreaming(): Promise<void> {
const apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
const payload = {
messages: [
{ role: "user", content: "Explain the significance of the resurrection." }
],
auto_routing: true,
stream: true
};
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
const reader = response.body?.getReader();
if (!reader) throw new Error('No reader available');
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log(decoder.decode(value));
}
}
<?php
function makeV2CompletionStreaming() {
$apiUrl = 'https://platform.ai.gloo.com/ai/v2/chat/completions';
$apiKey = $_ENV['GLOO_API_KEY'] ?? 'YOUR_API_KEY';
$payload = json_encode([
'messages' => [
['role' => 'user', 'content' => 'Explain the significance of the resurrection.']
],
'auto_routing' => true,
'stream' => true
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
]);
// Stream the response
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) {
echo $data;
return strlen($data);
});
curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
}
?>
func makeV2CompletionStreaming() error {
apiUrl := "https://platform.ai.gloo.com/ai/v2/chat/completions"
payload := map[string]interface{}{
"messages": []map[string]string{
{"role": "user", "content": "Explain the significance of the resurrection."},
},
"auto_routing": true,
"stream": true,
}
jsonPayload, _ := json.Marshal(payload)
req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(jsonPayload))
if err != nil {
return err
}
req.Header.Add("Authorization", "Bearer "+apiKey)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
break
}
fmt.Print(string(line))
}
return nil
}
public void makeV2CompletionStreaming() throws IOException, InterruptedException {
String apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions";
String payload = """
{
"messages": [
{"role": "user", "content": "Explain the significance of the resurrection."}
],
"auto_routing": true,
"stream": true
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + API_KEY)
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<Stream<String>> response = client.send(
request,
HttpResponse.BodyHandlers.ofLines()
);
response.body().forEach(System.out::println);
}
Complete Examples
The following examples combine all three routing strategies into a single, runnable script for each language. You’ll want to first set up your environment variables in either an.env file:
GLOO_API_KEY=YOUR_API_KEY
export GLOO_API_KEY="your_actual_api_key_here"
import requests
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# --- Configuration ---
API_KEY = os.getenv("GLOO_API_KEY", "YOUR_API_KEY")
API_URL = "https://platform.ai.gloo.com/ai/v2/chat/completions"
# --- V2 Completion Functions ---
def make_v2_auto_routing(message, tradition="evangelical"):
"""Example 1: Auto-routing - Let Gloo AI select the optimal model."""
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
payload = {
"messages": [{"role": "user", "content": message}],
"auto_routing": True,
"tradition": tradition
}
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status()
return response.json()
def make_v2_model_family(message, model_family="anthropic"):
"""Example 2: Model family selection - Choose a provider family."""
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
payload = {
"messages": [{"role": "user", "content": message}],
"model_family": model_family
}
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status()
return response.json()
def make_v2_direct_model(message, model="gloo-anthropic-claude-sonnet-4.5"):
"""Example 3: Direct model selection - Specify an exact model."""
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
payload = {
"messages": [{"role": "user", "content": message}],
"model": model,
"temperature": 0.7,
"max_tokens": 500
}
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status()
return response.json()
# --- Main Execution ---
if __name__ == "__main__":
try:
# Example 1: Auto-routing
print("=== Example 1: Auto-Routing ===")
result1 = make_v2_auto_routing("How does the Old Testament connect to the New Testament?")
print(f"Model used: {result1.get('model')}")
print(f"Routing: {result1.get('routing_mechanism')}")
print(f"Response: {result1['choices'][0]['message']['content'][:200]}...")
# Example 2: Model family selection
print("\n=== Example 2: Model Family Selection ===")
result2 = make_v2_model_family("Draft a short sermon outline on forgiveness.", "anthropic")
print(f"Model used: {result2.get('model')}")
print(f"Response: {result2['choices'][0]['message']['content'][:200]}...")
# Example 3: Direct model selection
print("\n=== Example 3: Direct Model Selection ===")
result3 = make_v2_direct_model("Summarize the book of Romans in 3 sentences.")
print(f"Model used: {result3.get('model')}")
print(f"Response: {result3['choices'][0]['message']['content'][:200]}...")
except requests.exceptions.HTTPError as err:
print(f"An HTTP error occurred: {err}")
except Exception as err:
print(f"An error occurred: {err}")
// Load environment variables from .env file
require('dotenv').config();
const axios = require('axios');
// --- Configuration ---
const API_KEY = process.env.GLOO_API_KEY || "YOUR_API_KEY";
const API_URL = "https://platform.ai.gloo.com/ai/v2/chat/completions";
// --- V2 Completion Functions ---
async function makeV2AutoRouting(message, tradition = "evangelical") {
// Example 1: Auto-routing - Let Gloo AI select the optimal model
const response = await axios.post(API_URL, {
messages: [{ role: "user", content: message }],
auto_routing: true,
tradition: tradition
}, {
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
});
return response.data;
}
async function makeV2ModelFamily(message, modelFamily = "anthropic") {
// Example 2: Model family selection - Choose a provider family
const response = await axios.post(API_URL, {
messages: [{ role: "user", content: message }],
model_family: modelFamily
}, {
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
});
return response.data;
}
async function makeV2DirectModel(message, model = "gloo-anthropic-claude-sonnet-4.5") {
// Example 3: Direct model selection - Specify an exact model
const response = await axios.post(API_URL, {
messages: [{ role: "user", content: message }],
model: model,
temperature: 0.7,
max_tokens: 500
}, {
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
});
return response.data;
}
// --- Main Execution ---
async function main() {
try {
// Example 1: Auto-routing
console.log("=== Example 1: Auto-Routing ===");
const result1 = await makeV2AutoRouting("How does the Old Testament connect to the New Testament?");
console.log(`Model used: ${result1.model}`);
console.log(`Routing: ${result1.routing_mechanism}`);
console.log(`Response: ${result1.choices[0].message.content.substring(0, 200)}...`);
// Example 2: Model family selection
console.log("\n=== Example 2: Model Family Selection ===");
const result2 = await makeV2ModelFamily("Draft a short sermon outline on forgiveness.", "anthropic");
console.log(`Model used: ${result2.model}`);
console.log(`Response: ${result2.choices[0].message.content.substring(0, 200)}...`);
// Example 3: Direct model selection
console.log("\n=== Example 3: Direct Model Selection ===");
const result3 = await makeV2DirectModel("Summarize the book of Romans in 3 sentences.");
console.log(`Model used: ${result3.model}`);
console.log(`Response: ${result3.choices[0].message.content.substring(0, 200)}...`);
} catch (error) {
console.error("An error occurred:", error.response ? error.response.data : error.message);
}
}
main();
import axios from 'axios';
import * as dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();
// --- Type Definitions ---
interface V2CompletionResponse {
model: string;
routing_mechanism?: string;
routing_tier?: string;
routing_confidence?: number;
tradition?: string;
choices: Array<{
message: {
role: string;
content: string;
};
}>;
}
// --- Configuration ---
const API_KEY = process.env.GLOO_API_KEY || "YOUR_API_KEY";
const API_URL = "https://platform.ai.gloo.com/ai/v2/chat/completions";
// --- V2 Completion Functions ---
async function makeV2AutoRouting(message: string, tradition: string = "evangelical"): Promise<V2CompletionResponse> {
// Example 1: Auto-routing - Let Gloo AI select the optimal model
const response = await axios.post<V2CompletionResponse>(API_URL, {
messages: [{ role: "user", content: message }],
auto_routing: true,
tradition: tradition
}, {
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
});
return response.data;
}
async function makeV2ModelFamily(message: string, modelFamily: string = "anthropic"): Promise<V2CompletionResponse> {
// Example 2: Model family selection - Choose a provider family
const response = await axios.post<V2CompletionResponse>(API_URL, {
messages: [{ role: "user", content: message }],
model_family: modelFamily
}, {
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
});
return response.data;
}
async function makeV2DirectModel(message: string, model: string = "gloo-anthropic-claude-sonnet-4.5"): Promise<V2CompletionResponse> {
// Example 3: Direct model selection - Specify an exact model
const response = await axios.post<V2CompletionResponse>(API_URL, {
messages: [{ role: "user", content: message }],
model: model,
temperature: 0.7,
max_tokens: 500
}, {
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
});
return response.data;
}
// --- Main Execution ---
async function main(): Promise<void> {
try {
// Example 1: Auto-routing
console.log("=== Example 1: Auto-Routing ===");
const result1 = await makeV2AutoRouting("How does the Old Testament connect to the New Testament?");
console.log(`Model used: ${result1.model}`);
console.log(`Routing: ${result1.routing_mechanism}`);
console.log(`Response: ${result1.choices[0].message.content.substring(0, 200)}...`);
// Example 2: Model family selection
console.log("\n=== Example 2: Model Family Selection ===");
const result2 = await makeV2ModelFamily("Draft a short sermon outline on forgiveness.", "anthropic");
console.log(`Model used: ${result2.model}`);
console.log(`Response: ${result2.choices[0].message.content.substring(0, 200)}...`);
// Example 3: Direct model selection
console.log("\n=== Example 3: Direct Model Selection ===");
const result3 = await makeV2DirectModel("Summarize the book of Romans in 3 sentences.");
console.log(`Model used: ${result3.model}`);
console.log(`Response: ${result3.choices[0].message.content.substring(0, 200)}...`);
} catch (error: any) {
console.error("An error occurred:", error.response ? error.response.data : error.message);
}
}
main();
<?php
require_once 'vendor/autoload.php';
// Load .env file
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
// --- Configuration ---
$API_KEY = $_ENV['GLOO_API_KEY'] ?? getenv('GLOO_API_KEY') ?: 'YOUR_API_KEY';
$API_URL = 'https://platform.ai.gloo.com/ai/v2/chat/completions';
// --- V2 Completion Functions ---
function makeV2AutoRouting($message, $tradition, $apiUrl) {
global $API_KEY;
// Example 1: Auto-routing - Let Gloo AI select the optimal model
$payload = json_encode([
'messages' => [['role' => 'user', 'content' => $message]],
'auto_routing' => true,
'tradition' => $tradition
]);
return makeRequest($apiUrl, $payload, $API_KEY);
}
function makeV2ModelFamily($message, $modelFamily, $apiUrl) {
global $API_KEY;
// Example 2: Model family selection - Choose a provider family
$payload = json_encode([
'messages' => [['role' => 'user', 'content' => $message]],
'model_family' => $modelFamily
]);
return makeRequest($apiUrl, $payload, $API_KEY);
}
function makeV2DirectModel($message, $model, $apiUrl) {
global $API_KEY;
// Example 3: Direct model selection - Specify an exact model
$payload = json_encode([
'messages' => [['role' => 'user', 'content' => $message]],
'model' => $model,
'temperature' => 0.7,
'max_tokens' => 500
]);
return makeRequest($apiUrl, $payload, $API_KEY);
}
function makeRequest($apiUrl, $payload, $apiKey) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
]);
$result = curl_exec($ch);
if (curl_errno($ch)) throw new Exception(curl_error($ch));
curl_close($ch);
return json_decode($result, true);
}
// --- Main Execution ---
try {
// Example 1: Auto-routing
echo "=== Example 1: Auto-Routing ===\n";
$result1 = makeV2AutoRouting(
"How does the Old Testament connect to the New Testament?",
"evangelical",
$API_URL
);
echo "Model used: " . $result1['model'] . "\n";
echo "Routing: " . $result1['routing_mechanism'] . "\n";
echo "Response: " . substr($result1['choices'][0]['message']['content'], 0, 200) . "...\n";
// Example 2: Model family selection
echo "\n=== Example 2: Model Family Selection ===\n";
$result2 = makeV2ModelFamily(
"Draft a short sermon outline on forgiveness.",
"anthropic",
$API_URL
);
echo "Model used: " . $result2['model'] . "\n";
echo "Response: " . substr($result2['choices'][0]['message']['content'], 0, 200) . "...\n";
// Example 3: Direct model selection
echo "\n=== Example 3: Direct Model Selection ===\n";
$result3 = makeV2DirectModel(
"Summarize the book of Romans in 3 sentences.",
"gloo-anthropic-claude-sonnet-4.5",
$API_URL
);
echo "Model used: " . $result3['model'] . "\n";
echo "Response: " . substr($result3['choices'][0]['message']['content'], 0, 200) . "...\n";
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
// --- Configuration ---
var (
apiKey = getEnv("GLOO_API_KEY", "YOUR_API_KEY")
apiURL = "https://platform.ai.gloo.com/ai/v2/chat/completions"
)
// --- V2 Completion Functions ---
func makeV2AutoRouting(message, tradition string) (map[string]interface{}, error) {
// Example 1: Auto-routing - Let Gloo AI select the optimal model
payload := map[string]interface{}{
"messages": []map[string]string{{"role": "user", "content": message}},
"auto_routing": true,
"tradition": tradition,
}
return makeRequest(payload)
}
func makeV2ModelFamily(message, modelFamily string) (map[string]interface{}, error) {
// Example 2: Model family selection - Choose a provider family
payload := map[string]interface{}{
"messages": []map[string]string{{"role": "user", "content": message}},
"model_family": modelFamily,
}
return makeRequest(payload)
}
func makeV2DirectModel(message, model string) (map[string]interface{}, error) {
// Example 3: Direct model selection - Specify an exact model
payload := map[string]interface{}{
"messages": []map[string]string{{"role": "user", "content": message}},
"model": model,
"temperature": 0.7,
"max_tokens": 500,
}
return makeRequest(payload)
}
func makeRequest(payload map[string]interface{}) (map[string]interface{}, error) {
jsonPayload, _ := json.Marshal(payload)
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+apiKey)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API call failed: %s - %s", resp.Status, string(body))
}
var result map[string]interface{}
json.Unmarshal(body, &result)
return result, nil
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func getResponseContent(result map[string]interface{}) string {
if choices, ok := result["choices"].([]interface{}); ok && len(choices) > 0 {
if choice, ok := choices[0].(map[string]interface{}); ok {
if message, ok := choice["message"].(map[string]interface{}); ok {
if content, ok := message["content"].(string); ok {
if len(content) > 200 {
return content[:200] + "..."
}
return content
}
}
}
}
return ""
}
// --- Main Execution ---
func main() {
// Example 1: Auto-routing
fmt.Println("=== Example 1: Auto-Routing ===")
result1, err := makeV2AutoRouting("How does the Old Testament connect to the New Testament?", "evangelical")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Model used: %v\n", result1["model"])
fmt.Printf("Routing: %v\n", result1["routing_mechanism"])
fmt.Printf("Response: %s\n", getResponseContent(result1))
// Example 2: Model family selection
fmt.Println("\n=== Example 2: Model Family Selection ===")
result2, err := makeV2ModelFamily("Draft a short sermon outline on forgiveness.", "anthropic")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Model used: %v\n", result2["model"])
fmt.Printf("Response: %s\n", getResponseContent(result2))
// Example 3: Direct model selection
fmt.Println("\n=== Example 3: Direct Model Selection ===")
result3, err := makeV2DirectModel("Summarize the book of Romans in 3 sentences.", "gloo-anthropic-claude-sonnet-4.5")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Model used: %v\n", result3["model"])
fmt.Printf("Response: %s\n", getResponseContent(result3))
}
import com.google.gson.Gson;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GlooV2Example {
// --- Configuration ---
private static final String API_KEY = System.getenv().getOrDefault("GLOO_API_KEY", "YOUR_API_KEY");
private static final String API_URL = "https://platform.ai.gloo.com/ai/v2/chat/completions";
private final HttpClient httpClient = HttpClient.newHttpClient();
private final Gson gson = new Gson();
// --- V2 Completion Functions ---
public Map<String, Object> makeV2AutoRouting(String message, String tradition) throws IOException, InterruptedException {
// Example 1: Auto-routing - Let Gloo AI select the optimal model
Map<String, Object> payload = new HashMap<>();
Map<String, String> messageEntry = new HashMap<>();
messageEntry.put("role", "user");
messageEntry.put("content", message);
payload.put("messages", List.of(messageEntry));
payload.put("auto_routing", true);
payload.put("tradition", tradition);
return makeRequest(gson.toJson(payload));
}
public Map<String, Object> makeV2ModelFamily(String message, String modelFamily) throws IOException, InterruptedException {
// Example 2: Model family selection - Choose a provider family
Map<String, Object> payload = new HashMap<>();
Map<String, String> messageEntry = new HashMap<>();
messageEntry.put("role", "user");
messageEntry.put("content", message);
payload.put("messages", List.of(messageEntry));
payload.put("model_family", modelFamily);
return makeRequest(gson.toJson(payload));
}
public Map<String, Object> makeV2DirectModel(String message, String model) throws IOException, InterruptedException {
// Example 3: Direct model selection - Specify an exact model
Map<String, Object> payload = new HashMap<>();
Map<String, String> messageEntry = new HashMap<>();
messageEntry.put("role", "user");
messageEntry.put("content", message);
payload.put("messages", List.of(messageEntry));
payload.put("model", model);
payload.put("temperature", 0.7);
payload.put("max_tokens", 500);
return makeRequest(gson.toJson(payload));
}
private Map<String, Object> makeRequest(String payload) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_URL))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + API_KEY)
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) throw new IOException("API call failed: " + response.body());
return gson.fromJson(response.body(), Map.class);
}
private String getResponseContent(Map<String, Object> result) {
List<Map<String, Object>> choices = (List<Map<String, Object>>) result.get("choices");
Map<String, Object> message = (Map<String, Object>) choices.get(0).get("message");
String content = (String) message.get("content");
return content.length() > 200 ? content.substring(0, 200) + "..." : content;
}
// --- Main Execution ---
public static void main(String[] args) {
GlooV2Example client = new GlooV2Example();
try {
// Example 1: Auto-routing
System.out.println("=== Example 1: Auto-Routing ===");
Map<String, Object> result1 = client.makeV2AutoRouting(
"How does the Old Testament connect to the New Testament?", "evangelical");
System.out.println("Model used: " + result1.get("model"));
System.out.println("Routing: " + result1.get("routing_mechanism"));
System.out.println("Response: " + client.getResponseContent(result1));
// Example 2: Model family selection
System.out.println("\n=== Example 2: Model Family Selection ===");
Map<String, Object> result2 = client.makeV2ModelFamily(
"Draft a short sermon outline on forgiveness.", "anthropic");
System.out.println("Model used: " + result2.get("model"));
System.out.println("Response: " + client.getResponseContent(result2));
// Example 3: Direct model selection
System.out.println("\n=== Example 3: Direct Model Selection ===");
Map<String, Object> result3 = client.makeV2DirectModel(
"Summarize the book of Romans in 3 sentences.", "gloo-anthropic-claude-sonnet-4.5");
System.out.println("Model used: " + result3.get("model"));
System.out.println("Response: " + client.getResponseContent(result3));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Working Code Sample
View Complete Code
Clone or browse the complete working examples for all 6 languages (JavaScript, TypeScript, Python, PHP, Go, Java) with setup instructions.
Next Steps
Now that you understand the Completions V2 API, explore:- Completions V2 Guide - Full API documentation
- Supported Model IDs - All available models
- Tool Use - Function calling with completions
- Chat Tutorial - Stateful chat interactions

