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

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 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

ParameterTypeRequiredDescription
ctxcontext.Context✔️The context to use for the request.
requestcomponents.ChatRequest✔️The request object to use for the request.
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.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*/*