Chat - Go SDK
Chat - Go SDK
Chat method reference
The Go SDK and docs are currently in beta. Report issues on GitHub.
Overview
Available Operations
- Send - Create a chat completion
Send
Sends a request for a model response for the given chat conversation. Supports both streaming and non-streaming modes.
Example Usage: guardrail-blocked
1 package main 2 3 import( 4 "context" 5 "os" 6 openrouter "github.com/OpenRouterTeam/go-sdk" 7 "github.com/OpenRouterTeam/go-sdk/models/components" 8 "log" 9 ) 10 11 func main() { 12 ctx := context.Background() 13 14 s := openrouter.New( 15 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")), 16 ) 17 18 res, err := s.Chat.Send(ctx, components.ChatRequest{ 19 Messages: []components.ChatMessages{ 20 components.CreateChatMessagesSystem( 21 components.ChatSystemMessage{ 22 Content: components.CreateChatSystemMessageContentStr( 23 "You are a helpful assistant.", 24 ), 25 Role: components.ChatSystemMessageRoleSystem, 26 }, 27 ), 28 components.CreateChatMessagesUser( 29 components.ChatUserMessage{ 30 Content: components.CreateChatUserMessageContentStr( 31 "What is the capital of France?", 32 ), 33 Role: components.ChatUserMessageRoleUser, 34 }, 35 ), 36 }, 37 }, nil) 38 if err != nil { 39 log.Fatal(err) 40 } 41 if res != nil { 42 defer res.ChatStreamingResponse.Close() 43 44 for res.ChatStreamingResponse.Next() { 45 event := res.ChatStreamingResponse.Value() 46 log.Print(event) 47 // Handle the event 48 } 49 } 50 }
Example Usage: insufficient-permissions
1 package main 2 3 import( 4 "context" 5 "os" 6 openrouter "github.com/OpenRouterTeam/go-sdk" 7 "github.com/OpenRouterTeam/go-sdk/models/components" 8 "log" 9 ) 10 11 func main() { 12 ctx := context.Background() 13 14 s := openrouter.New( 15 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")), 16 ) 17 18 res, err := s.Chat.Send(ctx, components.ChatRequest{ 19 Messages: []components.ChatMessages{ 20 components.CreateChatMessagesSystem( 21 components.ChatSystemMessage{ 22 Content: components.CreateChatSystemMessageContentStr( 23 "You are a helpful assistant.", 24 ), 25 Role: components.ChatSystemMessageRoleSystem, 26 }, 27 ), 28 components.CreateChatMessagesUser( 29 components.ChatUserMessage{ 30 Content: components.CreateChatUserMessageContentStr( 31 "What is the capital of France?", 32 ), 33 Role: components.ChatUserMessageRoleUser, 34 }, 35 ), 36 }, 37 }, nil) 38 if err != nil { 39 log.Fatal(err) 40 } 41 if res != nil { 42 defer res.ChatStreamingResponse.Close() 43 44 for res.ChatStreamingResponse.Next() { 45 event := res.ChatStreamingResponse.Value() 46 log.Print(event) 47 // Handle the event 48 } 49 } 50 }
Parameters
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
ctx | context.Context | ✔️ | The context to use for the request. | |
chatRequest | components.ChatRequest | ✔️ | N/A | {"max_tokens": 150,"messages": [{"content": "You are a helpful assistant.","role": "system"},{"content": "What is the capital of France?","role": "user"}], “model”: “openai/gpt-4”, “temperature”: 0.7 } |
xOpenRouterExperimentalMetadata | *components.MetadataLevel | ➖ | Opt-in to surface routing metadata on the response under openrouter_metadata. Defaults to disabled. | enabled |
opts | []operations.Option | ➖ | The options for this request. |
Response
*operations.SendChatCompletionRequestResponse, error
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| sdkerrors.BadRequestResponseError | 400 | application/json |
| sdkerrors.UnauthorizedResponseError | 401 | application/json |
| sdkerrors.PaymentRequiredResponseError | 402 | application/json |
| sdkerrors.ForbiddenResponseError | 403 | application/json |
| sdkerrors.NotFoundResponseError | 404 | application/json |
| sdkerrors.RequestTimeoutResponseError | 408 | application/json |
| sdkerrors.PayloadTooLargeResponseError | 413 | application/json |
| sdkerrors.UnprocessableEntityResponseError | 422 | application/json |
| sdkerrors.TooManyRequestsResponseError | 429 | application/json |
| sdkerrors.InternalServerResponseError | 500 | application/json |
| sdkerrors.BadGatewayResponseError | 502 | application/json |
| sdkerrors.ServiceUnavailableResponseError | 503 | application/json |
| sdkerrors.EdgeNetworkTimeoutResponseError | 524 | application/json |
| sdkerrors.ProviderOverloadedResponseError | 529 | application/json |
| sdkerrors.APIError | 4XX, 5XX | */* |