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
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 Role: components.ChatSystemMessageRoleSystem, 23 Content: components.CreateChatSystemMessageContentStr( 24 "You are a helpful assistant.", 25 ), 26 }, 27 ), 28 components.CreateChatMessagesUser( 29 components.ChatUserMessage{ 30 Role: components.ChatUserMessageRoleUser, 31 Content: components.CreateChatUserMessageContentStr( 32 "What is the capital of France?", 33 ), 34 }, 35 ), 36 }, 37 Model: openrouter.Pointer("openai/gpt-4"), 38 MaxTokens: openrouter.Pointer[int64](150), 39 Temperature: openrouter.Pointer[float64](0.7), 40 }) 41 if err != nil { 42 log.Fatal(err) 43 } 44 if res != nil { 45 defer res.Object.Close() 46 47 for res.Object.Next() { 48 event := res.Object.Value() 49 log.Print(event) 50 // Handle the event 51 } 52 } 53 }
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | ✔️ | The context to use for the request. |
request | components.ChatRequest | ✔️ | The request object to use for the request. |
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.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 | */* |