### Install go-litellm Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/README.md Install the go-litellm package using the go get command. ```bash go get github.com/andrejsstepanovs/go-litellm@latest ``` -------------------------------- ### YAML Configuration File Example Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md An example YAML file demonstrating how to structure configuration settings for go-litellm, including connection details, target timeouts, and API parameters. This file can be used with Viper for external configuration. ```yaml litellm: url: "http://localhost:4000" targets: system: timeout: 1s retry_interval: 100ms retry_max_attempts: 3 retry_backoff_rate: 2.0 max_retry: 3 llm: timeout: 2m retry_interval: 100ms retry_max_attempts: 3 retry_backoff_rate: 2.0 max_retry: 3 mcp: timeout: 5m retry_interval: 100ms retry_max_attempts: 3 retry_backoff_rate: 2.0 max_retry: 3 api: key: "sk-xxx" temperature: 0.7 ``` -------------------------------- ### Example: Retrieve and Access Model Information Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/embeddings-and-models.md Demonstrates how to fetch a list of models and retrieve specific model details using the Get method. Checks if the model was found before accessing its properties. ```go models, _ := ai.Models(ctx) model, found := models.Get("claude-4") if found { fmt.Println("Owner:", model.OwnedBy) } ``` -------------------------------- ### Example: Load Connection from Viper Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md Demonstrates setting LiteLLM configuration values in Viper and then loading a new Connection object. ```go import "github.com/spf13/viper" // Set configuration viper.Set("litellm.url", "http://localhost:4000") viper.Set("litellm.targets.system.timeout", 1*time.Second) viper.Set("litellm.targets.llm.timeout", 2*time.Minute) viper.Set("litellm.targets.mcp.timeout", 5*time.Minute) // Load connection conn, err := litellm.New() if err != nil { log.Fatal(err) } ``` -------------------------------- ### Example: Get LLM Target Configuration Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md Retrieves the LLM target configuration from the connection's targets and prints its timeout setting. ```go targets := conn.Targets llmTarget := targets.Get(litellm.CLIENT_LLM) fmt.Println(llmTarget.Timeout) // time.Duration ``` -------------------------------- ### Example: Create and Validate Connection Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md Demonstrates how to create a new LiteLLM connection with a base URL and default targets, and then validate its configuration. ```go baseURL := &url.URL{ Scheme: "http", Host: "localhost:4000", } conn := litellm.Connection{ URL: *baseURL, Targets: litellm.NewTargets(), } if err := conn.Validate(); err != nil { log.Fatal(err) } ``` -------------------------------- ### Example: Create and Validate Config Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md Demonstrates how to create a `Config` object and validate its parameters before using it with the LiteLLM client. Handles potential validation errors. ```go cfg := client.Config{ APIKey: "sk-xxx", Temperature: 0.7, } if err := cfg.Validate(); err != nil { log.Fatal(err) } ``` -------------------------------- ### Perform Chat Completion in Go Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/INDEX.md Get a model instance, define user messages, create a completion request, and print the response. Refer to client.md and request.md for detailed setup. ```go // See client.md and request.md model, _ := ai.Model(ctx, "claude-4") messages := request.Messages{ request.UserMessageSimple("What is Go?"), } req := request.NewCompletionRequest(model, messages, nil, nil, 0.7) resp, _ := ai.Completion(ctx, req) fmt.Println(resp.String()) ``` -------------------------------- ### Example: Using Float32 Conversion Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/response.md Demonstrates how to obtain a float32 representation of an embedding vector from an API response. ```go embResponse, _ := ai.Embeddings(ctx, model, "text") float32Vec := embResponse.Data[0].Embedding.Float32() ``` -------------------------------- ### Minimal go-litellm Client Configuration Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md Demonstrates how to initialize the go-litellm client with direct configuration values for the base URL, connection targets, and API settings. This is useful for simple setups or when configuration is managed programmatically. ```go package main import ( "context" "log" "net/url" "time" "github.com/andrejsstepanovs/go-litellm/client" "github.com/andrejsstepanovs/go-litellm/conf/connections/litellm" ) func main() { // Create base URL baseURL, _ := url.Parse("http://localhost:4000") // Configure targets conn := litellm.Connection{ URL: *baseURL, Targets: litellm.Targets{ System: litellm.Target{ Timeout: 1 * time.Second, RetryInterval: 100 * time.Millisecond, RetryMaxAttempts: 3, RetryBackoffRate: 2.0, MaxRetry: 3, }, LLM: litellm.Target{ Timeout: 2 * time.Minute, RetryInterval: 100 * time.Millisecond, RetryMaxAttempts: 3, RetryBackoffRate: 2.0, MaxRetry: 3, }, MCP: litellm.Target{ Timeout: 5 * time.Minute, RetryInterval: 100 * time.Millisecond, RetryMaxAttempts: 3, RetryBackoffRate: 2.0, MaxRetry: 3, }, }, } // Create client config cfg := client.Config{ APIKey: "sk-xxx", Temperature: 0.7, } // Initialize client ai, err := client.New(cfg, conn) if err != nil { log.Fatal(err) } // Use client ctx := context.Background() model, _ := ai.Model(ctx, "claude-4") fmt.Println(model.ModelId) } ``` -------------------------------- ### Build Schema with Array Property Example Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/schemas-and-tokens.md Shows how to define a schema that includes an array of objects, where each object represents an employee with 'name' and 'role' properties. ```go schema, _ := request.NewSchemaBuilder("company"). AddStringProperty("name", "Company name"). AddArrayProperty("employees", "List of employees", request.Property{ Type: request.TypeObject, Properties: map[string]request.Property{ "name": {Type: request.TypeString, Description: "Employee name"}, "role": {Type: request.TypeString, Description: "Job title"}, }, Required: []string{"name"}, }). AddRequired("name"). Build() ``` -------------------------------- ### TextToSpeech Synthesis Example Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/audio.md Shows how to generate audio from text using the TextToSpeech method. Covers basic synthesis, specifying format and speed, and using streaming formats like Server-Sent Events (SSE). Generated audio files are stored in the system temporary directory. ```go model, _ := ai.Model(ctx, "tts-1") // Basic synthesis speechReq := request.Speech{ Model: model.ModelId, Input: "Hello, world!", Voice: "alloy", } res, err := ai.TextToSpeech(ctx, speechReq) if err != nil { log.Fatal(err) } fmt.Println("Audio file:", res.Full) fmt.Println("Format:", res.Extension) // With format and speed speechReq := request.Speech{ Model: model.ModelId, Input: "Welcome to the show", Voice: "nova", ResponseFormat: "wav", Speed: 0.8, // Slower playback } res, _ := ai.TextToSpeech(ctx, speechReq) // res.Full contains path to WAV file // With streaming format speechReq := request.Speech{ Model: model.ModelId, Input: "Stream this content", Voice: "shimmer", StreamFormat: "sse", // Server-sent events } res, _ := ai.TextToSpeech(ctx, speechReq) ``` -------------------------------- ### Get Response String Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/response.md Returns the content text from the primary choice's message as a string. Use this to get the plain text response. ```go func (r *Response) String() string ``` ```go resp, _ := ai.Completion(ctx, req) fmt.Println(resp.String()) // The actual response text ``` -------------------------------- ### SpeechToText Transcription Example Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/audio.md Demonstrates how to transcribe audio files to text using the SpeechToText method. Includes basic usage, transcription with extra parameters, and processing of segmented and word-level timing information. ```go model, _ := ai.Model(ctx, "whisper-1") // Basic transcription res, err := ai.SpeechToText(ctx, model, "audio.mp3", map[string]any{}) if err != nil { log.Fatal(err) } fmt.Println("Text:", res.Text) fmt.Println("Language:", res.Language) // With extra parameters extraParams := map[string]any{ "punctuate": true, "smart_format": true, } res, _ := ai.SpeechToText(ctx, model, "audio.oga", extraParams) // Process segments with timing for _, segment := range res.Segments { fmt.Printf("[%.1f - %.1f] %s\n", segment.Start, segment.End, segment.Text) } // Access word-level timing for _, word := range res.Words { fmt.Printf("%s: %.2f - %.2f\n", word.Word, word.Start, word.End) } ``` -------------------------------- ### Example Usage of NewRequest Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/request.md Demonstrates how to create a new `Request` object using the `NewRequest` constructor after retrieving model metadata. Ensure you handle potential errors when fetching model information. ```go model, _ := ai.Model(ctx, "claude-4") req := request.NewRequest(model) ``` -------------------------------- ### Example Usage of ToolResponses String Method Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/response.md Demonstrates how to call the `String()` method on `ToolResponses` to print concatenated response texts. Ensure `ai.ToolCall` is properly initialized. ```go toolRes, _ := ai.ToolCall(ctx, tool) fmt.Println(toolRes.String()) ``` -------------------------------- ### Tool-Aware Conversation in Go Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/README.md Example of a tool-aware conversation flow. It maintains history, sends available tools to the AI, executes tool calls when requested, and appends results to the conversation. This requires a running LiteLLM server. ```go package main import ( "context" "fmt" "log" "net/url" "time" "github.com/andrejsstepanovs/go-litellm/client" "github.com/andrejsstepanovs/go-litellm/conf/connections/litellm" "github.com/andrejsstepanovs/go-litellm/mcp" "github.com/andrejsstepanovs/go-litellm/models" "github.com/andrejsstepanovs/go-litellm/request" "github.com/andrejsstepanovs/go-litellm/response" ) func main() { ctx := context.Background() // Create LiteLLM client conn := litellm.Connection{ URL: *mustParseURL("http://localhost:4000"), Targets: litellm.Targets{ System: litellm.Target{Timeout: time.Second}, LLM: litellm.Target{Timeout: time.Minute}, MCP: litellm.Target{Timeout: time.Minute}, }, } cfg := client.Config{APIKey: "sk-1234", Temperature: 0.7} ai, err := client.New(cfg, conn) if err != nil { log.Fatal(err) } // Pick a model and list tools model, _ := ai.Model(ctx, "claude-4") tools, _ := ai.Tools(ctx) // Initial conversation messages := request.Messages{ request.UserMessageSimple("What's the current time in Riga?"), } finalResp := runToolAwareConversation(ctx, ai, model, tools, messages, 5) fmt.Println("Final Answer:", finalResp.String()) } func runToolAwareConversation(ctx context.Context, ai *client.Litellm, model models.ModelMeta, tools mcp.AvailableTools, messages request.Messages, maxIter int) response.Response { if maxIter <= 0 { return response.Response{} } req := request.NewCompletionRequest(model, messages, tools.ToLLMCallTools(), nil, 0.7) resp, err := ai.Completion(ctx, req) if err != nil { log.Fatal("Completion error:", err) } if resp.Choice().FinishReason == response.FINISH_REASON_TOOL { for _, toolCall := range resp.Choice().Message.ToolCalls.SortASC() { toolResp, err := ai.ToolCall(ctx, toolCall.Function) if err != nil { log.Fatal("Tool call error:", err) } for _, tr := range toolResp { messages = append(messages, request.ToolCallMessage(toolCall, tr)) } } return runToolAwareConversation(ctx, ai, model, tools, messages, maxIter-1) } return resp } func mustParseURL(s string) *url.URL { u, err := url.Parse(s) if err != nil { log.Fatal(err) } return u } ``` -------------------------------- ### NewRequest Constructor Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/request.md Creates a new `Request` object with basic model and stream settings initialized to false. Use this to start building a new API request. ```go func NewRequest(model models.ModelMeta) *Request ``` -------------------------------- ### Build Image Analysis Message with Cache Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/message-construction.md Construct a message for image analysis, including a system message with a cache point and a user message containing an image URL. This example demonstrates how to set cache control for specific message parts. ```go messages := request.Messages{ request.SystemMessageSimple( "You are an image analysis expert." ).CachePoint(), request.UserMessageImage( "Analyze this image", request.MessageImage("https://example.com/image.jpg"), ), } fmt.Printf("Cache points: %d\n", messages.CacheControlCount()) req := request.NewCompletionRequest(model, messages, nil, nil, 0.7) resp, _ := ai.Completion(ctx, req) fmt.Println(resp.String()) ``` -------------------------------- ### Perform Simple Chat Completion Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/README.md Use the Completion method to get a response from a language model. Specify the model, messages, and other parameters like temperature and max tokens. ```go model, _ := ai.Model(ctx, "claude-4") messages := request.Messages{request.UserMessageSimple("What is the capital of France?")} req := request.NewCompletionRequest(model, messages, nil, nil, 1) resp, _ := ai.Completion(ctx, req) fmt.Println(resp.String()) // The capital of France is Paris. ``` -------------------------------- ### Implement Get Method for Targets Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/types.md Provides a method to retrieve the specific Target configuration for a given endpoint name. This is useful for dynamically accessing timeout and retry settings based on the target endpoint. ```go func (t *Targets) Get(name TargetName) Target { // Implementation details would be here // This is a placeholder based on the provided signature switch name { case CLIENT_SYSTEM: return t.System case CLIENT_LLM: return t.LLM case CLIENT_MCP: return t.MCP default: // Return a default or zero value Target, or handle error return Target{} } } ``` -------------------------------- ### LLMCallToolFunctionProperty Structure Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/mcp-and-tools.md Details a single parameter within a function's input schema. It includes the parameter's description, JSON type, and optional constraints like enum values, format, or an example. ```go type LLMCallToolFunctionProperty struct { Description string Type string Enum []string Format string Example string } ``` -------------------------------- ### Default Retry Configuration Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md This configuration sets a starting retry interval of 100ms, uses an exponential backoff rate of 2.0, and limits retries to 3 attempts, resulting in a total maximum wait time of approximately 700ms before a timeout. ```go Target{ RetryInterval: 100 * time.Millisecond, RetryMaxAttempts: 3, RetryBackoffRate: 2.0, MaxRetry: 3, } ``` -------------------------------- ### Get Model by ID Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/embeddings-and-models.md Implements the Get method for the Models slice to find a specific model by its ID. Returns the model and a boolean indicating if it was found. ```go func (m Models) Get(id ModelID) (Model, bool) ``` -------------------------------- ### Client Initialization Variations Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/COMPLETION_REPORT.txt Demonstrates different ways to initialize the LiteLLM client. Choose the method that best suits your application's configuration needs. ```go package main import ( "fmt" "github.com/andrejsstepanovs/go-litellm" ) func main() { // Example 1: Default client initialization client1 := go_litellm.NewClient() fmt.Printf("Client 1: %+v\n", client1) // Example 2: Client initialization with custom configuration (e.g., API key) // Note: Replace "YOUR_API_KEY" with your actual API key client2 := go_litellm.NewClient(go_litellm.WithAPIKey("YOUR_API_KEY")) fmt.Printf("Client 2: %+v\n", client2) // Example 3: Client initialization with multiple options client3 := go_litellm.NewClient( go_litellm.WithAPIKey("ANOTHER_API_KEY"), go_litellm.WithBaseURL("https://api.example.com"), ) fmt.Printf("Client 3: %+v\n", client3) } ``` -------------------------------- ### Message() Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/response.md Gets the message from the primary choice of the response. ```APIDOC ## Method: Message Gets the message from the primary choice. ### Returns `ResponseMessage` | Message content from last choice ``` -------------------------------- ### Initialize LiteLLM Client Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/client.md Creates a new LiteLLM client instance with provided configuration and connection settings. Ensure all required parameters are correctly set before initialization. ```go package main import ( "log" "net/url" "time" "github.com/andrejsstepanovs/go-litellm/client" "github.com/andrejsstepanovs/go-litellm/conf/connections/litellm" ) func main() { baseURL, _ := url.Parse("http://localhost:4000") conn := litellm.Connection{ URL: *baseURL, Targets: litellm.Targets{ System: litellm.Target{Timeout: time.Second}, LLM: litellm.Target{Timeout: time.Minute * 2}, MCP: litellm.Target{Timeout: time.Minute * 5}, }, } cfg := client.Config{ APIKey: "sk-1234", Temperature: 0.7, } ai, err := client.New(cfg, conn) if err != nil { log.Fatal(err) } _ = ai // ready to use } ``` -------------------------------- ### Word Type Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/audio.md Represents a single transcribed word with its start and end times. ```APIDOC ## Type: Word Single word with timing information. ### Fields - **Word** (string) - The word text - **Start** (float32) - Start time in seconds - **End** (float32) - End time in seconds ``` -------------------------------- ### Word Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/types.md Represents a single word with its start and end timestamps, used within the AudioResponse. ```APIDOC ## Word ### Description A single word with timing information. ### Fields - **Word** (string) - Word text - **Start** (float32) - Start time in seconds - **End** (float32) - End time in seconds ``` -------------------------------- ### go-litellm Client Configuration with Viper Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md Shows how to configure the go-litellm client using the Viper configuration library, allowing settings to be loaded from files or environment variables. This is ideal for applications requiring flexible configuration management. ```go package main import ( "log" "time" "github.com/spf13/viper" "github.com/andrejsstepanovs/go-litellm/client" "github.com/andrejsstepanovs/go-litellm/conf/connections/litellm" ) func main() { // Load from file viper.SetConfigFile("config.yaml") viper.ReadInConfig() // Or set defaults viper.SetDefault("litellm.url", "http://localhost:4000") viper.SetDefault("litellm.targets.system.timeout", 1*time.Second) viper.SetDefault("litellm.targets.llm.timeout", 2*time.Minute) viper.SetDefault("litellm.targets.mcp.timeout", 5*time.Minute) // Load connection from Viper conn, err := litellm.New() if err != nil { log.Fatal(err) } // Create client cfg := client.Config{ APIKey: viper.GetString("api.key"), Temperature: float32(viper.GetFloat64("api.temperature")), } ai, err := client.New(cfg, conn) if err != nil { log.Fatal(err) } _ = ai } ``` -------------------------------- ### Define Word Type Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/audio.md Represents a single transcribed word with its start and end timestamps. ```go type Word struct { Word string // The word text Start float32 // Start time in seconds End float32 // End time in seconds } ``` -------------------------------- ### Call an MCP Tool in Go Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/README.md Demonstrates how to call a specific MCP tool with arguments. Ensure the tool is available and correctly configured. ```go tool := common.ToolCallFunction{ Name: "current_time", Arguments: map[string]string{"timezone": "Europe/Riga"}, } res, _ := ai.ToolCall(ctx, tool) fmt.Println(res.String()) ``` -------------------------------- ### Word Struct Definition Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/types.md Represents a single word with its corresponding start and end timestamps in seconds. ```go type Word struct { Word string // Word text Start float32 // Start time in seconds End float32 // End time in seconds } ``` -------------------------------- ### Initialize LiteLLM Client in Go Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/INDEX.md Initialize the LiteLLM client with custom connection configurations, including timeouts for different target types. Ensure API key and temperature are set in the client configuration. ```go // See configuration.md for detailed setup conn := litellm.Connection{ URL: *baseURL, Targets: litellm.Targets{ System: litellm.Target{Timeout: 1 * time.Second}, LLM: litellm.Target{Timeout: 2 * time.Minute}, MCP: litellm.Target{Timeout: 5 * time.Minute}, }, } cfg := client.Config{ APIKey: "sk-xxx", Temperature: 0.7, } ai, _ := client.New(cfg, conn) ``` -------------------------------- ### Get Response Message Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/response.md Retrieves the ResponseMessage from the primary choice of a response. This is useful for accessing structured message data. ```go func (r *Response) Message() ResponseMessage ``` -------------------------------- ### Tool Discovery and Invocation Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/mcp-and-tools.md This Go code demonstrates how to discover available tools, inspect their details (name, description, parameters), and directly invoke a specific tool with its arguments. This is useful for understanding what tools are available and how to use them. ```go // List all available tools tools, err := ai.Tools(ctx) if err != nil { log.Fatal(err) } // Find a specific tool for _, tool := range tools { if tool.Name == "get_weather" { fmt.Printf("Tool: %s\n", tool.Name) fmt.Printf("Description: %s\n", tool.Description) // Print parameters for paramName, param := range tool.InputSchema.Properties { fmt.Printf(" - %s (%s): %s\n", paramName, param.Type, param.Description) } } } // Call a tool directly tool := common.ToolCallFunction{ Name: "get_weather", Arguments: common.Arguments{ "location": "Paris", }, } result, err := ai.ToolCall(ctx, tool) if err != nil { log.Fatal(err) } fmt.Println(result.String()) ``` -------------------------------- ### Initialize LiteLLM Client Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/README.md Initialize the LiteLLM client with a base URL, connection configuration, and API key. Ensure the base URL is correctly parsed and connection timeouts are set for different targets. ```go package main import ( "context" "net/url" "time" "log" "github.com/andrejsstepanovs/go-litellm/client" "github.com/andrejsstepanovs/go-litellm/conf/connections/litellm" ) func main() { baseURL, _ := url.Parse("http://localhost:4000") conn := litellm.Connection{ URL: *baseURL, Targets: litellm.Targets{ System: litellm.Target{Timeout: time.Second}, LLM: litellm.Target{Timeout: time.Minute * 2}, MCP: litellm.Target{Timeout: time.Minute * 5}, }, } cfg := client.Config{ APIKey: "sk-1234", Temperature: 0.7, } ai, err := client.New(cfg, conn) if err != nil { log.Fatal(err) } _ = ai // ready to use } ``` -------------------------------- ### Get General Argument Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/mcp-and-tools.md Retrieves an argument preserving its original type. Use this when the exact type of the argument is important. ```go val, found := args.GetArgument("temperature") if found && val != nil { temp := val.(float64) fmt.Println(temp) // 0.8 } ``` -------------------------------- ### GET /v2/model/info Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/endpoints.md Retrieves mapping of model names to LiteLLM keys. This endpoint is part of the System Endpoints category. ```APIDOC ## GET /v2/model/info ### Description Retrieves mapping of model names to LiteLLM keys. This endpoint is part of the System Endpoints category. ### Method GET ### Endpoint /v2/model/info ### Response #### Success Response (200) - **model_name** (string) - Human-readable model name - **model_info.key** (string) - LiteLLM model key ### Response Example ```json { "data": [ { "model_name": "Claude 4", "model_info": { "key": "claude-4" } } ] } ``` ``` -------------------------------- ### Build Simple Schema and Use with Completion Request Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/schemas-and-tokens.md Demonstrates creating a simple schema using SchemaBuilder and setting it on a completion request. The response is then parsed as JSON. ```go schema, err := request.NewSchemaBuilder("city"). AddStringProperty("name", "City name"). AddIntegerProperty("population", "Number of inhabitants"). AddStringProperty("country", "Country name"). SetRequired([]string{"name", "country"}). SetStrict(true). Build() if err != nil { log.Fatal(err) } model, _ := ai.Model(ctx, "claude-4") messages := request.Messages{ request.UserMessageSimple("List 3 major cities"), } req := request.NewCompletionRequest(model, messages, nil, nil, 0.2) req.SetJSONSchema(schema) resp, _ := ai.Completion(ctx, req) // Parse response as JSON var result map[string]interface{} json.Unmarshal(resp.Bytes(), &result) ``` -------------------------------- ### Get Model Metadata by ID Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/client.md Retrieves metadata for a specific model using its ID. Ensure you have a valid context and model ID. ```go func (l *Litellm) Model(ctx context.Context, modelID models.ModelID) (models.ModelMeta, error) ``` ```go model, err := ai.Model(ctx, "claude-4") if err != nil { log.Fatal(err) } fmt.Printf("Max Input Tokens: %v\n", model.MaxInputTokens) ``` -------------------------------- ### Get Response Bytes Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/response.md Returns the response content as a byte slice. This is useful for handling raw data or when string conversion is not desired. ```go func (r *Response) Bytes() []byte ``` -------------------------------- ### Browse Available Models in Go Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/README.md Shows how to retrieve a list of all available models and their ownership. Useful for understanding the model landscape. ```go models, _ := ai.Models(ctx) for _, m := range models { fmt.Println(m.ID, m.OwnedBy) } ``` -------------------------------- ### Get Response Reasoning String Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/response.md Retrieves the reasoning content from the primary choice's message as a string. This is available if the model provides reasoning. ```go func (r *Response) ReasoningString() string ``` -------------------------------- ### ModelID Type Definition Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/embeddings-and-models.md Defines ModelID as a string type alias for model identifiers. Examples include 'claude-4', 'gpt-4o-mini', 'text-embedding-3-small', and 'whisper-1'. ```go type ModelID string ``` -------------------------------- ### Configuration from Code Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/COMPLETION_REPORT.txt Configures the LiteLLM client directly within the Go code using functional options. This provides flexibility for dynamic configuration. ```go package main import ( "fmt" "github.com/andrejsstepanovs/go-litellm" ) func main() { // Initialize client with configuration options set directly in code client := go_litellm.NewClient( go_litellm.WithAPIKey("MY_PROGRAMMATIC_API_KEY"), go_litellm.WithBaseURL("https://api.programmatic.com"), go_litellm.WithModel("claude-3-opus-20240229"), // Set a default model go_litellm.WithTimeout(30), // Set request timeout to 30 seconds ) fmt.Printf("Client configured programmatically.\n") // You can now use the 'client' for API calls. // For example, to use the default model set above: // resp, err := client.ChatCompletion(context.Background(), go_litellm.ChatCompletionRequest{ // Messages: []go_litellm.Message{{Role: "user", Content: "Hello"}}, // }) } ``` -------------------------------- ### Get Primary Response Choice Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/response.md Retrieves the last (primary) response choice from the Choices array. Useful for accessing the main result of a completion request. ```go func (r *Response) Choice() ResponseChoice ``` ```go choice := resp.Choice() fmt.Println(choice.FinishReason) // "stop" or "tool_calls" ``` -------------------------------- ### Creating Tool Arguments Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/mcp-and-tools.md This snippet shows how to create and retrieve arguments for tool calls. Arguments can be set using a map-like interface or a structured approach. This is essential for passing parameters to your tools. ```go // Create arguments from user input args := common.Arguments{} args.SetArgument("location", "Paris") args.SetArgument("units", "celsius") // Or use structured approach args := common.Arguments{ "location": "Paris", "units": "celsius", } // Retrieve arguments location, _ := args.GetStrArgument("location") value, found := args.GetArgument("units") ``` -------------------------------- ### GET /model_group/info Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/endpoints.md Retrieves detailed metadata for a specific model. This endpoint is part of the System Endpoints category and requires the model ID to be passed in the context. ```APIDOC ## GET /model_group/info ### Description Retrieves detailed metadata for a specific model. This endpoint is part of the System Endpoints category and requires the model ID to be passed in the context. ### Method GET ### Endpoint /model_group/info ### Query Parameters None (model ID passed in context) ### Response #### Success Response (200) - **model_group** (string) - Model identifier - **max_input_tokens** (float64) - Maximum context window - **max_output_tokens** (float64) - Maximum completion length - **input_cost_per_token** (float64) - Input token price - **output_cost_per_token** (float64) - Output token price - **providers** (string[]) - Supporting providers - **mode** (string) - Mode classification - **rpm** (int) - Requests per minute limit - **tpm** (int) - Tokens per minute limit - **supports_vision** (bool) - Image understanding capability - **supports_web_search** (bool) - Web search capability - **supports_reasoning** (bool) - Reasoning/thinking capability - **supports_function_calling** (bool) - Tool calling support - **supports_parallel_function_calling** (bool) - Parallel tool calls support - **supported_openai_params** (string[]) - Compatible OpenAI parameters ### Response Example ```json { "data": [ { "model_group": "string", "max_input_tokens": 128000, "max_output_tokens": 4096, "input_cost_per_token": 0.003, "output_cost_per_token": 0.006, "providers": ["provider1", "provider2"], "mode": "chat", "rpm": 10000, "tpm": 2000000, "supports_vision": true, "supports_web_search": false, "supports_reasoning": true, "supports_function_calling": true, "supports_parallel_function_calling": true, "supported_openai_params": ["temperature", "top_p", "frequency_penalty"] } ] } ``` ``` -------------------------------- ### Create Connection from Viper Configuration Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md Creates a new LiteLLM Connection by loading configuration from Viper, including the base URL and target timeouts. ```go func New() (Connection, error) ``` -------------------------------- ### Create Simple System Message Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/request.md Constructs a system message with basic string content. Ideal for setting initial instructions or context for the assistant. ```go func SystemMessageSimple(content string) Message ``` ```go msg := request.SystemMessageSimple("You are a helpful assistant.") ``` -------------------------------- ### New Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md Creates a new Connection object from Viper configuration, setting the base URL and target timeouts. ```APIDOC ## New ### Description Creates a new `Connection` object by loading configuration from Viper. This includes setting the base URL for the LiteLLM API and configuring timeouts for different target categories. ### Method func New() (Connection, error) ### Parameters None ### Returns Connection - The configured connection object. error - An error if the configuration cannot be loaded or is invalid. ``` -------------------------------- ### Get Generic Argument from Arguments Map Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/types.md Retrieves an argument from the Arguments map, preserving its original type. Returns the value and a boolean indicating if the key exists. ```go func (t *Arguments) GetArgument(name string) (any, bool) ``` -------------------------------- ### Configuration from YAML Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/COMPLETION_REPORT.txt Loads LiteLLM client configuration from a YAML file. This allows for externalizing settings and managing them separately from code. ```go package main import ( "fmt" "github.com/andrejsstepanovs/go-litellm" "gopkg.in/yaml.v3" ) // Example YAML structure for LiteLLM configuration const yamlConfig = ` api_key: "YOUR_YAML_API_KEY" base_url: "https://api.example-from-yaml.com" model: "gpt-3.5-turbo" ` type LiteLLMConfig struct { APIKey string `yaml:"api_key"` BaseURL string `yaml:"base_url"` Model string `yaml:"model"` } func main() { var config LiteLLMConfig // Unmarshal the YAML string into the config struct err := yaml.Unmarshal([]byte(yamlConfig), &config) if err != nil { fmt.Printf("Error unmarshalling YAML: %v\n", err) return } // Build client options from the configuration var clientOptions []go_litellm.ClientOption if config.APIKey != "" { clientOptions = append(clientOptions, go_litellm.WithAPIKey(config.APIKey)) } if config.BaseURL != "" { clientOptions = append(clientOptions, go_litellm.WithBaseURL(config.BaseURL)) } // Initialize the client with options from YAML client := go_litellm.NewClient(clientOptions...) fmt.Printf("Client initialized with BaseURL: %s, Model: %s\n", config.BaseURL, config.Model) // You can now use the 'client' for API calls, potentially using config.Model } ``` -------------------------------- ### Get Last Content Block of a Message Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/request.md The LastContent method retrieves the final content block from a message. It returns a pointer to MessageContent or nil if the message has no content. ```go func (m Message) LastContent() *MessageContent ``` -------------------------------- ### Implement Tool-Aware Loop in Go Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/INDEX.md Fetch available tools, construct a completion request with tool definitions, and process tool calls iteratively. The loop continues until the finish reason is not 'tool'. Refer to mcp-and-tools.md for details. ```go // See mcp-and-tools.md tools, _ := ai.Tools(ctx) messages := request.Messages{...} for { llmTools := tools.ToLLMCallTools() req := request.NewCompletionRequest(model, messages, llmTools, nil, 0.7) resp, _ := ai.Completion(ctx, req) if resp.Choice().FinishReason != response.FINISH_REASON_TOOL { fmt.Println(resp.String()) break } for _, tc := range resp.Choice().Message.ToolCalls.SortASC() { toolRes, _ := ai.ToolCall(ctx, tc.Function) messages = append(messages, request.ToolCallMessage(tc, toolRes[0])) } messages = append(messages, request.AIMessage(resp.Message())) } ``` -------------------------------- ### Get String Argument Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/mcp-and-tools.md Retrieves an argument as a string, converting types as needed. Handles string, number, boolean, and nil values, and marshals other types to JSON. ```go tz, found := args.GetStrArgument("timezone") if found { fmt.Println(tz) // "Europe/Riga" } ``` -------------------------------- ### Create Default Targets Configuration Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/configuration.md Creates a default Targets configuration, typically loaded from Viper configuration. ```go func NewTargets() Targets ``` -------------------------------- ### Create System Message with Custom Content Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/request.md Constructs a system message using custom content blocks. Use for detailed system instructions or context. ```go func SystemMessage(content MessageContents) Message ``` -------------------------------- ### Create a Cache TTL Option Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/message-construction.md Generates a functional option to set the Time-To-Live (TTL) for caching. This option is typically used when configuring caching behavior, for example, with `content.Cache()`. ```go opt := request.CacheTTL("1h") // Use with content.Cache() ``` -------------------------------- ### New Litellm Client Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/client.md Creates and returns a new Litellm client instance with the provided configuration and connection settings. ```APIDOC ## Constructor: New Creates and returns a new Litellm client instance. ```go func New(config Config, connection cfg.Connection) (*Litellm, error) ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **config** (Config) - Required - Client configuration with APIKey and Temperature - **connection** (cfg.Connection) - Required - Connection configuration with URL and timeout targets ### Returns - `*Litellm` - Initialized client - `error` - nil on success or validation error ### Example ```go package main import ( "log" "net/url" "time" "github.com/andrejsstepanovs/go-litellm/client" "github.com/andrejsstepanovs/go-litellm/conf/connections/litellm" ) func main() { baseURL, _ := url.Parse("http://localhost:4000") conn := litellm.Connection{ URL: *baseURL, Targets: litellm.Targets{ System: litellm.Target{Timeout: time.Second}, LLM: litellm.Target{Timeout: time.Minute * 2}, MCP: litellm.Target{Timeout: time.Minute * 5}, }, } cfg := client.Config{ APIKey: "sk-1234", Temperature: 0.7, } ai, err := client.New(cfg, conn) if err != nil { log.Fatal(err) } _ = ai // ready to use } ``` ``` -------------------------------- ### Get String Argument from Arguments Map Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/types.md Retrieves an argument as a string from the Arguments map, attempting type conversion if necessary. Returns the string value and a boolean indicating success. ```go func (t *Arguments) GetStrArgument(name string) (string, bool) ``` -------------------------------- ### List All Available Models with LiteLLM Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/embeddings-and-models.md Use this method to retrieve a list of all models supported by LiteLLM. It's useful for discovering available models and checking their ownership. The returned list can be iterated or searched for specific models. ```go func (l *Litellm) Models(ctx context.Context) (models.Models, error) ``` ```go models, err := ai.Models(ctx) if err != nil { log.Fatal(err) } for _, m := range models { fmt.Printf("ID: %s, Owner: %s\n", m.ID, m.OwnedBy) } // Search for specific model if found := models.Get("claude-4"); found { fmt.Println("Claude 4 is available") } ``` -------------------------------- ### Get Model Name to Key Mapping Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/embeddings-and-models.md Retrieves a mapping of user-friendly model names to their corresponding LiteLLM internal keys. Useful for looking up model keys by display name. ```APIDOC ## GET /v2/model/info ### Description Retrieves mapping of model names to LiteLLM keys. ### Method GET ### Endpoint `/v2/model/info` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go ctx := context.Background() infoMap, err := ai.ModelInfoMap(ctx) if err != nil { log.Fatal(err) } for name, key := range infoMap { fmt.Printf("%s => %s\n", name, key) // Example: "Claude 4" => "claude-4" } // Lookup by display name if key, found := infoMap["Claude 4"]; found { model, _ := ai.Model(ctx, models.ModelID(key)) fmt.Println(model.MaxInputTokens) } ``` ### Response #### Success Response (200) - **modelInfoMap** (map[string]string) - Map of name → key #### Response Example ```json { "Claude 4": "claude-4", "GPT-4": "gpt-4" } ``` ``` -------------------------------- ### List Available MCP Tools Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/README.md Retrieve a list of available Model-based Control Plane (MCP) tools using the Tools method. Iterate through the returned tools to display their names and descriptions. ```go tools, _ := ai.Tools(ctx) for _, tool := range tools { fmt.Printf("%s - %s\n", tool.Name, tool.Description) } ``` -------------------------------- ### List Available MCP Tools Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/endpoints.md This client method retrieves a list of all available MCP tools. The response includes details about each tool, such as its type, name, description, input schema, and MCP server information. ```go tools, err := Litellm.Tools(ctx) if err != nil { // Handle error } ``` -------------------------------- ### Create User Message with Image Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/request.md Constructs a user message that includes both optional text and an image URL. Use when the user provides visual context. ```go func UserMessageImage(content string, imageUrl ImageUrl) Message ``` ```go msg := request.UserMessageImage("Describe this image", request.MessageImage("https://example.com/image.jpg")) ``` -------------------------------- ### Build Simple Conversation Messages Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/message-construction.md Construct a basic conversation with system and user messages. Add assistant responses and subsequent user messages to continue the turn-based interaction. ```go messages := request.Messages{ request.SystemMessageSimple("You are a helpful assistant."), request.UserMessageSimple("What is Go?"), } req := request.NewCompletionRequest(model, messages, nil, nil, 0.7) resp, _ := ai.Completion(ctx, req) fmt.Println(resp.String()) // Add response messages.AddMessage(request.AIMessage(resp.Message())) // Next turn messages.AddMessage(request.UserMessageSimple("Why use it?")) ``` -------------------------------- ### LiteLLM Client Configuration Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/client.md Defines the configuration structure for the LiteLLM client, including API key and default temperature. ```go type Config struct { APIKey string // Bearer token for API authentication Temperature float32 // Default temperature for completions (required) } ``` -------------------------------- ### Tools Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/client.md Lists available MCP tools. It takes a context and returns a slice of available tools or an error. ```APIDOC ## Method: Tools ### Description Lists available MCP tools. It takes a context and returns a slice of available tools or an error. ### Signature ```go func (l *Litellm) Tools(ctx context.Context) (mcp.AvailableTools, error) ``` ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```go tools, err := ai.Tools(ctx) if err != nil { log.Fatal(err) } for _, tool := range tools { fmt.Printf("%s - %s\n", tool.Name, tool.Description) } ``` ### Response #### Success Response - **AvailableTools** (slice of objects) - A slice containing details of available MCP tools. - **Name** (string) - The name of the tool. - **Description** (string) - A description of the tool. #### Response Example ```json [ { "Name": "tool1", "Description": "This is the first tool." }, { "Name": "tool2", "Description": "This is the second tool." } ] ``` ### Throws - error: Request failed or parsing error. ``` -------------------------------- ### Initialize SchemaBuilder Source: https://github.com/andrejsstepanovs/go-litellm/blob/main/_autodocs/api-reference/schemas-and-tokens.md Use NewSchemaBuilder to create an initialized builder for a schema with a given name. ```go builder := request.NewSchemaBuilder("person") ```