{idToken}
import requests
url = "https://app.keytrustee.org/"
headers = {
"Authorization": f"Bearer {idToken}",
"X-Auth-Token": authToken,
"X-KeyTrustee-Application-Id": 123, # Replace with your actual application ID
"Content-Type": "application/json"
}
data = {
"messages": [{"role": "user", "content": inputText}],
"model": "gpt-4o-mini-2024-07-18",
"stream": False
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
const response = await fetch("https://app.keytrustee.org/api/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${idToken}`,
"X-Auth-Token": authToken,
"X-KeyTrustee-Application-Id": 2, // Replace with your actual application ID
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: inputText }],
model: "gpt-4o-mini-2024-07-18",
stream: false,
}),
});
const result = await response.json();
console.log(result);
import requests
from time import sleep
url = "https://app.keytrustee.org/api/completions"
headers = {
"Authorization": f"Bearer {idToken}",
"X-Auth-Token": authToken,
"X-KeyTrustee-Application-Id": 123, # Replace with your actual application ID
"Content-Type": "application/json"
}
data = {
"messages": [{"role": "user", "content": inputText}],
"model": "gpt-4o-mini-2024-07-18",
"stream": True
}
with requests.post(url, headers=headers, json=data, stream=True) as response:
if response.status_code != 200:
raise Exception(f"HTTP error! status: {response.status_code}")
buffer = ""
for chunk in response.iter_content(chunk_size=None):
if chunk:
buffer += chunk.decode('utf-8')
while "}{" in buffer:
json_str, buffer = buffer.split("}{", 1)
json_str += "}"
try:
chunk_data = json.loads(json_str)
if "choices" in chunk_data:
content = chunk_data['choices'][0]['delta'].get('content', '')
print(content, end='', flush=True)
except Exception as e:
print(f"Error parsing JSON: {e}")
if buffer:
chunk_data = json.loads(buffer)
content = chunk_data['choices'][0]['delta'].get('content', '')
print(content)
async function startChatStreaming() {
const inputText = document.getElementById("inputText").value;
const outputText = document.getElementById("outputText");
const idToken = localStorage.getItem("idToken");
const authToken = localStorage.getItem("authToken");
const response = await fetch("https://app.keytrustee.org/api/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${idToken}`,
"X-Auth-Token": authToken,
"X-KeyTrustee-Application-Id": "123", // Replace with your actual application ID
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: inputText }],
model: "gpt-4o-mini-2024-07-18",
stream: true,
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8");
let buffer = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
let boundary;
while ((boundary = buffer.indexOf("}{")) !== -1) {
const jsonString = buffer.substring(0, boundary + 1).trim();
buffer = buffer.slice(boundary + 1);
try {
const chunk = JSON.parse(jsonString);
if (chunk.choices && chunk.choices[0] && chunk.choices[0].delta.content) {
outputText.textContent += chunk.choices[0].delta.content;
}
} catch (e) {
console.error("Error parsing JSON:", e);
}
}
}
if (buffer.length > 0) {
try {
const chunk = JSON.parse(buffer.trim());
if (chunk.choices[0].delta.content) {
outputText.textContent += chunk.choices[0].delta.content;
}
} catch (e) {
console.error("Error parsing JSON:", e);
}
}
}