The Send() method is used to make chat completion requests to the Edgee AI Gateway.
Arguments
Parameter Type Description model stringThe model identifier to use (e.g., "openai/gpt-4o") inputanyThe input for the completion. Can be a string, InputObject, *InputObject, or map[string]interface{}
The Send() method accept multiple input types:
When input is a string, it’s automatically converted to a user message:
response , err := client . Send ( "gpt-4o" , "What is the capital of France?" )
if err != nil {
log . Fatal ( err )
}
// Equivalent to: input: InputObject{Messages: []Message{{Role: "user", Content: "What is the capital of France?"}}}
fmt . Println ( response . Text ())
// "The capital of France is Paris."
When input is an InputObject, you have full control over the conversation:
Property Type Description Messages []MessageArray of conversation messages Tools[]ToolArray of function tools available to the model ToolChoiceanyControls which tool (if any) the model should call. Can be string ("auto", "none") or map[string]interface{}. See Tools documentation for details Tags[]stringOptional tags to categorize and label the request for analytics and filtering. Can also be sent via the x-edgee-tags header (comma-separated) EnableCompressionboolEnable token compression for this request. If true, the request will be compressed to the compression rate specified in the API key settings. If false, the request will not be compressed. CompressionRatefloat64The compression rate to use for this request. If EnableCompression is true, this value will be used to compress the request. The value should be between 0.0 and 1.0. The default value is 0.75.
Example with InputObject:
import "github.com/edgee-ai/go-sdk/edgee"
input := edgee . InputObject {
Messages : [] edgee . Message {
{ Role : "user" , Content : "What is 2+2?" },
},
}
response , err := client . Send ( "gpt-4o" , input )
if err != nil {
log . Fatal ( err )
}
fmt . Println ( response . Text ())
// "2+2 equals 4."
Example with Tags:
input := edgee . InputObject {
Messages : [] edgee . Message {
{ Role : "user" , Content : "Summarize this article" },
},
Tags : [] string { "summarization" , "production" , "user-123" },
}
response , err := client . Send ( "gpt-4o" , input )
You can also use a map[string]interface{} for dynamic input:
input := map [ string ] interface {}{
"messages" : [] map [ string ] string {
{ "role" : "user" , "content" : "What is 2+2?" },
},
}
response , err := client . Send ( "gpt-4o" , input )
if err != nil {
log . Fatal ( err )
}
Message Object
Each message in the Messages array has the following structure:
Property Type Description Role stringThe role of the message sender: "system", "developer", "user", "assistant", or "tool" ContentstringThe message content. Required for system, user, tool and developer roles. Optional for assistant when ToolCalls is present Name*stringOptional name for the message sender ToolCalls[]ToolCallArray of tool calls made by the assistant. Only present in assistant messages ToolCallID*stringID of the tool call this message is responding to. Required for tool role messages
Message Roles
system : System instructions that set the behavior of the assistant
developer : Instructions provided by the application developer, prioritized ahead of user messages.
user : Instructions provided by an end user.
assistant : Assistant responses (can include ToolCalls)
tool : Results from tool/function calls (requires ToolCallID)
Example - System and User Messages:
input := edgee . InputObject {
Messages : [] edgee . Message {
{ Role : "system" , Content : "You are a helpful assistant." },
{ Role : "user" , Content : "What is 2+2?" },
{ Role : "assistant" , Content : "2+2 equals 4." },
{ Role : "user" , Content : "What about 3+3?" },
},
}
response , err := client . Send ( "gpt-4o" , input )
if err != nil {
log . Fatal ( err )
}
fmt . Println ( response . Text ())
// "3+3 equals 6."
For complete tool calling examples and best practices, see Tools documentation .
Return Value
The Send() method returns (SendResponse, error). On success, the SendResponse contains:
SendResponse Object
Property Type Description IDstringUnique identifier for the completion ObjectstringObject type (typically "chat.completion") Createdint64Unix timestamp of when the completion was created ModelstringModel identifier used for the completion Choices[]ChoiceArray of completion choices (typically one) Usage*UsageToken usage information (if provided by the API) Compression*CompressionToken compression metrics (if compression was applied)
Choice Object
Each choice in the Choices array contains:
Property Type Description IndexintThe index of this choice in the array Message*MessageThe assistant’s message response FinishReason*stringReason why the generation stopped. Possible values: "stop", "length", "tool_calls", "content_filter", or nil
Example - Handling Multiple Choices:
response , err := client . Send ( "gpt-4o" , "Give me a creative idea." )
if err != nil {
log . Fatal ( err )
}
// Process all choices
for _ , choice := range response . Choices {
fmt . Printf ( "Choice %d : %s \n " , choice . Index , choice . Message . Content )
if choice . FinishReason != nil {
fmt . Printf ( "Finish reason: %s \n " , * choice . FinishReason )
}
}
Message Object (in Response)
The Message in each choice has:
Property Type Description RolestringThe role of the message (typically "assistant") ContentstringThe text content of the response. Empty when ToolCalls is present ToolCalls[]ToolCallArray of tool calls requested by the model (if any). See Tools documentation for details
Usage Object
Token usage information (when available):
Property Type Description PromptTokensintNumber of tokens in the prompt (after compression if applied) CompletionTokensintNumber of tokens in the completion TotalTokensintTotal tokens used (prompt + completion)
Example - Accessing Token Usage:
response , err := client . Send ( "gpt-4o" , "Explain quantum computing briefly." )
if err != nil {
log . Fatal ( err )
}
if response . Usage != nil {
fmt . Printf ( "Prompt tokens: %d \n " , response . Usage . PromptTokens )
fmt . Printf ( "Completion tokens: %d \n " , response . Usage . CompletionTokens )
fmt . Printf ( "Total tokens: %d \n " , response . Usage . TotalTokens )
}
Compression Object
Token compression metrics (when compression is applied):
Property Type Description InputTokensintOriginal number of input tokens before compression SavedTokensintNumber of tokens saved by compression Ratefloat64Compression rate as a decimal (0-1). For example, 0.61 means 61% compression
Example - Accessing Compression Metrics:
response , err := client . Send ( "gpt-4o" , edgee . InputObject {
Messages : [] edgee . Message {
{ Role : "user" , Content : "Analyze this long document with lots of context..." },
},
EnableCompression : true ,
CompressionRate : 0.8 ,
})
if err != nil {
log . Fatal ( err )
}
if response . Compression != nil {
fmt . Printf ( "Original input tokens: %d \n " , response . Compression . InputTokens )
fmt . Printf ( "Tokens saved: %d \n " , response . Compression . SavedTokens )
fmt . Printf ( "Compression rate: %.1f%% \n " , response . Compression . Rate * 100 )
}
The Compression object is only present when token compression is applied to the request. Simple queries may not trigger compression.
Convenience Methods
The SendResponse struct provides convenience methods for easier access:
Method Return Type Description Text()stringShortcut to Choices[0].Message.Content MessageContent()*MessageShortcut to Choices[0].Message FinishReason()stringShortcut to *Choices[0].FinishReason (returns empty string if nil) ToolCalls()[]ToolCallShortcut to Choices[0].Message.ToolCalls
Example - Using Convenience Methods:
response , err := client . Send ( "gpt-4o" , "Hello!" )
if err != nil {
log . Fatal ( err )
}
// Instead of: response.Choices[0].Message.Content
fmt . Println ( response . Text ())
// Instead of: response.Choices[0].Message
if msg := response . MessageContent (); msg != nil {
fmt . Printf ( "Role: %s \n " , msg . Role )
}
// Instead of: *response.Choices[0].FinishReason
fmt . Println ( response . FinishReason ())
// Instead of: response.Choices[0].Message.ToolCalls
if toolCalls := response . ToolCalls (); len ( toolCalls ) > 0 {
fmt . Printf ( "Tool calls: %+v \n " , toolCalls )
}
Error Handling
The Send() method return Go errors:
response , err := client . Send ( "gpt-4o" , "Hello!" )
if err != nil {
// Handle errors
log . Fatalf ( "Request failed: %v " , err )
}
Common Errors
API errors : fmt.Errorf("API error %d: %s", statusCode, message) - The API returned an error status
Network errors : Standard Go HTTP errors
Invalid input : fmt.Errorf("unsupported input type: %T", input) - Invalid request structure
JSON errors : Errors from JSON marshaling/unmarshaling