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

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/models/components"
8 "log"
9)
10
11func 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

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/models/components"
8 "log"
9)
10
11func 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

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
chatRequestcomponents.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.MetadataLevelOpt-in to surface routing metadata on the response under openrouter_metadata. Defaults to disabled.enabled
opts[]operations.OptionThe options for this request.

Response

*operations.SendChatCompletionRequestResponse, error

Errors

Error TypeStatus CodeContent Type
sdkerrors.BadRequestResponseError400application/json
sdkerrors.UnauthorizedResponseError401application/json
sdkerrors.PaymentRequiredResponseError402application/json
sdkerrors.ForbiddenResponseError403application/json
sdkerrors.NotFoundResponseError404application/json
sdkerrors.RequestTimeoutResponseError408application/json
sdkerrors.PayloadTooLargeResponseError413application/json
sdkerrors.UnprocessableEntityResponseError422application/json
sdkerrors.TooManyRequestsResponseError429application/json
sdkerrors.InternalServerResponseError500application/json
sdkerrors.BadGatewayResponseError502application/json
sdkerrors.ServiceUnavailableResponseError503application/json
sdkerrors.EdgeNetworkTimeoutResponseError524application/json
sdkerrors.ProviderOverloadedResponseError529application/json
sdkerrors.APIError4XX, 5XX*/*