⌘K
Resources
Code Examples
Ready-to-use code examples in multiple programming languages to help you get started quickly with the Lumyx AI API.
Python
Basic Chat Completion
Python
import requests
import json
API_KEY = "nova_your_api_key_here"
BASE_URL = "https://api.lumyx-ai.site/v1"
def chat(message):
response = requests.post(
f"{BASE_URL}/chat/completions",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"model": "nova_1_1",
"messages": [{"role": "user", "content": message}]
}
)
data = response.json()
return data["choices"][0]["message"]["content"]
# Usage
reply = chat("What is the capital of France?")
print(reply)
Streaming Response
Python
import requests
def chat_stream(message):
response = requests.post(
f"{BASE_URL}/chat/completions",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"model": "nova_1_1",
"messages": [{"role": "user", "content": message}],
"stream": True
},
stream=True
)
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: ') and line != 'data: [DONE]':
data = json.loads(line[6:])
if 'choices' in data and data['choices']:
delta = data['choices'][0].get('delta', {})
content = delta.get('content', '')
if content:
print(content, end='', flush=True)
print()
# Usage
chat_stream("Tell me a short story")
Multi-turn Conversation
Python
class Conversation:
def __init__(self, model="nova_1_1"):
self.model = model
self.messages = []
def add_system_message(self, content):
self.messages.append({"role": "system", "content": content})
def chat(self, message):
self.messages.append({"role": "user", "content": message})
response = requests.post(
f"{BASE_URL}/chat/completions",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"model": self.model,
"messages": self.messages
}
)
data = response.json()
assistant_message = data["choices"][0]["message"]["content"]
self.messages.append({"role": "assistant", "content": assistant_message})
return assistant_message
# Usage
conv = Conversation()
conv.add_system_message("You are a helpful coding assistant.")
print(conv.chat("How do I read a file in Python?"))
print(conv.chat("What about writing to it?"))
JavaScript / Node.js
Basic Chat Completion (Fetch)
JavaScript
const API_KEY = 'nova_your_api_key_here';
const BASE_URL = 'https://api.lumyx-ai.site/v1';
async function chat(message) {
const response = await fetch(`${BASE_URL}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'nova_1_1',
messages: [{ role: 'user', content: message }]
})
});
const data = await response.json();
return data.choices[0].message.content;
}
// Usage
chat('What is the capital of France?')
.then(reply => console.log(reply));
Streaming with EventSource
JavaScript
async function chatStream(message, onToken) {
const response = await fetch(`${BASE_URL}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'nova_1_1',
messages: [{ role: 'user', content: message }],
stream: true
})
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ') && line !== 'data: [DONE]') {
const data = JSON.parse(line.slice(6));
const content = data.choices?.[0]?.delta?.content || '';
if (content) onToken(content);
}
}
}
}
// Usage
chatStream('Tell me a joke', token => process.stdout.write(token));
cURL
Basic Request
cURL
curl -X POST https://api.lumyx-ai.site/v1/chat/completions \
-H "Authorization: Bearer nova_your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"model": "nova_1_1",
"messages": [
{"role": "user", "content": "Hello, how are you?"}
]
}'
With System Message
cURL
curl -X POST https://api.lumyx-ai.site/v1/chat/completions \
-H "Authorization: Bearer nova_your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"model": "nova_1_1",
"messages": [
{"role": "system", "content": "You are a helpful assistant that speaks like a pirate."},
{"role": "user", "content": "What is the weather like today?"}
],
"temperature": 0.8
}'
Agents Chat
cURL
curl -X POST https://api.lumyx-ai.site/v1/agents/chat/completions \
-H "Authorization: Bearer nova_your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"persona_id": 1,
"messages": [
{"role": "user", "content": "Help me debug this Python code"}
],
"model": "nova_1_1"
}'
Using with OpenAI SDK
Since Lumyx AI is OpenAI-compatible, you can use the official OpenAI SDK:
Python (OpenAI SDK)
Python
from openai import OpenAI
client = OpenAI(
api_key="nova_your_key",
base_url="https://api.lumyx-ai.site/v1"
)
response = client.chat.completions.create(
model="nova_1_1",
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(response.choices[0].message.content)
Node.js (OpenAI SDK)
JavaScript
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'nova_your_key',
baseURL: 'https://api.lumyx-ai.site/v1'
});
const response = await client.chat.completions.create({
model: 'nova_1_1',
messages: [
{ role: 'user', content: 'Hello!' }
]
});
console.log(response.choices[0].message.content);
API Playground
Try these examples interactively in the API Playground.