Galileo doesn’t have a Go client for Evaluate yet. Below is an example of how you could use our APIs to log your Evaluate Runs with Go.
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/google/uuid"
)

const (
	ROOT_URL      = "YOUR_GALILEO_URL"
	GALILEO_API_KEY = "YOUR_API_KEY"
)

type Node struct {
	NodeID             string `json:"node_id"`
	NodeType           string `json:"node_type"`
	NodeName           string `json:"node_name"`
	NodeInput          string `json:"node_input"`
	NodeOutput         string `json:"node_output"`
	ChainRootID        string `json:"chain_root_id"`
	Step               int    `json:"step"`
	HasChildren        bool   `json:"has_children"`
	CreationTimestamp  int64  `json:"creation_timestamp"`
	Latency            int    `json:"latency"`
	QueryInputTokens   int    `json:"query_input_tokens"`
	QueryOutputTokens  int    `json:"query_output_tokens"`
	QueryTotalTokens   int    `json:"query_total_tokens"`
}

type PromptScorersConfiguration struct {
	Latency                              bool `json:"latency"`
	Cost                                 bool `json:"cost"`
	Pii                                  bool `json:"pii"`
	InputPii                             bool `json:"input_pii"`
	Bleu                                 bool `json:"bleu"`
	Rouge                                bool `json:"rouge"`
	ProtectStatus                        bool `json:"protect_status"`
	ContextRelevance                     bool `json:"context_relevance"`
	Toxicity                             bool `json:"toxicity"`
	InputToxicity                        bool `json:"input_toxicity"`
	Tone                                 bool `json:"tone"`
	InputTone                            bool `json:"input_tone"`
	Sexist                               bool `json:"sexist"`
	InputSexist                          bool `json:"input_sexist"`
	PromptInjection                      bool `json:"prompt_injection"`
	AdherenceNli                         bool `json:"adherence_nli"`
	ChunkAttributionUtilizationNli       bool `json:"chunk_attribution_utilization_nli"`
	CompletenessNli                      bool `json:"completeness_nli"`
	Uncertainty                          bool `json:"uncertainty"`
	Factuality                           bool `json:"factuality"`
	Groundedness                         bool `json:"groundedness"`
	PromptPerplexity                     bool `json:"prompt_perplexity"`
	ChunkAttributionUtilizationGpt       bool `json:"chunk_attribution_utilization_gpt"`
	CompletenessGpt                      bool `json:"completeness_gpt"`
}

type CustomLogRequest struct {
	Rows                      []Node                   `json:"rows"`
	PromptScorersConfiguration PromptScorersConfiguration `json:"prompt_scorers_configuration"`
}

func login() error {
	loginURL := fmt.Sprintf("%s/login/api_key", ROOT_URL)
	payload := map[string]string{"api_key": GALILEO_API_KEY}
	jsonPayload, _ := json.Marshal(payload)

	req, err := http.NewRequest("POST", loginURL, bytes.NewBuffer(jsonPayload))
	if err != nil {
		return fmt.Errorf("failed to create request: %v", err)
	}

	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return fmt.Errorf("failed to send request: %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("login request failed with status: %d", resp.StatusCode)
	}

	var loginResponse map[string]interface{}
	json.NewDecoder(resp.Body).Decode(&loginResponse)
	fmt.Println("Login Response:", loginResponse)
	return nil
}

func createProject() (map[string]interface{}, error) {
	projectsURL := fmt.Sprintf("%s/projects", ROOT_URL)
	payload := map[string]interface{}{
		"name":      fmt.Sprintf("project__%s", generateUUID()),
		"is_private": false,
		"type":      "prompt_evaluation",
	}
	jsonPayload, _ := json.Marshal(payload)

	req, err := http.NewRequest("POST", projectsURL, bytes.NewBuffer(jsonPayload))
	if err != nil {
		return nil, fmt.Errorf("failed to create request: %v", err)
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Galileo-Api-Key", GALILEO_API_KEY)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("failed to send request: %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("create project request failed with status: %d", resp.StatusCode)
	}

	var createProjectResponse map[string]interface{}
	json.NewDecoder(resp.Body).Decode(&createProjectResponse)
	fmt.Println("Project Created:", createProjectResponse)
	return createProjectResponse, nil
}

func createRun(projectId string, runName string) (map[string]interface{}, error) {
	createRunURL := fmt.Sprintf("%s/projects/%s/runs", ROOT_URL, projectId)
	payload := map[string]string{
		"name":      runName,
		"task_type": "prompt_chain",
	}
	jsonPayload, _ := json.Marshal(payload)

	req, err := http.NewRequest("POST", createRunURL, bytes.NewBuffer(jsonPayload))
	if err != nil {
		return nil, fmt.Errorf("failed to create request: %v", err)
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Galileo-Api-Key", GALILEO_API_KEY)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("failed to send request: %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("create run request failed with status: %d", resp.StatusCode)
	}

	var createRunResponse map[string]interface{}
	json.NewDecoder(resp.Body).Decode(&createRunResponse)
	fmt.Println("Run Created:", createRunResponse)
	return createRunResponse, nil
}

func customLog(projectId string, runId string) error {
	customLogURL := fmt.Sprintf("%s/projects/%s/runs/%s/chains/ingest", ROOT_URL, projectId, runId)

	node := Node{
		NodeID:            "6c735b85-62be-4d63-928c-c5ea45690a25",
		NodeType:          "llm",
		NodeName:          "LLM",
		NodeInput:         "Tell me a joke about bears!",
		NodeOutput:        "Here is one: Why did the bear go to the doctor? Because it had a grizzly cough!",
		ChainRootID:       "6c735b85-62be-4d63-928c-c5ea45690a25",
		Step:              0,
		HasChildren:       false,
		CreationTimestamp: time.Now().Unix(),
		Latency:           0,
		QueryInputTokens:  0,
		QueryOutputTokens: 0,
		QueryTotalTokens:  0,
	}

	promptScorersConfiguration := PromptScorersConfiguration{
		Latency: false,
		Cost: false,
		Pii: false,
		InputPii: false,
		Bleu: false,
		Rouge: false,
		ProtectStatus: false,
		ContextRelevance: false,
		Toxicity: false,
		InputToxicity: false,
		Tone: false,
		InputTone: false,
		Sexist: false,
		InputSexist: false,
		PromptInjection: false,
		AdherenceNli: false,
		ChunkAttributionUtilizationNli: false,
		CompletenessNli: false,
		Uncertainty: false,
		Factuality: true,
		Groundedness: true,
		PromptPerplexity: false,
		ChunkAttributionUtilizationGpt: false,
		CompletenessGpt: false,
	}

	requestBody := CustomLogRequest{
		Rows: []Node{node},
		PromptScorersConfiguration: promptScorersConfiguration,
	}

	jsonPayload, _ := json.Marshal(requestBody)

	req, err := http.NewRequest("POST", customLogURL, bytes.NewBuffer(jsonPayload))
	if err != nil {
		return fmt.Errorf("failed to create request: %v", err)
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Galileo-Api-Key", GALILEO_API_KEY)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return fmt.Errorf("failed to send request: %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("custom log request failed with status: %d", resp.StatusCode)
	}

	fmt.Println("Custom log request was successful")
	return nil
}

func generateUUID() string {
	return uuid.New().String()
}

func main() {
	fmt.Println("Creating Project")
	createProjectResponse, err := createProject()
	if err != nil {
		log.Fatalf("Failed to create project: %v", err)
	}

	runName := generateUUID()
	fmt.Println("Creating Run")
	runResponse, err := createRun(createProjectResponse["id"].(string), runName)
	if err != nil {
		log.Fatalf("Failed to create run: %v", err)
	}

	fmt.Println("Logging data to Galileo")
	if err := customLog(createProjectResponse["id"].(string), runResponse["id"].(string)); err != nil {
		log.Fatalf("Failed to log data: %v", err)
	}
}

Was this page helpful?