const API_KEY = "my_workspace_api_key";
const PROJECT_ID = "my_project_id";
const PROMPT_ID = "my_prompt_id";
const BASE_URL = "https://api.adaline.ai/v2/logs";
async function createLogTrace(trace, spans = []) {
const response = await fetch(`${BASE_URL}/trace`, {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
projectId: PROJECT_ID,
trace,
spans,
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(`API Error: ${error.error}`);
}
return response.json();
}
async function main() {
try {
const traceResult = await createLogTrace(
{
startedAt: Date.now(),
endedAt: Date.now() + 60000,
name: "user_interaction",
status: "success",
referenceId: "trace_001",
sessionId: "session_123",
attributes: {
"app_name": "my_app",
"user_id": "user_123"
},
tags: ["dev-env"]
},
[
{
promptId: PROMPT_ID,
startedAt: Date.now(),
endedAt: Date.now() + 30000,
name: "llm_call",
status: "success",
content: {
"type": "Model",
"provider": "openai",
"model": "gpt-4o-mini",
"variables": {
"user_input": {
"modality": "text",
"value": "librarian"
}
},
"cost": 0.002,
"input": "{\n \"model\":\"gpt-4o-mini\",\n \"max_tokens\": 50,\n \"messages\": [{\n \"role\": \"system\",\n \"content\": [\n {\n \"type\": \"text\",\n \"text\": \"You are a helpful librarian. You answer in 1-2 lines.\"\n }\n ]\n },\n {\n \"role\": \"user\",\n \"content\": [\n {\n \"type\": \"text\",\n \"text\": \"What is Adaline?\"\n }\n ]\n }\n ]\n}",
"output": "{\n \"id\": \"chatcmpl-BjKjQqVBN0WdTbkFHZ2C6tLXerKvl\",\n \"object\": \"chat.completion\",\n \"created\": 1750144152,\n \"model\": \"gpt-4o-mini-2024-07-18\",\n \"choices\": [\n {\n \"index\": 0,\n \"message\": {\n \"role\": \"assistant\",\n \"content\": \"Adaline, or Adaptive Linear Neuron, is a type of artificial neural network that uses a linear activation function and employs the least mean squares (LMS) algorithm for training, aiming to minimize the difference between predicted and actual outputs.\",\n \"refusal\": null,\n \"annotations\": []\n },\n \"logprobs\": null,\n \"finish_reason\": \"stop\"\n }\n ],\n \"usage\": {\n \"prompt_tokens\": 31,\n \"completion_tokens\": 47,\n \"total_tokens\": 78,\n \"prompt_tokens_details\": {\n \"cached_tokens\": 0,\n \"audio_tokens\": 0\n },\n \"completion_tokens_details\": {\n \"reasoning_tokens\": 0,\n \"audio_tokens\": 0,\n \"accepted_prediction_tokens\": 0,\n \"rejected_prediction_tokens\": 0\n }\n },\n \"service_tier\": \"default\",\n \"system_fingerprint\": \"fp_34a54ae93c\"\n}"
},
referenceId: "span_001",
sessionId: "session_123",
attributes: {
"app_name": "my_app",
"user_id": "user_123"
},
tags: ["dev-env"]
},
]
);
console.log("traceResult:", traceResult);
} catch (error) {
console.error("Error creating trace:", error);
}
}
main();