Observe
GoLang
Galileo doesn’t have a Go client for Observe yet. Below is an example of how you could use our APIs to log your records with Go.
Write Data
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/google/uuid"
)
type RetrieverOutput struct {
PageContent string `json:"page_content"`
Metadata map[string]string `json:"metadata"`
}
type Record struct {
NodeID string `json:"node_id"`
ChainID string `json:"chain_id"`
ChainRootID string `json:"chain_root_id"`
LatencyMS int `json:"latency_ms"`
StatusCode int `json:"status_code"`
InputText string `json:"input_text"`
OutputText string `json:"output_text"`
NodeType string `json:"node_type"`
Model string `json:"model"`
NumInputTokens int `json:"num_input_tokens"`
NumOutputTokens int `json:"num_output_tokens"`
OutputLogProbs map[string]interface{} `json:"output_logprobs,omitempty"`
CreatedAt string `json:"created_at"`
}
type RequestBody struct {
Records []Record `json:"records"`
}
func main() {
// Replace these with actual values
accessToken := "your_access_token"
baseURL := "https://your_base_url"
projectID := "your_project_id"
chainID := "parent_node_id"
chainRootID := "chain_node_id"
// Generate a new UUID for node_id
nodeID := uuid.New().String()
// Prepare the retriever output
retrieverOutput := []RetrieverOutput{
{
PageContent: "chunk 1 content",
Metadata: map[string]string{"key": "value"},
},
{
PageContent: "chunk 2 content",
Metadata: map[string]string{"key": "value"},
},
}
// Serialize the retriever output
outputText, err := json.Marshal(retrieverOutput)
if err != nil {
log.Fatalf("Failed to serialize retriever output: %v", err)
}
// Create the record
record := Record{
NodeID: nodeID,
ChainID: chainID,
ChainRootID: chainRootID,
LatencyMS: 894,
StatusCode: 200,
InputText: "This is a prompt.",
OutputText: string(outputText),
NodeType: "retriever",
Model: "gpt-3.5-turbo",
NumInputTokens: 7,
NumOutputTokens: 8,
OutputLogProbs: make(map[string]interface{}), // Optional
CreatedAt: time.Now().Format("2006-01-02T15:04:05.999999"),
}
// Create the request body
requestBody := RequestBody{
Records: []Record{record},
}
// Serialize the request body
jsonBody, err := json.Marshal(requestBody)
if err != nil {
log.Fatalf("Failed to serialize request body: %v", err)
}
// Set up the request
url := fmt.Sprintf("%s/projects/%s/observe/ingest", baseURL, projectID)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
if err != nil {
log.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+accessToken)
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Failed to send request: %v", err)
}
defer resp.Body.Close()
// Check the response
if resp.StatusCode != http.StatusOK {
log.Fatalf("POST request failed with status code: %d", resp.StatusCode)
}
fmt.Println("Request was successful")
}
Read/GET Data
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
)
func main() {
accessToken := "your_access_token" // Replace with your actual access token
baseURL := "https://your_base_url" // Replace with your actual base URL
projectName := "YOUR_PROJECT_NAME" // Replace with your actual project name
// Create the request URL with query parameters
requestURL, err := url.Parse(fmt.Sprintf("%s/projects", baseURL))
if err != nil {
log.Fatalf("Failed to parse URL: %v", err)
}
query := requestURL.Query()
query.Add("project_name", projectName)
requestURL.RawQuery = query.Encode()
// Create the HTTP request
req, err := http.NewRequest("GET", requestURL.String(), nil)
if err != nil {
log.Fatalf("Failed to create request: %v", err)
}
// Set the headers
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+accessToken)
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Failed to send request: %v", err)
}
defer resp.Body.Close()
// Check if the response was successful
if resp.StatusCode != http.StatusOK {
log.Fatalf("Request failed with status code: %d", resp.StatusCode)
}
// Parse the response
var projects []map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&projects); err != nil {
log.Fatalf("Failed to parse response: %v", err)
}
// Extract the project ID from the first project in the response
if len(projects) > 0 {
projectID := projects[0]["id"].(string)
fmt.Println("Project ID:", projectID)
} else {
log.Println("No projects found with the specified name.")
}
}
Was this page helpful?