Galileo doesn’t have a Java client for Observe yet. Below is an example of how you could use our APIs to log your records with Java.

Writing / Logging Data to Galileo Observe

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Main {
    public static void main(String[] args) {
        String accessToken = "your_access_token";  // Replace with your actual access token
        String baseUrl = "https://your_base_url";  // Replace with your actual base URL
        String projectId = "your_project_id";      // Replace with your actual project ID

        // Generate a UUID for node_id
        String nodeId = UUID.randomUUID().toString();

        // Construct the records map
        Map<String, Object> record = new HashMap\<>();
        record.put("node_id", nodeId);
        record.put("chain_id", "your_chain_id");           // Replace with the actual chain_id
        record.put("chain_root_id", "your_chain_root_id"); // Replace with the actual chain_root_id
        record.put("latency_ms", 894);
        record.put("status_code", 200);
        record.put("input_text", "This is a prompt.");

        // Example retriever output
        Map<String, Object> retrieverOutput1 = new HashMap\<>();
        retrieverOutput1.put("page_content", "chunk 1 content");
        retrieverOutput1.put("metadata", Map.of("key", "value"));

        Map<String, Object> retrieverOutput2 = new HashMap\<>();
        retrieverOutput2.put("page_content", "chunk 2 content");
        retrieverOutput2.put("metadata", Map.of("key", "value"));

        Map<String, Object>[] retrieverOutput = new Map[]{retrieverOutput1, retrieverOutput2};

        // Serialize the retriever output
        Gson gson = new GsonBuilder().create();
        String serializedRetrieverOutput = gson.toJson(retrieverOutput);
        record.put("output_text", serializedRetrieverOutput);

        record.put("node_type", "retriever");
        record.put("model", "gpt-3.5-turbo");
        record.put("num_input_tokens", 7);
        record.put("num_output_tokens", 8);
        record.put("output_logprobs", new HashMap\<>());  // Empty map for optional logprobs
        record.put("created_at", "2023-08-07T15:14:30.519922");  // Timestamp in the given format

        Map<String, Object> records = new HashMap\<>();
        records.put("records", new Map[]{record});

        try {
            // Set up the connection
            URL url = new URL(baseUrl + "/projects/" + projectId + "/observe/ingest");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Authorization", "Bearer " + accessToken);
            conn.setDoOutput(true);

            // Write the JSON payload to the output stream
            String jsonPayload = gson.toJson(records);
            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = jsonPayload.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // Handle the response
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println("Request was successful.");
            } else {
                System.out.println("Request failed with HTTP error code : " + responseCode);
            }

            conn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Reading Data from Galileo Observe

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class Main {
    public static void main(String[] args) {
        String accessToken = "your_access_token";
        String baseUrl = "https://your_galileo_base_url";
        String projectName = "your_project_name";

        try {
            URL url = new URL(baseUrl + "/projects?project_name=" + projectName);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Authorization", "Bearer " + accessToken);

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            StringBuilder response = new StringBuilder();
            String output;
            while ((output = br.readLine()) != null) {
                response.append(output);
            }

            String jsonResponse = response.toString();

            conn.disconnect();

            System.out.println("Project ID: " + projectId);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Was this page helpful?