Docs
LLmHub API Create Chat Completion
LLmHub API Create Chat Completion
Official API documentation for LLmHub API (api.llmhub.dev)
Endpoint
POST https://api.llmhub.dev/v1/chat/completions
Creates a model response for the given chat conversation.
Request Body
Parameter | Type | Required | Description |
---|---|---|---|
model | string | Yes | ID of the model to use. Use "automatic" to enable LLMHub's intelligent routing system. |
messages | array | Yes | A list of messages comprising the conversation so far. |
temperature | number | No | What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. Default: 1.0 |
max_tokens | integer | No | The maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model's context length. If not specified, the default value 4096 is used. |
frequency_penalty | number | No | Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. Default: 0 |
presence_penalty | number | No | Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. Default: 0 |
stream | boolean | No | If set, partial message deltas will be sent. Tokens will be sent as data-only server-sent events (SSE) as they become available, with the stream terminated by a data: [DONE] message. Default: false |
top_p | number | No | An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. Default: 1 |
response_format | object | No | Specifies the format that the model must output. Can be used to ensure the output is in a specific format. |
stop | string or array | No | Up to 4 sequences where the API will stop generating further tokens. |
tool_choice | string or object | No | Controls which (if any) tool is called by the model. |
tools | array | No | A list of tools the model may call. |
logprobs | boolean | No | Whether to return log probabilities of the output tokens or not. |
top_logprobs | integer | No | An integer between 0 and 20 specifying the number of most likely tokens to return at each token position. |
Create Chat Completions
This endpoint allows you to generate chat completions using LLMHUB's smart autorouting system. LLMHUB automatically selects the best model to process your prompt and returns the optimal response.
HTTP Endpoint:
POST https://api.llmhub.dev/v1/chat/completions
Request Body Parameters
- model: (string) The model to use, e.g.
"automatic"
. - messages: (array) A list of message objects. Each object must include:
- role: (string) The role of the message sender (e.g.,
"system"
,"user"
). - content: (string) The message text.
- role: (string) The role of the message sender (e.g.,
- max_tokens: (integer) Maximum number of tokens to generate.
- temperature: (number) Sampling temperature used to control randomness.
- stream: (boolean) Whether to stream the response.
Code Examples
cURL
curl https://api.llmhub.dev/v1/chat/completions \
-H "Authorization: Bearer <your_api_key>" \
-H "Content-Type: application/json" \
-d '{
"model": "automatic",
"messages": [
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"}
],
"max_tokens": 1024,
"temperature": 0.7,
"stream": false
}'
Python (OpenAI SDK)
# Please install OpenAI SDK first: `pip3 install openai`
from openai import OpenAI
client = OpenAI(
base_url="https://api.llmhub.dev/v1",
api_key="<LLMHub API Key>"
)
response = client.chat.completions.create(
model="automatic",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
print(response.choices[0].message.content)
Go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type RequestBody struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
MaxTokens int `json:"max_tokens"`
Temperature float64 `json:"temperature"`
Stream bool `json:"stream"`
}
type Choice struct {
Message Message `json:"message"`
}
type Response struct {
Choices []Choice `json:"choices"`
}
func main() {
url := "https://api.llmhub.dev/v1/chat/completions"
apiKey := "<your_api_key>"
body := RequestBody{
Model: "automatic",
Messages: []Message{
{Role: "system", Content: "You are a helpful assistant"},
{Role: "user", Content: "Hello"},
},
MaxTokens: 1024,
Temperature: 0.7,
Stream: false,
}
jsonBody, _ := json.Marshal(body)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
req.Header.Set("Authorization", "Bearer " + apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
var res Response
json.Unmarshal(data, &res)
if len(res.Choices) > 0 {
fmt.Println(res.Choices[0].Message.Content)
}
}
Node.js
const axios = require('axios');
const data = {
model: "automatic",
messages: [
{ role: "system", content: "You are a helpful assistant" },
{ role: "user", content: "Hello" }
],
max_tokens: 1024,
temperature: 0.7,
stream: false
};
axios.post('https://api.llmhub.dev/v1/chat/completions', data, {
headers: {
'Authorization': `Bearer <your_api_key>`,
'Content-Type': 'application/json'
}
})
.then(response => {
console.log(response.data.choices[0].message.content);
})
.catch(error => {
console.error(error);
});
Ruby
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse("https://api.llmhub.dev/v1/chat/completions")
header = {
"Content-Type": "application/json",
"Authorization": "Bearer <your_api_key>"
}
body = {
model: "automatic",
messages: [
{ role: "system", content: "You are a helpful assistant" },
{ role: "user", content: "Hello" }
],
max_tokens: 1024,
temperature: 0.7,
stream: false
}
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri, header)
request.body = body.to_json
response = http.request(request)
result = JSON.parse(response.body)
puts result["choices"][0]["message"]["content"]
C#
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
class Program {
static async Task Main(string[] args) {
var url = "https://api.llmhub.dev/v1/chat/completions";
var apiKey = "<your_api_key>";
var payload = new {
model = "automatic",
messages = new [] {
new { role = "system", content = "You are a helpful assistant" },
new { role = "user", content = "Hello" }
},
max_tokens = 1024,
temperature = 0.7,
stream = false
};
var jsonPayload = JsonConvert.SerializeObject(payload);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
}
PHP
<?php
$apiKey = "<your_api_key>";
$url = "https://api.llmhub.dev/v1/chat/completions";
$data = [
"model" => "automatic",
"messages" => [
["role" => "system", "content" => "You are a helpful assistant"],
["role" => "user", "content" => "Hello"]
],
"max_tokens" => 1024,
"temperature" => 0.7,
"stream" => false
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $apiKey",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
echo $result['choices'][0]['message']['content'];
?>
Java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONObject;
public class ChatCompletion {
public static void main(String[] args) throws Exception {
String apiKey = "<your_api_key>";
URL url = new URL("https://api.llmhub.dev/v1/chat/completions");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + apiKey);
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
JSONObject payload = new JSONObject();
payload.put("model", "automatic");
JSONArray messages = new JSONArray();
messages.put(new JSONObject().put("role", "system").put("content", "You are a helpful assistant"));
messages.put(new JSONObject().put("role", "user").put("content", "Hello"));
payload.put("messages", messages);
payload.put("max_tokens", 1024);
payload.put("temperature", 0.7);
payload.put("stream", false);
OutputStream os = conn.getOutputStream();
os.write(payload.toString().getBytes("UTF-8"));
os.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONObject jsonResponse = new JSONObject(response.toString());
String result = jsonResponse.getJSONArray("choices")
.getJSONObject(0)
.getJSONObject("message")
.getString("content");
System.out.println(result);
}
}
PowerShell
$apiKey = "<your_api_key>"
$url = "https://api.llmhub.dev/v1/chat/completions"
$body = @{
model = "automatic"
messages = @(
@{ role = "system"; content = "You are a helpful assistant" },
@{ role = "user"; content = "Hello" }
)
max_tokens = 1024
temperature = 0.7
stream = $false
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri $url -Method Post -Headers @{
"Authorization" = "Bearer $apiKey"
"Content-Type" = "application/json"
} -Body $body
$response.choices[0].message.content