Don't have an api key? get one here!
API Usage
Scroll through the examples or use the documentation links on the left to jump to a section.
Python
Getting started
Install the OpenAI Python module.
pip install openaiOpenAI API
Create a chat completion with the Kronos Labs OpenAI-compatible endpoint.
from openai import OpenAI
client = OpenAI(
base_url="https://www.kronoslabs.org/v1",
api_key="YOUR_KRONOS_LABS_API_KEY",
)
completion = client.chat.completions.create(
model="Hyperion",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a short product brief."},
],
)
print(completion.choices[0].message.content)Tool calling
Let the model request a function call, run your local function, then send the result back.
import json
from openai import OpenAI
client = OpenAI(
base_url="https://www.kronoslabs.org/v1",
api_key="YOUR_KRONOS_LABS_API_KEY",
)
def get_delivery_estimate(order_id: str) -> str:
return "Order " + order_id + " is expected to arrive tomorrow."
tools = [
{
"type": "function",
"function": {
"name": "get_delivery_estimate",
"description": "Get the delivery estimate for an order.",
"parameters": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "The order identifier.",
}
},
"required": ["order_id"],
"additionalProperties": False,
},
},
}
]
messages = [
{"role": "user", "content": "When will order KL-1024 arrive?"},
]
response = client.chat.completions.create(
model="Hyperion",
messages=messages,
tools=tools,
)
message = response.choices[0].message
messages.append(message.model_dump(exclude_none=True))
for tool_call in message.tool_calls or []:
if tool_call.function.name == "get_delivery_estimate":
args = json.loads(tool_call.function.arguments)
result = get_delivery_estimate(args["order_id"])
messages.append(
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": result,
}
)
final_response = client.chat.completions.create(
model="Hyperion",
messages=messages,
)
print(final_response.choices[0].message.content)Streaming
Stream tokens as they are generated.
from openai import OpenAI
client = OpenAI(
base_url="https://www.kronoslabs.org/v1",
api_key="YOUR_KRONOS_LABS_API_KEY",
)
stream = client.chat.completions.create(
model="Hyperion",
messages=[{"role": "user", "content": "Explain transformers briefly."}],
stream=True,
)
for event in stream:
token = event.choices[0].delta.content
if token:
print(token, end="")JavaScript
Getting started
Install the OpenAI JavaScript package.
npm install openaiOpenAI API
Create a chat completion with the Kronos Labs OpenAI-compatible endpoint.
import OpenAI from "openai"
const client = new OpenAI({
baseURL: "https://www.kronoslabs.org/v1",
apiKey: process.env.KRONOS_LABS_API_KEY,
})
const completion = await client.chat.completions.create({
model: "Hyperion",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Write a short product brief." },
],
})
console.log(completion.choices[0].message.content)Tool calling
Let the model request a function call, run your local function, then send the result back.
import OpenAI from "openai"
const client = new OpenAI({
baseURL: "https://www.kronoslabs.org/v1",
apiKey: process.env.KRONOS_LABS_API_KEY,
})
function getDeliveryEstimate(orderId) {
return `Order ${orderId} is expected to arrive tomorrow.`
}
const tools = [
{
type: "function",
function: {
name: "get_delivery_estimate",
description: "Get the delivery estimate for an order.",
parameters: {
type: "object",
properties: {
order_id: {
type: "string",
description: "The order identifier.",
},
},
required: ["order_id"],
additionalProperties: false,
},
},
},
]
const messages = [
{ role: "user", content: "When will order KL-1024 arrive?" },
]
const response = await client.chat.completions.create({
model: "Hyperion",
messages,
tools,
})
const message = response.choices[0].message
messages.push(message)
for (const toolCall of message.tool_calls ?? []) {
if (toolCall.function.name === "get_delivery_estimate") {
const args = JSON.parse(toolCall.function.arguments)
messages.push({
role: "tool",
tool_call_id: toolCall.id,
content: getDeliveryEstimate(args.order_id),
})
}
}
const finalResponse = await client.chat.completions.create({
model: "Hyperion",
messages,
})
console.log(finalResponse.choices[0].message.content)Streaming
Stream tokens as they are generated.
import OpenAI from "openai"
const client = new OpenAI({
baseURL: "https://www.kronoslabs.org/v1",
apiKey: process.env.KRONOS_LABS_API_KEY,
})
const stream = await client.chat.completions.create({
model: "Hyperion",
messages: [{ role: "user", content: "Explain transformers briefly." }],
stream: true,
})
for await (const event of stream) {
const token = event.choices[0]?.delta?.content
if (token) process.stdout.write(token)
}