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
1 package main 2 3 import( 4 "context" 5 "os" 6 openrouter "github.com/OpenRouterTeam/go-sdk" 7 "github.com/OpenRouterTeam/go-sdk/optionalnullable" 8 "github.com/OpenRouterTeam/go-sdk/models/components" 9 "log" 10 ) 11 12 func main() { 13 ctx := context.Background() 14 15 s := openrouter.New( 16 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")), 17 ) 18 19 res, err := s.Chat.Send(ctx, components.ChatRequest{ 20 MaxTokens: optionalnullable.From(openrouter.Pointer[int64](150)), 21 Messages: []components.ChatMessages{ 22 components.CreateChatMessagesSystem( 23 components.ChatSystemMessage{ 24 Content: components.CreateChatSystemMessageContentStr( 25 "You are a helpful assistant.", 26 ), 27 Role: components.ChatSystemMessageRoleSystem, 28 }, 29 ), 30 components.CreateChatMessagesUser( 31 components.ChatUserMessage{ 32 Content: components.CreateChatUserMessageContentStr( 33 "What is the capital of France?", 34 ), 35 Role: components.ChatUserMessageRoleUser, 36 }, 37 ), 38 }, 39 Model: openrouter.Pointer("openai/gpt-4"), 40 Temperature: optionalnullable.From(openrouter.Pointer[float64](0.7)), 41 }) 42 if err != nil { 43 log.Fatal(err) 44 } 45 if res != nil { 46 defer res.Object.Close() 47 48 for res.Object.Next() { 49 event := res.Object.Value() 50 log.Print(event) 51 // Handle the event 52 } 53 } 54 }
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 | */* |