diff --git a/client/client.go b/client/client.go index 9f7ef92..aea7738 100644 --- a/client/client.go +++ b/client/client.go @@ -7,6 +7,7 @@ import ( context "context" json "encoding/json" errors "errors" + fmt "fmt" v2 "github.com/cohere-ai/cohere-go/v2" connectors "github.com/cohere-ai/cohere-go/v2/connectors" core "github.com/cohere-ai/cohere-go/v2/core" @@ -15,6 +16,7 @@ import ( finetuningclient "github.com/cohere-ai/cohere-go/v2/finetuning/client" models "github.com/cohere-ai/cohere-go/v2/models" option "github.com/cohere-ai/cohere-go/v2/option" + v2v2 "github.com/cohere-ai/cohere-go/v2/v2" io "io" http "net/http" os "os" @@ -25,6 +27,7 @@ type Client struct { caller *core.Caller header http.Header + V2 *v2v2.Client EmbedJobs *embedjobs.Client Datasets *datasets.Client Connectors *connectors.Client @@ -46,6 +49,7 @@ func NewClient(opts ...option.RequestOption) *Client { }, ), header: options.ToHeader(), + V2: v2v2.NewClient(opts...), EmbedJobs: embedjobs.NewClient(opts...), Datasets: datasets.NewClient(opts...), Connectors: connectors.NewClient(opts...), @@ -55,7 +59,7 @@ func NewClient(opts ...option.RequestOption) *Client { } // Generates a text response to a user message. -// To learn how to use the Chat API with Streaming and RAG follow our [Text Generation guides](https://docs.cohere.com/docs/chat-api). +// To learn how to use the Chat API and RAG follow our [Text Generation guides](https://docs.cohere.com/docs/chat-api). func (c *Client) ChatStream( ctx context.Context, request *v2.ChatStreamRequest, @@ -73,6 +77,9 @@ func (c *Client) ChatStream( endpointURL := baseURL + "/v1/chat" headers := core.MergeHeaders(c.header.Clone(), options.ToHeader()) + if request.Accepts != nil { + headers.Add("Accepts", fmt.Sprintf("%v", request.Accepts)) + } errorDecoder := func(statusCode int, body io.Reader) error { raw, err := io.ReadAll(body) @@ -167,19 +174,21 @@ func (c *Client) ChatStream( return streamer.Stream( ctx, &core.StreamParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Headers: headers, + Client: options.HTTPClient, + Request: request, + ErrorDecoder: errorDecoder, }, ) } // Generates a text response to a user message. -// To learn how to use the Chat API with Streaming and RAG follow our [Text Generation guides](https://docs.cohere.com/docs/chat-api). +// To learn how to use the Chat API and RAG follow our [Text Generation guides](https://docs.cohere.com/docs/chat-api). func (c *Client) Chat( ctx context.Context, request *v2.ChatRequest, @@ -197,6 +206,9 @@ func (c *Client) Chat( endpointURL := baseURL + "/v1/chat" headers := core.MergeHeaders(c.header.Clone(), options.ToHeader()) + if request.Accepts != nil { + headers.Add("Accepts", fmt.Sprintf("%v", request.Accepts)) + } errorDecoder := func(statusCode int, body io.Reader) error { raw, err := io.ReadAll(body) @@ -291,14 +303,16 @@ func (c *Client) Chat( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -421,13 +435,15 @@ func (c *Client) GenerateStream( return streamer.Stream( ctx, &core.StreamParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Headers: headers, + Client: options.HTTPClient, + Request: request, + ErrorDecoder: errorDecoder, }, ) } @@ -547,14 +563,16 @@ func (c *Client) Generate( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -678,14 +696,16 @@ func (c *Client) Embed( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -805,14 +825,16 @@ func (c *Client) Rerank( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -933,14 +955,16 @@ func (c *Client) Classify( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -1063,14 +1087,16 @@ func (c *Client) Summarize( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -1190,14 +1216,16 @@ func (c *Client) Tokenize( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -1317,14 +1345,16 @@ func (c *Client) Detokenize( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -1443,13 +1473,15 @@ func (c *Client) CheckApiKey( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err diff --git a/connectors/client.go b/connectors/client.go index 40827ab..0b098e7 100644 --- a/connectors/client.go +++ b/connectors/client.go @@ -158,13 +158,15 @@ func (c *Client) List( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -284,14 +286,16 @@ func (c *Client) Create( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -412,13 +416,15 @@ func (c *Client) Get( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -539,13 +545,15 @@ func (c *Client) Delete( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodDelete, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodDelete, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -667,14 +675,16 @@ func (c *Client) Update( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPatch, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPatch, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -804,13 +814,15 @@ func (c *Client) OAuthAuthorize( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err diff --git a/core/core.go b/core/core.go index 3e28f90..6b5a8f3 100644 --- a/core/core.go +++ b/core/core.go @@ -10,6 +10,8 @@ import ( "mime/multipart" "net/http" "net/url" + "reflect" + "strings" ) const ( @@ -136,6 +138,8 @@ type CallParams struct { Method string MaxAttempts uint Headers http.Header + BodyProperties map[string]interface{} + QueryParameters url.Values Client HTTPClient Request interface{} Response interface{} @@ -145,7 +149,15 @@ type CallParams struct { // Call issues an API call according to the given call parameters. func (c *Caller) Call(ctx context.Context, params *CallParams) error { - req, err := newRequest(ctx, params.URL, params.Method, params.Headers, params.Request) + url := buildURL(params.URL, params.QueryParameters) + req, err := newRequest( + ctx, + url, + params.Method, + params.Headers, + params.Request, + params.BodyProperties, + ) if err != nil { return err } @@ -212,6 +224,23 @@ func (c *Caller) Call(ctx context.Context, params *CallParams) error { return nil } +// buildURL constructs the final URL by appending the given query parameters (if any). +func buildURL( + url string, + queryParameters url.Values, +) string { + if len(queryParameters) == 0 { + return url + } + if strings.ContainsRune(url, '?') { + url += "&" + } else { + url += "?" + } + url += queryParameters.Encode() + return url +} + // newRequest returns a new *http.Request with all of the fields // required to issue the call. func newRequest( @@ -220,8 +249,9 @@ func newRequest( method string, endpointHeaders http.Header, request interface{}, + bodyProperties map[string]interface{}, ) (*http.Request, error) { - requestBody, err := newRequestBody(request) + requestBody, err := newRequestBody(request, bodyProperties) if err != nil { return nil, err } @@ -238,20 +268,25 @@ func newRequest( } // newRequestBody returns a new io.Reader that represents the HTTP request body. -func newRequestBody(request interface{}) (io.Reader, error) { - var requestBody io.Reader - if request != nil { - if body, ok := request.(io.Reader); ok { - requestBody = body - } else { - requestBytes, err := json.Marshal(request) - if err != nil { - return nil, err - } - requestBody = bytes.NewReader(requestBytes) +func newRequestBody(request interface{}, bodyProperties map[string]interface{}) (io.Reader, error) { + if isNil(request) { + if len(bodyProperties) == 0 { + return nil, nil } + requestBytes, err := json.Marshal(bodyProperties) + if err != nil { + return nil, err + } + return bytes.NewReader(requestBytes), nil + } + if body, ok := request.(io.Reader); ok { + return body, nil + } + requestBytes, err := MarshalJSONWithExtraProperties(request, bodyProperties) + if err != nil { + return nil, err } - return requestBody, nil + return bytes.NewReader(requestBytes), nil } // decodeError decodes the error from the given HTTP response. Note that @@ -278,3 +313,9 @@ func decodeError(response *http.Response, errorDecoder ErrorDecoder) error { } return NewAPIError(response.StatusCode, errors.New(string(bytes))) } + +// isNil is used to determine if the request value is equal to nil (i.e. an interface +// value that holds a nil concrete value is itself non-nil). +func isNil(value interface{}) bool { + return value == nil || reflect.ValueOf(value).IsNil() +} diff --git a/core/core_test.go b/core/core_test.go index f476f9e..e6eaef3 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -9,6 +9,7 @@ import ( "io" "net/http" "net/http/httptest" + "net/url" "strconv" "testing" @@ -21,11 +22,14 @@ type TestCase struct { description string // Server-side assertions. + givePathSuffix string giveMethod string giveResponseIsOptional bool giveHeader http.Header giveErrorDecoder ErrorDecoder giveRequest *Request + giveQueryParams url.Values + giveBodyProperties map[string]interface{} // Client-side assertions. wantResponse *Response @@ -39,7 +43,9 @@ type Request struct { // Response a simple response body. type Response struct { - Id string `json:"id"` + Id string `json:"id"` + ExtraBodyProperties map[string]interface{} `json:"extraBodyProperties,omitempty"` + QueryParameters url.Values `json:"queryParameters,omitempty"` } // NotFoundError represents a 404. @@ -64,6 +70,23 @@ func TestCall(t *testing.T) { Id: "123", }, }, + { + description: "GET success with query", + givePathSuffix: "?limit=1", + giveMethod: http.MethodGet, + giveHeader: http.Header{ + "X-API-Status": []string{"success"}, + }, + giveRequest: &Request{ + Id: "123", + }, + wantResponse: &Response{ + Id: "123", + QueryParameters: url.Values{ + "limit": []string{"1"}, + }, + }, + }, { description: "GET not found", giveMethod: http.MethodGet, @@ -81,6 +104,18 @@ func TestCall(t *testing.T) { ), }, }, + { + description: "POST empty body", + giveMethod: http.MethodPost, + giveHeader: http.Header{ + "X-API-Status": []string{"fail"}, + }, + giveRequest: nil, + wantError: NewAPIError( + http.StatusBadRequest, + errors.New("invalid request"), + ), + }, { description: "POST optional response", giveMethod: http.MethodPost, @@ -106,6 +141,62 @@ func TestCall(t *testing.T) { errors.New("failed to process request"), ), }, + { + description: "POST extra properties", + giveMethod: http.MethodPost, + giveHeader: http.Header{ + "X-API-Status": []string{"success"}, + }, + giveRequest: new(Request), + giveBodyProperties: map[string]interface{}{ + "key": "value", + }, + wantResponse: &Response{ + ExtraBodyProperties: map[string]interface{}{ + "key": "value", + }, + }, + }, + { + description: "GET extra query parameters", + giveMethod: http.MethodGet, + giveHeader: http.Header{ + "X-API-Status": []string{"success"}, + }, + giveQueryParams: url.Values{ + "extra": []string{"true"}, + }, + giveRequest: &Request{ + Id: "123", + }, + wantResponse: &Response{ + Id: "123", + QueryParameters: url.Values{ + "extra": []string{"true"}, + }, + }, + }, + { + description: "GET merge extra query parameters", + givePathSuffix: "?limit=1", + giveMethod: http.MethodGet, + giveHeader: http.Header{ + "X-API-Status": []string{"success"}, + }, + giveRequest: &Request{ + Id: "123", + }, + giveQueryParams: url.Values{ + "extra": []string{"true"}, + }, + wantResponse: &Response{ + Id: "123", + QueryParameters: url.Values{ + "limit": []string{"1"}, + "extra": []string{"true"}, + }, + }, + }, } for _, test := range tests { t.Run(test.description, func(t *testing.T) { @@ -122,9 +213,11 @@ func TestCall(t *testing.T) { err := caller.Call( context.Background(), &CallParams{ - URL: server.URL, + URL: server.URL + test.givePathSuffix, Method: test.giveMethod, Headers: test.giveHeader, + BodyProperties: test.giveBodyProperties, + QueryParameters: test.giveQueryParams, Request: test.giveRequest, Response: &response, ResponseIsOptional: test.giveResponseIsOptional, @@ -215,10 +308,17 @@ func newTestServer(t *testing.T, tc *TestCase) *httptest.Server { assert.Equal(t, value, r.Header.Values(header)) } + request := new(Request) + bytes, err := io.ReadAll(r.Body) + if tc.giveRequest == nil { + require.Empty(t, bytes) + w.WriteHeader(http.StatusBadRequest) + _, err = w.Write([]byte("invalid request")) + require.NoError(t, err) + return + } require.NoError(t, err) - - request := new(Request) require.NoError(t, json.Unmarshal(bytes, request)) switch request.Id { @@ -249,8 +349,14 @@ func newTestServer(t *testing.T, tc *TestCase) *httptest.Server { return } + extraBodyProperties := make(map[string]interface{}) + require.NoError(t, json.Unmarshal(bytes, &extraBodyProperties)) + delete(extraBodyProperties, "id") + response := &Response{ - Id: request.Id, + Id: request.Id, + ExtraBodyProperties: extraBodyProperties, + QueryParameters: r.URL.Query(), } bytes, err = json.Marshal(response) require.NoError(t, err) diff --git a/core/query.go b/core/query.go index 3febdc7..2670ff7 100644 --- a/core/query.go +++ b/core/query.go @@ -116,7 +116,14 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error { continue } for i := 0; i < sv.Len(); i++ { - values.Add(name, valueString(sv.Index(i), opts, sf)) + value := sv.Index(i) + if isStructPointer(value) && !value.IsNil() { + if err := reflectValue(values, value.Elem(), name); err != nil { + return err + } + } else { + values.Add(name, valueString(value, opts, sf)) + } } continue } @@ -197,6 +204,11 @@ func isEmptyValue(v reflect.Value) bool { return false } +// isStructPointer returns true if the given reflect.Value is a pointer to a struct. +func isStructPointer(v reflect.Value) bool { + return v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct +} + // tagOptions is the string following a comma in a struct field's "url" tag, or // the empty string. It does not include the leading comma. type tagOptions []string diff --git a/core/query_test.go b/core/query_test.go index aad030a..5498fa9 100644 --- a/core/query_test.go +++ b/core/query_test.go @@ -157,4 +157,31 @@ func TestQueryValues(t *testing.T) { require.NoError(t, err) assert.Empty(t, values.Encode()) }) + + t.Run("object array", func(t *testing.T) { + type object struct { + Key string `json:"key" url:"key"` + Value string `json:"value" url:"value"` + } + type example struct { + Objects []*object `json:"objects,omitempty" url:"objects,omitempty"` + } + + values, err := QueryValues( + &example{ + Objects: []*object{ + { + Key: "hello", + Value: "world", + }, + { + Key: "foo", + Value: "bar", + }, + }, + }, + ) + require.NoError(t, err) + assert.Equal(t, "objects%5Bkey%5D=hello&objects%5Bkey%5D=foo&objects%5Bvalue%5D=world&objects%5Bvalue%5D=bar", values.Encode()) + }) } diff --git a/core/request_option.go b/core/request_option.go index f616b59..482cb7a 100644 --- a/core/request_option.go +++ b/core/request_option.go @@ -5,6 +5,7 @@ package core import ( fmt "fmt" http "net/http" + url "net/url" ) // RequestOption adapts the behavior of the client or an individual request. @@ -17,12 +18,14 @@ type RequestOption interface { // This type is primarily used by the generated code and is not meant // to be used directly; use the option package instead. type RequestOptions struct { - BaseURL string - HTTPClient HTTPClient - HTTPHeader http.Header - MaxAttempts uint - Token string - ClientName *string + BaseURL string + HTTPClient HTTPClient + HTTPHeader http.Header + BodyProperties map[string]interface{} + QueryParameters url.Values + MaxAttempts uint + Token string + ClientName *string } // NewRequestOptions returns a new *RequestOptions value. @@ -31,7 +34,9 @@ type RequestOptions struct { // to be used directly; use RequestOption instead. func NewRequestOptions(opts ...RequestOption) *RequestOptions { options := &RequestOptions{ - HTTPHeader: make(http.Header), + HTTPHeader: make(http.Header), + BodyProperties: make(map[string]interface{}), + QueryParameters: make(url.Values), } for _, opt := range opts { opt.applyRequestOptions(options) @@ -56,7 +61,7 @@ func (r *RequestOptions) cloneHeader() http.Header { headers := r.HTTPHeader.Clone() headers.Set("X-Fern-Language", "Go") headers.Set("X-Fern-SDK-Name", "github.com/cohere-ai/cohere-go/v2") - headers.Set("X-Fern-SDK-Version", "v2.11.0") + headers.Set("X-Fern-SDK-Version", "v2.12.0") return headers } @@ -87,6 +92,24 @@ func (h *HTTPHeaderOption) applyRequestOptions(opts *RequestOptions) { opts.HTTPHeader = h.HTTPHeader } +// BodyPropertiesOption implements the RequestOption interface. +type BodyPropertiesOption struct { + BodyProperties map[string]interface{} +} + +func (b *BodyPropertiesOption) applyRequestOptions(opts *RequestOptions) { + opts.BodyProperties = b.BodyProperties +} + +// QueryParametersOption implements the RequestOption interface. +type QueryParametersOption struct { + QueryParameters url.Values +} + +func (q *QueryParametersOption) applyRequestOptions(opts *RequestOptions) { + opts.QueryParameters = q.QueryParameters +} + // MaxAttemptsOption implements the RequestOption interface. type MaxAttemptsOption struct { MaxAttempts uint diff --git a/core/stream.go b/core/stream.go index 5d97e93..30e374d 100644 --- a/core/stream.go +++ b/core/stream.go @@ -6,10 +6,20 @@ import ( "encoding/json" "io" "net/http" + "net/url" "strings" ) -const defaultStreamDelimiter = '\n' +const ( + // DefaultDataPrefix is the default prefix used for SSE streaming. + DefaultSSEDataPrefix = "data: " + + // DefaultTerminator is the default terminator used for SSE streaming. + DefaultSSETerminator = "[DONE]" + + // The default stream delimiter used to split messages. + defaultStreamDelimiter = '\n' +) // Streamer calls APIs and streams responses using a *Stream. type Streamer[T any] struct { @@ -27,19 +37,31 @@ func NewStreamer[T any](caller *Caller) *Streamer[T] { // StreamParams represents the parameters used to issue an API streaming call. type StreamParams struct { - URL string - Method string - Delimiter string - MaxAttempts uint - Headers http.Header - Client HTTPClient - Request interface{} - ErrorDecoder ErrorDecoder + URL string + Method string + Prefix string + Delimiter string + Terminator string + MaxAttempts uint + Headers http.Header + BodyProperties map[string]interface{} + QueryParameters url.Values + Client HTTPClient + Request interface{} + ErrorDecoder ErrorDecoder } // Stream issues an API streaming call according to the given stream parameters. func (s *Streamer[T]) Stream(ctx context.Context, params *StreamParams) (*Stream[T], error) { - req, err := newRequest(ctx, params.URL, params.Method, params.Headers, params.Request) + url := buildURL(params.URL, params.QueryParameters) + req, err := newRequest( + ctx, + url, + params.Method, + params.Headers, + params.Request, + params.BodyProperties, + ) if err != nil { return nil, err } @@ -86,6 +108,13 @@ func (s *Streamer[T]) Stream(ctx context.Context, params *StreamParams) (*Stream if params.Delimiter != "" { opts = append(opts, WithDelimiter(params.Delimiter)) } + if params.Prefix != "" { + opts = append(opts, WithPrefix(params.Prefix)) + } + if params.Terminator != "" { + opts = append(opts, WithTerminator(params.Terminator)) + } + return NewStream[T](resp, opts...), nil } @@ -107,6 +136,24 @@ func WithDelimiter(delimiter string) StreamOption { } } +// WithPrefix overrides the prefix for the Stream. +// +// By default, the Stream doesn't have a prefix. +func WithPrefix(prefix string) StreamOption { + return func(opts *streamOptions) { + opts.prefix = prefix + } +} + +// WithTerminator overrides the terminator for the Stream. +// +// By default, the Stream terminates on EOF. +func WithTerminator(terminator string) StreamOption { + return func(opts *streamOptions) { + opts.terminator = terminator + } +} + // NewStream constructs a new Stream from the given *http.Response. func NewStream[T any](response *http.Response, opts ...StreamOption) *Stream[T] { options := new(streamOptions) @@ -114,7 +161,7 @@ func NewStream[T any](response *http.Response, opts ...StreamOption) *Stream[T] opt(options) } return &Stream[T]{ - reader: newStreamReader(response.Body, options.delimiter), + reader: newStreamReader(response.Body, options), closer: response.Body, } } @@ -149,9 +196,12 @@ type streamReader interface { // By default, the streamReader uses a simple a *bufio.Reader // which splits on newlines, and otherwise use a *bufio.Scanner to // split on custom delimiters. -func newStreamReader(reader io.Reader, delimiter string) streamReader { - if len(delimiter) > 0 { - return newScannerStreamReader(reader, delimiter) +func newStreamReader(reader io.Reader, options *streamOptions) streamReader { + if !options.isEmpty() { + if options.delimiter == "" { + options.delimiter = string(defaultStreamDelimiter) + } + return newScannerStreamReader(reader, options) } return newBufferStreamReader(reader) } @@ -176,37 +226,69 @@ func (b *bufferStreamReader) ReadFromStream() ([]byte, error) { // configurable delimiters. type scannerStreamReader struct { scanner *bufio.Scanner + options *streamOptions } -func newScannerStreamReader(reader io.Reader, delimiter string) *scannerStreamReader { +func newScannerStreamReader( + reader io.Reader, + options *streamOptions, +) *scannerStreamReader { scanner := bufio.NewScanner(reader) - scanner.Split(func(data []byte, atEOF bool) (int, []byte, error) { - if atEOF && len(data) == 0 { + stream := &scannerStreamReader{ + scanner: scanner, + options: options, + } + scanner.Split(func(bytes []byte, atEOF bool) (int, []byte, error) { + if atEOF && len(bytes) == 0 { return 0, nil, nil } - if i := strings.Index(string(data), delimiter); i >= 0 { - return i + len(delimiter), data[0:i], nil - } - if atEOF { - return len(data), data, nil + n, data, err := stream.parse(bytes) + if stream.isTerminated(data) { + return 0, nil, io.EOF } - return 0, nil, nil + return n, data, err }) - return &scannerStreamReader{ - scanner: scanner, - } + return stream } -func (b *scannerStreamReader) ReadFromStream() ([]byte, error) { - if b.scanner.Scan() { - return b.scanner.Bytes(), nil +func (s *scannerStreamReader) ReadFromStream() ([]byte, error) { + if s.scanner.Scan() { + return s.scanner.Bytes(), nil } - if err := b.scanner.Err(); err != nil { + if err := s.scanner.Err(); err != nil { return nil, err } return nil, io.EOF } +func (s *scannerStreamReader) parse(bytes []byte) (int, []byte, error) { + var start int + if s.options != nil && s.options.prefix != "" { + if i := strings.Index(string(bytes), s.options.prefix); i >= 0 { + start = i + len(s.options.prefix) + } + } + data := bytes[start:] + if i := strings.Index(string(data), s.options.delimiter); i >= 0 { + data = data[:i+len(s.options.delimiter)] + } + n := start + len(data) + len(s.options.delimiter) + return n, data, nil +} + +func (s *scannerStreamReader) isTerminated(bytes []byte) bool { + if s.options == nil || s.options.terminator == "" { + return false + } + return strings.Contains(string(bytes), s.options.terminator) +} + type streamOptions struct { - delimiter string + delimiter string + prefix string + terminator string +} + +func (s *streamOptions) isEmpty() bool { + return s.delimiter == "" && s.prefix == "" && s.terminator == "" } diff --git a/datasets/client.go b/datasets/client.go index a3e300c..c07d3d2 100644 --- a/datasets/client.go +++ b/datasets/client.go @@ -159,13 +159,15 @@ func (c *Client) List( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -326,14 +328,16 @@ func (c *Client) Create( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: requestBuffer, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: requestBuffer, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -452,13 +456,15 @@ func (c *Client) GetUsage( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -578,13 +584,15 @@ func (c *Client) Get( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -704,13 +712,15 @@ func (c *Client) Delete( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodDelete, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodDelete, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err diff --git a/embedjobs/client.go b/embedjobs/client.go index 2c7fde8..cb513db 100644 --- a/embedjobs/client.go +++ b/embedjobs/client.go @@ -149,13 +149,15 @@ func (c *Client) List( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -275,14 +277,16 @@ func (c *Client) Create( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -403,13 +407,15 @@ func (c *Client) Get( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -529,12 +535,14 @@ func (c *Client) Cancel( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + ErrorDecoder: errorDecoder, }, ); err != nil { return err diff --git a/finetuning/client/client.go b/finetuning/client/client.go index fde2f0e..1da4cbe 100644 --- a/finetuning/client/client.go +++ b/finetuning/client/client.go @@ -123,13 +123,15 @@ func (c *Client) ListFinetunedModels( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -213,14 +215,16 @@ func (c *Client) CreateFinetunedModel( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPost, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -305,13 +309,15 @@ func (c *Client) GetFinetunedModel( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -396,13 +402,15 @@ func (c *Client) DeleteFinetunedModel( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodDelete, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodDelete, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -488,14 +496,16 @@ func (c *Client) UpdateFinetunedModel( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodPatch, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Request: request, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodPatch, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -589,13 +599,15 @@ func (c *Client) ListEvents( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -689,13 +701,15 @@ func (c *Client) ListTrainingStepMetrics( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err diff --git a/finetuning/types.go b/finetuning/types.go index f01ac32..933a5e0 100644 --- a/finetuning/types.go +++ b/finetuning/types.go @@ -17,7 +17,7 @@ type BaseModel struct { Version *string `json:"version,omitempty" url:"version,omitempty"` // The type of the base model. BaseType BaseType `json:"base_type" url:"base_type"` - // The fine-tuning strategy. + // Deprecated: The fine-tuning strategy. Strategy *Strategy `json:"strategy,omitempty" url:"strategy,omitempty"` extraProperties map[string]interface{} @@ -61,7 +61,7 @@ func (b *BaseModel) String() string { // The possible types of fine-tuned models. // // - BASE_TYPE_UNSPECIFIED: Unspecified model. -// - BASE_TYPE_GENERATIVE: Generative model. +// - BASE_TYPE_GENERATIVE: Deprecated: Generative model. // - BASE_TYPE_CLASSIFICATION: Classification model. // - BASE_TYPE_RERANK: Rerank model. // - BASE_TYPE_CHAT: Chat model. @@ -355,6 +355,14 @@ type Hyperparameters struct { TrainEpochs *int `json:"train_epochs,omitempty" url:"train_epochs,omitempty"` // The learning rate to be used during training. LearningRate *float64 `json:"learning_rate,omitempty" url:"learning_rate,omitempty"` + // Controls the scaling factor for LoRA updates. Higher values make the + // updates more impactful. + LoraAlpha *int `json:"lora_alpha,omitempty" url:"lora_alpha,omitempty"` + // Specifies the rank for low-rank matrices. Lower ranks reduce parameters + // but may limit model flexibility. + LoraRank *int `json:"lora_rank,omitempty" url:"lora_rank,omitempty"` + // The combination of LoRA modules to target. + LoraTargetModules *LoraTargetModules `json:"lora_target_modules,omitempty" url:"lora_target_modules,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage @@ -536,6 +544,40 @@ func (l *ListTrainingStepMetricsResponse) String() string { return fmt.Sprintf("%#v", l) } +// The possible combinations of LoRA modules to target. +// +// - LORA_TARGET_MODULES_UNSPECIFIED: Unspecified LoRA target modules. +// - LORA_TARGET_MODULES_QV: LoRA adapts the query and value matrices in transformer attention layers. +// - LORA_TARGET_MODULES_QKVO: LoRA adapts query, key, value, and output matrices in attention layers. +// - LORA_TARGET_MODULES_QKVO_FFN: LoRA adapts attention projection matrices and feed-forward networks (FFN). +type LoraTargetModules string + +const ( + LoraTargetModulesLoraTargetModulesUnspecified LoraTargetModules = "LORA_TARGET_MODULES_UNSPECIFIED" + LoraTargetModulesLoraTargetModulesQv LoraTargetModules = "LORA_TARGET_MODULES_QV" + LoraTargetModulesLoraTargetModulesQkvo LoraTargetModules = "LORA_TARGET_MODULES_QKVO" + LoraTargetModulesLoraTargetModulesQkvoFfn LoraTargetModules = "LORA_TARGET_MODULES_QKVO_FFN" +) + +func NewLoraTargetModulesFromString(s string) (LoraTargetModules, error) { + switch s { + case "LORA_TARGET_MODULES_UNSPECIFIED": + return LoraTargetModulesLoraTargetModulesUnspecified, nil + case "LORA_TARGET_MODULES_QV": + return LoraTargetModulesLoraTargetModulesQv, nil + case "LORA_TARGET_MODULES_QKVO": + return LoraTargetModulesLoraTargetModulesQkvo, nil + case "LORA_TARGET_MODULES_QKVO_FFN": + return LoraTargetModulesLoraTargetModulesQkvoFfn, nil + } + var t LoraTargetModules + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (l LoraTargetModules) Ptr() *LoraTargetModules { + return &l +} + // The configuration used for fine-tuning. type Settings struct { // The base model to fine-tune. @@ -546,7 +588,7 @@ type Settings struct { Hyperparameters *Hyperparameters `json:"hyperparameters,omitempty" url:"hyperparameters,omitempty"` // read-only. Whether the model is single-label or multi-label (only for classification). MultiLabel *bool `json:"multi_label,omitempty" url:"multi_label,omitempty"` - // The Weights & Biases configuration. + // The Weights & Biases configuration (Chat fine-tuning only). Wandb *WandbConfig `json:"wandb,omitempty" url:"wandb,omitempty"` extraProperties map[string]interface{} @@ -644,7 +686,7 @@ func (s Status) Ptr() *Status { // The possible strategy used to serve a fine-tuned models. // // - STRATEGY_UNSPECIFIED: Unspecified strategy. -// - STRATEGY_VANILLA: Serve the fine-tuned model on a dedicated GPU. +// - STRATEGY_VANILLA: Deprecated: Serve the fine-tuned model on a dedicated GPU. // - STRATEGY_TFEW: Serve the fine-tuned model on a shared GPU. type Strategy string diff --git a/models/client.go b/models/client.go index 88bdd06..7932f9f 100644 --- a/models/client.go +++ b/models/client.go @@ -150,13 +150,15 @@ func (c *Client) Get( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err @@ -284,13 +286,15 @@ func (c *Client) List( if err := c.caller.Call( ctx, &core.CallParams{ - URL: endpointURL, - Method: http.MethodGet, - MaxAttempts: options.MaxAttempts, - Headers: headers, - Client: options.HTTPClient, - Response: &response, - ErrorDecoder: errorDecoder, + URL: endpointURL, + Method: http.MethodGet, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: errorDecoder, }, ); err != nil { return nil, err diff --git a/option/request_option.go b/option/request_option.go index db015d9..7c7a202 100644 --- a/option/request_option.go +++ b/option/request_option.go @@ -5,6 +5,7 @@ package option import ( core "github.com/cohere-ai/cohere-go/v2/core" http "net/http" + url "net/url" ) // RequestOption adapts the behavior of an indivdual request. @@ -33,6 +34,28 @@ func WithHTTPHeader(httpHeader http.Header) *core.HTTPHeaderOption { } } +// WithBodyProperties adds the given body properties to the request. +func WithBodyProperties(bodyProperties map[string]interface{}) *core.BodyPropertiesOption { + copiedBodyProperties := make(map[string]interface{}, len(bodyProperties)) + for key, value := range bodyProperties { + copiedBodyProperties[key] = value + } + return &core.BodyPropertiesOption{ + BodyProperties: copiedBodyProperties, + } +} + +// WithQueryParameters adds the given query parameters to the request. +func WithQueryParameters(queryParameters url.Values) *core.QueryParametersOption { + copiedQueryParameters := make(url.Values, len(queryParameters)) + for key, values := range queryParameters { + copiedQueryParameters[key] = values + } + return &core.QueryParametersOption{ + QueryParameters: copiedQueryParameters, + } +} + // WithMaxAttempts configures the maximum number of retry attempts. func WithMaxAttempts(attempts uint) *core.MaxAttemptsOption { return &core.MaxAttemptsOption{ diff --git a/types.go b/types.go index 1e21b06..7557ada 100644 --- a/types.go +++ b/types.go @@ -10,6 +10,8 @@ import ( ) type ChatRequest struct { + // Pass text/event-stream to receive the streamed response as server-sent events. The default is `\n` delimited events. + Accepts *string `json:"-" url:"-"` // Text input for the model to respond to. // // Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments @@ -237,6 +239,8 @@ func (c *ChatRequest) MarshalJSON() ([]byte, error) { } type ChatStreamRequest struct { + // Pass text/event-stream to receive the streamed response as server-sent events. The default is `\n` delimited events. + Accepts *string `json:"-" url:"-"` // Text input for the model to respond to. // // Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments @@ -491,6 +495,10 @@ type DetokenizeRequest struct { type EmbedRequest struct { // An array of strings for the model to embed. Maximum number of texts per call is `96`. We recommend reducing the length of each text to be under `512` tokens for optimal quality. Texts []string `json:"texts,omitempty" url:"-"` + // An array of image data URIs for the model to embed. Maximum number of images per call is `1`. + // + // The image must be a valid [data URI](https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/data). The image must be in either `image/jpeg` or `image/png` format and has a maximum size of 5MB. + Images []string `json:"images,omitempty" url:"-"` // Defaults to embed-english-v2.0 // // The identifier of the model. Smaller "light" models are faster, while larger models will perform better. [Custom models](/docs/training-custom-models) can also be supplied with their full ID. @@ -947,6 +955,258 @@ func (a *ApiMetaTokens) String() string { return fmt.Sprintf("%#v", a) } +// A message from the assistant role can contain text and tool call information. +type AssistantMessage struct { + ToolCalls []*ToolCallV2 `json:"tool_calls,omitempty" url:"tool_calls,omitempty"` + // A chain-of-thought style reflection and plan that the model generates when working with Tools. + ToolPlan *string `json:"tool_plan,omitempty" url:"tool_plan,omitempty"` + Content *AssistantMessageContent `json:"content,omitempty" url:"content,omitempty"` + Citations []*Citation `json:"citations,omitempty" url:"citations,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (a *AssistantMessage) GetExtraProperties() map[string]interface{} { + return a.extraProperties +} + +func (a *AssistantMessage) UnmarshalJSON(data []byte) error { + type unmarshaler AssistantMessage + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *a = AssistantMessage(value) + + extraProperties, err := core.ExtractExtraProperties(data, *a) + if err != nil { + return err + } + a.extraProperties = extraProperties + + a._rawJSON = json.RawMessage(data) + return nil +} + +func (a *AssistantMessage) String() string { + if len(a._rawJSON) > 0 { + if value, err := core.StringifyJSON(a._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(a); err == nil { + return value + } + return fmt.Sprintf("%#v", a) +} + +type AssistantMessageContent struct { + String string + AssistantMessageContentItemList []*AssistantMessageContentItem +} + +func (a *AssistantMessageContent) UnmarshalJSON(data []byte) error { + var valueString string + if err := json.Unmarshal(data, &valueString); err == nil { + a.String = valueString + return nil + } + var valueAssistantMessageContentItemList []*AssistantMessageContentItem + if err := json.Unmarshal(data, &valueAssistantMessageContentItemList); err == nil { + a.AssistantMessageContentItemList = valueAssistantMessageContentItemList + return nil + } + return fmt.Errorf("%s cannot be deserialized as a %T", data, a) +} + +func (a AssistantMessageContent) MarshalJSON() ([]byte, error) { + if a.String != "" { + return json.Marshal(a.String) + } + if a.AssistantMessageContentItemList != nil { + return json.Marshal(a.AssistantMessageContentItemList) + } + return nil, fmt.Errorf("type %T does not include a non-empty union type", a) +} + +type AssistantMessageContentVisitor interface { + VisitString(string) error + VisitAssistantMessageContentItemList([]*AssistantMessageContentItem) error +} + +func (a *AssistantMessageContent) Accept(visitor AssistantMessageContentVisitor) error { + if a.String != "" { + return visitor.VisitString(a.String) + } + if a.AssistantMessageContentItemList != nil { + return visitor.VisitAssistantMessageContentItemList(a.AssistantMessageContentItemList) + } + return fmt.Errorf("type %T does not include a non-empty union type", a) +} + +type AssistantMessageContentItem struct { + Type string + Text *TextContent +} + +func (a *AssistantMessageContentItem) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + Type string `json:"type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + a.Type = unmarshaler.Type + if unmarshaler.Type == "" { + return fmt.Errorf("%T did not include discriminant type", a) + } + switch unmarshaler.Type { + case "text": + value := new(TextContent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + a.Text = value + } + return nil +} + +func (a AssistantMessageContentItem) MarshalJSON() ([]byte, error) { + if a.Text != nil { + return core.MarshalJSONWithExtraProperty(a.Text, "type", "text") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", a) +} + +type AssistantMessageContentItemVisitor interface { + VisitText(*TextContent) error +} + +func (a *AssistantMessageContentItem) Accept(visitor AssistantMessageContentItemVisitor) error { + if a.Text != nil { + return visitor.VisitText(a.Text) + } + return fmt.Errorf("type %T does not define a non-empty union type", a) +} + +// A message from the assistant role can contain text and tool call information. +type AssistantMessageResponse struct { + ToolCalls []*ToolCallV2 `json:"tool_calls,omitempty" url:"tool_calls,omitempty"` + // A chain-of-thought style reflection and plan that the model generates when working with Tools. + ToolPlan *string `json:"tool_plan,omitempty" url:"tool_plan,omitempty"` + Content []*AssistantMessageResponseContentItem `json:"content,omitempty" url:"content,omitempty"` + Citations []*Citation `json:"citations,omitempty" url:"citations,omitempty"` + role string + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (a *AssistantMessageResponse) GetExtraProperties() map[string]interface{} { + return a.extraProperties +} + +func (a *AssistantMessageResponse) Role() string { + return a.role +} + +func (a *AssistantMessageResponse) UnmarshalJSON(data []byte) error { + type embed AssistantMessageResponse + var unmarshaler = struct { + embed + Role string `json:"role"` + }{ + embed: embed(*a), + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + *a = AssistantMessageResponse(unmarshaler.embed) + if unmarshaler.Role != "assistant" { + return fmt.Errorf("unexpected value for literal on type %T; expected %v got %v", a, "assistant", unmarshaler.Role) + } + a.role = unmarshaler.Role + + extraProperties, err := core.ExtractExtraProperties(data, *a, "role") + if err != nil { + return err + } + a.extraProperties = extraProperties + + a._rawJSON = json.RawMessage(data) + return nil +} + +func (a *AssistantMessageResponse) MarshalJSON() ([]byte, error) { + type embed AssistantMessageResponse + var marshaler = struct { + embed + Role string `json:"role"` + }{ + embed: embed(*a), + Role: "assistant", + } + return json.Marshal(marshaler) +} + +func (a *AssistantMessageResponse) String() string { + if len(a._rawJSON) > 0 { + if value, err := core.StringifyJSON(a._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(a); err == nil { + return value + } + return fmt.Sprintf("%#v", a) +} + +type AssistantMessageResponseContentItem struct { + Type string + Text *TextContent +} + +func (a *AssistantMessageResponseContentItem) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + Type string `json:"type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + a.Type = unmarshaler.Type + if unmarshaler.Type == "" { + return fmt.Errorf("%T did not include discriminant type", a) + } + switch unmarshaler.Type { + case "text": + value := new(TextContent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + a.Text = value + } + return nil +} + +func (a AssistantMessageResponseContentItem) MarshalJSON() ([]byte, error) { + if a.Text != nil { + return core.MarshalJSONWithExtraProperty(a.Text, "type", "text") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", a) +} + +type AssistantMessageResponseContentItemVisitor interface { + VisitText(*TextContent) error +} + +func (a *AssistantMessageResponseContentItem) Accept(visitor AssistantMessageResponseContentItemVisitor) error { + if a.Text != nil { + return visitor.VisitText(a.Text) + } + return fmt.Errorf("type %T does not define a non-empty union type", a) +} + // The token_type specifies the way the token is passed in the Authorization header. Valid values are "bearer", "basic", and "noscheme". type AuthTokenType string @@ -1117,29 +1377,26 @@ func (c *ChatConnector) String() string { return fmt.Sprintf("%#v", c) } -type ChatDataMetrics struct { - // The sum of all turns of valid train examples. - NumTrainTurns *int64 `json:"num_train_turns,omitempty" url:"num_train_turns,omitempty"` - // The sum of all turns of valid eval examples. - NumEvalTurns *int64 `json:"num_eval_turns,omitempty" url:"num_eval_turns,omitempty"` - // The preamble of this dataset. - Preamble *string `json:"preamble,omitempty" url:"preamble,omitempty"` +// A streamed delta event which contains a delta of chat text content. +type ChatContentDeltaEvent struct { + Index *int `json:"index,omitempty" url:"index,omitempty"` + Delta *ChatContentDeltaEventDelta `json:"delta,omitempty" url:"delta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatDataMetrics) GetExtraProperties() map[string]interface{} { +func (c *ChatContentDeltaEvent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatDataMetrics) UnmarshalJSON(data []byte) error { - type unmarshaler ChatDataMetrics +func (c *ChatContentDeltaEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatContentDeltaEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatDataMetrics(value) + *c = ChatContentDeltaEvent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1151,7 +1408,7 @@ func (c *ChatDataMetrics) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatDataMetrics) String() string { +func (c *ChatContentDeltaEvent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1163,35 +1420,24 @@ func (c *ChatDataMetrics) String() string { return fmt.Sprintf("%#v", c) } -// Relevant information that could be used by the model to generate a more accurate reply. -// The contents of each document are generally short (under 300 words), and are passed in the form of a -// dictionary of strings. Some suggested keys are "text", "author", "date". Both the key name and the value will be -// passed to the model. -type ChatDocument = map[string]string - -// Represents a single message in the chat history, excluding the current user turn. It has two properties: `role` and `message`. The `role` identifies the sender (`CHATBOT`, `SYSTEM`, or `USER`), while the `message` contains the text content. -// -// The chat_history parameter should not be used for `SYSTEM` messages in most cases. Instead, to add a `SYSTEM` role message at the beginning of a conversation, the `preamble` parameter should be used. -type ChatMessage struct { - // Contents of the chat message. - Message string `json:"message" url:"message"` - ToolCalls []*ToolCall `json:"tool_calls,omitempty" url:"tool_calls,omitempty"` +type ChatContentDeltaEventDelta struct { + Message *ChatContentDeltaEventDeltaMessage `json:"message,omitempty" url:"message,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatMessage) GetExtraProperties() map[string]interface{} { +func (c *ChatContentDeltaEventDelta) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatMessage) UnmarshalJSON(data []byte) error { - type unmarshaler ChatMessage +func (c *ChatContentDeltaEventDelta) UnmarshalJSON(data []byte) error { + type unmarshaler ChatContentDeltaEventDelta var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatMessage(value) + *c = ChatContentDeltaEventDelta(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1203,7 +1449,7 @@ func (c *ChatMessage) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatMessage) String() string { +func (c *ChatContentDeltaEventDelta) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1215,61 +1461,65 @@ func (c *ChatMessage) String() string { return fmt.Sprintf("%#v", c) } -// Defaults to `"accurate"`. -// -// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results. -// -// Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments -type ChatRequestCitationQuality string +type ChatContentDeltaEventDeltaMessage struct { + Content *ChatContentDeltaEventDeltaMessageContent `json:"content,omitempty" url:"content,omitempty"` -const ( - ChatRequestCitationQualityFast ChatRequestCitationQuality = "fast" - ChatRequestCitationQualityAccurate ChatRequestCitationQuality = "accurate" - ChatRequestCitationQualityOff ChatRequestCitationQuality = "off" -) + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} -func NewChatRequestCitationQualityFromString(s string) (ChatRequestCitationQuality, error) { - switch s { - case "fast": - return ChatRequestCitationQualityFast, nil - case "accurate": - return ChatRequestCitationQualityAccurate, nil - case "off": - return ChatRequestCitationQualityOff, nil +func (c *ChatContentDeltaEventDeltaMessage) GetExtraProperties() map[string]interface{} { + return c.extraProperties +} + +func (c *ChatContentDeltaEventDeltaMessage) UnmarshalJSON(data []byte) error { + type unmarshaler ChatContentDeltaEventDeltaMessage + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err } - var t ChatRequestCitationQuality - return "", fmt.Errorf("%s is not a valid %T", s, t) + *c = ChatContentDeltaEventDeltaMessage(value) + + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err + } + c.extraProperties = extraProperties + + c._rawJSON = json.RawMessage(data) + return nil } -func (c ChatRequestCitationQuality) Ptr() *ChatRequestCitationQuality { - return &c +func (c *ChatContentDeltaEventDeltaMessage) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) } -// (internal) Sets inference and model options for RAG search query and tool use generations. Defaults are used when options are not specified here, meaning that other parameters outside of connectors_search_options are ignored (such as model= or temperature=). -type ChatRequestConnectorsSearchOptions struct { - // If specified, the backend will make a best effort to sample tokens - // deterministically, such that repeated requests with the same - // seed and parameters should return the same result. However, - // determinism cannot be totally guaranteed. - // - // Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments - Seed *int `json:"seed,omitempty" url:"seed,omitempty"` +type ChatContentDeltaEventDeltaMessageContent struct { + Text *string `json:"text,omitempty" url:"text,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatRequestConnectorsSearchOptions) GetExtraProperties() map[string]interface{} { +func (c *ChatContentDeltaEventDeltaMessageContent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatRequestConnectorsSearchOptions) UnmarshalJSON(data []byte) error { - type unmarshaler ChatRequestConnectorsSearchOptions +func (c *ChatContentDeltaEventDeltaMessageContent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatContentDeltaEventDeltaMessageContent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatRequestConnectorsSearchOptions(value) + *c = ChatContentDeltaEventDeltaMessageContent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1281,7 +1531,7 @@ func (c *ChatRequestConnectorsSearchOptions) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatRequestConnectorsSearchOptions) String() string { +func (c *ChatContentDeltaEventDeltaMessageContent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1293,97 +1543,25 @@ func (c *ChatRequestConnectorsSearchOptions) String() string { return fmt.Sprintf("%#v", c) } -// Defaults to `AUTO` when `connectors` are specified and `OFF` in all other cases. -// -// Dictates how the prompt will be constructed. -// -// With `prompt_truncation` set to "AUTO", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be changed and ranked by relevance. -// -// With `prompt_truncation` set to "AUTO_PRESERVE_ORDER", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be preserved as they are inputted into the API. -// -// With `prompt_truncation` set to "OFF", no elements will be dropped. If the sum of the inputs exceeds the model's context length limit, a `TooManyTokens` error will be returned. -// -// Compatible Deployments: -// -// - AUTO: Cohere Platform Only -// - AUTO_PRESERVE_ORDER: Azure, AWS Sagemaker/Bedrock, Private Deployments -type ChatRequestPromptTruncation string - -const ( - ChatRequestPromptTruncationOff ChatRequestPromptTruncation = "OFF" - ChatRequestPromptTruncationAuto ChatRequestPromptTruncation = "AUTO" - ChatRequestPromptTruncationAutoPreserveOrder ChatRequestPromptTruncation = "AUTO_PRESERVE_ORDER" -) - -func NewChatRequestPromptTruncationFromString(s string) (ChatRequestPromptTruncation, error) { - switch s { - case "OFF": - return ChatRequestPromptTruncationOff, nil - case "AUTO": - return ChatRequestPromptTruncationAuto, nil - case "AUTO_PRESERVE_ORDER": - return ChatRequestPromptTruncationAutoPreserveOrder, nil - } - var t ChatRequestPromptTruncation - return "", fmt.Errorf("%s is not a valid %T", s, t) -} - -func (c ChatRequestPromptTruncation) Ptr() *ChatRequestPromptTruncation { - return &c -} - -// Used to select the [safety instruction](/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`. -// When `NONE` is specified, the safety instruction will be omitted. -// -// Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters. -// -// **Note**: This parameter is only compatible with models [Command R 08-2024](/docs/command-r#august-2024-release), [Command R+ 08-2024](/docs/command-r-plus#august-2024-release) and newer. -// -// Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments -type ChatRequestSafetyMode string - -const ( - ChatRequestSafetyModeContextual ChatRequestSafetyMode = "CONTEXTUAL" - ChatRequestSafetyModeStrict ChatRequestSafetyMode = "STRICT" - ChatRequestSafetyModeNone ChatRequestSafetyMode = "NONE" -) - -func NewChatRequestSafetyModeFromString(s string) (ChatRequestSafetyMode, error) { - switch s { - case "CONTEXTUAL": - return ChatRequestSafetyModeContextual, nil - case "STRICT": - return ChatRequestSafetyModeStrict, nil - case "NONE": - return ChatRequestSafetyModeNone, nil - } - var t ChatRequestSafetyMode - return "", fmt.Errorf("%s is not a valid %T", s, t) -} - -func (c ChatRequestSafetyMode) Ptr() *ChatRequestSafetyMode { - return &c -} - -type ChatSearchQueriesGenerationEvent struct { - // Generated search queries, meant to be used as part of the RAG flow. - SearchQueries []*ChatSearchQuery `json:"search_queries,omitempty" url:"search_queries,omitempty"` +// A streamed delta event which signifies that the content block has ended. +type ChatContentEndEvent struct { + Index *int `json:"index,omitempty" url:"index,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatSearchQueriesGenerationEvent) GetExtraProperties() map[string]interface{} { +func (c *ChatContentEndEvent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatSearchQueriesGenerationEvent) UnmarshalJSON(data []byte) error { - type unmarshaler ChatSearchQueriesGenerationEvent +func (c *ChatContentEndEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatContentEndEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatSearchQueriesGenerationEvent(value) + *c = ChatContentEndEvent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1395,7 +1573,7 @@ func (c *ChatSearchQueriesGenerationEvent) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatSearchQueriesGenerationEvent) String() string { +func (c *ChatContentEndEvent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1407,28 +1585,26 @@ func (c *ChatSearchQueriesGenerationEvent) String() string { return fmt.Sprintf("%#v", c) } -// The generated search query. Contains the text of the query and a unique identifier for the query. -type ChatSearchQuery struct { - // The text of the search query. - Text string `json:"text" url:"text"` - // Unique identifier for the generated search query. Useful for submitting feedback. - GenerationId string `json:"generation_id" url:"generation_id"` +// A streamed delta event which signifies that a new content block has started. +type ChatContentStartEvent struct { + Index *int `json:"index,omitempty" url:"index,omitempty"` + Delta *ChatContentStartEventDelta `json:"delta,omitempty" url:"delta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatSearchQuery) GetExtraProperties() map[string]interface{} { +func (c *ChatContentStartEvent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatSearchQuery) UnmarshalJSON(data []byte) error { - type unmarshaler ChatSearchQuery +func (c *ChatContentStartEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatContentStartEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatSearchQuery(value) + *c = ChatContentStartEvent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1440,7 +1616,7 @@ func (c *ChatSearchQuery) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatSearchQuery) String() string { +func (c *ChatContentStartEvent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1452,32 +1628,24 @@ func (c *ChatSearchQuery) String() string { return fmt.Sprintf("%#v", c) } -type ChatSearchResult struct { - SearchQuery *ChatSearchQuery `json:"search_query,omitempty" url:"search_query,omitempty"` - // The connector from which this result comes from. - Connector *ChatSearchResultConnector `json:"connector,omitempty" url:"connector,omitempty"` - // Identifiers of documents found by this search query. - DocumentIds []string `json:"document_ids,omitempty" url:"document_ids,omitempty"` - // An error message if the search failed. - ErrorMessage *string `json:"error_message,omitempty" url:"error_message,omitempty"` - // Whether a chat request should continue or not if the request to this connector fails. - ContinueOnFailure *bool `json:"continue_on_failure,omitempty" url:"continue_on_failure,omitempty"` +type ChatContentStartEventDelta struct { + Message *ChatContentStartEventDeltaMessage `json:"message,omitempty" url:"message,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatSearchResult) GetExtraProperties() map[string]interface{} { +func (c *ChatContentStartEventDelta) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatSearchResult) UnmarshalJSON(data []byte) error { - type unmarshaler ChatSearchResult +func (c *ChatContentStartEventDelta) UnmarshalJSON(data []byte) error { + type unmarshaler ChatContentStartEventDelta var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatSearchResult(value) + *c = ChatContentStartEventDelta(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1489,7 +1657,7 @@ func (c *ChatSearchResult) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatSearchResult) String() string { +func (c *ChatContentStartEventDelta) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1501,26 +1669,24 @@ func (c *ChatSearchResult) String() string { return fmt.Sprintf("%#v", c) } -// The connector used for fetching documents. -type ChatSearchResultConnector struct { - // The identifier of the connector. - Id string `json:"id" url:"id"` +type ChatContentStartEventDeltaMessage struct { + Content *ChatContentStartEventDeltaMessageContent `json:"content,omitempty" url:"content,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatSearchResultConnector) GetExtraProperties() map[string]interface{} { +func (c *ChatContentStartEventDeltaMessage) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatSearchResultConnector) UnmarshalJSON(data []byte) error { - type unmarshaler ChatSearchResultConnector +func (c *ChatContentStartEventDeltaMessage) UnmarshalJSON(data []byte) error { + type unmarshaler ChatContentStartEventDeltaMessage var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatSearchResultConnector(value) + *c = ChatContentStartEventDeltaMessage(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1532,7 +1698,7 @@ func (c *ChatSearchResultConnector) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatSearchResultConnector) String() string { +func (c *ChatContentStartEventDeltaMessage) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1544,27 +1710,25 @@ func (c *ChatSearchResultConnector) String() string { return fmt.Sprintf("%#v", c) } -type ChatSearchResultsEvent struct { - // Conducted searches and the ids of documents retrieved from each of them. - SearchResults []*ChatSearchResult `json:"search_results,omitempty" url:"search_results,omitempty"` - // Documents fetched from searches or provided by the user. - Documents []ChatDocument `json:"documents,omitempty" url:"documents,omitempty"` +type ChatContentStartEventDeltaMessageContent struct { + Text *string `json:"text,omitempty" url:"text,omitempty"` + Type *string `json:"type,omitempty" url:"type,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatSearchResultsEvent) GetExtraProperties() map[string]interface{} { +func (c *ChatContentStartEventDeltaMessageContent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatSearchResultsEvent) UnmarshalJSON(data []byte) error { - type unmarshaler ChatSearchResultsEvent +func (c *ChatContentStartEventDeltaMessageContent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatContentStartEventDeltaMessageContent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatSearchResultsEvent(value) + *c = ChatContentStartEventDeltaMessageContent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1576,7 +1740,7 @@ func (c *ChatSearchResultsEvent) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatSearchResultsEvent) String() string { +func (c *ChatContentStartEventDeltaMessageContent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1588,31 +1752,29 @@ func (c *ChatSearchResultsEvent) String() string { return fmt.Sprintf("%#v", c) } -type ChatStreamEndEvent struct { - // - `COMPLETE` - the model sent back a finished reply - // - `ERROR_LIMIT` - the reply was cut off because the model reached the maximum number of tokens for its context length - // - `MAX_TOKENS` - the reply was cut off because the model reached the maximum number of tokens specified by the max_tokens parameter - // - `ERROR` - something went wrong when generating the reply - // - `ERROR_TOXIC` - the model generated a reply that was deemed toxic - FinishReason ChatStreamEndEventFinishReason `json:"finish_reason" url:"finish_reason"` - // The consolidated response from the model. Contains the generated reply and all the other information streamed back in the previous events. - Response *NonStreamedChatResponse `json:"response,omitempty" url:"response,omitempty"` +type ChatDataMetrics struct { + // The sum of all turns of valid train examples. + NumTrainTurns *int64 `json:"num_train_turns,omitempty" url:"num_train_turns,omitempty"` + // The sum of all turns of valid eval examples. + NumEvalTurns *int64 `json:"num_eval_turns,omitempty" url:"num_eval_turns,omitempty"` + // The preamble of this dataset. + Preamble *string `json:"preamble,omitempty" url:"preamble,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatStreamEndEvent) GetExtraProperties() map[string]interface{} { +func (c *ChatDataMetrics) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatStreamEndEvent) UnmarshalJSON(data []byte) error { - type unmarshaler ChatStreamEndEvent +func (c *ChatDataMetrics) UnmarshalJSON(data []byte) error { + type unmarshaler ChatDataMetrics var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatStreamEndEvent(value) + *c = ChatDataMetrics(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1624,7 +1786,7 @@ func (c *ChatStreamEndEvent) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatStreamEndEvent) String() string { +func (c *ChatDataMetrics) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1636,58 +1798,73 @@ func (c *ChatStreamEndEvent) String() string { return fmt.Sprintf("%#v", c) } -// - `COMPLETE` - the model sent back a finished reply -// - `ERROR_LIMIT` - the reply was cut off because the model reached the maximum number of tokens for its context length -// - `MAX_TOKENS` - the reply was cut off because the model reached the maximum number of tokens specified by the max_tokens parameter -// - `ERROR` - something went wrong when generating the reply -// - `ERROR_TOXIC` - the model generated a reply that was deemed toxic -type ChatStreamEndEventFinishReason string +// Relevant information that could be used by the model to generate a more accurate reply. +// The contents of each document are generally short (under 300 words), and are passed in the form of a +// dictionary of strings. Some suggested keys are "text", "author", "date". Both the key name and the value will be +// passed to the model. +type ChatDocument = map[string]string + +// The reason a chat request has finished. +// +// - **complete**: The model finished sending a complete message. +// - **max_tokens**: The number of generated tokens exceeded the model's context length or the value specified via the `max_tokens` parameter. +// - **stop_sequence**: One of the provided `stop_sequence` entries was reached in the model's generation. +// - **tool_call**: The model generated a Tool Call and is expecting a Tool Message in return +// - **error**: The generation failed due to an internal error +type ChatFinishReason string const ( - ChatStreamEndEventFinishReasonComplete ChatStreamEndEventFinishReason = "COMPLETE" - ChatStreamEndEventFinishReasonErrorLimit ChatStreamEndEventFinishReason = "ERROR_LIMIT" - ChatStreamEndEventFinishReasonMaxTokens ChatStreamEndEventFinishReason = "MAX_TOKENS" - ChatStreamEndEventFinishReasonError ChatStreamEndEventFinishReason = "ERROR" - ChatStreamEndEventFinishReasonErrorToxic ChatStreamEndEventFinishReason = "ERROR_TOXIC" + ChatFinishReasonComplete ChatFinishReason = "COMPLETE" + ChatFinishReasonStopSequence ChatFinishReason = "STOP_SEQUENCE" + ChatFinishReasonMaxTokens ChatFinishReason = "MAX_TOKENS" + ChatFinishReasonToolCall ChatFinishReason = "TOOL_CALL" + ChatFinishReasonError ChatFinishReason = "ERROR" ) -func NewChatStreamEndEventFinishReasonFromString(s string) (ChatStreamEndEventFinishReason, error) { +func NewChatFinishReasonFromString(s string) (ChatFinishReason, error) { switch s { case "COMPLETE": - return ChatStreamEndEventFinishReasonComplete, nil - case "ERROR_LIMIT": - return ChatStreamEndEventFinishReasonErrorLimit, nil + return ChatFinishReasonComplete, nil + case "STOP_SEQUENCE": + return ChatFinishReasonStopSequence, nil case "MAX_TOKENS": - return ChatStreamEndEventFinishReasonMaxTokens, nil + return ChatFinishReasonMaxTokens, nil + case "TOOL_CALL": + return ChatFinishReasonToolCall, nil case "ERROR": - return ChatStreamEndEventFinishReasonError, nil - case "ERROR_TOXIC": - return ChatStreamEndEventFinishReasonErrorToxic, nil + return ChatFinishReasonError, nil } - var t ChatStreamEndEventFinishReason + var t ChatFinishReason return "", fmt.Errorf("%s is not a valid %T", s, t) } -func (c ChatStreamEndEventFinishReason) Ptr() *ChatStreamEndEventFinishReason { +func (c ChatFinishReason) Ptr() *ChatFinishReason { return &c } -type ChatStreamEvent struct { +// Represents a single message in the chat history, excluding the current user turn. It has two properties: `role` and `message`. The `role` identifies the sender (`CHATBOT`, `SYSTEM`, or `USER`), while the `message` contains the text content. +// +// The chat_history parameter should not be used for `SYSTEM` messages in most cases. Instead, to add a `SYSTEM` role message at the beginning of a conversation, the `preamble` parameter should be used. +type ChatMessage struct { + // Contents of the chat message. + Message string `json:"message" url:"message"` + ToolCalls []*ToolCall `json:"tool_calls,omitempty" url:"tool_calls,omitempty"` + extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatStreamEvent) GetExtraProperties() map[string]interface{} { +func (c *ChatMessage) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatStreamEvent) UnmarshalJSON(data []byte) error { - type unmarshaler ChatStreamEvent +func (c *ChatMessage) UnmarshalJSON(data []byte) error { + type unmarshaler ChatMessage var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatStreamEvent(value) + *c = ChatMessage(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1699,7 +1876,7 @@ func (c *ChatStreamEvent) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatStreamEvent) String() string { +func (c *ChatMessage) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1711,61 +1888,26 @@ func (c *ChatStreamEvent) String() string { return fmt.Sprintf("%#v", c) } -// Defaults to `"accurate"`. -// -// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results. -// -// Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments -type ChatStreamRequestCitationQuality string - -const ( - ChatStreamRequestCitationQualityFast ChatStreamRequestCitationQuality = "fast" - ChatStreamRequestCitationQualityAccurate ChatStreamRequestCitationQuality = "accurate" - ChatStreamRequestCitationQualityOff ChatStreamRequestCitationQuality = "off" -) - -func NewChatStreamRequestCitationQualityFromString(s string) (ChatStreamRequestCitationQuality, error) { - switch s { - case "fast": - return ChatStreamRequestCitationQualityFast, nil - case "accurate": - return ChatStreamRequestCitationQualityAccurate, nil - case "off": - return ChatStreamRequestCitationQualityOff, nil - } - var t ChatStreamRequestCitationQuality - return "", fmt.Errorf("%s is not a valid %T", s, t) -} - -func (c ChatStreamRequestCitationQuality) Ptr() *ChatStreamRequestCitationQuality { - return &c -} - -// (internal) Sets inference and model options for RAG search query and tool use generations. Defaults are used when options are not specified here, meaning that other parameters outside of connectors_search_options are ignored (such as model= or temperature=). -type ChatStreamRequestConnectorsSearchOptions struct { - // If specified, the backend will make a best effort to sample tokens - // deterministically, such that repeated requests with the same - // seed and parameters should return the same result. However, - // determinism cannot be totally guaranteed. - // - // Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments - Seed *int `json:"seed,omitempty" url:"seed,omitempty"` +// A streamed event which signifies that the chat message has ended. +type ChatMessageEndEvent struct { + Id *string `json:"id,omitempty" url:"id,omitempty"` + Delta *ChatMessageEndEventDelta `json:"delta,omitempty" url:"delta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatStreamRequestConnectorsSearchOptions) GetExtraProperties() map[string]interface{} { +func (c *ChatMessageEndEvent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatStreamRequestConnectorsSearchOptions) UnmarshalJSON(data []byte) error { - type unmarshaler ChatStreamRequestConnectorsSearchOptions +func (c *ChatMessageEndEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatMessageEndEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatStreamRequestConnectorsSearchOptions(value) + *c = ChatMessageEndEvent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1777,7 +1919,7 @@ func (c *ChatStreamRequestConnectorsSearchOptions) UnmarshalJSON(data []byte) er return nil } -func (c *ChatStreamRequestConnectorsSearchOptions) String() string { +func (c *ChatMessageEndEvent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1789,97 +1931,25 @@ func (c *ChatStreamRequestConnectorsSearchOptions) String() string { return fmt.Sprintf("%#v", c) } -// Defaults to `AUTO` when `connectors` are specified and `OFF` in all other cases. -// -// Dictates how the prompt will be constructed. -// -// With `prompt_truncation` set to "AUTO", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be changed and ranked by relevance. -// -// With `prompt_truncation` set to "AUTO_PRESERVE_ORDER", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be preserved as they are inputted into the API. -// -// With `prompt_truncation` set to "OFF", no elements will be dropped. If the sum of the inputs exceeds the model's context length limit, a `TooManyTokens` error will be returned. -// -// Compatible Deployments: -// -// - AUTO: Cohere Platform Only -// - AUTO_PRESERVE_ORDER: Azure, AWS Sagemaker/Bedrock, Private Deployments -type ChatStreamRequestPromptTruncation string - -const ( - ChatStreamRequestPromptTruncationOff ChatStreamRequestPromptTruncation = "OFF" - ChatStreamRequestPromptTruncationAuto ChatStreamRequestPromptTruncation = "AUTO" - ChatStreamRequestPromptTruncationAutoPreserveOrder ChatStreamRequestPromptTruncation = "AUTO_PRESERVE_ORDER" -) - -func NewChatStreamRequestPromptTruncationFromString(s string) (ChatStreamRequestPromptTruncation, error) { - switch s { - case "OFF": - return ChatStreamRequestPromptTruncationOff, nil - case "AUTO": - return ChatStreamRequestPromptTruncationAuto, nil - case "AUTO_PRESERVE_ORDER": - return ChatStreamRequestPromptTruncationAutoPreserveOrder, nil - } - var t ChatStreamRequestPromptTruncation - return "", fmt.Errorf("%s is not a valid %T", s, t) -} - -func (c ChatStreamRequestPromptTruncation) Ptr() *ChatStreamRequestPromptTruncation { - return &c -} - -// Used to select the [safety instruction](/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`. -// When `NONE` is specified, the safety instruction will be omitted. -// -// Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters. -// -// **Note**: This parameter is only compatible with models [Command R 08-2024](/docs/command-r#august-2024-release), [Command R+ 08-2024](/docs/command-r-plus#august-2024-release) and newer. -// -// Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments -type ChatStreamRequestSafetyMode string - -const ( - ChatStreamRequestSafetyModeContextual ChatStreamRequestSafetyMode = "CONTEXTUAL" - ChatStreamRequestSafetyModeStrict ChatStreamRequestSafetyMode = "STRICT" - ChatStreamRequestSafetyModeNone ChatStreamRequestSafetyMode = "NONE" -) - -func NewChatStreamRequestSafetyModeFromString(s string) (ChatStreamRequestSafetyMode, error) { - switch s { - case "CONTEXTUAL": - return ChatStreamRequestSafetyModeContextual, nil - case "STRICT": - return ChatStreamRequestSafetyModeStrict, nil - case "NONE": - return ChatStreamRequestSafetyModeNone, nil - } - var t ChatStreamRequestSafetyMode - return "", fmt.Errorf("%s is not a valid %T", s, t) -} - -func (c ChatStreamRequestSafetyMode) Ptr() *ChatStreamRequestSafetyMode { - return &c -} - -type ChatStreamStartEvent struct { - // Unique identifier for the generated reply. Useful for submitting feedback. - GenerationId string `json:"generation_id" url:"generation_id"` +type ChatMessageEndEventDelta struct { + FinishReason *ChatFinishReason `json:"finish_reason,omitempty" url:"finish_reason,omitempty"` + Usage *Usage `json:"usage,omitempty" url:"usage,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatStreamStartEvent) GetExtraProperties() map[string]interface{} { +func (c *ChatMessageEndEventDelta) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatStreamStartEvent) UnmarshalJSON(data []byte) error { - type unmarshaler ChatStreamStartEvent +func (c *ChatMessageEndEventDelta) UnmarshalJSON(data []byte) error { + type unmarshaler ChatMessageEndEventDelta var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatStreamStartEvent(value) + *c = ChatMessageEndEventDelta(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1891,7 +1961,7 @@ func (c *ChatStreamStartEvent) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatStreamStartEvent) String() string { +func (c *ChatMessageEndEventDelta) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1903,25 +1973,27 @@ func (c *ChatStreamStartEvent) String() string { return fmt.Sprintf("%#v", c) } -type ChatTextGenerationEvent struct { - // The next batch of text generated by the model. - Text string `json:"text" url:"text"` +// A streamed event which signifies that a stream has started. +type ChatMessageStartEvent struct { + // Unique identifier for the generated reply. + Id *string `json:"id,omitempty" url:"id,omitempty"` + Delta *ChatMessageStartEventDelta `json:"delta,omitempty" url:"delta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatTextGenerationEvent) GetExtraProperties() map[string]interface{} { +func (c *ChatMessageStartEvent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatTextGenerationEvent) UnmarshalJSON(data []byte) error { - type unmarshaler ChatTextGenerationEvent +func (c *ChatMessageStartEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatMessageStartEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatTextGenerationEvent(value) + *c = ChatMessageStartEvent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1933,7 +2005,7 @@ func (c *ChatTextGenerationEvent) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatTextGenerationEvent) String() string { +func (c *ChatMessageStartEvent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1945,24 +2017,24 @@ func (c *ChatTextGenerationEvent) String() string { return fmt.Sprintf("%#v", c) } -type ChatToolCallsChunkEvent struct { - ToolCallDelta *ToolCallDelta `json:"tool_call_delta,omitempty" url:"tool_call_delta,omitempty"` +type ChatMessageStartEventDelta struct { + Message *ChatMessageStartEventDeltaMessage `json:"message,omitempty" url:"message,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatToolCallsChunkEvent) GetExtraProperties() map[string]interface{} { +func (c *ChatMessageStartEventDelta) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatToolCallsChunkEvent) UnmarshalJSON(data []byte) error { - type unmarshaler ChatToolCallsChunkEvent +func (c *ChatMessageStartEventDelta) UnmarshalJSON(data []byte) error { + type unmarshaler ChatMessageStartEventDelta var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatToolCallsChunkEvent(value) + *c = ChatMessageStartEventDelta(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -1974,7 +2046,7 @@ func (c *ChatToolCallsChunkEvent) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatToolCallsChunkEvent) String() string { +func (c *ChatMessageStartEventDelta) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -1986,26 +2058,25 @@ func (c *ChatToolCallsChunkEvent) String() string { return fmt.Sprintf("%#v", c) } -type ChatToolCallsGenerationEvent struct { - // The text generated related to the tool calls generated - Text *string `json:"text,omitempty" url:"text,omitempty"` - ToolCalls []*ToolCall `json:"tool_calls,omitempty" url:"tool_calls,omitempty"` +type ChatMessageStartEventDeltaMessage struct { + // The role of the message. + Role *string `json:"role,omitempty" url:"role,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ChatToolCallsGenerationEvent) GetExtraProperties() map[string]interface{} { +func (c *ChatMessageStartEventDeltaMessage) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ChatToolCallsGenerationEvent) UnmarshalJSON(data []byte) error { - type unmarshaler ChatToolCallsGenerationEvent +func (c *ChatMessageStartEventDeltaMessage) UnmarshalJSON(data []byte) error { + type unmarshaler ChatMessageStartEventDeltaMessage var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ChatToolCallsGenerationEvent(value) + *c = ChatMessageStartEventDeltaMessage(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2017,7 +2088,7 @@ func (c *ChatToolCallsGenerationEvent) UnmarshalJSON(data []byte) error { return nil } -func (c *ChatToolCallsGenerationEvent) String() string { +func (c *ChatMessageStartEventDeltaMessage) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2029,109 +2100,154 @@ func (c *ChatToolCallsGenerationEvent) String() string { return fmt.Sprintf("%#v", c) } -type CheckApiKeyResponse struct { - Valid bool `json:"valid" url:"valid"` - OrganizationId *string `json:"organization_id,omitempty" url:"organization_id,omitempty"` - OwnerId *string `json:"owner_id,omitempty" url:"owner_id,omitempty"` - - extraProperties map[string]interface{} - _rawJSON json.RawMessage -} - -func (c *CheckApiKeyResponse) GetExtraProperties() map[string]interface{} { - return c.extraProperties +// Represents a single message in the chat history from a given role. +type ChatMessageV2 struct { + Role string + User *UserMessage + Assistant *AssistantMessage + System *SystemMessage + Tool *ToolMessageV2 } -func (c *CheckApiKeyResponse) UnmarshalJSON(data []byte) error { - type unmarshaler CheckApiKeyResponse - var value unmarshaler - if err := json.Unmarshal(data, &value); err != nil { - return err +func (c *ChatMessageV2) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + Role string `json:"role"` } - *c = CheckApiKeyResponse(value) - - extraProperties, err := core.ExtractExtraProperties(data, *c) - if err != nil { + if err := json.Unmarshal(data, &unmarshaler); err != nil { return err } - c.extraProperties = extraProperties - - c._rawJSON = json.RawMessage(data) + c.Role = unmarshaler.Role + if unmarshaler.Role == "" { + return fmt.Errorf("%T did not include discriminant role", c) + } + switch unmarshaler.Role { + case "user": + value := new(UserMessage) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + c.User = value + case "assistant": + value := new(AssistantMessage) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + c.Assistant = value + case "system": + value := new(SystemMessage) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + c.System = value + case "tool": + value := new(ToolMessageV2) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + c.Tool = value + } return nil } -func (c *CheckApiKeyResponse) String() string { - if len(c._rawJSON) > 0 { - if value, err := core.StringifyJSON(c._rawJSON); err == nil { - return value - } +func (c ChatMessageV2) MarshalJSON() ([]byte, error) { + if c.User != nil { + return core.MarshalJSONWithExtraProperty(c.User, "role", "user") } - if value, err := core.StringifyJSON(c); err == nil { - return value + if c.Assistant != nil { + return core.MarshalJSONWithExtraProperty(c.Assistant, "role", "assistant") } - return fmt.Sprintf("%#v", c) + if c.System != nil { + return core.MarshalJSONWithExtraProperty(c.System, "role", "system") + } + if c.Tool != nil { + return core.MarshalJSONWithExtraProperty(c.Tool, "role", "tool") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", c) } -type ClassifyDataMetrics struct { - LabelMetrics []*LabelMetric `json:"label_metrics,omitempty" url:"label_metrics,omitempty"` - - extraProperties map[string]interface{} - _rawJSON json.RawMessage +type ChatMessageV2Visitor interface { + VisitUser(*UserMessage) error + VisitAssistant(*AssistantMessage) error + VisitSystem(*SystemMessage) error + VisitTool(*ToolMessageV2) error } -func (c *ClassifyDataMetrics) GetExtraProperties() map[string]interface{} { - return c.extraProperties +func (c *ChatMessageV2) Accept(visitor ChatMessageV2Visitor) error { + if c.User != nil { + return visitor.VisitUser(c.User) + } + if c.Assistant != nil { + return visitor.VisitAssistant(c.Assistant) + } + if c.System != nil { + return visitor.VisitSystem(c.System) + } + if c.Tool != nil { + return visitor.VisitTool(c.Tool) + } + return fmt.Errorf("type %T does not define a non-empty union type", c) } -func (c *ClassifyDataMetrics) UnmarshalJSON(data []byte) error { - type unmarshaler ClassifyDataMetrics - var value unmarshaler - if err := json.Unmarshal(data, &value); err != nil { - return err - } - *c = ClassifyDataMetrics(value) +// A list of chat messages in chronological order, representing a conversation between the user and the model. +// +// Messages can be from `User`, `Assistant`, `Tool` and `System` roles. Learn more about messages and roles in [the Chat API guide](https://docs.cohere.com/v2/docs/chat-api). +type ChatMessages = []*ChatMessageV2 - extraProperties, err := core.ExtractExtraProperties(data, *c) - if err != nil { - return err - } - c.extraProperties = extraProperties +// Defaults to `"accurate"`. +// +// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results. +// +// Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments +type ChatRequestCitationQuality string - c._rawJSON = json.RawMessage(data) - return nil -} +const ( + ChatRequestCitationQualityFast ChatRequestCitationQuality = "fast" + ChatRequestCitationQualityAccurate ChatRequestCitationQuality = "accurate" + ChatRequestCitationQualityOff ChatRequestCitationQuality = "off" +) -func (c *ClassifyDataMetrics) String() string { - if len(c._rawJSON) > 0 { - if value, err := core.StringifyJSON(c._rawJSON); err == nil { - return value - } - } - if value, err := core.StringifyJSON(c); err == nil { - return value +func NewChatRequestCitationQualityFromString(s string) (ChatRequestCitationQuality, error) { + switch s { + case "fast": + return ChatRequestCitationQualityFast, nil + case "accurate": + return ChatRequestCitationQualityAccurate, nil + case "off": + return ChatRequestCitationQualityOff, nil } - return fmt.Sprintf("%#v", c) + var t ChatRequestCitationQuality + return "", fmt.Errorf("%s is not a valid %T", s, t) } -type ClassifyExample struct { - Text *string `json:"text,omitempty" url:"text,omitempty"` - Label *string `json:"label,omitempty" url:"label,omitempty"` +func (c ChatRequestCitationQuality) Ptr() *ChatRequestCitationQuality { + return &c +} + +// (internal) Sets inference and model options for RAG search query and tool use generations. Defaults are used when options are not specified here, meaning that other parameters outside of connectors_search_options are ignored (such as model= or temperature=). +type ChatRequestConnectorsSearchOptions struct { + // If specified, the backend will make a best effort to sample tokens + // deterministically, such that repeated requests with the same + // seed and parameters should return the same result. However, + // determinism cannot be totally guaranteed. + // + // Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments + Seed *int `json:"seed,omitempty" url:"seed,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ClassifyExample) GetExtraProperties() map[string]interface{} { +func (c *ChatRequestConnectorsSearchOptions) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ClassifyExample) UnmarshalJSON(data []byte) error { - type unmarshaler ClassifyExample +func (c *ChatRequestConnectorsSearchOptions) UnmarshalJSON(data []byte) error { + type unmarshaler ChatRequestConnectorsSearchOptions var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ClassifyExample(value) + *c = ChatRequestConnectorsSearchOptions(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2143,7 +2259,7 @@ func (c *ClassifyExample) UnmarshalJSON(data []byte) error { return nil } -func (c *ClassifyExample) String() string { +func (c *ChatRequestConnectorsSearchOptions) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2155,54 +2271,102 @@ func (c *ClassifyExample) String() string { return fmt.Sprintf("%#v", c) } -// One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length. -// Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model. -// If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned. -type ClassifyRequestTruncate string +// Defaults to `AUTO` when `connectors` are specified and `OFF` in all other cases. +// +// Dictates how the prompt will be constructed. +// +// With `prompt_truncation` set to "AUTO", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be changed and ranked by relevance. +// +// With `prompt_truncation` set to "AUTO_PRESERVE_ORDER", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be preserved as they are inputted into the API. +// +// With `prompt_truncation` set to "OFF", no elements will be dropped. If the sum of the inputs exceeds the model's context length limit, a `TooManyTokens` error will be returned. +// +// Compatible Deployments: +// +// - AUTO: Cohere Platform Only +// - AUTO_PRESERVE_ORDER: Azure, AWS Sagemaker/Bedrock, Private Deployments +type ChatRequestPromptTruncation string const ( - ClassifyRequestTruncateNone ClassifyRequestTruncate = "NONE" - ClassifyRequestTruncateStart ClassifyRequestTruncate = "START" - ClassifyRequestTruncateEnd ClassifyRequestTruncate = "END" + ChatRequestPromptTruncationOff ChatRequestPromptTruncation = "OFF" + ChatRequestPromptTruncationAuto ChatRequestPromptTruncation = "AUTO" + ChatRequestPromptTruncationAutoPreserveOrder ChatRequestPromptTruncation = "AUTO_PRESERVE_ORDER" ) -func NewClassifyRequestTruncateFromString(s string) (ClassifyRequestTruncate, error) { +func NewChatRequestPromptTruncationFromString(s string) (ChatRequestPromptTruncation, error) { switch s { - case "NONE": - return ClassifyRequestTruncateNone, nil - case "START": - return ClassifyRequestTruncateStart, nil - case "END": - return ClassifyRequestTruncateEnd, nil - } - var t ClassifyRequestTruncate + case "OFF": + return ChatRequestPromptTruncationOff, nil + case "AUTO": + return ChatRequestPromptTruncationAuto, nil + case "AUTO_PRESERVE_ORDER": + return ChatRequestPromptTruncationAutoPreserveOrder, nil + } + var t ChatRequestPromptTruncation return "", fmt.Errorf("%s is not a valid %T", s, t) } -func (c ClassifyRequestTruncate) Ptr() *ClassifyRequestTruncate { +func (c ChatRequestPromptTruncation) Ptr() *ChatRequestPromptTruncation { return &c } -type ClassifyResponse struct { - Id string `json:"id" url:"id"` - Classifications []*ClassifyResponseClassificationsItem `json:"classifications,omitempty" url:"classifications,omitempty"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +// Used to select the [safety instruction](/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`. +// When `NONE` is specified, the safety instruction will be omitted. +// +// Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters. +// +// **Note**: This parameter is only compatible with models [Command R 08-2024](/docs/command-r#august-2024-release), [Command R+ 08-2024](/docs/command-r-plus#august-2024-release) and newer. +// +// Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments +type ChatRequestSafetyMode string + +const ( + ChatRequestSafetyModeContextual ChatRequestSafetyMode = "CONTEXTUAL" + ChatRequestSafetyModeStrict ChatRequestSafetyMode = "STRICT" + ChatRequestSafetyModeNone ChatRequestSafetyMode = "NONE" +) + +func NewChatRequestSafetyModeFromString(s string) (ChatRequestSafetyMode, error) { + switch s { + case "CONTEXTUAL": + return ChatRequestSafetyModeContextual, nil + case "STRICT": + return ChatRequestSafetyModeStrict, nil + case "NONE": + return ChatRequestSafetyModeNone, nil + } + var t ChatRequestSafetyMode + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (c ChatRequestSafetyMode) Ptr() *ChatRequestSafetyMode { + return &c +} + +type ChatResponse struct { + // Unique identifier for the generated reply. Useful for submitting feedback. + Id string `json:"id" url:"id"` + FinishReason ChatFinishReason `json:"finish_reason" url:"finish_reason"` + // The prompt that was used. Only present when `return_prompt` in the request is set to true. + Prompt *string `json:"prompt,omitempty" url:"prompt,omitempty"` + Message *AssistantMessageResponse `json:"message,omitempty" url:"message,omitempty"` + Usage *Usage `json:"usage,omitempty" url:"usage,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ClassifyResponse) GetExtraProperties() map[string]interface{} { +func (c *ChatResponse) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ClassifyResponse) UnmarshalJSON(data []byte) error { - type unmarshaler ClassifyResponse +func (c *ChatResponse) UnmarshalJSON(data []byte) error { + type unmarshaler ChatResponse var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ClassifyResponse(value) + *c = ChatResponse(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2214,7 +2378,7 @@ func (c *ClassifyResponse) UnmarshalJSON(data []byte) error { return nil } -func (c *ClassifyResponse) String() string { +func (c *ChatResponse) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2226,38 +2390,25 @@ func (c *ClassifyResponse) String() string { return fmt.Sprintf("%#v", c) } -type ClassifyResponseClassificationsItem struct { - Id string `json:"id" url:"id"` - // The input text that was classified - Input *string `json:"input,omitempty" url:"input,omitempty"` - // The predicted label for the associated query (only filled for single-label models) - Prediction *string `json:"prediction,omitempty" url:"prediction,omitempty"` - // An array containing the predicted labels for the associated query (only filled for single-label classification) - Predictions []string `json:"predictions,omitempty" url:"predictions,omitempty"` - // The confidence score for the top predicted class (only filled for single-label classification) - Confidence *float64 `json:"confidence,omitempty" url:"confidence,omitempty"` - // An array containing the confidence scores of all the predictions in the same order - Confidences []float64 `json:"confidences,omitempty" url:"confidences,omitempty"` - // A map containing each label and its confidence score according to the classifier. All the confidence scores add up to 1 for single-label classification. For multi-label classification the label confidences are independent of each other, so they don't have to sum up to 1. - Labels map[string]*ClassifyResponseClassificationsItemLabelsValue `json:"labels,omitempty" url:"labels,omitempty"` - // The type of classification performed - ClassificationType ClassifyResponseClassificationsItemClassificationType `json:"classification_type" url:"classification_type"` +type ChatSearchQueriesGenerationEvent struct { + // Generated search queries, meant to be used as part of the RAG flow. + SearchQueries []*ChatSearchQuery `json:"search_queries,omitempty" url:"search_queries,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ClassifyResponseClassificationsItem) GetExtraProperties() map[string]interface{} { +func (c *ChatSearchQueriesGenerationEvent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ClassifyResponseClassificationsItem) UnmarshalJSON(data []byte) error { - type unmarshaler ClassifyResponseClassificationsItem +func (c *ChatSearchQueriesGenerationEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatSearchQueriesGenerationEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ClassifyResponseClassificationsItem(value) + *c = ChatSearchQueriesGenerationEvent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2269,7 +2420,7 @@ func (c *ClassifyResponseClassificationsItem) UnmarshalJSON(data []byte) error { return nil } -func (c *ClassifyResponseClassificationsItem) String() string { +func (c *ChatSearchQueriesGenerationEvent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2281,47 +2432,28 @@ func (c *ClassifyResponseClassificationsItem) String() string { return fmt.Sprintf("%#v", c) } -// The type of classification performed -type ClassifyResponseClassificationsItemClassificationType string - -const ( - ClassifyResponseClassificationsItemClassificationTypeSingleLabel ClassifyResponseClassificationsItemClassificationType = "single-label" - ClassifyResponseClassificationsItemClassificationTypeMultiLabel ClassifyResponseClassificationsItemClassificationType = "multi-label" -) - -func NewClassifyResponseClassificationsItemClassificationTypeFromString(s string) (ClassifyResponseClassificationsItemClassificationType, error) { - switch s { - case "single-label": - return ClassifyResponseClassificationsItemClassificationTypeSingleLabel, nil - case "multi-label": - return ClassifyResponseClassificationsItemClassificationTypeMultiLabel, nil - } - var t ClassifyResponseClassificationsItemClassificationType - return "", fmt.Errorf("%s is not a valid %T", s, t) -} - -func (c ClassifyResponseClassificationsItemClassificationType) Ptr() *ClassifyResponseClassificationsItemClassificationType { - return &c -} - -type ClassifyResponseClassificationsItemLabelsValue struct { - Confidence *float64 `json:"confidence,omitempty" url:"confidence,omitempty"` +// The generated search query. Contains the text of the query and a unique identifier for the query. +type ChatSearchQuery struct { + // The text of the search query. + Text string `json:"text" url:"text"` + // Unique identifier for the generated search query. Useful for submitting feedback. + GenerationId string `json:"generation_id" url:"generation_id"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ClassifyResponseClassificationsItemLabelsValue) GetExtraProperties() map[string]interface{} { +func (c *ChatSearchQuery) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ClassifyResponseClassificationsItemLabelsValue) UnmarshalJSON(data []byte) error { - type unmarshaler ClassifyResponseClassificationsItemLabelsValue +func (c *ChatSearchQuery) UnmarshalJSON(data []byte) error { + type unmarshaler ChatSearchQuery var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ClassifyResponseClassificationsItemLabelsValue(value) + *c = ChatSearchQuery(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2333,7 +2465,7 @@ func (c *ClassifyResponseClassificationsItemLabelsValue) UnmarshalJSON(data []by return nil } -func (c *ClassifyResponseClassificationsItemLabelsValue) String() string { +func (c *ChatSearchQuery) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2345,24 +2477,32 @@ func (c *ClassifyResponseClassificationsItemLabelsValue) String() string { return fmt.Sprintf("%#v", c) } -type ClientClosedRequestErrorBody struct { - Data *string `json:"data,omitempty" url:"data,omitempty"` +type ChatSearchResult struct { + SearchQuery *ChatSearchQuery `json:"search_query,omitempty" url:"search_query,omitempty"` + // The connector from which this result comes from. + Connector *ChatSearchResultConnector `json:"connector,omitempty" url:"connector,omitempty"` + // Identifiers of documents found by this search query. + DocumentIds []string `json:"document_ids,omitempty" url:"document_ids,omitempty"` + // An error message if the search failed. + ErrorMessage *string `json:"error_message,omitempty" url:"error_message,omitempty"` + // Whether a chat request should continue or not if the request to this connector fails. + ContinueOnFailure *bool `json:"continue_on_failure,omitempty" url:"continue_on_failure,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ClientClosedRequestErrorBody) GetExtraProperties() map[string]interface{} { +func (c *ChatSearchResult) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ClientClosedRequestErrorBody) UnmarshalJSON(data []byte) error { - type unmarshaler ClientClosedRequestErrorBody +func (c *ChatSearchResult) UnmarshalJSON(data []byte) error { + type unmarshaler ChatSearchResult var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ClientClosedRequestErrorBody(value) + *c = ChatSearchResult(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2374,7 +2514,7 @@ func (c *ClientClosedRequestErrorBody) UnmarshalJSON(data []byte) error { return nil } -func (c *ClientClosedRequestErrorBody) String() string { +func (c *ChatSearchResult) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2386,99 +2526,26 @@ func (c *ClientClosedRequestErrorBody) String() string { return fmt.Sprintf("%#v", c) } -// One of the Cohere API endpoints that the model can be used with. -type CompatibleEndpoint string - -const ( - CompatibleEndpointChat CompatibleEndpoint = "chat" - CompatibleEndpointEmbed CompatibleEndpoint = "embed" - CompatibleEndpointClassify CompatibleEndpoint = "classify" - CompatibleEndpointSummarize CompatibleEndpoint = "summarize" - CompatibleEndpointRerank CompatibleEndpoint = "rerank" - CompatibleEndpointRate CompatibleEndpoint = "rate" - CompatibleEndpointGenerate CompatibleEndpoint = "generate" -) - -func NewCompatibleEndpointFromString(s string) (CompatibleEndpoint, error) { - switch s { - case "chat": - return CompatibleEndpointChat, nil - case "embed": - return CompatibleEndpointEmbed, nil - case "classify": - return CompatibleEndpointClassify, nil - case "summarize": - return CompatibleEndpointSummarize, nil - case "rerank": - return CompatibleEndpointRerank, nil - case "rate": - return CompatibleEndpointRate, nil - case "generate": - return CompatibleEndpointGenerate, nil - } - var t CompatibleEndpoint - return "", fmt.Errorf("%s is not a valid %T", s, t) -} - -func (c CompatibleEndpoint) Ptr() *CompatibleEndpoint { - return &c -} - -// A connector allows you to integrate data sources with the '/chat' endpoint to create grounded generations with citations to the data source. -// documents to help answer users. -type Connector struct { - // The unique identifier of the connector (used in both `/connectors` & `/chat` endpoints). - // This is automatically created from the name of the connector upon registration. +// The connector used for fetching documents. +type ChatSearchResultConnector struct { + // The identifier of the connector. Id string `json:"id" url:"id"` - // The organization to which this connector belongs. This is automatically set to - // the organization of the user who created the connector. - OrganizationId *string `json:"organization_id,omitempty" url:"organization_id,omitempty"` - // A human-readable name for the connector. - Name string `json:"name" url:"name"` - // A description of the connector. - Description *string `json:"description,omitempty" url:"description,omitempty"` - // The URL of the connector that will be used to search for documents. - Url *string `json:"url,omitempty" url:"url,omitempty"` - // The UTC time at which the connector was created. - CreatedAt time.Time `json:"created_at" url:"created_at"` - // The UTC time at which the connector was last updated. - UpdatedAt time.Time `json:"updated_at" url:"updated_at"` - // A list of fields to exclude from the prompt (fields remain in the document). - Excludes []string `json:"excludes,omitempty" url:"excludes,omitempty"` - // The type of authentication/authorization used by the connector. Possible values: [oauth, service_auth] - AuthType *string `json:"auth_type,omitempty" url:"auth_type,omitempty"` - // The OAuth 2.0 configuration for the connector. - Oauth *ConnectorOAuth `json:"oauth,omitempty" url:"oauth,omitempty"` - // The OAuth status for the user making the request. One of ["valid", "expired", ""]. Empty string (field is omitted) means the user has not authorized the connector yet. - AuthStatus *ConnectorAuthStatus `json:"auth_status,omitempty" url:"auth_status,omitempty"` - // Whether the connector is active or not. - Active *bool `json:"active,omitempty" url:"active,omitempty"` - // Whether a chat request should continue or not if the request to this connector fails. - ContinueOnFailure *bool `json:"continue_on_failure,omitempty" url:"continue_on_failure,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *Connector) GetExtraProperties() map[string]interface{} { +func (c *ChatSearchResultConnector) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *Connector) UnmarshalJSON(data []byte) error { - type embed Connector - var unmarshaler = struct { - embed - CreatedAt *core.DateTime `json:"created_at"` - UpdatedAt *core.DateTime `json:"updated_at"` - }{ - embed: embed(*c), - } - if err := json.Unmarshal(data, &unmarshaler); err != nil { +func (c *ChatSearchResultConnector) UnmarshalJSON(data []byte) error { + type unmarshaler ChatSearchResultConnector + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { return err } - *c = Connector(unmarshaler.embed) - c.CreatedAt = unmarshaler.CreatedAt.Time() - c.UpdatedAt = unmarshaler.UpdatedAt.Time() + *c = ChatSearchResultConnector(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2490,21 +2557,7 @@ func (c *Connector) UnmarshalJSON(data []byte) error { return nil } -func (c *Connector) MarshalJSON() ([]byte, error) { - type embed Connector - var marshaler = struct { - embed - CreatedAt *core.DateTime `json:"created_at"` - UpdatedAt *core.DateTime `json:"updated_at"` - }{ - embed: embed(*c), - CreatedAt: core.NewDateTime(c.CreatedAt), - UpdatedAt: core.NewDateTime(c.UpdatedAt), - } - return json.Marshal(marshaler) -} - -func (c *Connector) String() string { +func (c *ChatSearchResultConnector) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2516,56 +2569,27 @@ func (c *Connector) String() string { return fmt.Sprintf("%#v", c) } -// The OAuth status for the user making the request. One of ["valid", "expired", ""]. Empty string (field is omitted) means the user has not authorized the connector yet. -type ConnectorAuthStatus string - -const ( - ConnectorAuthStatusValid ConnectorAuthStatus = "valid" - ConnectorAuthStatusExpired ConnectorAuthStatus = "expired" -) - -func NewConnectorAuthStatusFromString(s string) (ConnectorAuthStatus, error) { - switch s { - case "valid": - return ConnectorAuthStatusValid, nil - case "expired": - return ConnectorAuthStatusExpired, nil - } - var t ConnectorAuthStatus - return "", fmt.Errorf("%s is not a valid %T", s, t) -} - -func (c ConnectorAuthStatus) Ptr() *ConnectorAuthStatus { - return &c -} - -type ConnectorOAuth struct { - // The OAuth 2.0 client ID. This field is encrypted at rest. - ClientId *string `json:"client_id,omitempty" url:"client_id,omitempty"` - // The OAuth 2.0 client Secret. This field is encrypted at rest and never returned in a response. - ClientSecret *string `json:"client_secret,omitempty" url:"client_secret,omitempty"` - // The OAuth 2.0 /authorize endpoint to use when users authorize the connector. - AuthorizeUrl string `json:"authorize_url" url:"authorize_url"` - // The OAuth 2.0 /token endpoint to use when users authorize the connector. - TokenUrl string `json:"token_url" url:"token_url"` - // The OAuth scopes to request when users authorize the connector. - Scope *string `json:"scope,omitempty" url:"scope,omitempty"` +type ChatSearchResultsEvent struct { + // Conducted searches and the ids of documents retrieved from each of them. + SearchResults []*ChatSearchResult `json:"search_results,omitempty" url:"search_results,omitempty"` + // Documents fetched from searches or provided by the user. + Documents []ChatDocument `json:"documents,omitempty" url:"documents,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *ConnectorOAuth) GetExtraProperties() map[string]interface{} { +func (c *ChatSearchResultsEvent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *ConnectorOAuth) UnmarshalJSON(data []byte) error { - type unmarshaler ConnectorOAuth +func (c *ChatSearchResultsEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatSearchResultsEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = ConnectorOAuth(value) + *c = ChatSearchResultsEvent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2577,7 +2601,7 @@ func (c *ConnectorOAuth) UnmarshalJSON(data []byte) error { return nil } -func (c *ConnectorOAuth) String() string { +func (c *ChatSearchResultsEvent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2589,33 +2613,31 @@ func (c *ConnectorOAuth) String() string { return fmt.Sprintf("%#v", c) } -type CreateConnectorOAuth struct { - // The OAuth 2.0 client ID. This fields is encrypted at rest. - ClientId *string `json:"client_id,omitempty" url:"client_id,omitempty"` - // The OAuth 2.0 client Secret. This field is encrypted at rest and never returned in a response. - ClientSecret *string `json:"client_secret,omitempty" url:"client_secret,omitempty"` - // The OAuth 2.0 /authorize endpoint to use when users authorize the connector. - AuthorizeUrl *string `json:"authorize_url,omitempty" url:"authorize_url,omitempty"` - // The OAuth 2.0 /token endpoint to use when users authorize the connector. - TokenUrl *string `json:"token_url,omitempty" url:"token_url,omitempty"` - // The OAuth scopes to request when users authorize the connector. - Scope *string `json:"scope,omitempty" url:"scope,omitempty"` +type ChatStreamEndEvent struct { + // - `COMPLETE` - the model sent back a finished reply + // - `ERROR_LIMIT` - the reply was cut off because the model reached the maximum number of tokens for its context length + // - `MAX_TOKENS` - the reply was cut off because the model reached the maximum number of tokens specified by the max_tokens parameter + // - `ERROR` - something went wrong when generating the reply + // - `ERROR_TOXIC` - the model generated a reply that was deemed toxic + FinishReason ChatStreamEndEventFinishReason `json:"finish_reason" url:"finish_reason"` + // The consolidated response from the model. Contains the generated reply and all the other information streamed back in the previous events. + Response *NonStreamedChatResponse `json:"response,omitempty" url:"response,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *CreateConnectorOAuth) GetExtraProperties() map[string]interface{} { +func (c *ChatStreamEndEvent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *CreateConnectorOAuth) UnmarshalJSON(data []byte) error { - type unmarshaler CreateConnectorOAuth +func (c *ChatStreamEndEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatStreamEndEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = CreateConnectorOAuth(value) + *c = ChatStreamEndEvent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2627,7 +2649,7 @@ func (c *CreateConnectorOAuth) UnmarshalJSON(data []byte) error { return nil } -func (c *CreateConnectorOAuth) String() string { +func (c *ChatStreamEndEvent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2639,24 +2661,58 @@ func (c *CreateConnectorOAuth) String() string { return fmt.Sprintf("%#v", c) } -type CreateConnectorResponse struct { - Connector *Connector `json:"connector,omitempty" url:"connector,omitempty"` +// - `COMPLETE` - the model sent back a finished reply +// - `ERROR_LIMIT` - the reply was cut off because the model reached the maximum number of tokens for its context length +// - `MAX_TOKENS` - the reply was cut off because the model reached the maximum number of tokens specified by the max_tokens parameter +// - `ERROR` - something went wrong when generating the reply +// - `ERROR_TOXIC` - the model generated a reply that was deemed toxic +type ChatStreamEndEventFinishReason string + +const ( + ChatStreamEndEventFinishReasonComplete ChatStreamEndEventFinishReason = "COMPLETE" + ChatStreamEndEventFinishReasonErrorLimit ChatStreamEndEventFinishReason = "ERROR_LIMIT" + ChatStreamEndEventFinishReasonMaxTokens ChatStreamEndEventFinishReason = "MAX_TOKENS" + ChatStreamEndEventFinishReasonError ChatStreamEndEventFinishReason = "ERROR" + ChatStreamEndEventFinishReasonErrorToxic ChatStreamEndEventFinishReason = "ERROR_TOXIC" +) + +func NewChatStreamEndEventFinishReasonFromString(s string) (ChatStreamEndEventFinishReason, error) { + switch s { + case "COMPLETE": + return ChatStreamEndEventFinishReasonComplete, nil + case "ERROR_LIMIT": + return ChatStreamEndEventFinishReasonErrorLimit, nil + case "MAX_TOKENS": + return ChatStreamEndEventFinishReasonMaxTokens, nil + case "ERROR": + return ChatStreamEndEventFinishReasonError, nil + case "ERROR_TOXIC": + return ChatStreamEndEventFinishReasonErrorToxic, nil + } + var t ChatStreamEndEventFinishReason + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (c ChatStreamEndEventFinishReason) Ptr() *ChatStreamEndEventFinishReason { + return &c +} +type ChatStreamEvent struct { extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *CreateConnectorResponse) GetExtraProperties() map[string]interface{} { +func (c *ChatStreamEvent) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *CreateConnectorResponse) UnmarshalJSON(data []byte) error { - type unmarshaler CreateConnectorResponse +func (c *ChatStreamEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatStreamEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = CreateConnectorResponse(value) + *c = ChatStreamEvent(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2668,7 +2724,7 @@ func (c *CreateConnectorResponse) UnmarshalJSON(data []byte) error { return nil } -func (c *CreateConnectorResponse) String() string { +func (c *ChatStreamEvent) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2680,26 +2736,23 @@ func (c *CreateConnectorResponse) String() string { return fmt.Sprintf("%#v", c) } -type CreateConnectorServiceAuth struct { - Type AuthTokenType `json:"type" url:"type"` - // The token that will be used in the HTTP Authorization header when making requests to the connector. This field is encrypted at rest and never returned in a response. - Token string `json:"token" url:"token"` - +// The streamed event types +type ChatStreamEventType struct { extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *CreateConnectorServiceAuth) GetExtraProperties() map[string]interface{} { +func (c *ChatStreamEventType) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *CreateConnectorServiceAuth) UnmarshalJSON(data []byte) error { - type unmarshaler CreateConnectorServiceAuth +func (c *ChatStreamEventType) UnmarshalJSON(data []byte) error { + type unmarshaler ChatStreamEventType var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = CreateConnectorServiceAuth(value) + *c = ChatStreamEventType(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2711,7 +2764,7 @@ func (c *CreateConnectorServiceAuth) UnmarshalJSON(data []byte) error { return nil } -func (c *CreateConnectorServiceAuth) String() string { +func (c *ChatStreamEventType) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2723,26 +2776,61 @@ func (c *CreateConnectorServiceAuth) String() string { return fmt.Sprintf("%#v", c) } -// Response from creating an embed job. -type CreateEmbedJobResponse struct { - JobId string `json:"job_id" url:"job_id"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +// Defaults to `"accurate"`. +// +// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results. +// +// Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments +type ChatStreamRequestCitationQuality string + +const ( + ChatStreamRequestCitationQualityFast ChatStreamRequestCitationQuality = "fast" + ChatStreamRequestCitationQualityAccurate ChatStreamRequestCitationQuality = "accurate" + ChatStreamRequestCitationQualityOff ChatStreamRequestCitationQuality = "off" +) + +func NewChatStreamRequestCitationQualityFromString(s string) (ChatStreamRequestCitationQuality, error) { + switch s { + case "fast": + return ChatStreamRequestCitationQualityFast, nil + case "accurate": + return ChatStreamRequestCitationQualityAccurate, nil + case "off": + return ChatStreamRequestCitationQualityOff, nil + } + var t ChatStreamRequestCitationQuality + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (c ChatStreamRequestCitationQuality) Ptr() *ChatStreamRequestCitationQuality { + return &c +} + +// (internal) Sets inference and model options for RAG search query and tool use generations. Defaults are used when options are not specified here, meaning that other parameters outside of connectors_search_options are ignored (such as model= or temperature=). +type ChatStreamRequestConnectorsSearchOptions struct { + // If specified, the backend will make a best effort to sample tokens + // deterministically, such that repeated requests with the same + // seed and parameters should return the same result. However, + // determinism cannot be totally guaranteed. + // + // Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments + Seed *int `json:"seed,omitempty" url:"seed,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (c *CreateEmbedJobResponse) GetExtraProperties() map[string]interface{} { +func (c *ChatStreamRequestConnectorsSearchOptions) GetExtraProperties() map[string]interface{} { return c.extraProperties } -func (c *CreateEmbedJobResponse) UnmarshalJSON(data []byte) error { - type unmarshaler CreateEmbedJobResponse +func (c *ChatStreamRequestConnectorsSearchOptions) UnmarshalJSON(data []byte) error { + type unmarshaler ChatStreamRequestConnectorsSearchOptions var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *c = CreateEmbedJobResponse(value) + *c = ChatStreamRequestConnectorsSearchOptions(value) extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { @@ -2754,7 +2842,7 @@ func (c *CreateEmbedJobResponse) UnmarshalJSON(data []byte) error { return nil } -func (c *CreateEmbedJobResponse) String() string { +func (c *ChatStreamRequestConnectorsSearchOptions) String() string { if len(c._rawJSON) > 0 { if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value @@ -2766,2702 +2854,5680 @@ func (c *CreateEmbedJobResponse) String() string { return fmt.Sprintf("%#v", c) } -type Dataset struct { - // The dataset ID - Id string `json:"id" url:"id"` - // The name of the dataset - Name string `json:"name" url:"name"` - // The creation date - CreatedAt time.Time `json:"created_at" url:"created_at"` - // The last update date - UpdatedAt time.Time `json:"updated_at" url:"updated_at"` - DatasetType DatasetType `json:"dataset_type" url:"dataset_type"` - ValidationStatus DatasetValidationStatus `json:"validation_status" url:"validation_status"` - // Errors found during validation - ValidationError *string `json:"validation_error,omitempty" url:"validation_error,omitempty"` - // the avro schema of the dataset - Schema *string `json:"schema,omitempty" url:"schema,omitempty"` - RequiredFields []string `json:"required_fields,omitempty" url:"required_fields,omitempty"` - PreserveFields []string `json:"preserve_fields,omitempty" url:"preserve_fields,omitempty"` - // the underlying files that make up the dataset - DatasetParts []*DatasetPart `json:"dataset_parts,omitempty" url:"dataset_parts,omitempty"` - // warnings found during validation - ValidationWarnings []string `json:"validation_warnings,omitempty" url:"validation_warnings,omitempty"` +// Defaults to `AUTO` when `connectors` are specified and `OFF` in all other cases. +// +// Dictates how the prompt will be constructed. +// +// With `prompt_truncation` set to "AUTO", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be changed and ranked by relevance. +// +// With `prompt_truncation` set to "AUTO_PRESERVE_ORDER", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be preserved as they are inputted into the API. +// +// With `prompt_truncation` set to "OFF", no elements will be dropped. If the sum of the inputs exceeds the model's context length limit, a `TooManyTokens` error will be returned. +// +// Compatible Deployments: +// +// - AUTO: Cohere Platform Only +// - AUTO_PRESERVE_ORDER: Azure, AWS Sagemaker/Bedrock, Private Deployments +type ChatStreamRequestPromptTruncation string - extraProperties map[string]interface{} - _rawJSON json.RawMessage +const ( + ChatStreamRequestPromptTruncationOff ChatStreamRequestPromptTruncation = "OFF" + ChatStreamRequestPromptTruncationAuto ChatStreamRequestPromptTruncation = "AUTO" + ChatStreamRequestPromptTruncationAutoPreserveOrder ChatStreamRequestPromptTruncation = "AUTO_PRESERVE_ORDER" +) + +func NewChatStreamRequestPromptTruncationFromString(s string) (ChatStreamRequestPromptTruncation, error) { + switch s { + case "OFF": + return ChatStreamRequestPromptTruncationOff, nil + case "AUTO": + return ChatStreamRequestPromptTruncationAuto, nil + case "AUTO_PRESERVE_ORDER": + return ChatStreamRequestPromptTruncationAutoPreserveOrder, nil + } + var t ChatStreamRequestPromptTruncation + return "", fmt.Errorf("%s is not a valid %T", s, t) } -func (d *Dataset) GetExtraProperties() map[string]interface{} { - return d.extraProperties +func (c ChatStreamRequestPromptTruncation) Ptr() *ChatStreamRequestPromptTruncation { + return &c } -func (d *Dataset) UnmarshalJSON(data []byte) error { - type embed Dataset - var unmarshaler = struct { - embed - CreatedAt *core.DateTime `json:"created_at"` - UpdatedAt *core.DateTime `json:"updated_at"` - }{ - embed: embed(*d), - } - if err := json.Unmarshal(data, &unmarshaler); err != nil { - return err - } - *d = Dataset(unmarshaler.embed) - d.CreatedAt = unmarshaler.CreatedAt.Time() - d.UpdatedAt = unmarshaler.UpdatedAt.Time() - - extraProperties, err := core.ExtractExtraProperties(data, *d) - if err != nil { - return err - } - d.extraProperties = extraProperties +// Used to select the [safety instruction](/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`. +// When `NONE` is specified, the safety instruction will be omitted. +// +// Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters. +// +// **Note**: This parameter is only compatible with models [Command R 08-2024](/docs/command-r#august-2024-release), [Command R+ 08-2024](/docs/command-r-plus#august-2024-release) and newer. +// +// Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments +type ChatStreamRequestSafetyMode string - d._rawJSON = json.RawMessage(data) - return nil -} +const ( + ChatStreamRequestSafetyModeContextual ChatStreamRequestSafetyMode = "CONTEXTUAL" + ChatStreamRequestSafetyModeStrict ChatStreamRequestSafetyMode = "STRICT" + ChatStreamRequestSafetyModeNone ChatStreamRequestSafetyMode = "NONE" +) -func (d *Dataset) MarshalJSON() ([]byte, error) { - type embed Dataset - var marshaler = struct { - embed - CreatedAt *core.DateTime `json:"created_at"` - UpdatedAt *core.DateTime `json:"updated_at"` - }{ - embed: embed(*d), - CreatedAt: core.NewDateTime(d.CreatedAt), - UpdatedAt: core.NewDateTime(d.UpdatedAt), +func NewChatStreamRequestSafetyModeFromString(s string) (ChatStreamRequestSafetyMode, error) { + switch s { + case "CONTEXTUAL": + return ChatStreamRequestSafetyModeContextual, nil + case "STRICT": + return ChatStreamRequestSafetyModeStrict, nil + case "NONE": + return ChatStreamRequestSafetyModeNone, nil } - return json.Marshal(marshaler) + var t ChatStreamRequestSafetyMode + return "", fmt.Errorf("%s is not a valid %T", s, t) } -func (d *Dataset) String() string { - if len(d._rawJSON) > 0 { - if value, err := core.StringifyJSON(d._rawJSON); err == nil { - return value - } - } - if value, err := core.StringifyJSON(d); err == nil { - return value - } - return fmt.Sprintf("%#v", d) +func (c ChatStreamRequestSafetyMode) Ptr() *ChatStreamRequestSafetyMode { + return &c } -type DatasetPart struct { - // The dataset part ID - Id string `json:"id" url:"id"` - // The name of the dataset part - Name string `json:"name" url:"name"` - // The download url of the file - Url *string `json:"url,omitempty" url:"url,omitempty"` - // The index of the file - Index *int `json:"index,omitempty" url:"index,omitempty"` - // The size of the file in bytes - SizeBytes *int `json:"size_bytes,omitempty" url:"size_bytes,omitempty"` - // The number of rows in the file - NumRows *int `json:"num_rows,omitempty" url:"num_rows,omitempty"` - // The download url of the original file - OriginalUrl *string `json:"original_url,omitempty" url:"original_url,omitempty"` - // The first few rows of the parsed file - Samples []string `json:"samples,omitempty" url:"samples,omitempty"` +type ChatStreamStartEvent struct { + // Unique identifier for the generated reply. Useful for submitting feedback. + GenerationId string `json:"generation_id" url:"generation_id"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (d *DatasetPart) GetExtraProperties() map[string]interface{} { - return d.extraProperties +func (c *ChatStreamStartEvent) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (d *DatasetPart) UnmarshalJSON(data []byte) error { - type unmarshaler DatasetPart +func (c *ChatStreamStartEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatStreamStartEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *d = DatasetPart(value) + *c = ChatStreamStartEvent(value) - extraProperties, err := core.ExtractExtraProperties(data, *d) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - d.extraProperties = extraProperties + c.extraProperties = extraProperties - d._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (d *DatasetPart) String() string { - if len(d._rawJSON) > 0 { - if value, err := core.StringifyJSON(d._rawJSON); err == nil { +func (c *ChatStreamStartEvent) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(d); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", d) + return fmt.Sprintf("%#v", c) } -// The type of the dataset -type DatasetType string - -const ( - DatasetTypeEmbedInput DatasetType = "embed-input" - DatasetTypeEmbedResult DatasetType = "embed-result" - DatasetTypeClusterResult DatasetType = "cluster-result" - DatasetTypeClusterOutliers DatasetType = "cluster-outliers" - DatasetTypeRerankerFinetuneInput DatasetType = "reranker-finetune-input" - DatasetTypeSingleLabelClassificationFinetuneInput DatasetType = "single-label-classification-finetune-input" - DatasetTypeChatFinetuneInput DatasetType = "chat-finetune-input" - DatasetTypeMultiLabelClassificationFinetuneInput DatasetType = "multi-label-classification-finetune-input" -) +type ChatTextGenerationEvent struct { + // The next batch of text generated by the model. + Text string `json:"text" url:"text"` -func NewDatasetTypeFromString(s string) (DatasetType, error) { - switch s { - case "embed-input": - return DatasetTypeEmbedInput, nil - case "embed-result": - return DatasetTypeEmbedResult, nil - case "cluster-result": - return DatasetTypeClusterResult, nil - case "cluster-outliers": - return DatasetTypeClusterOutliers, nil - case "reranker-finetune-input": - return DatasetTypeRerankerFinetuneInput, nil - case "single-label-classification-finetune-input": - return DatasetTypeSingleLabelClassificationFinetuneInput, nil - case "chat-finetune-input": - return DatasetTypeChatFinetuneInput, nil - case "multi-label-classification-finetune-input": - return DatasetTypeMultiLabelClassificationFinetuneInput, nil - } - var t DatasetType - return "", fmt.Errorf("%s is not a valid %T", s, t) + extraProperties map[string]interface{} + _rawJSON json.RawMessage } -func (d DatasetType) Ptr() *DatasetType { - return &d +func (c *ChatTextGenerationEvent) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -// The validation status of the dataset -type DatasetValidationStatus string - -const ( - DatasetValidationStatusUnknown DatasetValidationStatus = "unknown" - DatasetValidationStatusQueued DatasetValidationStatus = "queued" - DatasetValidationStatusProcessing DatasetValidationStatus = "processing" - DatasetValidationStatusFailed DatasetValidationStatus = "failed" - DatasetValidationStatusValidated DatasetValidationStatus = "validated" - DatasetValidationStatusSkipped DatasetValidationStatus = "skipped" -) +func (c *ChatTextGenerationEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatTextGenerationEvent + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *c = ChatTextGenerationEvent(value) -func NewDatasetValidationStatusFromString(s string) (DatasetValidationStatus, error) { - switch s { - case "unknown": - return DatasetValidationStatusUnknown, nil - case "queued": - return DatasetValidationStatusQueued, nil - case "processing": - return DatasetValidationStatusProcessing, nil - case "failed": - return DatasetValidationStatusFailed, nil - case "validated": - return DatasetValidationStatusValidated, nil - case "skipped": - return DatasetValidationStatusSkipped, nil + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err } - var t DatasetValidationStatus - return "", fmt.Errorf("%s is not a valid %T", s, t) -} + c.extraProperties = extraProperties -func (d DatasetValidationStatus) Ptr() *DatasetValidationStatus { - return &d + c._rawJSON = json.RawMessage(data) + return nil } -type DeleteConnectorResponse = map[string]interface{} +func (c *ChatTextGenerationEvent) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) +} -type DetokenizeResponse struct { - // A string representing the list of tokens. - Text string `json:"text" url:"text"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +// A streamed event delta which signifies a delta in tool call arguments. +type ChatToolCallDeltaEvent struct { + Index *int `json:"index,omitempty" url:"index,omitempty"` + Delta *ChatToolCallDeltaEventDelta `json:"delta,omitempty" url:"delta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (d *DetokenizeResponse) GetExtraProperties() map[string]interface{} { - return d.extraProperties +func (c *ChatToolCallDeltaEvent) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (d *DetokenizeResponse) UnmarshalJSON(data []byte) error { - type unmarshaler DetokenizeResponse +func (c *ChatToolCallDeltaEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallDeltaEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *d = DetokenizeResponse(value) + *c = ChatToolCallDeltaEvent(value) - extraProperties, err := core.ExtractExtraProperties(data, *d) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - d.extraProperties = extraProperties + c.extraProperties = extraProperties - d._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (d *DetokenizeResponse) String() string { - if len(d._rawJSON) > 0 { - if value, err := core.StringifyJSON(d._rawJSON); err == nil { +func (c *ChatToolCallDeltaEvent) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(d); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", d) + return fmt.Sprintf("%#v", c) } -type EmbedByTypeResponse struct { - Id string `json:"id" url:"id"` - // An object with different embedding types. The length of each embedding type array will be the same as the length of the original `texts` array. - Embeddings *EmbedByTypeResponseEmbeddings `json:"embeddings,omitempty" url:"embeddings,omitempty"` - // The text entries for which embeddings were returned. - Texts []string `json:"texts,omitempty" url:"texts,omitempty"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +type ChatToolCallDeltaEventDelta struct { + ToolCall *ChatToolCallDeltaEventDeltaToolCall `json:"tool_call,omitempty" url:"tool_call,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (e *EmbedByTypeResponse) GetExtraProperties() map[string]interface{} { - return e.extraProperties +func (c *ChatToolCallDeltaEventDelta) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (e *EmbedByTypeResponse) UnmarshalJSON(data []byte) error { - type unmarshaler EmbedByTypeResponse +func (c *ChatToolCallDeltaEventDelta) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallDeltaEventDelta var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *e = EmbedByTypeResponse(value) + *c = ChatToolCallDeltaEventDelta(value) - extraProperties, err := core.ExtractExtraProperties(data, *e) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - e.extraProperties = extraProperties + c.extraProperties = extraProperties - e._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (e *EmbedByTypeResponse) String() string { - if len(e._rawJSON) > 0 { - if value, err := core.StringifyJSON(e._rawJSON); err == nil { +func (c *ChatToolCallDeltaEventDelta) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(e); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", e) + return fmt.Sprintf("%#v", c) } -// An object with different embedding types. The length of each embedding type array will be the same as the length of the original `texts` array. -type EmbedByTypeResponseEmbeddings struct { - // An array of float embeddings. - Float [][]float64 `json:"float,omitempty" url:"float,omitempty"` - // An array of signed int8 embeddings. Each value is between -128 and 127. - Int8 [][]int `json:"int8,omitempty" url:"int8,omitempty"` - // An array of unsigned int8 embeddings. Each value is between 0 and 255. - Uint8 [][]int `json:"uint8,omitempty" url:"uint8,omitempty"` - // An array of packed signed binary embeddings. The length of each binary embedding is 1/8 the length of the float embeddings of the provided model. Each value is between -128 and 127. - Binary [][]int `json:"binary,omitempty" url:"binary,omitempty"` - // An array of packed unsigned binary embeddings. The length of each binary embedding is 1/8 the length of the float embeddings of the provided model. Each value is between 0 and 255. - Ubinary [][]int `json:"ubinary,omitempty" url:"ubinary,omitempty"` +type ChatToolCallDeltaEventDeltaToolCall struct { + Function *ChatToolCallDeltaEventDeltaToolCallFunction `json:"function,omitempty" url:"function,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (e *EmbedByTypeResponseEmbeddings) GetExtraProperties() map[string]interface{} { - return e.extraProperties +func (c *ChatToolCallDeltaEventDeltaToolCall) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (e *EmbedByTypeResponseEmbeddings) UnmarshalJSON(data []byte) error { - type unmarshaler EmbedByTypeResponseEmbeddings +func (c *ChatToolCallDeltaEventDeltaToolCall) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallDeltaEventDeltaToolCall var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *e = EmbedByTypeResponseEmbeddings(value) + *c = ChatToolCallDeltaEventDeltaToolCall(value) - extraProperties, err := core.ExtractExtraProperties(data, *e) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - e.extraProperties = extraProperties + c.extraProperties = extraProperties - e._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (e *EmbedByTypeResponseEmbeddings) String() string { - if len(e._rawJSON) > 0 { - if value, err := core.StringifyJSON(e._rawJSON); err == nil { +func (c *ChatToolCallDeltaEventDeltaToolCall) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(e); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", e) + return fmt.Sprintf("%#v", c) } -type EmbedFloatsResponse struct { - Id string `json:"id" url:"id"` - // An array of embeddings, where each embedding is an array of floats. The length of the `embeddings` array will be the same as the length of the original `texts` array. - Embeddings [][]float64 `json:"embeddings,omitempty" url:"embeddings,omitempty"` - // The text entries for which embeddings were returned. - Texts []string `json:"texts,omitempty" url:"texts,omitempty"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +type ChatToolCallDeltaEventDeltaToolCallFunction struct { + Arguments *string `json:"arguments,omitempty" url:"arguments,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (e *EmbedFloatsResponse) GetExtraProperties() map[string]interface{} { - return e.extraProperties +func (c *ChatToolCallDeltaEventDeltaToolCallFunction) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (e *EmbedFloatsResponse) UnmarshalJSON(data []byte) error { - type unmarshaler EmbedFloatsResponse +func (c *ChatToolCallDeltaEventDeltaToolCallFunction) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallDeltaEventDeltaToolCallFunction var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *e = EmbedFloatsResponse(value) + *c = ChatToolCallDeltaEventDeltaToolCallFunction(value) - extraProperties, err := core.ExtractExtraProperties(data, *e) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - e.extraProperties = extraProperties + c.extraProperties = extraProperties - e._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (e *EmbedFloatsResponse) String() string { - if len(e._rawJSON) > 0 { - if value, err := core.StringifyJSON(e._rawJSON); err == nil { +func (c *ChatToolCallDeltaEventDeltaToolCallFunction) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(e); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", e) + return fmt.Sprintf("%#v", c) } -// Specifies the type of input passed to the model. Required for embedding models v3 and higher. -// -// - `"search_document"`: Used for embeddings stored in a vector database for search use-cases. -// - `"search_query"`: Used for embeddings of search queries run against a vector DB to find relevant documents. -// - `"classification"`: Used for embeddings passed through a text classifier. -// - `"clustering"`: Used for the embeddings run through a clustering algorithm. -type EmbedInputType string +// A streamed event delta which signifies a tool call has finished streaming. +type ChatToolCallEndEvent struct { + Index *int `json:"index,omitempty" url:"index,omitempty"` -const ( - EmbedInputTypeSearchDocument EmbedInputType = "search_document" - EmbedInputTypeSearchQuery EmbedInputType = "search_query" - EmbedInputTypeClassification EmbedInputType = "classification" - EmbedInputTypeClustering EmbedInputType = "clustering" -) + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} -func NewEmbedInputTypeFromString(s string) (EmbedInputType, error) { - switch s { - case "search_document": - return EmbedInputTypeSearchDocument, nil - case "search_query": - return EmbedInputTypeSearchQuery, nil - case "classification": - return EmbedInputTypeClassification, nil - case "clustering": - return EmbedInputTypeClustering, nil +func (c *ChatToolCallEndEvent) GetExtraProperties() map[string]interface{} { + return c.extraProperties +} + +func (c *ChatToolCallEndEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallEndEvent + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err } - var t EmbedInputType - return "", fmt.Errorf("%s is not a valid %T", s, t) + *c = ChatToolCallEndEvent(value) + + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err + } + c.extraProperties = extraProperties + + c._rawJSON = json.RawMessage(data) + return nil } -func (e EmbedInputType) Ptr() *EmbedInputType { - return &e +func (c *ChatToolCallEndEvent) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) } -type EmbedJob struct { - // ID of the embed job - JobId string `json:"job_id" url:"job_id"` - // The name of the embed job - Name *string `json:"name,omitempty" url:"name,omitempty"` - // The status of the embed job - Status EmbedJobStatus `json:"status" url:"status"` - // The creation date of the embed job - CreatedAt time.Time `json:"created_at" url:"created_at"` - // ID of the input dataset - InputDatasetId string `json:"input_dataset_id" url:"input_dataset_id"` - // ID of the resulting output dataset - OutputDatasetId *string `json:"output_dataset_id,omitempty" url:"output_dataset_id,omitempty"` - // ID of the model used to embed - Model string `json:"model" url:"model"` - // The truncation option used - Truncate EmbedJobTruncate `json:"truncate" url:"truncate"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +// A streamed event delta which signifies a tool call has started streaming. +type ChatToolCallStartEvent struct { + Index *int `json:"index,omitempty" url:"index,omitempty"` + Delta *ChatToolCallStartEventDelta `json:"delta,omitempty" url:"delta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (e *EmbedJob) GetExtraProperties() map[string]interface{} { - return e.extraProperties +func (c *ChatToolCallStartEvent) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (e *EmbedJob) UnmarshalJSON(data []byte) error { - type embed EmbedJob - var unmarshaler = struct { - embed - CreatedAt *core.DateTime `json:"created_at"` - }{ - embed: embed(*e), - } - if err := json.Unmarshal(data, &unmarshaler); err != nil { +func (c *ChatToolCallStartEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallStartEvent + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { return err } - *e = EmbedJob(unmarshaler.embed) - e.CreatedAt = unmarshaler.CreatedAt.Time() + *c = ChatToolCallStartEvent(value) - extraProperties, err := core.ExtractExtraProperties(data, *e) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - e.extraProperties = extraProperties + c.extraProperties = extraProperties - e._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (e *EmbedJob) MarshalJSON() ([]byte, error) { - type embed EmbedJob - var marshaler = struct { - embed - CreatedAt *core.DateTime `json:"created_at"` - }{ - embed: embed(*e), - CreatedAt: core.NewDateTime(e.CreatedAt), - } - return json.Marshal(marshaler) -} - -func (e *EmbedJob) String() string { - if len(e._rawJSON) > 0 { - if value, err := core.StringifyJSON(e._rawJSON); err == nil { +func (c *ChatToolCallStartEvent) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(e); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", e) + return fmt.Sprintf("%#v", c) } -// The status of the embed job -type EmbedJobStatus string - -const ( - EmbedJobStatusProcessing EmbedJobStatus = "processing" - EmbedJobStatusComplete EmbedJobStatus = "complete" - EmbedJobStatusCancelling EmbedJobStatus = "cancelling" - EmbedJobStatusCancelled EmbedJobStatus = "cancelled" - EmbedJobStatusFailed EmbedJobStatus = "failed" -) +type ChatToolCallStartEventDelta struct { + ToolCall *ChatToolCallStartEventDeltaToolCall `json:"tool_call,omitempty" url:"tool_call,omitempty"` -func NewEmbedJobStatusFromString(s string) (EmbedJobStatus, error) { - switch s { - case "processing": - return EmbedJobStatusProcessing, nil - case "complete": - return EmbedJobStatusComplete, nil - case "cancelling": - return EmbedJobStatusCancelling, nil - case "cancelled": - return EmbedJobStatusCancelled, nil - case "failed": - return EmbedJobStatusFailed, nil - } - var t EmbedJobStatus - return "", fmt.Errorf("%s is not a valid %T", s, t) + extraProperties map[string]interface{} + _rawJSON json.RawMessage } -func (e EmbedJobStatus) Ptr() *EmbedJobStatus { - return &e +func (c *ChatToolCallStartEventDelta) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -// The truncation option used -type EmbedJobTruncate string - -const ( - EmbedJobTruncateStart EmbedJobTruncate = "START" - EmbedJobTruncateEnd EmbedJobTruncate = "END" -) +func (c *ChatToolCallStartEventDelta) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallStartEventDelta + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *c = ChatToolCallStartEventDelta(value) -func NewEmbedJobTruncateFromString(s string) (EmbedJobTruncate, error) { - switch s { - case "START": - return EmbedJobTruncateStart, nil - case "END": - return EmbedJobTruncateEnd, nil + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err } - var t EmbedJobTruncate - return "", fmt.Errorf("%s is not a valid %T", s, t) + c.extraProperties = extraProperties + + c._rawJSON = json.RawMessage(data) + return nil } -func (e EmbedJobTruncate) Ptr() *EmbedJobTruncate { - return &e +func (c *ChatToolCallStartEventDelta) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) } -// One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length. -// -// Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model. -// -// If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned. -type EmbedRequestTruncate string +type ChatToolCallStartEventDeltaToolCall struct { + Id *string `json:"id,omitempty" url:"id,omitempty"` + Type *string `json:"type,omitempty" url:"type,omitempty"` + Function *ChatToolCallStartEventDeltaToolCallFunction `json:"function,omitempty" url:"function,omitempty"` -const ( - EmbedRequestTruncateNone EmbedRequestTruncate = "NONE" - EmbedRequestTruncateStart EmbedRequestTruncate = "START" - EmbedRequestTruncateEnd EmbedRequestTruncate = "END" -) - -func NewEmbedRequestTruncateFromString(s string) (EmbedRequestTruncate, error) { - switch s { - case "NONE": - return EmbedRequestTruncateNone, nil - case "START": - return EmbedRequestTruncateStart, nil - case "END": - return EmbedRequestTruncateEnd, nil - } - var t EmbedRequestTruncate - return "", fmt.Errorf("%s is not a valid %T", s, t) -} - -func (e EmbedRequestTruncate) Ptr() *EmbedRequestTruncate { - return &e + extraProperties map[string]interface{} + _rawJSON json.RawMessage } -type EmbedResponse struct { - ResponseType string - EmbeddingsFloats *EmbedFloatsResponse - EmbeddingsByType *EmbedByTypeResponse +func (c *ChatToolCallStartEventDeltaToolCall) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (e *EmbedResponse) UnmarshalJSON(data []byte) error { - var unmarshaler struct { - ResponseType string `json:"response_type"` - } - if err := json.Unmarshal(data, &unmarshaler); err != nil { +func (c *ChatToolCallStartEventDeltaToolCall) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallStartEventDeltaToolCall + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { return err } - e.ResponseType = unmarshaler.ResponseType - switch unmarshaler.ResponseType { - case "embeddings_floats": - value := new(EmbedFloatsResponse) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - e.EmbeddingsFloats = value - case "embeddings_by_type": - value := new(EmbedByTypeResponse) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - e.EmbeddingsByType = value + *c = ChatToolCallStartEventDeltaToolCall(value) + + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err } + c.extraProperties = extraProperties + + c._rawJSON = json.RawMessage(data) return nil } -func (e EmbedResponse) MarshalJSON() ([]byte, error) { - if e.EmbeddingsFloats != nil { - return core.MarshalJSONWithExtraProperty(e.EmbeddingsFloats, "response_type", "embeddings_floats") +func (c *ChatToolCallStartEventDeltaToolCall) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } } - if e.EmbeddingsByType != nil { - return core.MarshalJSONWithExtraProperty(e.EmbeddingsByType, "response_type", "embeddings_by_type") + if value, err := core.StringifyJSON(c); err == nil { + return value } - return nil, fmt.Errorf("type %T does not define a non-empty union type", e) + return fmt.Sprintf("%#v", c) } -type EmbedResponseVisitor interface { - VisitEmbeddingsFloats(*EmbedFloatsResponse) error - VisitEmbeddingsByType(*EmbedByTypeResponse) error -} +type ChatToolCallStartEventDeltaToolCallFunction struct { + Name *string `json:"name,omitempty" url:"name,omitempty"` + Arguments *string `json:"arguments,omitempty" url:"arguments,omitempty"` -func (e *EmbedResponse) Accept(visitor EmbedResponseVisitor) error { - if e.EmbeddingsFloats != nil { - return visitor.VisitEmbeddingsFloats(e.EmbeddingsFloats) - } - if e.EmbeddingsByType != nil { - return visitor.VisitEmbeddingsByType(e.EmbeddingsByType) - } - return fmt.Errorf("type %T does not define a non-empty union type", e) + extraProperties map[string]interface{} + _rawJSON json.RawMessage } -type EmbeddingType string +func (c *ChatToolCallStartEventDeltaToolCallFunction) GetExtraProperties() map[string]interface{} { + return c.extraProperties +} -const ( - EmbeddingTypeFloat EmbeddingType = "float" - EmbeddingTypeInt8 EmbeddingType = "int8" - EmbeddingTypeUint8 EmbeddingType = "uint8" - EmbeddingTypeBinary EmbeddingType = "binary" - EmbeddingTypeUbinary EmbeddingType = "ubinary" -) +func (c *ChatToolCallStartEventDeltaToolCallFunction) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallStartEventDeltaToolCallFunction + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *c = ChatToolCallStartEventDeltaToolCallFunction(value) -func NewEmbeddingTypeFromString(s string) (EmbeddingType, error) { - switch s { - case "float": - return EmbeddingTypeFloat, nil - case "int8": - return EmbeddingTypeInt8, nil - case "uint8": - return EmbeddingTypeUint8, nil - case "binary": - return EmbeddingTypeBinary, nil - case "ubinary": - return EmbeddingTypeUbinary, nil + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err } - var t EmbeddingType - return "", fmt.Errorf("%s is not a valid %T", s, t) + c.extraProperties = extraProperties + + c._rawJSON = json.RawMessage(data) + return nil } -func (e EmbeddingType) Ptr() *EmbeddingType { - return &e +func (c *ChatToolCallStartEventDeltaToolCallFunction) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) } -type FinetuneDatasetMetrics struct { - // The number of tokens of valid examples that can be used for training. - TrainableTokenCount *int64 `json:"trainable_token_count,omitempty" url:"trainable_token_count,omitempty"` - // The overall number of examples. - TotalExamples *int64 `json:"total_examples,omitempty" url:"total_examples,omitempty"` - // The number of training examples. - TrainExamples *int64 `json:"train_examples,omitempty" url:"train_examples,omitempty"` - // The size in bytes of all training examples. - TrainSizeBytes *int64 `json:"train_size_bytes,omitempty" url:"train_size_bytes,omitempty"` - // Number of evaluation examples. - EvalExamples *int64 `json:"eval_examples,omitempty" url:"eval_examples,omitempty"` - // The size in bytes of all eval examples. - EvalSizeBytes *int64 `json:"eval_size_bytes,omitempty" url:"eval_size_bytes,omitempty"` +type ChatToolCallsChunkEvent struct { + ToolCallDelta *ToolCallDelta `json:"tool_call_delta,omitempty" url:"tool_call_delta,omitempty"` + Text *string `json:"text,omitempty" url:"text,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (f *FinetuneDatasetMetrics) GetExtraProperties() map[string]interface{} { - return f.extraProperties +func (c *ChatToolCallsChunkEvent) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (f *FinetuneDatasetMetrics) UnmarshalJSON(data []byte) error { - type unmarshaler FinetuneDatasetMetrics +func (c *ChatToolCallsChunkEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallsChunkEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *f = FinetuneDatasetMetrics(value) + *c = ChatToolCallsChunkEvent(value) - extraProperties, err := core.ExtractExtraProperties(data, *f) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - f.extraProperties = extraProperties + c.extraProperties = extraProperties - f._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (f *FinetuneDatasetMetrics) String() string { - if len(f._rawJSON) > 0 { - if value, err := core.StringifyJSON(f._rawJSON); err == nil { +func (c *ChatToolCallsChunkEvent) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(f); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", f) + return fmt.Sprintf("%#v", c) } -type FinishReason string +type ChatToolCallsGenerationEvent struct { + // The text generated related to the tool calls generated + Text *string `json:"text,omitempty" url:"text,omitempty"` + ToolCalls []*ToolCall `json:"tool_calls,omitempty" url:"tool_calls,omitempty"` -const ( - FinishReasonComplete FinishReason = "COMPLETE" - FinishReasonStopSequence FinishReason = "STOP_SEQUENCE" - FinishReasonError FinishReason = "ERROR" - FinishReasonErrorToxic FinishReason = "ERROR_TOXIC" - FinishReasonErrorLimit FinishReason = "ERROR_LIMIT" - FinishReasonUserCancel FinishReason = "USER_CANCEL" - FinishReasonMaxTokens FinishReason = "MAX_TOKENS" -) + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} -func NewFinishReasonFromString(s string) (FinishReason, error) { - switch s { - case "COMPLETE": - return FinishReasonComplete, nil - case "STOP_SEQUENCE": - return FinishReasonStopSequence, nil - case "ERROR": - return FinishReasonError, nil - case "ERROR_TOXIC": - return FinishReasonErrorToxic, nil - case "ERROR_LIMIT": - return FinishReasonErrorLimit, nil - case "USER_CANCEL": - return FinishReasonUserCancel, nil - case "MAX_TOKENS": - return FinishReasonMaxTokens, nil +func (c *ChatToolCallsGenerationEvent) GetExtraProperties() map[string]interface{} { + return c.extraProperties +} + +func (c *ChatToolCallsGenerationEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolCallsGenerationEvent + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err } - var t FinishReason - return "", fmt.Errorf("%s is not a valid %T", s, t) + *c = ChatToolCallsGenerationEvent(value) + + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err + } + c.extraProperties = extraProperties + + c._rawJSON = json.RawMessage(data) + return nil } -func (f FinishReason) Ptr() *FinishReason { - return &f +func (c *ChatToolCallsGenerationEvent) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) } -type GatewayTimeoutErrorBody struct { - Data *string `json:"data,omitempty" url:"data,omitempty"` +// A streamed event which contains a delta of tool plan text. +type ChatToolPlanDeltaEvent struct { + Delta *ChatToolPlanDeltaEventDelta `json:"delta,omitempty" url:"delta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (g *GatewayTimeoutErrorBody) GetExtraProperties() map[string]interface{} { - return g.extraProperties +func (c *ChatToolPlanDeltaEvent) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (g *GatewayTimeoutErrorBody) UnmarshalJSON(data []byte) error { - type unmarshaler GatewayTimeoutErrorBody +func (c *ChatToolPlanDeltaEvent) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolPlanDeltaEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *g = GatewayTimeoutErrorBody(value) + *c = ChatToolPlanDeltaEvent(value) - extraProperties, err := core.ExtractExtraProperties(data, *g) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - g.extraProperties = extraProperties + c.extraProperties = extraProperties - g._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (g *GatewayTimeoutErrorBody) String() string { - if len(g._rawJSON) > 0 { - if value, err := core.StringifyJSON(g._rawJSON); err == nil { +func (c *ChatToolPlanDeltaEvent) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(g); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", g) + return fmt.Sprintf("%#v", c) } -// One of `GENERATION|ALL|NONE` to specify how and if the token likelihoods are returned with the response. Defaults to `NONE`. -// -// If `GENERATION` is selected, the token likelihoods will only be provided for generated text. -// -// If `ALL` is selected, the token likelihoods will be provided both for the prompt and the generated text. -type GenerateRequestReturnLikelihoods string - -const ( - GenerateRequestReturnLikelihoodsGeneration GenerateRequestReturnLikelihoods = "GENERATION" - GenerateRequestReturnLikelihoodsAll GenerateRequestReturnLikelihoods = "ALL" - GenerateRequestReturnLikelihoodsNone GenerateRequestReturnLikelihoods = "NONE" -) +type ChatToolPlanDeltaEventDelta struct { + ToolPlan *string `json:"tool_plan,omitempty" url:"tool_plan,omitempty"` -func NewGenerateRequestReturnLikelihoodsFromString(s string) (GenerateRequestReturnLikelihoods, error) { - switch s { - case "GENERATION": - return GenerateRequestReturnLikelihoodsGeneration, nil - case "ALL": - return GenerateRequestReturnLikelihoodsAll, nil - case "NONE": - return GenerateRequestReturnLikelihoodsNone, nil - } - var t GenerateRequestReturnLikelihoods - return "", fmt.Errorf("%s is not a valid %T", s, t) + extraProperties map[string]interface{} + _rawJSON json.RawMessage } -func (g GenerateRequestReturnLikelihoods) Ptr() *GenerateRequestReturnLikelihoods { - return &g +func (c *ChatToolPlanDeltaEventDelta) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -// One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length. -// -// Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model. -// -// If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned. -type GenerateRequestTruncate string - -const ( - GenerateRequestTruncateNone GenerateRequestTruncate = "NONE" - GenerateRequestTruncateStart GenerateRequestTruncate = "START" - GenerateRequestTruncateEnd GenerateRequestTruncate = "END" -) +func (c *ChatToolPlanDeltaEventDelta) UnmarshalJSON(data []byte) error { + type unmarshaler ChatToolPlanDeltaEventDelta + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *c = ChatToolPlanDeltaEventDelta(value) -func NewGenerateRequestTruncateFromString(s string) (GenerateRequestTruncate, error) { - switch s { - case "NONE": - return GenerateRequestTruncateNone, nil - case "START": - return GenerateRequestTruncateStart, nil - case "END": - return GenerateRequestTruncateEnd, nil + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err } - var t GenerateRequestTruncate - return "", fmt.Errorf("%s is not a valid %T", s, t) + c.extraProperties = extraProperties + + c._rawJSON = json.RawMessage(data) + return nil } -func (g GenerateRequestTruncate) Ptr() *GenerateRequestTruncate { - return &g +func (c *ChatToolPlanDeltaEventDelta) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) } -type GenerateStreamEnd struct { - IsFinished bool `json:"is_finished" url:"is_finished"` - FinishReason *FinishReason `json:"finish_reason,omitempty" url:"finish_reason,omitempty"` - Response *GenerateStreamEndResponse `json:"response,omitempty" url:"response,omitempty"` +type CheckApiKeyResponse struct { + Valid bool `json:"valid" url:"valid"` + OrganizationId *string `json:"organization_id,omitempty" url:"organization_id,omitempty"` + OwnerId *string `json:"owner_id,omitempty" url:"owner_id,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (g *GenerateStreamEnd) GetExtraProperties() map[string]interface{} { - return g.extraProperties +func (c *CheckApiKeyResponse) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (g *GenerateStreamEnd) UnmarshalJSON(data []byte) error { - type unmarshaler GenerateStreamEnd +func (c *CheckApiKeyResponse) UnmarshalJSON(data []byte) error { + type unmarshaler CheckApiKeyResponse var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *g = GenerateStreamEnd(value) + *c = CheckApiKeyResponse(value) - extraProperties, err := core.ExtractExtraProperties(data, *g) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - g.extraProperties = extraProperties + c.extraProperties = extraProperties - g._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (g *GenerateStreamEnd) String() string { - if len(g._rawJSON) > 0 { - if value, err := core.StringifyJSON(g._rawJSON); err == nil { +func (c *CheckApiKeyResponse) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(g); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", g) + return fmt.Sprintf("%#v", c) } -type GenerateStreamEndResponse struct { - Id string `json:"id" url:"id"` - Prompt *string `json:"prompt,omitempty" url:"prompt,omitempty"` - Generations []*SingleGenerationInStream `json:"generations,omitempty" url:"generations,omitempty"` +// Citation information containing sources and the text cited. +type Citation struct { + // Start index of the cited snippet in the original source text. + Start *int `json:"start,omitempty" url:"start,omitempty"` + // End index of the cited snippet in the original source text. + End *int `json:"end,omitempty" url:"end,omitempty"` + // Text snippet that is being cited. + Text *string `json:"text,omitempty" url:"text,omitempty"` + Sources []*Source `json:"sources,omitempty" url:"sources,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (g *GenerateStreamEndResponse) GetExtraProperties() map[string]interface{} { - return g.extraProperties +func (c *Citation) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (g *GenerateStreamEndResponse) UnmarshalJSON(data []byte) error { - type unmarshaler GenerateStreamEndResponse +func (c *Citation) UnmarshalJSON(data []byte) error { + type unmarshaler Citation var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *g = GenerateStreamEndResponse(value) + *c = Citation(value) - extraProperties, err := core.ExtractExtraProperties(data, *g) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - g.extraProperties = extraProperties + c.extraProperties = extraProperties - g._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (g *GenerateStreamEndResponse) String() string { - if len(g._rawJSON) > 0 { - if value, err := core.StringifyJSON(g._rawJSON); err == nil { +func (c *Citation) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(g); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", g) + return fmt.Sprintf("%#v", c) } -type GenerateStreamError struct { - // Refers to the nth generation. Only present when `num_generations` is greater than zero. - Index *int `json:"index,omitempty" url:"index,omitempty"` - IsFinished bool `json:"is_finished" url:"is_finished"` - FinishReason FinishReason `json:"finish_reason" url:"finish_reason"` - // Error message - Err string `json:"err" url:"err"` +// A streamed event which signifies a citation has finished streaming. +type CitationEndEvent struct { + Index *int `json:"index,omitempty" url:"index,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (g *GenerateStreamError) GetExtraProperties() map[string]interface{} { - return g.extraProperties +func (c *CitationEndEvent) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (g *GenerateStreamError) UnmarshalJSON(data []byte) error { - type unmarshaler GenerateStreamError +func (c *CitationEndEvent) UnmarshalJSON(data []byte) error { + type unmarshaler CitationEndEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *g = GenerateStreamError(value) + *c = CitationEndEvent(value) - extraProperties, err := core.ExtractExtraProperties(data, *g) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - g.extraProperties = extraProperties + c.extraProperties = extraProperties - g._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (g *GenerateStreamError) String() string { - if len(g._rawJSON) > 0 { - if value, err := core.StringifyJSON(g._rawJSON); err == nil { +func (c *CitationEndEvent) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(g); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", g) + return fmt.Sprintf("%#v", c) } -type GenerateStreamEvent struct { +// Options for controlling citation generation. +type CitationOptions struct { + // Defaults to `"accurate"`. + // Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results. + Mode *CitationOptionsMode `json:"mode,omitempty" url:"mode,omitempty"` + extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (g *GenerateStreamEvent) GetExtraProperties() map[string]interface{} { - return g.extraProperties +func (c *CitationOptions) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (g *GenerateStreamEvent) UnmarshalJSON(data []byte) error { - type unmarshaler GenerateStreamEvent +func (c *CitationOptions) UnmarshalJSON(data []byte) error { + type unmarshaler CitationOptions var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *g = GenerateStreamEvent(value) + *c = CitationOptions(value) - extraProperties, err := core.ExtractExtraProperties(data, *g) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - g.extraProperties = extraProperties + c.extraProperties = extraProperties - g._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (g *GenerateStreamEvent) String() string { - if len(g._rawJSON) > 0 { - if value, err := core.StringifyJSON(g._rawJSON); err == nil { +func (c *CitationOptions) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(g); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", g) -} - -// One of `GENERATION|ALL|NONE` to specify how and if the token likelihoods are returned with the response. Defaults to `NONE`. -// -// If `GENERATION` is selected, the token likelihoods will only be provided for generated text. -// -// If `ALL` is selected, the token likelihoods will be provided both for the prompt and the generated text. -type GenerateStreamRequestReturnLikelihoods string - -const ( - GenerateStreamRequestReturnLikelihoodsGeneration GenerateStreamRequestReturnLikelihoods = "GENERATION" - GenerateStreamRequestReturnLikelihoodsAll GenerateStreamRequestReturnLikelihoods = "ALL" - GenerateStreamRequestReturnLikelihoodsNone GenerateStreamRequestReturnLikelihoods = "NONE" -) - -func NewGenerateStreamRequestReturnLikelihoodsFromString(s string) (GenerateStreamRequestReturnLikelihoods, error) { - switch s { - case "GENERATION": - return GenerateStreamRequestReturnLikelihoodsGeneration, nil - case "ALL": - return GenerateStreamRequestReturnLikelihoodsAll, nil - case "NONE": - return GenerateStreamRequestReturnLikelihoodsNone, nil - } - var t GenerateStreamRequestReturnLikelihoods - return "", fmt.Errorf("%s is not a valid %T", s, t) -} - -func (g GenerateStreamRequestReturnLikelihoods) Ptr() *GenerateStreamRequestReturnLikelihoods { - return &g + return fmt.Sprintf("%#v", c) } -// One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length. -// -// Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model. -// -// If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned. -type GenerateStreamRequestTruncate string +// Defaults to `"accurate"`. +// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results. +type CitationOptionsMode string const ( - GenerateStreamRequestTruncateNone GenerateStreamRequestTruncate = "NONE" - GenerateStreamRequestTruncateStart GenerateStreamRequestTruncate = "START" - GenerateStreamRequestTruncateEnd GenerateStreamRequestTruncate = "END" + CitationOptionsModeFast CitationOptionsMode = "FAST" + CitationOptionsModeAccurate CitationOptionsMode = "ACCURATE" + CitationOptionsModeOff CitationOptionsMode = "OFF" ) -func NewGenerateStreamRequestTruncateFromString(s string) (GenerateStreamRequestTruncate, error) { +func NewCitationOptionsModeFromString(s string) (CitationOptionsMode, error) { switch s { - case "NONE": - return GenerateStreamRequestTruncateNone, nil - case "START": - return GenerateStreamRequestTruncateStart, nil - case "END": - return GenerateStreamRequestTruncateEnd, nil + case "FAST": + return CitationOptionsModeFast, nil + case "ACCURATE": + return CitationOptionsModeAccurate, nil + case "OFF": + return CitationOptionsModeOff, nil } - var t GenerateStreamRequestTruncate + var t CitationOptionsMode return "", fmt.Errorf("%s is not a valid %T", s, t) } -func (g GenerateStreamRequestTruncate) Ptr() *GenerateStreamRequestTruncate { - return &g +func (c CitationOptionsMode) Ptr() *CitationOptionsMode { + return &c } -type GenerateStreamText struct { - // A segment of text of the generation. - Text string `json:"text" url:"text"` - // Refers to the nth generation. Only present when `num_generations` is greater than zero, and only when text responses are being streamed. - Index *int `json:"index,omitempty" url:"index,omitempty"` - IsFinished bool `json:"is_finished" url:"is_finished"` +// A streamed event which signifies a citation has been created. +type CitationStartEvent struct { + Index *int `json:"index,omitempty" url:"index,omitempty"` + Delta *CitationStartEventDelta `json:"delta,omitempty" url:"delta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (g *GenerateStreamText) GetExtraProperties() map[string]interface{} { - return g.extraProperties +func (c *CitationStartEvent) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (g *GenerateStreamText) UnmarshalJSON(data []byte) error { - type unmarshaler GenerateStreamText +func (c *CitationStartEvent) UnmarshalJSON(data []byte) error { + type unmarshaler CitationStartEvent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *g = GenerateStreamText(value) + *c = CitationStartEvent(value) - extraProperties, err := core.ExtractExtraProperties(data, *g) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - g.extraProperties = extraProperties + c.extraProperties = extraProperties - g._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (g *GenerateStreamText) String() string { - if len(g._rawJSON) > 0 { - if value, err := core.StringifyJSON(g._rawJSON); err == nil { +func (c *CitationStartEvent) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(g); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", g) + return fmt.Sprintf("%#v", c) } -// Response in content type stream when `stream` is `true` in the request parameters. Generation tokens are streamed with the GenerationStream response. The final response is of type GenerationFinalResponse. -type GenerateStreamedResponse struct { - EventType string - TextGeneration *GenerateStreamText - StreamEnd *GenerateStreamEnd - StreamError *GenerateStreamError +type CitationStartEventDelta struct { + Message *CitationStartEventDeltaMessage `json:"message,omitempty" url:"message,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage } -func (g *GenerateStreamedResponse) UnmarshalJSON(data []byte) error { - var unmarshaler struct { - EventType string `json:"event_type"` - } - if err := json.Unmarshal(data, &unmarshaler); err != nil { - return err - } - g.EventType = unmarshaler.EventType - switch unmarshaler.EventType { - case "text-generation": - value := new(GenerateStreamText) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - g.TextGeneration = value - case "stream-end": - value := new(GenerateStreamEnd) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - g.StreamEnd = value - case "stream-error": - value := new(GenerateStreamError) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - g.StreamError = value - } - return nil +func (c *CitationStartEventDelta) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (g GenerateStreamedResponse) MarshalJSON() ([]byte, error) { - if g.TextGeneration != nil { - return core.MarshalJSONWithExtraProperty(g.TextGeneration, "event_type", "text-generation") - } - if g.StreamEnd != nil { - return core.MarshalJSONWithExtraProperty(g.StreamEnd, "event_type", "stream-end") +func (c *CitationStartEventDelta) UnmarshalJSON(data []byte) error { + type unmarshaler CitationStartEventDelta + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err } - if g.StreamError != nil { - return core.MarshalJSONWithExtraProperty(g.StreamError, "event_type", "stream-error") + *c = CitationStartEventDelta(value) + + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err } - return nil, fmt.Errorf("type %T does not define a non-empty union type", g) -} + c.extraProperties = extraProperties -type GenerateStreamedResponseVisitor interface { - VisitTextGeneration(*GenerateStreamText) error - VisitStreamEnd(*GenerateStreamEnd) error - VisitStreamError(*GenerateStreamError) error + c._rawJSON = json.RawMessage(data) + return nil } -func (g *GenerateStreamedResponse) Accept(visitor GenerateStreamedResponseVisitor) error { - if g.TextGeneration != nil { - return visitor.VisitTextGeneration(g.TextGeneration) - } - if g.StreamEnd != nil { - return visitor.VisitStreamEnd(g.StreamEnd) +func (c *CitationStartEventDelta) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } } - if g.StreamError != nil { - return visitor.VisitStreamError(g.StreamError) + if value, err := core.StringifyJSON(c); err == nil { + return value } - return fmt.Errorf("type %T does not define a non-empty union type", g) + return fmt.Sprintf("%#v", c) } -type Generation struct { - Id string `json:"id" url:"id"` - // Prompt used for generations. - Prompt *string `json:"prompt,omitempty" url:"prompt,omitempty"` - // List of generated results - Generations []*SingleGeneration `json:"generations,omitempty" url:"generations,omitempty"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +type CitationStartEventDeltaMessage struct { + Citations *Citation `json:"citations,omitempty" url:"citations,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (g *Generation) GetExtraProperties() map[string]interface{} { - return g.extraProperties +func (c *CitationStartEventDeltaMessage) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (g *Generation) UnmarshalJSON(data []byte) error { - type unmarshaler Generation +func (c *CitationStartEventDeltaMessage) UnmarshalJSON(data []byte) error { + type unmarshaler CitationStartEventDeltaMessage var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *g = Generation(value) + *c = CitationStartEventDeltaMessage(value) - extraProperties, err := core.ExtractExtraProperties(data, *g) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - g.extraProperties = extraProperties + c.extraProperties = extraProperties - g._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (g *Generation) String() string { - if len(g._rawJSON) > 0 { - if value, err := core.StringifyJSON(g._rawJSON); err == nil { +func (c *CitationStartEventDeltaMessage) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(g); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", g) + return fmt.Sprintf("%#v", c) } -type GetConnectorResponse struct { - Connector *Connector `json:"connector,omitempty" url:"connector,omitempty"` +type ClassifyDataMetrics struct { + LabelMetrics []*LabelMetric `json:"label_metrics,omitempty" url:"label_metrics,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (g *GetConnectorResponse) GetExtraProperties() map[string]interface{} { - return g.extraProperties +func (c *ClassifyDataMetrics) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (g *GetConnectorResponse) UnmarshalJSON(data []byte) error { - type unmarshaler GetConnectorResponse +func (c *ClassifyDataMetrics) UnmarshalJSON(data []byte) error { + type unmarshaler ClassifyDataMetrics var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *g = GetConnectorResponse(value) + *c = ClassifyDataMetrics(value) - extraProperties, err := core.ExtractExtraProperties(data, *g) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - g.extraProperties = extraProperties + c.extraProperties = extraProperties - g._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (g *GetConnectorResponse) String() string { - if len(g._rawJSON) > 0 { - if value, err := core.StringifyJSON(g._rawJSON); err == nil { +func (c *ClassifyDataMetrics) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(g); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", g) + return fmt.Sprintf("%#v", c) } -// Contains information about the model and which API endpoints it can be used with. -type GetModelResponse struct { - // Specify this name in the `model` parameter of API requests to use your chosen model. - Name *string `json:"name,omitempty" url:"name,omitempty"` - // The API endpoints that the model is compatible with. - Endpoints []CompatibleEndpoint `json:"endpoints,omitempty" url:"endpoints,omitempty"` - // Whether the model has been fine-tuned or not. - Finetuned *bool `json:"finetuned,omitempty" url:"finetuned,omitempty"` - // The maximum number of tokens that the model can process in a single request. Note that not all of these tokens are always available due to special tokens and preambles that Cohere has added by default. - ContextLength *float64 `json:"context_length,omitempty" url:"context_length,omitempty"` - // Public URL to the tokenizer's configuration file. - TokenizerUrl *string `json:"tokenizer_url,omitempty" url:"tokenizer_url,omitempty"` - // The API endpoints that the model is default to. - DefaultEndpoints []CompatibleEndpoint `json:"default_endpoints,omitempty" url:"default_endpoints,omitempty"` +type ClassifyExample struct { + Text *string `json:"text,omitempty" url:"text,omitempty"` + Label *string `json:"label,omitempty" url:"label,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (g *GetModelResponse) GetExtraProperties() map[string]interface{} { - return g.extraProperties +func (c *ClassifyExample) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (g *GetModelResponse) UnmarshalJSON(data []byte) error { - type unmarshaler GetModelResponse +func (c *ClassifyExample) UnmarshalJSON(data []byte) error { + type unmarshaler ClassifyExample var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *g = GetModelResponse(value) + *c = ClassifyExample(value) - extraProperties, err := core.ExtractExtraProperties(data, *g) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - g.extraProperties = extraProperties + c.extraProperties = extraProperties - g._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (g *GetModelResponse) String() string { - if len(g._rawJSON) > 0 { - if value, err := core.StringifyJSON(g._rawJSON); err == nil { +func (c *ClassifyExample) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(g); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", g) + return fmt.Sprintf("%#v", c) } -type JsonResponseFormat struct { - // A JSON schema object that the output will adhere to. There are some restrictions we have on the schema, refer to [our guide](/docs/structured-outputs-json#schema-constraints) for more information. - // Example (required name and age object): - // - // ```json - // - // { - // "type": "object", - // "properties": { - // "name": { "type": "string" }, - // "age": { "type": "integer" } - // }, - // "required": ["name", "age"] - // } - // - // ``` - // - // **Note**: This field must not be specified when the `type` is set to `"text"`. - Schema map[string]interface{} `json:"schema,omitempty" url:"schema,omitempty"` +// One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length. +// Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model. +// If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned. +type ClassifyRequestTruncate string + +const ( + ClassifyRequestTruncateNone ClassifyRequestTruncate = "NONE" + ClassifyRequestTruncateStart ClassifyRequestTruncate = "START" + ClassifyRequestTruncateEnd ClassifyRequestTruncate = "END" +) + +func NewClassifyRequestTruncateFromString(s string) (ClassifyRequestTruncate, error) { + switch s { + case "NONE": + return ClassifyRequestTruncateNone, nil + case "START": + return ClassifyRequestTruncateStart, nil + case "END": + return ClassifyRequestTruncateEnd, nil + } + var t ClassifyRequestTruncate + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (c ClassifyRequestTruncate) Ptr() *ClassifyRequestTruncate { + return &c +} + +type ClassifyResponse struct { + Id string `json:"id" url:"id"` + Classifications []*ClassifyResponseClassificationsItem `json:"classifications,omitempty" url:"classifications,omitempty"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (j *JsonResponseFormat) GetExtraProperties() map[string]interface{} { - return j.extraProperties +func (c *ClassifyResponse) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (j *JsonResponseFormat) UnmarshalJSON(data []byte) error { - type unmarshaler JsonResponseFormat +func (c *ClassifyResponse) UnmarshalJSON(data []byte) error { + type unmarshaler ClassifyResponse var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *j = JsonResponseFormat(value) + *c = ClassifyResponse(value) - extraProperties, err := core.ExtractExtraProperties(data, *j) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - j.extraProperties = extraProperties + c.extraProperties = extraProperties - j._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (j *JsonResponseFormat) String() string { - if len(j._rawJSON) > 0 { - if value, err := core.StringifyJSON(j._rawJSON); err == nil { +func (c *ClassifyResponse) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(j); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", j) + return fmt.Sprintf("%#v", c) } -type LabelMetric struct { - // Total number of examples for this label - TotalExamples *int64 `json:"total_examples,omitempty" url:"total_examples,omitempty"` - // value of the label - Label *string `json:"label,omitempty" url:"label,omitempty"` - // samples for this label - Samples []string `json:"samples,omitempty" url:"samples,omitempty"` +type ClassifyResponseClassificationsItem struct { + Id string `json:"id" url:"id"` + // The input text that was classified + Input *string `json:"input,omitempty" url:"input,omitempty"` + // The predicted label for the associated query (only filled for single-label models) + Prediction *string `json:"prediction,omitempty" url:"prediction,omitempty"` + // An array containing the predicted labels for the associated query (only filled for single-label classification) + Predictions []string `json:"predictions,omitempty" url:"predictions,omitempty"` + // The confidence score for the top predicted class (only filled for single-label classification) + Confidence *float64 `json:"confidence,omitempty" url:"confidence,omitempty"` + // An array containing the confidence scores of all the predictions in the same order + Confidences []float64 `json:"confidences,omitempty" url:"confidences,omitempty"` + // A map containing each label and its confidence score according to the classifier. All the confidence scores add up to 1 for single-label classification. For multi-label classification the label confidences are independent of each other, so they don't have to sum up to 1. + Labels map[string]*ClassifyResponseClassificationsItemLabelsValue `json:"labels,omitempty" url:"labels,omitempty"` + // The type of classification performed + ClassificationType ClassifyResponseClassificationsItemClassificationType `json:"classification_type" url:"classification_type"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (l *LabelMetric) GetExtraProperties() map[string]interface{} { - return l.extraProperties +func (c *ClassifyResponseClassificationsItem) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (l *LabelMetric) UnmarshalJSON(data []byte) error { - type unmarshaler LabelMetric +func (c *ClassifyResponseClassificationsItem) UnmarshalJSON(data []byte) error { + type unmarshaler ClassifyResponseClassificationsItem var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *l = LabelMetric(value) + *c = ClassifyResponseClassificationsItem(value) - extraProperties, err := core.ExtractExtraProperties(data, *l) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - l.extraProperties = extraProperties + c.extraProperties = extraProperties - l._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (l *LabelMetric) String() string { - if len(l._rawJSON) > 0 { - if value, err := core.StringifyJSON(l._rawJSON); err == nil { +func (c *ClassifyResponseClassificationsItem) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(l); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", l) + return fmt.Sprintf("%#v", c) } -type ListConnectorsResponse struct { - Connectors []*Connector `json:"connectors,omitempty" url:"connectors,omitempty"` - // Total number of connectors. - TotalCount *float64 `json:"total_count,omitempty" url:"total_count,omitempty"` +// The type of classification performed +type ClassifyResponseClassificationsItemClassificationType string + +const ( + ClassifyResponseClassificationsItemClassificationTypeSingleLabel ClassifyResponseClassificationsItemClassificationType = "single-label" + ClassifyResponseClassificationsItemClassificationTypeMultiLabel ClassifyResponseClassificationsItemClassificationType = "multi-label" +) + +func NewClassifyResponseClassificationsItemClassificationTypeFromString(s string) (ClassifyResponseClassificationsItemClassificationType, error) { + switch s { + case "single-label": + return ClassifyResponseClassificationsItemClassificationTypeSingleLabel, nil + case "multi-label": + return ClassifyResponseClassificationsItemClassificationTypeMultiLabel, nil + } + var t ClassifyResponseClassificationsItemClassificationType + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (c ClassifyResponseClassificationsItemClassificationType) Ptr() *ClassifyResponseClassificationsItemClassificationType { + return &c +} + +type ClassifyResponseClassificationsItemLabelsValue struct { + Confidence *float64 `json:"confidence,omitempty" url:"confidence,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (l *ListConnectorsResponse) GetExtraProperties() map[string]interface{} { - return l.extraProperties +func (c *ClassifyResponseClassificationsItemLabelsValue) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (l *ListConnectorsResponse) UnmarshalJSON(data []byte) error { - type unmarshaler ListConnectorsResponse +func (c *ClassifyResponseClassificationsItemLabelsValue) UnmarshalJSON(data []byte) error { + type unmarshaler ClassifyResponseClassificationsItemLabelsValue var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *l = ListConnectorsResponse(value) + *c = ClassifyResponseClassificationsItemLabelsValue(value) - extraProperties, err := core.ExtractExtraProperties(data, *l) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - l.extraProperties = extraProperties + c.extraProperties = extraProperties - l._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (l *ListConnectorsResponse) String() string { - if len(l._rawJSON) > 0 { - if value, err := core.StringifyJSON(l._rawJSON); err == nil { +func (c *ClassifyResponseClassificationsItemLabelsValue) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(l); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", l) + return fmt.Sprintf("%#v", c) } -type ListEmbedJobResponse struct { - EmbedJobs []*EmbedJob `json:"embed_jobs,omitempty" url:"embed_jobs,omitempty"` +type ClientClosedRequestErrorBody struct { + Data *string `json:"data,omitempty" url:"data,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (l *ListEmbedJobResponse) GetExtraProperties() map[string]interface{} { - return l.extraProperties +func (c *ClientClosedRequestErrorBody) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (l *ListEmbedJobResponse) UnmarshalJSON(data []byte) error { - type unmarshaler ListEmbedJobResponse +func (c *ClientClosedRequestErrorBody) UnmarshalJSON(data []byte) error { + type unmarshaler ClientClosedRequestErrorBody var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *l = ListEmbedJobResponse(value) + *c = ClientClosedRequestErrorBody(value) - extraProperties, err := core.ExtractExtraProperties(data, *l) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - l.extraProperties = extraProperties + c.extraProperties = extraProperties - l._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (l *ListEmbedJobResponse) String() string { - if len(l._rawJSON) > 0 { - if value, err := core.StringifyJSON(l._rawJSON); err == nil { +func (c *ClientClosedRequestErrorBody) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(l); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", l) + return fmt.Sprintf("%#v", c) } -type ListModelsResponse struct { - Models []*GetModelResponse `json:"models,omitempty" url:"models,omitempty"` - // A token to retrieve the next page of results. Provide in the page_token parameter of the next request. - NextPageToken *string `json:"next_page_token,omitempty" url:"next_page_token,omitempty"` +// One of the Cohere API endpoints that the model can be used with. +type CompatibleEndpoint string + +const ( + CompatibleEndpointChat CompatibleEndpoint = "chat" + CompatibleEndpointEmbed CompatibleEndpoint = "embed" + CompatibleEndpointClassify CompatibleEndpoint = "classify" + CompatibleEndpointSummarize CompatibleEndpoint = "summarize" + CompatibleEndpointRerank CompatibleEndpoint = "rerank" + CompatibleEndpointRate CompatibleEndpoint = "rate" + CompatibleEndpointGenerate CompatibleEndpoint = "generate" +) + +func NewCompatibleEndpointFromString(s string) (CompatibleEndpoint, error) { + switch s { + case "chat": + return CompatibleEndpointChat, nil + case "embed": + return CompatibleEndpointEmbed, nil + case "classify": + return CompatibleEndpointClassify, nil + case "summarize": + return CompatibleEndpointSummarize, nil + case "rerank": + return CompatibleEndpointRerank, nil + case "rate": + return CompatibleEndpointRate, nil + case "generate": + return CompatibleEndpointGenerate, nil + } + var t CompatibleEndpoint + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (c CompatibleEndpoint) Ptr() *CompatibleEndpoint { + return &c +} + +// A connector allows you to integrate data sources with the '/chat' endpoint to create grounded generations with citations to the data source. +// documents to help answer users. +type Connector struct { + // The unique identifier of the connector (used in both `/connectors` & `/chat` endpoints). + // This is automatically created from the name of the connector upon registration. + Id string `json:"id" url:"id"` + // The organization to which this connector belongs. This is automatically set to + // the organization of the user who created the connector. + OrganizationId *string `json:"organization_id,omitempty" url:"organization_id,omitempty"` + // A human-readable name for the connector. + Name string `json:"name" url:"name"` + // A description of the connector. + Description *string `json:"description,omitempty" url:"description,omitempty"` + // The URL of the connector that will be used to search for documents. + Url *string `json:"url,omitempty" url:"url,omitempty"` + // The UTC time at which the connector was created. + CreatedAt time.Time `json:"created_at" url:"created_at"` + // The UTC time at which the connector was last updated. + UpdatedAt time.Time `json:"updated_at" url:"updated_at"` + // A list of fields to exclude from the prompt (fields remain in the document). + Excludes []string `json:"excludes,omitempty" url:"excludes,omitempty"` + // The type of authentication/authorization used by the connector. Possible values: [oauth, service_auth] + AuthType *string `json:"auth_type,omitempty" url:"auth_type,omitempty"` + // The OAuth 2.0 configuration for the connector. + Oauth *ConnectorOAuth `json:"oauth,omitempty" url:"oauth,omitempty"` + // The OAuth status for the user making the request. One of ["valid", "expired", ""]. Empty string (field is omitted) means the user has not authorized the connector yet. + AuthStatus *ConnectorAuthStatus `json:"auth_status,omitempty" url:"auth_status,omitempty"` + // Whether the connector is active or not. + Active *bool `json:"active,omitempty" url:"active,omitempty"` + // Whether a chat request should continue or not if the request to this connector fails. + ContinueOnFailure *bool `json:"continue_on_failure,omitempty" url:"continue_on_failure,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (l *ListModelsResponse) GetExtraProperties() map[string]interface{} { - return l.extraProperties +func (c *Connector) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (l *ListModelsResponse) UnmarshalJSON(data []byte) error { - type unmarshaler ListModelsResponse - var value unmarshaler - if err := json.Unmarshal(data, &value); err != nil { +func (c *Connector) UnmarshalJSON(data []byte) error { + type embed Connector + var unmarshaler = struct { + embed + CreatedAt *core.DateTime `json:"created_at"` + UpdatedAt *core.DateTime `json:"updated_at"` + }{ + embed: embed(*c), + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { return err } - *l = ListModelsResponse(value) + *c = Connector(unmarshaler.embed) + c.CreatedAt = unmarshaler.CreatedAt.Time() + c.UpdatedAt = unmarshaler.UpdatedAt.Time() - extraProperties, err := core.ExtractExtraProperties(data, *l) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - l.extraProperties = extraProperties + c.extraProperties = extraProperties - l._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (l *ListModelsResponse) String() string { - if len(l._rawJSON) > 0 { - if value, err := core.StringifyJSON(l._rawJSON); err == nil { - return value - } - } - if value, err := core.StringifyJSON(l); err == nil { - return value - } - return fmt.Sprintf("%#v", l) -} +func (c *Connector) MarshalJSON() ([]byte, error) { + type embed Connector + var marshaler = struct { + embed + CreatedAt *core.DateTime `json:"created_at"` + UpdatedAt *core.DateTime `json:"updated_at"` + }{ + embed: embed(*c), + CreatedAt: core.NewDateTime(c.CreatedAt), + UpdatedAt: core.NewDateTime(c.UpdatedAt), + } + return json.Marshal(marshaler) +} -type Message struct { - Role string - Chatbot *ChatMessage - System *ChatMessage - User *ChatMessage - Tool *ToolMessage +func (c *Connector) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) } -func (m *Message) UnmarshalJSON(data []byte) error { +// The OAuth status for the user making the request. One of ["valid", "expired", ""]. Empty string (field is omitted) means the user has not authorized the connector yet. +type ConnectorAuthStatus string + +const ( + ConnectorAuthStatusValid ConnectorAuthStatus = "valid" + ConnectorAuthStatusExpired ConnectorAuthStatus = "expired" +) + +func NewConnectorAuthStatusFromString(s string) (ConnectorAuthStatus, error) { + switch s { + case "valid": + return ConnectorAuthStatusValid, nil + case "expired": + return ConnectorAuthStatusExpired, nil + } + var t ConnectorAuthStatus + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (c ConnectorAuthStatus) Ptr() *ConnectorAuthStatus { + return &c +} + +type ConnectorOAuth struct { + // The OAuth 2.0 client ID. This field is encrypted at rest. + ClientId *string `json:"client_id,omitempty" url:"client_id,omitempty"` + // The OAuth 2.0 client Secret. This field is encrypted at rest and never returned in a response. + ClientSecret *string `json:"client_secret,omitempty" url:"client_secret,omitempty"` + // The OAuth 2.0 /authorize endpoint to use when users authorize the connector. + AuthorizeUrl string `json:"authorize_url" url:"authorize_url"` + // The OAuth 2.0 /token endpoint to use when users authorize the connector. + TokenUrl string `json:"token_url" url:"token_url"` + // The OAuth scopes to request when users authorize the connector. + Scope *string `json:"scope,omitempty" url:"scope,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (c *ConnectorOAuth) GetExtraProperties() map[string]interface{} { + return c.extraProperties +} + +func (c *ConnectorOAuth) UnmarshalJSON(data []byte) error { + type unmarshaler ConnectorOAuth + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *c = ConnectorOAuth(value) + + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err + } + c.extraProperties = extraProperties + + c._rawJSON = json.RawMessage(data) + return nil +} + +func (c *ConnectorOAuth) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) +} + +// A Content block which contains information about the content type and the content itself. +type Content struct { + Type string + Text *TextContent +} + +func (c *Content) UnmarshalJSON(data []byte) error { var unmarshaler struct { - Role string `json:"role"` + Type string `json:"type"` } if err := json.Unmarshal(data, &unmarshaler); err != nil { return err } - m.Role = unmarshaler.Role - switch unmarshaler.Role { - case "CHATBOT": - value := new(ChatMessage) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - m.Chatbot = value - case "SYSTEM": - value := new(ChatMessage) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - m.System = value - case "USER": - value := new(ChatMessage) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - m.User = value - case "TOOL": - value := new(ToolMessage) + c.Type = unmarshaler.Type + if unmarshaler.Type == "" { + return fmt.Errorf("%T did not include discriminant type", c) + } + switch unmarshaler.Type { + case "text": + value := new(TextContent) if err := json.Unmarshal(data, &value); err != nil { return err } - m.Tool = value + c.Text = value } return nil } -func (m Message) MarshalJSON() ([]byte, error) { - if m.Chatbot != nil { - return core.MarshalJSONWithExtraProperty(m.Chatbot, "role", "CHATBOT") - } - if m.System != nil { - return core.MarshalJSONWithExtraProperty(m.System, "role", "SYSTEM") - } - if m.User != nil { - return core.MarshalJSONWithExtraProperty(m.User, "role", "USER") - } - if m.Tool != nil { - return core.MarshalJSONWithExtraProperty(m.Tool, "role", "TOOL") +func (c Content) MarshalJSON() ([]byte, error) { + if c.Text != nil { + return core.MarshalJSONWithExtraProperty(c.Text, "type", "text") } - return nil, fmt.Errorf("type %T does not define a non-empty union type", m) + return nil, fmt.Errorf("type %T does not define a non-empty union type", c) } -type MessageVisitor interface { - VisitChatbot(*ChatMessage) error - VisitSystem(*ChatMessage) error - VisitUser(*ChatMessage) error - VisitTool(*ToolMessage) error +type ContentVisitor interface { + VisitText(*TextContent) error } -func (m *Message) Accept(visitor MessageVisitor) error { - if m.Chatbot != nil { - return visitor.VisitChatbot(m.Chatbot) - } - if m.System != nil { - return visitor.VisitSystem(m.System) - } - if m.User != nil { - return visitor.VisitUser(m.User) +func (c *Content) Accept(visitor ContentVisitor) error { + if c.Text != nil { + return visitor.VisitText(c.Text) } - if m.Tool != nil { - return visitor.VisitTool(m.Tool) - } - return fmt.Errorf("type %T does not define a non-empty union type", m) + return fmt.Errorf("type %T does not define a non-empty union type", c) } -type Metrics struct { - FinetuneDatasetMetrics *FinetuneDatasetMetrics `json:"finetune_dataset_metrics,omitempty" url:"finetune_dataset_metrics,omitempty"` - EmbedData *MetricsEmbedData `json:"embed_data,omitempty" url:"embed_data,omitempty"` +type CreateConnectorOAuth struct { + // The OAuth 2.0 client ID. This fields is encrypted at rest. + ClientId *string `json:"client_id,omitempty" url:"client_id,omitempty"` + // The OAuth 2.0 client Secret. This field is encrypted at rest and never returned in a response. + ClientSecret *string `json:"client_secret,omitempty" url:"client_secret,omitempty"` + // The OAuth 2.0 /authorize endpoint to use when users authorize the connector. + AuthorizeUrl *string `json:"authorize_url,omitempty" url:"authorize_url,omitempty"` + // The OAuth 2.0 /token endpoint to use when users authorize the connector. + TokenUrl *string `json:"token_url,omitempty" url:"token_url,omitempty"` + // The OAuth scopes to request when users authorize the connector. + Scope *string `json:"scope,omitempty" url:"scope,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (m *Metrics) GetExtraProperties() map[string]interface{} { - return m.extraProperties +func (c *CreateConnectorOAuth) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (m *Metrics) UnmarshalJSON(data []byte) error { - type unmarshaler Metrics +func (c *CreateConnectorOAuth) UnmarshalJSON(data []byte) error { + type unmarshaler CreateConnectorOAuth var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *m = Metrics(value) + *c = CreateConnectorOAuth(value) - extraProperties, err := core.ExtractExtraProperties(data, *m) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - m.extraProperties = extraProperties + c.extraProperties = extraProperties - m._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (m *Metrics) String() string { - if len(m._rawJSON) > 0 { - if value, err := core.StringifyJSON(m._rawJSON); err == nil { +func (c *CreateConnectorOAuth) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(m); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", m) + return fmt.Sprintf("%#v", c) } -type MetricsEmbedData struct { - // the fields in the dataset - Fields []*MetricsEmbedDataFieldsItem `json:"fields,omitempty" url:"fields,omitempty"` +type CreateConnectorResponse struct { + Connector *Connector `json:"connector,omitempty" url:"connector,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (m *MetricsEmbedData) GetExtraProperties() map[string]interface{} { - return m.extraProperties +func (c *CreateConnectorResponse) GetExtraProperties() map[string]interface{} { + return c.extraProperties } -func (m *MetricsEmbedData) UnmarshalJSON(data []byte) error { - type unmarshaler MetricsEmbedData +func (c *CreateConnectorResponse) UnmarshalJSON(data []byte) error { + type unmarshaler CreateConnectorResponse var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *m = MetricsEmbedData(value) + *c = CreateConnectorResponse(value) - extraProperties, err := core.ExtractExtraProperties(data, *m) + extraProperties, err := core.ExtractExtraProperties(data, *c) if err != nil { return err } - m.extraProperties = extraProperties + c.extraProperties = extraProperties - m._rawJSON = json.RawMessage(data) + c._rawJSON = json.RawMessage(data) return nil } -func (m *MetricsEmbedData) String() string { - if len(m._rawJSON) > 0 { - if value, err := core.StringifyJSON(m._rawJSON); err == nil { +func (c *CreateConnectorResponse) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(m); err == nil { + if value, err := core.StringifyJSON(c); err == nil { return value } - return fmt.Sprintf("%#v", m) + return fmt.Sprintf("%#v", c) } -type MetricsEmbedDataFieldsItem struct { - // the name of the field - Name *string `json:"name,omitempty" url:"name,omitempty"` - // the number of times the field appears in the dataset - Count *float64 `json:"count,omitempty" url:"count,omitempty"` +type CreateConnectorServiceAuth struct { + Type AuthTokenType `json:"type" url:"type"` + // The token that will be used in the HTTP Authorization header when making requests to the connector. This field is encrypted at rest and never returned in a response. + Token string `json:"token" url:"token"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (m *MetricsEmbedDataFieldsItem) GetExtraProperties() map[string]interface{} { - return m.extraProperties +func (c *CreateConnectorServiceAuth) GetExtraProperties() map[string]interface{} { + return c.extraProperties +} + +func (c *CreateConnectorServiceAuth) UnmarshalJSON(data []byte) error { + type unmarshaler CreateConnectorServiceAuth + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *c = CreateConnectorServiceAuth(value) + + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err + } + c.extraProperties = extraProperties + + c._rawJSON = json.RawMessage(data) + return nil +} + +func (c *CreateConnectorServiceAuth) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) +} + +// Response from creating an embed job. +type CreateEmbedJobResponse struct { + JobId string `json:"job_id" url:"job_id"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (c *CreateEmbedJobResponse) GetExtraProperties() map[string]interface{} { + return c.extraProperties +} + +func (c *CreateEmbedJobResponse) UnmarshalJSON(data []byte) error { + type unmarshaler CreateEmbedJobResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *c = CreateEmbedJobResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *c) + if err != nil { + return err + } + c.extraProperties = extraProperties + + c._rawJSON = json.RawMessage(data) + return nil +} + +func (c *CreateEmbedJobResponse) String() string { + if len(c._rawJSON) > 0 { + if value, err := core.StringifyJSON(c._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(c); err == nil { + return value + } + return fmt.Sprintf("%#v", c) +} + +type Dataset struct { + // The dataset ID + Id string `json:"id" url:"id"` + // The name of the dataset + Name string `json:"name" url:"name"` + // The creation date + CreatedAt time.Time `json:"created_at" url:"created_at"` + // The last update date + UpdatedAt time.Time `json:"updated_at" url:"updated_at"` + DatasetType DatasetType `json:"dataset_type" url:"dataset_type"` + ValidationStatus DatasetValidationStatus `json:"validation_status" url:"validation_status"` + // Errors found during validation + ValidationError *string `json:"validation_error,omitempty" url:"validation_error,omitempty"` + // the avro schema of the dataset + Schema *string `json:"schema,omitempty" url:"schema,omitempty"` + RequiredFields []string `json:"required_fields,omitempty" url:"required_fields,omitempty"` + PreserveFields []string `json:"preserve_fields,omitempty" url:"preserve_fields,omitempty"` + // the underlying files that make up the dataset + DatasetParts []*DatasetPart `json:"dataset_parts,omitempty" url:"dataset_parts,omitempty"` + // warnings found during validation + ValidationWarnings []string `json:"validation_warnings,omitempty" url:"validation_warnings,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (d *Dataset) GetExtraProperties() map[string]interface{} { + return d.extraProperties +} + +func (d *Dataset) UnmarshalJSON(data []byte) error { + type embed Dataset + var unmarshaler = struct { + embed + CreatedAt *core.DateTime `json:"created_at"` + UpdatedAt *core.DateTime `json:"updated_at"` + }{ + embed: embed(*d), + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + *d = Dataset(unmarshaler.embed) + d.CreatedAt = unmarshaler.CreatedAt.Time() + d.UpdatedAt = unmarshaler.UpdatedAt.Time() + + extraProperties, err := core.ExtractExtraProperties(data, *d) + if err != nil { + return err + } + d.extraProperties = extraProperties + + d._rawJSON = json.RawMessage(data) + return nil +} + +func (d *Dataset) MarshalJSON() ([]byte, error) { + type embed Dataset + var marshaler = struct { + embed + CreatedAt *core.DateTime `json:"created_at"` + UpdatedAt *core.DateTime `json:"updated_at"` + }{ + embed: embed(*d), + CreatedAt: core.NewDateTime(d.CreatedAt), + UpdatedAt: core.NewDateTime(d.UpdatedAt), + } + return json.Marshal(marshaler) +} + +func (d *Dataset) String() string { + if len(d._rawJSON) > 0 { + if value, err := core.StringifyJSON(d._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(d); err == nil { + return value + } + return fmt.Sprintf("%#v", d) +} + +type DatasetPart struct { + // The dataset part ID + Id string `json:"id" url:"id"` + // The name of the dataset part + Name string `json:"name" url:"name"` + // The download url of the file + Url *string `json:"url,omitempty" url:"url,omitempty"` + // The index of the file + Index *int `json:"index,omitempty" url:"index,omitempty"` + // The size of the file in bytes + SizeBytes *int `json:"size_bytes,omitempty" url:"size_bytes,omitempty"` + // The number of rows in the file + NumRows *int `json:"num_rows,omitempty" url:"num_rows,omitempty"` + // The download url of the original file + OriginalUrl *string `json:"original_url,omitempty" url:"original_url,omitempty"` + // The first few rows of the parsed file + Samples []string `json:"samples,omitempty" url:"samples,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (d *DatasetPart) GetExtraProperties() map[string]interface{} { + return d.extraProperties +} + +func (d *DatasetPart) UnmarshalJSON(data []byte) error { + type unmarshaler DatasetPart + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *d = DatasetPart(value) + + extraProperties, err := core.ExtractExtraProperties(data, *d) + if err != nil { + return err + } + d.extraProperties = extraProperties + + d._rawJSON = json.RawMessage(data) + return nil +} + +func (d *DatasetPart) String() string { + if len(d._rawJSON) > 0 { + if value, err := core.StringifyJSON(d._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(d); err == nil { + return value + } + return fmt.Sprintf("%#v", d) +} + +// The type of the dataset +type DatasetType string + +const ( + DatasetTypeEmbedInput DatasetType = "embed-input" + DatasetTypeEmbedResult DatasetType = "embed-result" + DatasetTypeClusterResult DatasetType = "cluster-result" + DatasetTypeClusterOutliers DatasetType = "cluster-outliers" + DatasetTypeRerankerFinetuneInput DatasetType = "reranker-finetune-input" + DatasetTypeSingleLabelClassificationFinetuneInput DatasetType = "single-label-classification-finetune-input" + DatasetTypeChatFinetuneInput DatasetType = "chat-finetune-input" + DatasetTypeMultiLabelClassificationFinetuneInput DatasetType = "multi-label-classification-finetune-input" +) + +func NewDatasetTypeFromString(s string) (DatasetType, error) { + switch s { + case "embed-input": + return DatasetTypeEmbedInput, nil + case "embed-result": + return DatasetTypeEmbedResult, nil + case "cluster-result": + return DatasetTypeClusterResult, nil + case "cluster-outliers": + return DatasetTypeClusterOutliers, nil + case "reranker-finetune-input": + return DatasetTypeRerankerFinetuneInput, nil + case "single-label-classification-finetune-input": + return DatasetTypeSingleLabelClassificationFinetuneInput, nil + case "chat-finetune-input": + return DatasetTypeChatFinetuneInput, nil + case "multi-label-classification-finetune-input": + return DatasetTypeMultiLabelClassificationFinetuneInput, nil + } + var t DatasetType + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (d DatasetType) Ptr() *DatasetType { + return &d +} + +// The validation status of the dataset +type DatasetValidationStatus string + +const ( + DatasetValidationStatusUnknown DatasetValidationStatus = "unknown" + DatasetValidationStatusQueued DatasetValidationStatus = "queued" + DatasetValidationStatusProcessing DatasetValidationStatus = "processing" + DatasetValidationStatusFailed DatasetValidationStatus = "failed" + DatasetValidationStatusValidated DatasetValidationStatus = "validated" + DatasetValidationStatusSkipped DatasetValidationStatus = "skipped" +) + +func NewDatasetValidationStatusFromString(s string) (DatasetValidationStatus, error) { + switch s { + case "unknown": + return DatasetValidationStatusUnknown, nil + case "queued": + return DatasetValidationStatusQueued, nil + case "processing": + return DatasetValidationStatusProcessing, nil + case "failed": + return DatasetValidationStatusFailed, nil + case "validated": + return DatasetValidationStatusValidated, nil + case "skipped": + return DatasetValidationStatusSkipped, nil + } + var t DatasetValidationStatus + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (d DatasetValidationStatus) Ptr() *DatasetValidationStatus { + return &d +} + +type DeleteConnectorResponse = map[string]interface{} + +type DetokenizeResponse struct { + // A string representing the list of tokens. + Text string `json:"text" url:"text"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (d *DetokenizeResponse) GetExtraProperties() map[string]interface{} { + return d.extraProperties +} + +func (d *DetokenizeResponse) UnmarshalJSON(data []byte) error { + type unmarshaler DetokenizeResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *d = DetokenizeResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *d) + if err != nil { + return err + } + d.extraProperties = extraProperties + + d._rawJSON = json.RawMessage(data) + return nil +} + +func (d *DetokenizeResponse) String() string { + if len(d._rawJSON) > 0 { + if value, err := core.StringifyJSON(d._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(d); err == nil { + return value + } + return fmt.Sprintf("%#v", d) +} + +// Relevant information that could be used by the model to generate a more accurate reply. +// The content of each document are generally short (should be under 300 words). Metadata should be used to provide additional information, both the key name and the value will be +// passed to the model. +type Document struct { + // A relevant documents that the model can cite to generate a more accurate reply. Each document is a string-string dictionary. + Data map[string]string `json:"data,omitempty" url:"data,omitempty"` + // Unique identifier for this document which will be referenced in citations. If not provided an ID will be automatically generated. + Id *string `json:"id,omitempty" url:"id,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (d *Document) GetExtraProperties() map[string]interface{} { + return d.extraProperties +} + +func (d *Document) UnmarshalJSON(data []byte) error { + type unmarshaler Document + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *d = Document(value) + + extraProperties, err := core.ExtractExtraProperties(data, *d) + if err != nil { + return err + } + d.extraProperties = extraProperties + + d._rawJSON = json.RawMessage(data) + return nil +} + +func (d *Document) String() string { + if len(d._rawJSON) > 0 { + if value, err := core.StringifyJSON(d._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(d); err == nil { + return value + } + return fmt.Sprintf("%#v", d) +} + +// Document content. +type DocumentContent struct { + Document *Document `json:"document,omitempty" url:"document,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (d *DocumentContent) GetExtraProperties() map[string]interface{} { + return d.extraProperties +} + +func (d *DocumentContent) UnmarshalJSON(data []byte) error { + type unmarshaler DocumentContent + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *d = DocumentContent(value) + + extraProperties, err := core.ExtractExtraProperties(data, *d) + if err != nil { + return err + } + d.extraProperties = extraProperties + + d._rawJSON = json.RawMessage(data) + return nil +} + +func (d *DocumentContent) String() string { + if len(d._rawJSON) > 0 { + if value, err := core.StringifyJSON(d._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(d); err == nil { + return value + } + return fmt.Sprintf("%#v", d) +} + +// A document source object containing the unique identifier of the document and the document itself. +type DocumentSource struct { + // The unique identifier of the document + Id *string `json:"id,omitempty" url:"id,omitempty"` + Document map[string]interface{} `json:"document,omitempty" url:"document,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (d *DocumentSource) GetExtraProperties() map[string]interface{} { + return d.extraProperties +} + +func (d *DocumentSource) UnmarshalJSON(data []byte) error { + type unmarshaler DocumentSource + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *d = DocumentSource(value) + + extraProperties, err := core.ExtractExtraProperties(data, *d) + if err != nil { + return err + } + d.extraProperties = extraProperties + + d._rawJSON = json.RawMessage(data) + return nil +} + +func (d *DocumentSource) String() string { + if len(d._rawJSON) > 0 { + if value, err := core.StringifyJSON(d._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(d); err == nil { + return value + } + return fmt.Sprintf("%#v", d) +} + +type EmbedByTypeResponse struct { + Id string `json:"id" url:"id"` + // An object with different embedding types. The length of each embedding type array will be the same as the length of the original `texts` array. + Embeddings *EmbedByTypeResponseEmbeddings `json:"embeddings,omitempty" url:"embeddings,omitempty"` + // The text entries for which embeddings were returned. + Texts []string `json:"texts,omitempty" url:"texts,omitempty"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (e *EmbedByTypeResponse) GetExtraProperties() map[string]interface{} { + return e.extraProperties +} + +func (e *EmbedByTypeResponse) UnmarshalJSON(data []byte) error { + type unmarshaler EmbedByTypeResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *e = EmbedByTypeResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *e) + if err != nil { + return err + } + e.extraProperties = extraProperties + + e._rawJSON = json.RawMessage(data) + return nil +} + +func (e *EmbedByTypeResponse) String() string { + if len(e._rawJSON) > 0 { + if value, err := core.StringifyJSON(e._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(e); err == nil { + return value + } + return fmt.Sprintf("%#v", e) +} + +// An object with different embedding types. The length of each embedding type array will be the same as the length of the original `texts` array. +type EmbedByTypeResponseEmbeddings struct { + // An array of float embeddings. + Float [][]float64 `json:"float,omitempty" url:"float,omitempty"` + // An array of signed int8 embeddings. Each value is between -128 and 127. + Int8 [][]int `json:"int8,omitempty" url:"int8,omitempty"` + // An array of unsigned int8 embeddings. Each value is between 0 and 255. + Uint8 [][]int `json:"uint8,omitempty" url:"uint8,omitempty"` + // An array of packed signed binary embeddings. The length of each binary embedding is 1/8 the length of the float embeddings of the provided model. Each value is between -128 and 127. + Binary [][]int `json:"binary,omitempty" url:"binary,omitempty"` + // An array of packed unsigned binary embeddings. The length of each binary embedding is 1/8 the length of the float embeddings of the provided model. Each value is between 0 and 255. + Ubinary [][]int `json:"ubinary,omitempty" url:"ubinary,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (e *EmbedByTypeResponseEmbeddings) GetExtraProperties() map[string]interface{} { + return e.extraProperties +} + +func (e *EmbedByTypeResponseEmbeddings) UnmarshalJSON(data []byte) error { + type unmarshaler EmbedByTypeResponseEmbeddings + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *e = EmbedByTypeResponseEmbeddings(value) + + extraProperties, err := core.ExtractExtraProperties(data, *e) + if err != nil { + return err + } + e.extraProperties = extraProperties + + e._rawJSON = json.RawMessage(data) + return nil +} + +func (e *EmbedByTypeResponseEmbeddings) String() string { + if len(e._rawJSON) > 0 { + if value, err := core.StringifyJSON(e._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(e); err == nil { + return value + } + return fmt.Sprintf("%#v", e) +} + +type EmbedFloatsResponse struct { + Id string `json:"id" url:"id"` + // An array of embeddings, where each embedding is an array of floats. The length of the `embeddings` array will be the same as the length of the original `texts` array. + Embeddings [][]float64 `json:"embeddings,omitempty" url:"embeddings,omitempty"` + // The text entries for which embeddings were returned. + Texts []string `json:"texts,omitempty" url:"texts,omitempty"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (e *EmbedFloatsResponse) GetExtraProperties() map[string]interface{} { + return e.extraProperties +} + +func (e *EmbedFloatsResponse) UnmarshalJSON(data []byte) error { + type unmarshaler EmbedFloatsResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *e = EmbedFloatsResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *e) + if err != nil { + return err + } + e.extraProperties = extraProperties + + e._rawJSON = json.RawMessage(data) + return nil +} + +func (e *EmbedFloatsResponse) String() string { + if len(e._rawJSON) > 0 { + if value, err := core.StringifyJSON(e._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(e); err == nil { + return value + } + return fmt.Sprintf("%#v", e) +} + +// Specifies the type of input passed to the model. Required for embedding models v3 and higher. +// +// - `"search_document"`: Used for embeddings stored in a vector database for search use-cases. +// - `"search_query"`: Used for embeddings of search queries run against a vector DB to find relevant documents. +// - `"classification"`: Used for embeddings passed through a text classifier. +// - `"clustering"`: Used for the embeddings run through a clustering algorithm. +// - `"image"`: Used for embeddings with image input. +type EmbedInputType string + +const ( + EmbedInputTypeSearchDocument EmbedInputType = "search_document" + EmbedInputTypeSearchQuery EmbedInputType = "search_query" + EmbedInputTypeClassification EmbedInputType = "classification" + EmbedInputTypeClustering EmbedInputType = "clustering" + EmbedInputTypeImage EmbedInputType = "image" +) + +func NewEmbedInputTypeFromString(s string) (EmbedInputType, error) { + switch s { + case "search_document": + return EmbedInputTypeSearchDocument, nil + case "search_query": + return EmbedInputTypeSearchQuery, nil + case "classification": + return EmbedInputTypeClassification, nil + case "clustering": + return EmbedInputTypeClustering, nil + case "image": + return EmbedInputTypeImage, nil + } + var t EmbedInputType + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (e EmbedInputType) Ptr() *EmbedInputType { + return &e +} + +type EmbedJob struct { + // ID of the embed job + JobId string `json:"job_id" url:"job_id"` + // The name of the embed job + Name *string `json:"name,omitempty" url:"name,omitempty"` + // The status of the embed job + Status EmbedJobStatus `json:"status" url:"status"` + // The creation date of the embed job + CreatedAt time.Time `json:"created_at" url:"created_at"` + // ID of the input dataset + InputDatasetId string `json:"input_dataset_id" url:"input_dataset_id"` + // ID of the resulting output dataset + OutputDatasetId *string `json:"output_dataset_id,omitempty" url:"output_dataset_id,omitempty"` + // ID of the model used to embed + Model string `json:"model" url:"model"` + // The truncation option used + Truncate EmbedJobTruncate `json:"truncate" url:"truncate"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (e *EmbedJob) GetExtraProperties() map[string]interface{} { + return e.extraProperties +} + +func (e *EmbedJob) UnmarshalJSON(data []byte) error { + type embed EmbedJob + var unmarshaler = struct { + embed + CreatedAt *core.DateTime `json:"created_at"` + }{ + embed: embed(*e), + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + *e = EmbedJob(unmarshaler.embed) + e.CreatedAt = unmarshaler.CreatedAt.Time() + + extraProperties, err := core.ExtractExtraProperties(data, *e) + if err != nil { + return err + } + e.extraProperties = extraProperties + + e._rawJSON = json.RawMessage(data) + return nil +} + +func (e *EmbedJob) MarshalJSON() ([]byte, error) { + type embed EmbedJob + var marshaler = struct { + embed + CreatedAt *core.DateTime `json:"created_at"` + }{ + embed: embed(*e), + CreatedAt: core.NewDateTime(e.CreatedAt), + } + return json.Marshal(marshaler) +} + +func (e *EmbedJob) String() string { + if len(e._rawJSON) > 0 { + if value, err := core.StringifyJSON(e._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(e); err == nil { + return value + } + return fmt.Sprintf("%#v", e) +} + +// The status of the embed job +type EmbedJobStatus string + +const ( + EmbedJobStatusProcessing EmbedJobStatus = "processing" + EmbedJobStatusComplete EmbedJobStatus = "complete" + EmbedJobStatusCancelling EmbedJobStatus = "cancelling" + EmbedJobStatusCancelled EmbedJobStatus = "cancelled" + EmbedJobStatusFailed EmbedJobStatus = "failed" +) + +func NewEmbedJobStatusFromString(s string) (EmbedJobStatus, error) { + switch s { + case "processing": + return EmbedJobStatusProcessing, nil + case "complete": + return EmbedJobStatusComplete, nil + case "cancelling": + return EmbedJobStatusCancelling, nil + case "cancelled": + return EmbedJobStatusCancelled, nil + case "failed": + return EmbedJobStatusFailed, nil + } + var t EmbedJobStatus + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (e EmbedJobStatus) Ptr() *EmbedJobStatus { + return &e +} + +// The truncation option used +type EmbedJobTruncate string + +const ( + EmbedJobTruncateStart EmbedJobTruncate = "START" + EmbedJobTruncateEnd EmbedJobTruncate = "END" +) + +func NewEmbedJobTruncateFromString(s string) (EmbedJobTruncate, error) { + switch s { + case "START": + return EmbedJobTruncateStart, nil + case "END": + return EmbedJobTruncateEnd, nil + } + var t EmbedJobTruncate + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (e EmbedJobTruncate) Ptr() *EmbedJobTruncate { + return &e +} + +// One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length. +// +// Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model. +// +// If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned. +type EmbedRequestTruncate string + +const ( + EmbedRequestTruncateNone EmbedRequestTruncate = "NONE" + EmbedRequestTruncateStart EmbedRequestTruncate = "START" + EmbedRequestTruncateEnd EmbedRequestTruncate = "END" +) + +func NewEmbedRequestTruncateFromString(s string) (EmbedRequestTruncate, error) { + switch s { + case "NONE": + return EmbedRequestTruncateNone, nil + case "START": + return EmbedRequestTruncateStart, nil + case "END": + return EmbedRequestTruncateEnd, nil + } + var t EmbedRequestTruncate + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (e EmbedRequestTruncate) Ptr() *EmbedRequestTruncate { + return &e +} + +type EmbedResponse struct { + ResponseType string + EmbeddingsFloats *EmbedFloatsResponse + EmbeddingsByType *EmbedByTypeResponse +} + +func (e *EmbedResponse) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + ResponseType string `json:"response_type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + e.ResponseType = unmarshaler.ResponseType + if unmarshaler.ResponseType == "" { + return fmt.Errorf("%T did not include discriminant response_type", e) + } + switch unmarshaler.ResponseType { + case "embeddings_floats": + value := new(EmbedFloatsResponse) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + e.EmbeddingsFloats = value + case "embeddings_by_type": + value := new(EmbedByTypeResponse) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + e.EmbeddingsByType = value + } + return nil +} + +func (e EmbedResponse) MarshalJSON() ([]byte, error) { + if e.EmbeddingsFloats != nil { + return core.MarshalJSONWithExtraProperty(e.EmbeddingsFloats, "response_type", "embeddings_floats") + } + if e.EmbeddingsByType != nil { + return core.MarshalJSONWithExtraProperty(e.EmbeddingsByType, "response_type", "embeddings_by_type") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", e) +} + +type EmbedResponseVisitor interface { + VisitEmbeddingsFloats(*EmbedFloatsResponse) error + VisitEmbeddingsByType(*EmbedByTypeResponse) error +} + +func (e *EmbedResponse) Accept(visitor EmbedResponseVisitor) error { + if e.EmbeddingsFloats != nil { + return visitor.VisitEmbeddingsFloats(e.EmbeddingsFloats) + } + if e.EmbeddingsByType != nil { + return visitor.VisitEmbeddingsByType(e.EmbeddingsByType) + } + return fmt.Errorf("type %T does not define a non-empty union type", e) +} + +type EmbeddingType string + +const ( + EmbeddingTypeFloat EmbeddingType = "float" + EmbeddingTypeInt8 EmbeddingType = "int8" + EmbeddingTypeUint8 EmbeddingType = "uint8" + EmbeddingTypeBinary EmbeddingType = "binary" + EmbeddingTypeUbinary EmbeddingType = "ubinary" +) + +func NewEmbeddingTypeFromString(s string) (EmbeddingType, error) { + switch s { + case "float": + return EmbeddingTypeFloat, nil + case "int8": + return EmbeddingTypeInt8, nil + case "uint8": + return EmbeddingTypeUint8, nil + case "binary": + return EmbeddingTypeBinary, nil + case "ubinary": + return EmbeddingTypeUbinary, nil + } + var t EmbeddingType + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (e EmbeddingType) Ptr() *EmbeddingType { + return &e +} + +type FinetuneDatasetMetrics struct { + // The number of tokens of valid examples that can be used for training. + TrainableTokenCount *int64 `json:"trainable_token_count,omitempty" url:"trainable_token_count,omitempty"` + // The overall number of examples. + TotalExamples *int64 `json:"total_examples,omitempty" url:"total_examples,omitempty"` + // The number of training examples. + TrainExamples *int64 `json:"train_examples,omitempty" url:"train_examples,omitempty"` + // The size in bytes of all training examples. + TrainSizeBytes *int64 `json:"train_size_bytes,omitempty" url:"train_size_bytes,omitempty"` + // Number of evaluation examples. + EvalExamples *int64 `json:"eval_examples,omitempty" url:"eval_examples,omitempty"` + // The size in bytes of all eval examples. + EvalSizeBytes *int64 `json:"eval_size_bytes,omitempty" url:"eval_size_bytes,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (f *FinetuneDatasetMetrics) GetExtraProperties() map[string]interface{} { + return f.extraProperties +} + +func (f *FinetuneDatasetMetrics) UnmarshalJSON(data []byte) error { + type unmarshaler FinetuneDatasetMetrics + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *f = FinetuneDatasetMetrics(value) + + extraProperties, err := core.ExtractExtraProperties(data, *f) + if err != nil { + return err + } + f.extraProperties = extraProperties + + f._rawJSON = json.RawMessage(data) + return nil +} + +func (f *FinetuneDatasetMetrics) String() string { + if len(f._rawJSON) > 0 { + if value, err := core.StringifyJSON(f._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(f); err == nil { + return value + } + return fmt.Sprintf("%#v", f) +} + +type FinishReason string + +const ( + FinishReasonComplete FinishReason = "COMPLETE" + FinishReasonStopSequence FinishReason = "STOP_SEQUENCE" + FinishReasonError FinishReason = "ERROR" + FinishReasonErrorToxic FinishReason = "ERROR_TOXIC" + FinishReasonErrorLimit FinishReason = "ERROR_LIMIT" + FinishReasonUserCancel FinishReason = "USER_CANCEL" + FinishReasonMaxTokens FinishReason = "MAX_TOKENS" +) + +func NewFinishReasonFromString(s string) (FinishReason, error) { + switch s { + case "COMPLETE": + return FinishReasonComplete, nil + case "STOP_SEQUENCE": + return FinishReasonStopSequence, nil + case "ERROR": + return FinishReasonError, nil + case "ERROR_TOXIC": + return FinishReasonErrorToxic, nil + case "ERROR_LIMIT": + return FinishReasonErrorLimit, nil + case "USER_CANCEL": + return FinishReasonUserCancel, nil + case "MAX_TOKENS": + return FinishReasonMaxTokens, nil + } + var t FinishReason + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (f FinishReason) Ptr() *FinishReason { + return &f +} + +type GatewayTimeoutErrorBody struct { + Data *string `json:"data,omitempty" url:"data,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (g *GatewayTimeoutErrorBody) GetExtraProperties() map[string]interface{} { + return g.extraProperties +} + +func (g *GatewayTimeoutErrorBody) UnmarshalJSON(data []byte) error { + type unmarshaler GatewayTimeoutErrorBody + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *g = GatewayTimeoutErrorBody(value) + + extraProperties, err := core.ExtractExtraProperties(data, *g) + if err != nil { + return err + } + g.extraProperties = extraProperties + + g._rawJSON = json.RawMessage(data) + return nil +} + +func (g *GatewayTimeoutErrorBody) String() string { + if len(g._rawJSON) > 0 { + if value, err := core.StringifyJSON(g._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(g); err == nil { + return value + } + return fmt.Sprintf("%#v", g) +} + +// One of `GENERATION|ALL|NONE` to specify how and if the token likelihoods are returned with the response. Defaults to `NONE`. +// +// If `GENERATION` is selected, the token likelihoods will only be provided for generated text. +// +// If `ALL` is selected, the token likelihoods will be provided both for the prompt and the generated text. +type GenerateRequestReturnLikelihoods string + +const ( + GenerateRequestReturnLikelihoodsGeneration GenerateRequestReturnLikelihoods = "GENERATION" + GenerateRequestReturnLikelihoodsAll GenerateRequestReturnLikelihoods = "ALL" + GenerateRequestReturnLikelihoodsNone GenerateRequestReturnLikelihoods = "NONE" +) + +func NewGenerateRequestReturnLikelihoodsFromString(s string) (GenerateRequestReturnLikelihoods, error) { + switch s { + case "GENERATION": + return GenerateRequestReturnLikelihoodsGeneration, nil + case "ALL": + return GenerateRequestReturnLikelihoodsAll, nil + case "NONE": + return GenerateRequestReturnLikelihoodsNone, nil + } + var t GenerateRequestReturnLikelihoods + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (g GenerateRequestReturnLikelihoods) Ptr() *GenerateRequestReturnLikelihoods { + return &g +} + +// One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length. +// +// Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model. +// +// If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned. +type GenerateRequestTruncate string + +const ( + GenerateRequestTruncateNone GenerateRequestTruncate = "NONE" + GenerateRequestTruncateStart GenerateRequestTruncate = "START" + GenerateRequestTruncateEnd GenerateRequestTruncate = "END" +) + +func NewGenerateRequestTruncateFromString(s string) (GenerateRequestTruncate, error) { + switch s { + case "NONE": + return GenerateRequestTruncateNone, nil + case "START": + return GenerateRequestTruncateStart, nil + case "END": + return GenerateRequestTruncateEnd, nil + } + var t GenerateRequestTruncate + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (g GenerateRequestTruncate) Ptr() *GenerateRequestTruncate { + return &g +} + +type GenerateStreamEnd struct { + IsFinished bool `json:"is_finished" url:"is_finished"` + FinishReason *FinishReason `json:"finish_reason,omitempty" url:"finish_reason,omitempty"` + Response *GenerateStreamEndResponse `json:"response,omitempty" url:"response,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (g *GenerateStreamEnd) GetExtraProperties() map[string]interface{} { + return g.extraProperties +} + +func (g *GenerateStreamEnd) UnmarshalJSON(data []byte) error { + type unmarshaler GenerateStreamEnd + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *g = GenerateStreamEnd(value) + + extraProperties, err := core.ExtractExtraProperties(data, *g) + if err != nil { + return err + } + g.extraProperties = extraProperties + + g._rawJSON = json.RawMessage(data) + return nil +} + +func (g *GenerateStreamEnd) String() string { + if len(g._rawJSON) > 0 { + if value, err := core.StringifyJSON(g._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(g); err == nil { + return value + } + return fmt.Sprintf("%#v", g) +} + +type GenerateStreamEndResponse struct { + Id string `json:"id" url:"id"` + Prompt *string `json:"prompt,omitempty" url:"prompt,omitempty"` + Generations []*SingleGenerationInStream `json:"generations,omitempty" url:"generations,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (g *GenerateStreamEndResponse) GetExtraProperties() map[string]interface{} { + return g.extraProperties +} + +func (g *GenerateStreamEndResponse) UnmarshalJSON(data []byte) error { + type unmarshaler GenerateStreamEndResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *g = GenerateStreamEndResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *g) + if err != nil { + return err + } + g.extraProperties = extraProperties + + g._rawJSON = json.RawMessage(data) + return nil +} + +func (g *GenerateStreamEndResponse) String() string { + if len(g._rawJSON) > 0 { + if value, err := core.StringifyJSON(g._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(g); err == nil { + return value + } + return fmt.Sprintf("%#v", g) +} + +type GenerateStreamError struct { + // Refers to the nth generation. Only present when `num_generations` is greater than zero. + Index *int `json:"index,omitempty" url:"index,omitempty"` + IsFinished bool `json:"is_finished" url:"is_finished"` + FinishReason FinishReason `json:"finish_reason" url:"finish_reason"` + // Error message + Err string `json:"err" url:"err"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (g *GenerateStreamError) GetExtraProperties() map[string]interface{} { + return g.extraProperties +} + +func (g *GenerateStreamError) UnmarshalJSON(data []byte) error { + type unmarshaler GenerateStreamError + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *g = GenerateStreamError(value) + + extraProperties, err := core.ExtractExtraProperties(data, *g) + if err != nil { + return err + } + g.extraProperties = extraProperties + + g._rawJSON = json.RawMessage(data) + return nil +} + +func (g *GenerateStreamError) String() string { + if len(g._rawJSON) > 0 { + if value, err := core.StringifyJSON(g._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(g); err == nil { + return value + } + return fmt.Sprintf("%#v", g) +} + +type GenerateStreamEvent struct { + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (g *GenerateStreamEvent) GetExtraProperties() map[string]interface{} { + return g.extraProperties +} + +func (g *GenerateStreamEvent) UnmarshalJSON(data []byte) error { + type unmarshaler GenerateStreamEvent + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *g = GenerateStreamEvent(value) + + extraProperties, err := core.ExtractExtraProperties(data, *g) + if err != nil { + return err + } + g.extraProperties = extraProperties + + g._rawJSON = json.RawMessage(data) + return nil +} + +func (g *GenerateStreamEvent) String() string { + if len(g._rawJSON) > 0 { + if value, err := core.StringifyJSON(g._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(g); err == nil { + return value + } + return fmt.Sprintf("%#v", g) +} + +// One of `GENERATION|ALL|NONE` to specify how and if the token likelihoods are returned with the response. Defaults to `NONE`. +// +// If `GENERATION` is selected, the token likelihoods will only be provided for generated text. +// +// If `ALL` is selected, the token likelihoods will be provided both for the prompt and the generated text. +type GenerateStreamRequestReturnLikelihoods string + +const ( + GenerateStreamRequestReturnLikelihoodsGeneration GenerateStreamRequestReturnLikelihoods = "GENERATION" + GenerateStreamRequestReturnLikelihoodsAll GenerateStreamRequestReturnLikelihoods = "ALL" + GenerateStreamRequestReturnLikelihoodsNone GenerateStreamRequestReturnLikelihoods = "NONE" +) + +func NewGenerateStreamRequestReturnLikelihoodsFromString(s string) (GenerateStreamRequestReturnLikelihoods, error) { + switch s { + case "GENERATION": + return GenerateStreamRequestReturnLikelihoodsGeneration, nil + case "ALL": + return GenerateStreamRequestReturnLikelihoodsAll, nil + case "NONE": + return GenerateStreamRequestReturnLikelihoodsNone, nil + } + var t GenerateStreamRequestReturnLikelihoods + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (g GenerateStreamRequestReturnLikelihoods) Ptr() *GenerateStreamRequestReturnLikelihoods { + return &g +} + +// One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length. +// +// Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model. +// +// If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned. +type GenerateStreamRequestTruncate string + +const ( + GenerateStreamRequestTruncateNone GenerateStreamRequestTruncate = "NONE" + GenerateStreamRequestTruncateStart GenerateStreamRequestTruncate = "START" + GenerateStreamRequestTruncateEnd GenerateStreamRequestTruncate = "END" +) + +func NewGenerateStreamRequestTruncateFromString(s string) (GenerateStreamRequestTruncate, error) { + switch s { + case "NONE": + return GenerateStreamRequestTruncateNone, nil + case "START": + return GenerateStreamRequestTruncateStart, nil + case "END": + return GenerateStreamRequestTruncateEnd, nil + } + var t GenerateStreamRequestTruncate + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (g GenerateStreamRequestTruncate) Ptr() *GenerateStreamRequestTruncate { + return &g +} + +type GenerateStreamText struct { + // A segment of text of the generation. + Text string `json:"text" url:"text"` + // Refers to the nth generation. Only present when `num_generations` is greater than zero, and only when text responses are being streamed. + Index *int `json:"index,omitempty" url:"index,omitempty"` + IsFinished bool `json:"is_finished" url:"is_finished"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (g *GenerateStreamText) GetExtraProperties() map[string]interface{} { + return g.extraProperties +} + +func (g *GenerateStreamText) UnmarshalJSON(data []byte) error { + type unmarshaler GenerateStreamText + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *g = GenerateStreamText(value) + + extraProperties, err := core.ExtractExtraProperties(data, *g) + if err != nil { + return err + } + g.extraProperties = extraProperties + + g._rawJSON = json.RawMessage(data) + return nil +} + +func (g *GenerateStreamText) String() string { + if len(g._rawJSON) > 0 { + if value, err := core.StringifyJSON(g._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(g); err == nil { + return value + } + return fmt.Sprintf("%#v", g) +} + +// Response in content type stream when `stream` is `true` in the request parameters. Generation tokens are streamed with the GenerationStream response. The final response is of type GenerationFinalResponse. +type GenerateStreamedResponse struct { + EventType string + TextGeneration *GenerateStreamText + StreamEnd *GenerateStreamEnd + StreamError *GenerateStreamError +} + +func (g *GenerateStreamedResponse) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + EventType string `json:"event_type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + g.EventType = unmarshaler.EventType + if unmarshaler.EventType == "" { + return fmt.Errorf("%T did not include discriminant event_type", g) + } + switch unmarshaler.EventType { + case "text-generation": + value := new(GenerateStreamText) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + g.TextGeneration = value + case "stream-end": + value := new(GenerateStreamEnd) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + g.StreamEnd = value + case "stream-error": + value := new(GenerateStreamError) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + g.StreamError = value + } + return nil +} + +func (g GenerateStreamedResponse) MarshalJSON() ([]byte, error) { + if g.TextGeneration != nil { + return core.MarshalJSONWithExtraProperty(g.TextGeneration, "event_type", "text-generation") + } + if g.StreamEnd != nil { + return core.MarshalJSONWithExtraProperty(g.StreamEnd, "event_type", "stream-end") + } + if g.StreamError != nil { + return core.MarshalJSONWithExtraProperty(g.StreamError, "event_type", "stream-error") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", g) +} + +type GenerateStreamedResponseVisitor interface { + VisitTextGeneration(*GenerateStreamText) error + VisitStreamEnd(*GenerateStreamEnd) error + VisitStreamError(*GenerateStreamError) error +} + +func (g *GenerateStreamedResponse) Accept(visitor GenerateStreamedResponseVisitor) error { + if g.TextGeneration != nil { + return visitor.VisitTextGeneration(g.TextGeneration) + } + if g.StreamEnd != nil { + return visitor.VisitStreamEnd(g.StreamEnd) + } + if g.StreamError != nil { + return visitor.VisitStreamError(g.StreamError) + } + return fmt.Errorf("type %T does not define a non-empty union type", g) +} + +type Generation struct { + Id string `json:"id" url:"id"` + // Prompt used for generations. + Prompt *string `json:"prompt,omitempty" url:"prompt,omitempty"` + // List of generated results + Generations []*SingleGeneration `json:"generations,omitempty" url:"generations,omitempty"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (g *Generation) GetExtraProperties() map[string]interface{} { + return g.extraProperties +} + +func (g *Generation) UnmarshalJSON(data []byte) error { + type unmarshaler Generation + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *g = Generation(value) + + extraProperties, err := core.ExtractExtraProperties(data, *g) + if err != nil { + return err + } + g.extraProperties = extraProperties + + g._rawJSON = json.RawMessage(data) + return nil +} + +func (g *Generation) String() string { + if len(g._rawJSON) > 0 { + if value, err := core.StringifyJSON(g._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(g); err == nil { + return value + } + return fmt.Sprintf("%#v", g) +} + +type GetConnectorResponse struct { + Connector *Connector `json:"connector,omitempty" url:"connector,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (g *GetConnectorResponse) GetExtraProperties() map[string]interface{} { + return g.extraProperties +} + +func (g *GetConnectorResponse) UnmarshalJSON(data []byte) error { + type unmarshaler GetConnectorResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *g = GetConnectorResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *g) + if err != nil { + return err + } + g.extraProperties = extraProperties + + g._rawJSON = json.RawMessage(data) + return nil +} + +func (g *GetConnectorResponse) String() string { + if len(g._rawJSON) > 0 { + if value, err := core.StringifyJSON(g._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(g); err == nil { + return value + } + return fmt.Sprintf("%#v", g) +} + +// Contains information about the model and which API endpoints it can be used with. +type GetModelResponse struct { + // Specify this name in the `model` parameter of API requests to use your chosen model. + Name *string `json:"name,omitempty" url:"name,omitempty"` + // The API endpoints that the model is compatible with. + Endpoints []CompatibleEndpoint `json:"endpoints,omitempty" url:"endpoints,omitempty"` + // Whether the model has been fine-tuned or not. + Finetuned *bool `json:"finetuned,omitempty" url:"finetuned,omitempty"` + // The maximum number of tokens that the model can process in a single request. Note that not all of these tokens are always available due to special tokens and preambles that Cohere has added by default. + ContextLength *float64 `json:"context_length,omitempty" url:"context_length,omitempty"` + // Public URL to the tokenizer's configuration file. + TokenizerUrl *string `json:"tokenizer_url,omitempty" url:"tokenizer_url,omitempty"` + // The API endpoints that the model is default to. + DefaultEndpoints []CompatibleEndpoint `json:"default_endpoints,omitempty" url:"default_endpoints,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (g *GetModelResponse) GetExtraProperties() map[string]interface{} { + return g.extraProperties +} + +func (g *GetModelResponse) UnmarshalJSON(data []byte) error { + type unmarshaler GetModelResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *g = GetModelResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *g) + if err != nil { + return err + } + g.extraProperties = extraProperties + + g._rawJSON = json.RawMessage(data) + return nil +} + +func (g *GetModelResponse) String() string { + if len(g._rawJSON) > 0 { + if value, err := core.StringifyJSON(g._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(g); err == nil { + return value + } + return fmt.Sprintf("%#v", g) +} + +type JsonResponseFormat struct { + // A JSON schema object that the output will adhere to. There are some restrictions we have on the schema, refer to [our guide](/docs/structured-outputs-json#schema-constraints) for more information. + // Example (required name and age object): + // + // ```json + // + // { + // "type": "object", + // "properties": { + // "name": { "type": "string" }, + // "age": { "type": "integer" } + // }, + // "required": ["name", "age"] + // } + // + // ``` + // + // **Note**: This field must not be specified when the `type` is set to `"text"`. + Schema map[string]interface{} `json:"schema,omitempty" url:"schema,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (j *JsonResponseFormat) GetExtraProperties() map[string]interface{} { + return j.extraProperties +} + +func (j *JsonResponseFormat) UnmarshalJSON(data []byte) error { + type unmarshaler JsonResponseFormat + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *j = JsonResponseFormat(value) + + extraProperties, err := core.ExtractExtraProperties(data, *j) + if err != nil { + return err + } + j.extraProperties = extraProperties + + j._rawJSON = json.RawMessage(data) + return nil +} + +func (j *JsonResponseFormat) String() string { + if len(j._rawJSON) > 0 { + if value, err := core.StringifyJSON(j._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(j); err == nil { + return value + } + return fmt.Sprintf("%#v", j) +} + +type JsonResponseFormatV2 struct { + // A [JSON schema](https://json-schema.org/overview/what-is-jsonschema) object that the output will adhere to. There are some restrictions we have on the schema, refer to [our guide](/docs/structured-outputs-json#schema-constraints) for more information. + // Example (required name and age object): + // + // ```json + // + // { + // "type": "object", + // "properties": { + // "name": { "type": "string" }, + // "age": { "type": "integer" } + // }, + // "required": ["name", "age"] + // } + // + // ``` + // + // **Note**: This field must not be specified when the `type` is set to `"text"`. + JsonSchema map[string]interface{} `json:"json_schema,omitempty" url:"json_schema,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (j *JsonResponseFormatV2) GetExtraProperties() map[string]interface{} { + return j.extraProperties +} + +func (j *JsonResponseFormatV2) UnmarshalJSON(data []byte) error { + type unmarshaler JsonResponseFormatV2 + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *j = JsonResponseFormatV2(value) + + extraProperties, err := core.ExtractExtraProperties(data, *j) + if err != nil { + return err + } + j.extraProperties = extraProperties + + j._rawJSON = json.RawMessage(data) + return nil +} + +func (j *JsonResponseFormatV2) String() string { + if len(j._rawJSON) > 0 { + if value, err := core.StringifyJSON(j._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(j); err == nil { + return value + } + return fmt.Sprintf("%#v", j) +} + +type LabelMetric struct { + // Total number of examples for this label + TotalExamples *int64 `json:"total_examples,omitempty" url:"total_examples,omitempty"` + // value of the label + Label *string `json:"label,omitempty" url:"label,omitempty"` + // samples for this label + Samples []string `json:"samples,omitempty" url:"samples,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (l *LabelMetric) GetExtraProperties() map[string]interface{} { + return l.extraProperties +} + +func (l *LabelMetric) UnmarshalJSON(data []byte) error { + type unmarshaler LabelMetric + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *l = LabelMetric(value) + + extraProperties, err := core.ExtractExtraProperties(data, *l) + if err != nil { + return err + } + l.extraProperties = extraProperties + + l._rawJSON = json.RawMessage(data) + return nil +} + +func (l *LabelMetric) String() string { + if len(l._rawJSON) > 0 { + if value, err := core.StringifyJSON(l._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(l); err == nil { + return value + } + return fmt.Sprintf("%#v", l) +} + +type ListConnectorsResponse struct { + Connectors []*Connector `json:"connectors,omitempty" url:"connectors,omitempty"` + // Total number of connectors. + TotalCount *float64 `json:"total_count,omitempty" url:"total_count,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (l *ListConnectorsResponse) GetExtraProperties() map[string]interface{} { + return l.extraProperties +} + +func (l *ListConnectorsResponse) UnmarshalJSON(data []byte) error { + type unmarshaler ListConnectorsResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *l = ListConnectorsResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *l) + if err != nil { + return err + } + l.extraProperties = extraProperties + + l._rawJSON = json.RawMessage(data) + return nil +} + +func (l *ListConnectorsResponse) String() string { + if len(l._rawJSON) > 0 { + if value, err := core.StringifyJSON(l._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(l); err == nil { + return value + } + return fmt.Sprintf("%#v", l) +} + +type ListEmbedJobResponse struct { + EmbedJobs []*EmbedJob `json:"embed_jobs,omitempty" url:"embed_jobs,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (l *ListEmbedJobResponse) GetExtraProperties() map[string]interface{} { + return l.extraProperties +} + +func (l *ListEmbedJobResponse) UnmarshalJSON(data []byte) error { + type unmarshaler ListEmbedJobResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *l = ListEmbedJobResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *l) + if err != nil { + return err + } + l.extraProperties = extraProperties + + l._rawJSON = json.RawMessage(data) + return nil +} + +func (l *ListEmbedJobResponse) String() string { + if len(l._rawJSON) > 0 { + if value, err := core.StringifyJSON(l._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(l); err == nil { + return value + } + return fmt.Sprintf("%#v", l) +} + +type ListModelsResponse struct { + Models []*GetModelResponse `json:"models,omitempty" url:"models,omitempty"` + // A token to retrieve the next page of results. Provide in the page_token parameter of the next request. + NextPageToken *string `json:"next_page_token,omitempty" url:"next_page_token,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (l *ListModelsResponse) GetExtraProperties() map[string]interface{} { + return l.extraProperties +} + +func (l *ListModelsResponse) UnmarshalJSON(data []byte) error { + type unmarshaler ListModelsResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *l = ListModelsResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *l) + if err != nil { + return err + } + l.extraProperties = extraProperties + + l._rawJSON = json.RawMessage(data) + return nil +} + +func (l *ListModelsResponse) String() string { + if len(l._rawJSON) > 0 { + if value, err := core.StringifyJSON(l._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(l); err == nil { + return value + } + return fmt.Sprintf("%#v", l) +} + +type Message struct { + Role string + Chatbot *ChatMessage + System *ChatMessage + User *ChatMessage + Tool *ToolMessage +} + +func (m *Message) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + Role string `json:"role"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + m.Role = unmarshaler.Role + if unmarshaler.Role == "" { + return fmt.Errorf("%T did not include discriminant role", m) + } + switch unmarshaler.Role { + case "CHATBOT": + value := new(ChatMessage) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + m.Chatbot = value + case "SYSTEM": + value := new(ChatMessage) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + m.System = value + case "USER": + value := new(ChatMessage) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + m.User = value + case "TOOL": + value := new(ToolMessage) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + m.Tool = value + } + return nil +} + +func (m Message) MarshalJSON() ([]byte, error) { + if m.Chatbot != nil { + return core.MarshalJSONWithExtraProperty(m.Chatbot, "role", "CHATBOT") + } + if m.System != nil { + return core.MarshalJSONWithExtraProperty(m.System, "role", "SYSTEM") + } + if m.User != nil { + return core.MarshalJSONWithExtraProperty(m.User, "role", "USER") + } + if m.Tool != nil { + return core.MarshalJSONWithExtraProperty(m.Tool, "role", "TOOL") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", m) +} + +type MessageVisitor interface { + VisitChatbot(*ChatMessage) error + VisitSystem(*ChatMessage) error + VisitUser(*ChatMessage) error + VisitTool(*ToolMessage) error +} + +func (m *Message) Accept(visitor MessageVisitor) error { + if m.Chatbot != nil { + return visitor.VisitChatbot(m.Chatbot) + } + if m.System != nil { + return visitor.VisitSystem(m.System) + } + if m.User != nil { + return visitor.VisitUser(m.User) + } + if m.Tool != nil { + return visitor.VisitTool(m.Tool) + } + return fmt.Errorf("type %T does not define a non-empty union type", m) +} + +type Metrics struct { + FinetuneDatasetMetrics *FinetuneDatasetMetrics `json:"finetune_dataset_metrics,omitempty" url:"finetune_dataset_metrics,omitempty"` + EmbedData *MetricsEmbedData `json:"embed_data,omitempty" url:"embed_data,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (m *Metrics) GetExtraProperties() map[string]interface{} { + return m.extraProperties +} + +func (m *Metrics) UnmarshalJSON(data []byte) error { + type unmarshaler Metrics + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *m = Metrics(value) + + extraProperties, err := core.ExtractExtraProperties(data, *m) + if err != nil { + return err + } + m.extraProperties = extraProperties + + m._rawJSON = json.RawMessage(data) + return nil +} + +func (m *Metrics) String() string { + if len(m._rawJSON) > 0 { + if value, err := core.StringifyJSON(m._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(m); err == nil { + return value + } + return fmt.Sprintf("%#v", m) +} + +type MetricsEmbedData struct { + // the fields in the dataset + Fields []*MetricsEmbedDataFieldsItem `json:"fields,omitempty" url:"fields,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (m *MetricsEmbedData) GetExtraProperties() map[string]interface{} { + return m.extraProperties +} + +func (m *MetricsEmbedData) UnmarshalJSON(data []byte) error { + type unmarshaler MetricsEmbedData + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *m = MetricsEmbedData(value) + + extraProperties, err := core.ExtractExtraProperties(data, *m) + if err != nil { + return err + } + m.extraProperties = extraProperties + + m._rawJSON = json.RawMessage(data) + return nil +} + +func (m *MetricsEmbedData) String() string { + if len(m._rawJSON) > 0 { + if value, err := core.StringifyJSON(m._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(m); err == nil { + return value + } + return fmt.Sprintf("%#v", m) +} + +type MetricsEmbedDataFieldsItem struct { + // the name of the field + Name *string `json:"name,omitempty" url:"name,omitempty"` + // the number of times the field appears in the dataset + Count *float64 `json:"count,omitempty" url:"count,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (m *MetricsEmbedDataFieldsItem) GetExtraProperties() map[string]interface{} { + return m.extraProperties +} + +func (m *MetricsEmbedDataFieldsItem) UnmarshalJSON(data []byte) error { + type unmarshaler MetricsEmbedDataFieldsItem + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *m = MetricsEmbedDataFieldsItem(value) + + extraProperties, err := core.ExtractExtraProperties(data, *m) + if err != nil { + return err + } + m.extraProperties = extraProperties + + m._rawJSON = json.RawMessage(data) + return nil +} + +func (m *MetricsEmbedDataFieldsItem) String() string { + if len(m._rawJSON) > 0 { + if value, err := core.StringifyJSON(m._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(m); err == nil { + return value + } + return fmt.Sprintf("%#v", m) +} + +type NonStreamedChatResponse struct { + // Contents of the reply generated by the model. + Text string `json:"text" url:"text"` + // Unique identifier for the generated reply. Useful for submitting feedback. + GenerationId *string `json:"generation_id,omitempty" url:"generation_id,omitempty"` + // Inline citations for the generated reply. + Citations []*ChatCitation `json:"citations,omitempty" url:"citations,omitempty"` + // Documents seen by the model when generating the reply. + Documents []ChatDocument `json:"documents,omitempty" url:"documents,omitempty"` + // Denotes that a search for documents is required during the RAG flow. + IsSearchRequired *bool `json:"is_search_required,omitempty" url:"is_search_required,omitempty"` + // Generated search queries, meant to be used as part of the RAG flow. + SearchQueries []*ChatSearchQuery `json:"search_queries,omitempty" url:"search_queries,omitempty"` + // Documents retrieved from each of the conducted searches. + SearchResults []*ChatSearchResult `json:"search_results,omitempty" url:"search_results,omitempty"` + FinishReason *FinishReason `json:"finish_reason,omitempty" url:"finish_reason,omitempty"` + ToolCalls []*ToolCall `json:"tool_calls,omitempty" url:"tool_calls,omitempty"` + // A list of previous messages between the user and the model, meant to give the model conversational context for responding to the user's `message`. + ChatHistory []*Message `json:"chat_history,omitempty" url:"chat_history,omitempty"` + // The prompt that was used. Only present when `return_prompt` in the request is set to true. + Prompt *string `json:"prompt,omitempty" url:"prompt,omitempty"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (n *NonStreamedChatResponse) GetExtraProperties() map[string]interface{} { + return n.extraProperties +} + +func (n *NonStreamedChatResponse) UnmarshalJSON(data []byte) error { + type unmarshaler NonStreamedChatResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *n = NonStreamedChatResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *n) + if err != nil { + return err + } + n.extraProperties = extraProperties + + n._rawJSON = json.RawMessage(data) + return nil +} + +func (n *NonStreamedChatResponse) String() string { + if len(n._rawJSON) > 0 { + if value, err := core.StringifyJSON(n._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(n); err == nil { + return value + } + return fmt.Sprintf("%#v", n) +} + +type NotImplementedErrorBody struct { + Data *string `json:"data,omitempty" url:"data,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (n *NotImplementedErrorBody) GetExtraProperties() map[string]interface{} { + return n.extraProperties +} + +func (n *NotImplementedErrorBody) UnmarshalJSON(data []byte) error { + type unmarshaler NotImplementedErrorBody + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *n = NotImplementedErrorBody(value) + + extraProperties, err := core.ExtractExtraProperties(data, *n) + if err != nil { + return err + } + n.extraProperties = extraProperties + + n._rawJSON = json.RawMessage(data) + return nil +} + +func (n *NotImplementedErrorBody) String() string { + if len(n._rawJSON) > 0 { + if value, err := core.StringifyJSON(n._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(n); err == nil { + return value + } + return fmt.Sprintf("%#v", n) +} + +type OAuthAuthorizeResponse struct { + // The OAuth 2.0 redirect url. Redirect the user to this url to authorize the connector. + RedirectUrl *string `json:"redirect_url,omitempty" url:"redirect_url,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (o *OAuthAuthorizeResponse) GetExtraProperties() map[string]interface{} { + return o.extraProperties +} + +func (o *OAuthAuthorizeResponse) UnmarshalJSON(data []byte) error { + type unmarshaler OAuthAuthorizeResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *o = OAuthAuthorizeResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *o) + if err != nil { + return err + } + o.extraProperties = extraProperties + + o._rawJSON = json.RawMessage(data) + return nil +} + +func (o *OAuthAuthorizeResponse) String() string { + if len(o._rawJSON) > 0 { + if value, err := core.StringifyJSON(o._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(o); err == nil { + return value + } + return fmt.Sprintf("%#v", o) +} + +type ParseInfo struct { + Separator *string `json:"separator,omitempty" url:"separator,omitempty"` + Delimiter *string `json:"delimiter,omitempty" url:"delimiter,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (p *ParseInfo) GetExtraProperties() map[string]interface{} { + return p.extraProperties +} + +func (p *ParseInfo) UnmarshalJSON(data []byte) error { + type unmarshaler ParseInfo + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *p = ParseInfo(value) + + extraProperties, err := core.ExtractExtraProperties(data, *p) + if err != nil { + return err + } + p.extraProperties = extraProperties + + p._rawJSON = json.RawMessage(data) + return nil +} + +func (p *ParseInfo) String() string { + if len(p._rawJSON) > 0 { + if value, err := core.StringifyJSON(p._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(p); err == nil { + return value + } + return fmt.Sprintf("%#v", p) +} + +type RerankDocument = map[string]string + +type RerankRequestDocumentsItem struct { + String string + RerankDocument RerankDocument +} + +func (r *RerankRequestDocumentsItem) UnmarshalJSON(data []byte) error { + var valueString string + if err := json.Unmarshal(data, &valueString); err == nil { + r.String = valueString + return nil + } + var valueRerankDocument RerankDocument + if err := json.Unmarshal(data, &valueRerankDocument); err == nil { + r.RerankDocument = valueRerankDocument + return nil + } + return fmt.Errorf("%s cannot be deserialized as a %T", data, r) +} + +func (r RerankRequestDocumentsItem) MarshalJSON() ([]byte, error) { + if r.String != "" { + return json.Marshal(r.String) + } + if r.RerankDocument != nil { + return json.Marshal(r.RerankDocument) + } + return nil, fmt.Errorf("type %T does not include a non-empty union type", r) +} + +type RerankRequestDocumentsItemVisitor interface { + VisitString(string) error + VisitRerankDocument(RerankDocument) error +} + +func (r *RerankRequestDocumentsItem) Accept(visitor RerankRequestDocumentsItemVisitor) error { + if r.String != "" { + return visitor.VisitString(r.String) + } + if r.RerankDocument != nil { + return visitor.VisitRerankDocument(r.RerankDocument) + } + return fmt.Errorf("type %T does not include a non-empty union type", r) +} + +type RerankResponse struct { + Id *string `json:"id,omitempty" url:"id,omitempty"` + // An ordered list of ranked documents + Results []*RerankResponseResultsItem `json:"results,omitempty" url:"results,omitempty"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (r *RerankResponse) GetExtraProperties() map[string]interface{} { + return r.extraProperties +} + +func (r *RerankResponse) UnmarshalJSON(data []byte) error { + type unmarshaler RerankResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *r = RerankResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *r) + if err != nil { + return err + } + r.extraProperties = extraProperties + + r._rawJSON = json.RawMessage(data) + return nil +} + +func (r *RerankResponse) String() string { + if len(r._rawJSON) > 0 { + if value, err := core.StringifyJSON(r._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(r); err == nil { + return value + } + return fmt.Sprintf("%#v", r) +} + +type RerankResponseResultsItem struct { + // If `return_documents` is set as `false` this will return none, if `true` it will return the documents passed in + Document *RerankResponseResultsItemDocument `json:"document,omitempty" url:"document,omitempty"` + // Corresponds to the index in the original list of documents to which the ranked document belongs. (i.e. if the first value in the `results` object has an `index` value of 3, it means in the list of documents passed in, the document at `index=3` had the highest relevance) + Index int `json:"index" url:"index"` + // Relevance scores are normalized to be in the range `[0, 1]`. Scores close to `1` indicate a high relevance to the query, and scores closer to `0` indicate low relevance. It is not accurate to assume a score of 0.9 means the document is 2x more relevant than a document with a score of 0.45 + RelevanceScore float64 `json:"relevance_score" url:"relevance_score"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (r *RerankResponseResultsItem) GetExtraProperties() map[string]interface{} { + return r.extraProperties +} + +func (r *RerankResponseResultsItem) UnmarshalJSON(data []byte) error { + type unmarshaler RerankResponseResultsItem + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *r = RerankResponseResultsItem(value) + + extraProperties, err := core.ExtractExtraProperties(data, *r) + if err != nil { + return err + } + r.extraProperties = extraProperties + + r._rawJSON = json.RawMessage(data) + return nil +} + +func (r *RerankResponseResultsItem) String() string { + if len(r._rawJSON) > 0 { + if value, err := core.StringifyJSON(r._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(r); err == nil { + return value + } + return fmt.Sprintf("%#v", r) +} + +// If `return_documents` is set as `false` this will return none, if `true` it will return the documents passed in +type RerankResponseResultsItemDocument struct { + // The text of the document to rerank + Text string `json:"text" url:"text"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (r *RerankResponseResultsItemDocument) GetExtraProperties() map[string]interface{} { + return r.extraProperties +} + +func (r *RerankResponseResultsItemDocument) UnmarshalJSON(data []byte) error { + type unmarshaler RerankResponseResultsItemDocument + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *r = RerankResponseResultsItemDocument(value) + + extraProperties, err := core.ExtractExtraProperties(data, *r) + if err != nil { + return err + } + r.extraProperties = extraProperties + + r._rawJSON = json.RawMessage(data) + return nil +} + +func (r *RerankResponseResultsItemDocument) String() string { + if len(r._rawJSON) > 0 { + if value, err := core.StringifyJSON(r._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(r); err == nil { + return value + } + return fmt.Sprintf("%#v", r) +} + +type RerankerDataMetrics struct { + // The number of training queries. + NumTrainQueries *int64 `json:"num_train_queries,omitempty" url:"num_train_queries,omitempty"` + // The sum of all relevant passages of valid training examples. + NumTrainRelevantPassages *int64 `json:"num_train_relevant_passages,omitempty" url:"num_train_relevant_passages,omitempty"` + // The sum of all hard negatives of valid training examples. + NumTrainHardNegatives *int64 `json:"num_train_hard_negatives,omitempty" url:"num_train_hard_negatives,omitempty"` + // The number of evaluation queries. + NumEvalQueries *int64 `json:"num_eval_queries,omitempty" url:"num_eval_queries,omitempty"` + // The sum of all relevant passages of valid eval examples. + NumEvalRelevantPassages *int64 `json:"num_eval_relevant_passages,omitempty" url:"num_eval_relevant_passages,omitempty"` + // The sum of all hard negatives of valid eval examples. + NumEvalHardNegatives *int64 `json:"num_eval_hard_negatives,omitempty" url:"num_eval_hard_negatives,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (r *RerankerDataMetrics) GetExtraProperties() map[string]interface{} { + return r.extraProperties +} + +func (r *RerankerDataMetrics) UnmarshalJSON(data []byte) error { + type unmarshaler RerankerDataMetrics + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *r = RerankerDataMetrics(value) + + extraProperties, err := core.ExtractExtraProperties(data, *r) + if err != nil { + return err + } + r.extraProperties = extraProperties + + r._rawJSON = json.RawMessage(data) + return nil +} + +func (r *RerankerDataMetrics) String() string { + if len(r._rawJSON) > 0 { + if value, err := core.StringifyJSON(r._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(r); err == nil { + return value + } + return fmt.Sprintf("%#v", r) +} + +// Configuration for forcing the model output to adhere to the specified format. Supported on [Command R 03-2024](https://docs.cohere.com/docs/command-r), [Command R+ 04-2024](https://docs.cohere.com/docs/command-r-plus) and newer models. +// +// The model can be forced into outputting JSON objects (with up to 5 levels of nesting) by setting `{ "type": "json_object" }`. +// +// A [JSON Schema](https://json-schema.org/) can optionally be provided, to ensure a specific structure. +// +// **Note**: When using `{ "type": "json_object" }` your `message` should always explicitly instruct the model to generate a JSON (eg: _"Generate a JSON ..."_) . Otherwise the model may end up getting stuck generating an infinite stream of characters and eventually run out of context length. +// **Limitation**: The parameter is not supported in RAG mode (when any of `connectors`, `documents`, `tools`, `tool_results` are provided). +type ResponseFormat struct { + Type string + Text *TextResponseFormat + JsonObject *JsonResponseFormat +} + +func (r *ResponseFormat) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + Type string `json:"type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + r.Type = unmarshaler.Type + if unmarshaler.Type == "" { + return fmt.Errorf("%T did not include discriminant type", r) + } + switch unmarshaler.Type { + case "text": + value := new(TextResponseFormat) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + r.Text = value + case "json_object": + value := new(JsonResponseFormat) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + r.JsonObject = value + } + return nil +} + +func (r ResponseFormat) MarshalJSON() ([]byte, error) { + if r.Text != nil { + return core.MarshalJSONWithExtraProperty(r.Text, "type", "text") + } + if r.JsonObject != nil { + return core.MarshalJSONWithExtraProperty(r.JsonObject, "type", "json_object") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", r) +} + +type ResponseFormatVisitor interface { + VisitText(*TextResponseFormat) error + VisitJsonObject(*JsonResponseFormat) error +} + +func (r *ResponseFormat) Accept(visitor ResponseFormatVisitor) error { + if r.Text != nil { + return visitor.VisitText(r.Text) + } + if r.JsonObject != nil { + return visitor.VisitJsonObject(r.JsonObject) + } + return fmt.Errorf("type %T does not define a non-empty union type", r) +} + +// Configuration for forcing the model output to adhere to the specified format. Supported on [Command R](https://docs.cohere.com/v2/docs/command-r), [Command R+](https://docs.cohere.com/v2/docs/command-r-plus) and newer models. +// +// The model can be forced into outputting JSON objects by setting `{ "type": "json_object" }`. +// +// A [JSON Schema](https://json-schema.org/) can optionally be provided, to ensure a specific structure. +// +// **Note**: When using `{ "type": "json_object" }` your `message` should always explicitly instruct the model to generate a JSON (eg: _"Generate a JSON ..."_) . Otherwise the model may end up getting stuck generating an infinite stream of characters and eventually run out of context length. +// +// **Note**: When `json_schema` is not specified, the generated object can have up to 5 layers of nesting. +// +// **Limitation**: The parameter is not supported when used in combinations with the `documents` or `tools` parameters. +type ResponseFormatV2 struct { + Type string + Text *TextResponseFormatV2 + JsonObject *JsonResponseFormatV2 +} + +func (r *ResponseFormatV2) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + Type string `json:"type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + r.Type = unmarshaler.Type + if unmarshaler.Type == "" { + return fmt.Errorf("%T did not include discriminant type", r) + } + switch unmarshaler.Type { + case "text": + value := new(TextResponseFormatV2) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + r.Text = value + case "json_object": + value := new(JsonResponseFormatV2) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + r.JsonObject = value + } + return nil +} + +func (r ResponseFormatV2) MarshalJSON() ([]byte, error) { + if r.Text != nil { + return core.MarshalJSONWithExtraProperty(r.Text, "type", "text") + } + if r.JsonObject != nil { + return core.MarshalJSONWithExtraProperty(r.JsonObject, "type", "json_object") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", r) +} + +type ResponseFormatV2Visitor interface { + VisitText(*TextResponseFormatV2) error + VisitJsonObject(*JsonResponseFormatV2) error +} + +func (r *ResponseFormatV2) Accept(visitor ResponseFormatV2Visitor) error { + if r.Text != nil { + return visitor.VisitText(r.Text) + } + if r.JsonObject != nil { + return visitor.VisitJsonObject(r.JsonObject) + } + return fmt.Errorf("type %T does not define a non-empty union type", r) +} + +type SingleGeneration struct { + Id string `json:"id" url:"id"` + Text string `json:"text" url:"text"` + // Refers to the nth generation. Only present when `num_generations` is greater than zero. + Index *int `json:"index,omitempty" url:"index,omitempty"` + Likelihood *float64 `json:"likelihood,omitempty" url:"likelihood,omitempty"` + // Only returned if `return_likelihoods` is set to `GENERATION` or `ALL`. The likelihood refers to the average log-likelihood of the entire specified string, which is useful for [evaluating the performance of your model](likelihood-eval), especially if you've created a [custom model](/docs/training-custom-models). Individual token likelihoods provide the log-likelihood of each token. The first token will not have a likelihood. + TokenLikelihoods []*SingleGenerationTokenLikelihoodsItem `json:"token_likelihoods,omitempty" url:"token_likelihoods,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (s *SingleGeneration) GetExtraProperties() map[string]interface{} { + return s.extraProperties +} + +func (s *SingleGeneration) UnmarshalJSON(data []byte) error { + type unmarshaler SingleGeneration + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *s = SingleGeneration(value) + + extraProperties, err := core.ExtractExtraProperties(data, *s) + if err != nil { + return err + } + s.extraProperties = extraProperties + + s._rawJSON = json.RawMessage(data) + return nil +} + +func (s *SingleGeneration) String() string { + if len(s._rawJSON) > 0 { + if value, err := core.StringifyJSON(s._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(s); err == nil { + return value + } + return fmt.Sprintf("%#v", s) +} + +type SingleGenerationInStream struct { + Id string `json:"id" url:"id"` + // Full text of the generation. + Text string `json:"text" url:"text"` + // Refers to the nth generation. Only present when `num_generations` is greater than zero. + Index *int `json:"index,omitempty" url:"index,omitempty"` + FinishReason FinishReason `json:"finish_reason" url:"finish_reason"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (s *SingleGenerationInStream) GetExtraProperties() map[string]interface{} { + return s.extraProperties +} + +func (s *SingleGenerationInStream) UnmarshalJSON(data []byte) error { + type unmarshaler SingleGenerationInStream + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *s = SingleGenerationInStream(value) + + extraProperties, err := core.ExtractExtraProperties(data, *s) + if err != nil { + return err + } + s.extraProperties = extraProperties + + s._rawJSON = json.RawMessage(data) + return nil +} + +func (s *SingleGenerationInStream) String() string { + if len(s._rawJSON) > 0 { + if value, err := core.StringifyJSON(s._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(s); err == nil { + return value + } + return fmt.Sprintf("%#v", s) +} + +type SingleGenerationTokenLikelihoodsItem struct { + Token string `json:"token" url:"token"` + Likelihood float64 `json:"likelihood" url:"likelihood"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (s *SingleGenerationTokenLikelihoodsItem) GetExtraProperties() map[string]interface{} { + return s.extraProperties +} + +func (s *SingleGenerationTokenLikelihoodsItem) UnmarshalJSON(data []byte) error { + type unmarshaler SingleGenerationTokenLikelihoodsItem + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *s = SingleGenerationTokenLikelihoodsItem(value) + + extraProperties, err := core.ExtractExtraProperties(data, *s) + if err != nil { + return err + } + s.extraProperties = extraProperties + + s._rawJSON = json.RawMessage(data) + return nil +} + +func (s *SingleGenerationTokenLikelihoodsItem) String() string { + if len(s._rawJSON) > 0 { + if value, err := core.StringifyJSON(s._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(s); err == nil { + return value + } + return fmt.Sprintf("%#v", s) +} + +// A source object containing information about the source of the data cited. +type Source struct { + Type string + Tool *ToolSource + Document *DocumentSource +} + +func (s *Source) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + Type string `json:"type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + s.Type = unmarshaler.Type + if unmarshaler.Type == "" { + return fmt.Errorf("%T did not include discriminant type", s) + } + switch unmarshaler.Type { + case "tool": + value := new(ToolSource) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.Tool = value + case "document": + value := new(DocumentSource) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.Document = value + } + return nil +} + +func (s Source) MarshalJSON() ([]byte, error) { + if s.Tool != nil { + return core.MarshalJSONWithExtraProperty(s.Tool, "type", "tool") + } + if s.Document != nil { + return core.MarshalJSONWithExtraProperty(s.Document, "type", "document") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", s) +} + +type SourceVisitor interface { + VisitTool(*ToolSource) error + VisitDocument(*DocumentSource) error +} + +func (s *Source) Accept(visitor SourceVisitor) error { + if s.Tool != nil { + return visitor.VisitTool(s.Tool) + } + if s.Document != nil { + return visitor.VisitDocument(s.Document) + } + return fmt.Errorf("type %T does not define a non-empty union type", s) +} + +// StreamedChatResponse is returned in streaming mode (specified with `stream=True` in the request). +type StreamedChatResponse struct { + EventType string + StreamStart *ChatStreamStartEvent + SearchQueriesGeneration *ChatSearchQueriesGenerationEvent + SearchResults *ChatSearchResultsEvent + TextGeneration *ChatTextGenerationEvent + CitationGeneration *ChatCitationGenerationEvent + ToolCallsGeneration *ChatToolCallsGenerationEvent + StreamEnd *ChatStreamEndEvent + ToolCallsChunk *ChatToolCallsChunkEvent +} + +func (s *StreamedChatResponse) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + EventType string `json:"event_type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + s.EventType = unmarshaler.EventType + if unmarshaler.EventType == "" { + return fmt.Errorf("%T did not include discriminant event_type", s) + } + switch unmarshaler.EventType { + case "stream-start": + value := new(ChatStreamStartEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.StreamStart = value + case "search-queries-generation": + value := new(ChatSearchQueriesGenerationEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.SearchQueriesGeneration = value + case "search-results": + value := new(ChatSearchResultsEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.SearchResults = value + case "text-generation": + value := new(ChatTextGenerationEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.TextGeneration = value + case "citation-generation": + value := new(ChatCitationGenerationEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.CitationGeneration = value + case "tool-calls-generation": + value := new(ChatToolCallsGenerationEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.ToolCallsGeneration = value + case "stream-end": + value := new(ChatStreamEndEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.StreamEnd = value + case "tool-calls-chunk": + value := new(ChatToolCallsChunkEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.ToolCallsChunk = value + } + return nil +} + +func (s StreamedChatResponse) MarshalJSON() ([]byte, error) { + if s.StreamStart != nil { + return core.MarshalJSONWithExtraProperty(s.StreamStart, "event_type", "stream-start") + } + if s.SearchQueriesGeneration != nil { + return core.MarshalJSONWithExtraProperty(s.SearchQueriesGeneration, "event_type", "search-queries-generation") + } + if s.SearchResults != nil { + return core.MarshalJSONWithExtraProperty(s.SearchResults, "event_type", "search-results") + } + if s.TextGeneration != nil { + return core.MarshalJSONWithExtraProperty(s.TextGeneration, "event_type", "text-generation") + } + if s.CitationGeneration != nil { + return core.MarshalJSONWithExtraProperty(s.CitationGeneration, "event_type", "citation-generation") + } + if s.ToolCallsGeneration != nil { + return core.MarshalJSONWithExtraProperty(s.ToolCallsGeneration, "event_type", "tool-calls-generation") + } + if s.StreamEnd != nil { + return core.MarshalJSONWithExtraProperty(s.StreamEnd, "event_type", "stream-end") + } + if s.ToolCallsChunk != nil { + return core.MarshalJSONWithExtraProperty(s.ToolCallsChunk, "event_type", "tool-calls-chunk") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", s) +} + +type StreamedChatResponseVisitor interface { + VisitStreamStart(*ChatStreamStartEvent) error + VisitSearchQueriesGeneration(*ChatSearchQueriesGenerationEvent) error + VisitSearchResults(*ChatSearchResultsEvent) error + VisitTextGeneration(*ChatTextGenerationEvent) error + VisitCitationGeneration(*ChatCitationGenerationEvent) error + VisitToolCallsGeneration(*ChatToolCallsGenerationEvent) error + VisitStreamEnd(*ChatStreamEndEvent) error + VisitToolCallsChunk(*ChatToolCallsChunkEvent) error +} + +func (s *StreamedChatResponse) Accept(visitor StreamedChatResponseVisitor) error { + if s.StreamStart != nil { + return visitor.VisitStreamStart(s.StreamStart) + } + if s.SearchQueriesGeneration != nil { + return visitor.VisitSearchQueriesGeneration(s.SearchQueriesGeneration) + } + if s.SearchResults != nil { + return visitor.VisitSearchResults(s.SearchResults) + } + if s.TextGeneration != nil { + return visitor.VisitTextGeneration(s.TextGeneration) + } + if s.CitationGeneration != nil { + return visitor.VisitCitationGeneration(s.CitationGeneration) + } + if s.ToolCallsGeneration != nil { + return visitor.VisitToolCallsGeneration(s.ToolCallsGeneration) + } + if s.StreamEnd != nil { + return visitor.VisitStreamEnd(s.StreamEnd) + } + if s.ToolCallsChunk != nil { + return visitor.VisitToolCallsChunk(s.ToolCallsChunk) + } + return fmt.Errorf("type %T does not define a non-empty union type", s) +} + +// StreamedChatResponse is returned in streaming mode (specified with `stream=True` in the request). +type StreamedChatResponseV2 struct { + Type string + MessageStart *ChatMessageStartEvent + ContentStart *ChatContentStartEvent + ContentDelta *ChatContentDeltaEvent + ContentEnd *ChatContentEndEvent + ToolPlanDelta *ChatToolPlanDeltaEvent + ToolCallStart *ChatToolCallStartEvent + ToolCallDelta *ChatToolCallDeltaEvent + ToolCallEnd *ChatToolCallEndEvent + CitationStart *CitationStartEvent + CitationEnd *CitationEndEvent + MessageEnd *ChatMessageEndEvent +} + +func (s *StreamedChatResponseV2) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + Type string `json:"type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + s.Type = unmarshaler.Type + if unmarshaler.Type == "" { + return fmt.Errorf("%T did not include discriminant type", s) + } + switch unmarshaler.Type { + case "message-start": + value := new(ChatMessageStartEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.MessageStart = value + case "content-start": + value := new(ChatContentStartEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.ContentStart = value + case "content-delta": + value := new(ChatContentDeltaEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.ContentDelta = value + case "content-end": + value := new(ChatContentEndEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.ContentEnd = value + case "tool-plan-delta": + value := new(ChatToolPlanDeltaEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.ToolPlanDelta = value + case "tool-call-start": + value := new(ChatToolCallStartEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.ToolCallStart = value + case "tool-call-delta": + value := new(ChatToolCallDeltaEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.ToolCallDelta = value + case "tool-call-end": + value := new(ChatToolCallEndEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.ToolCallEnd = value + case "citation-start": + value := new(CitationStartEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.CitationStart = value + case "citation-end": + value := new(CitationEndEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.CitationEnd = value + case "message-end": + value := new(ChatMessageEndEvent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.MessageEnd = value + } + return nil +} + +func (s StreamedChatResponseV2) MarshalJSON() ([]byte, error) { + if s.MessageStart != nil { + return core.MarshalJSONWithExtraProperty(s.MessageStart, "type", "message-start") + } + if s.ContentStart != nil { + return core.MarshalJSONWithExtraProperty(s.ContentStart, "type", "content-start") + } + if s.ContentDelta != nil { + return core.MarshalJSONWithExtraProperty(s.ContentDelta, "type", "content-delta") + } + if s.ContentEnd != nil { + return core.MarshalJSONWithExtraProperty(s.ContentEnd, "type", "content-end") + } + if s.ToolPlanDelta != nil { + return core.MarshalJSONWithExtraProperty(s.ToolPlanDelta, "type", "tool-plan-delta") + } + if s.ToolCallStart != nil { + return core.MarshalJSONWithExtraProperty(s.ToolCallStart, "type", "tool-call-start") + } + if s.ToolCallDelta != nil { + return core.MarshalJSONWithExtraProperty(s.ToolCallDelta, "type", "tool-call-delta") + } + if s.ToolCallEnd != nil { + return core.MarshalJSONWithExtraProperty(s.ToolCallEnd, "type", "tool-call-end") + } + if s.CitationStart != nil { + return core.MarshalJSONWithExtraProperty(s.CitationStart, "type", "citation-start") + } + if s.CitationEnd != nil { + return core.MarshalJSONWithExtraProperty(s.CitationEnd, "type", "citation-end") + } + if s.MessageEnd != nil { + return core.MarshalJSONWithExtraProperty(s.MessageEnd, "type", "message-end") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", s) +} + +type StreamedChatResponseV2Visitor interface { + VisitMessageStart(*ChatMessageStartEvent) error + VisitContentStart(*ChatContentStartEvent) error + VisitContentDelta(*ChatContentDeltaEvent) error + VisitContentEnd(*ChatContentEndEvent) error + VisitToolPlanDelta(*ChatToolPlanDeltaEvent) error + VisitToolCallStart(*ChatToolCallStartEvent) error + VisitToolCallDelta(*ChatToolCallDeltaEvent) error + VisitToolCallEnd(*ChatToolCallEndEvent) error + VisitCitationStart(*CitationStartEvent) error + VisitCitationEnd(*CitationEndEvent) error + VisitMessageEnd(*ChatMessageEndEvent) error } -func (m *MetricsEmbedDataFieldsItem) UnmarshalJSON(data []byte) error { - type unmarshaler MetricsEmbedDataFieldsItem - var value unmarshaler - if err := json.Unmarshal(data, &value); err != nil { - return err +func (s *StreamedChatResponseV2) Accept(visitor StreamedChatResponseV2Visitor) error { + if s.MessageStart != nil { + return visitor.VisitMessageStart(s.MessageStart) } - *m = MetricsEmbedDataFieldsItem(value) - - extraProperties, err := core.ExtractExtraProperties(data, *m) - if err != nil { - return err + if s.ContentStart != nil { + return visitor.VisitContentStart(s.ContentStart) } - m.extraProperties = extraProperties - - m._rawJSON = json.RawMessage(data) - return nil -} - -func (m *MetricsEmbedDataFieldsItem) String() string { - if len(m._rawJSON) > 0 { - if value, err := core.StringifyJSON(m._rawJSON); err == nil { - return value - } + if s.ContentDelta != nil { + return visitor.VisitContentDelta(s.ContentDelta) } - if value, err := core.StringifyJSON(m); err == nil { - return value + if s.ContentEnd != nil { + return visitor.VisitContentEnd(s.ContentEnd) } - return fmt.Sprintf("%#v", m) + if s.ToolPlanDelta != nil { + return visitor.VisitToolPlanDelta(s.ToolPlanDelta) + } + if s.ToolCallStart != nil { + return visitor.VisitToolCallStart(s.ToolCallStart) + } + if s.ToolCallDelta != nil { + return visitor.VisitToolCallDelta(s.ToolCallDelta) + } + if s.ToolCallEnd != nil { + return visitor.VisitToolCallEnd(s.ToolCallEnd) + } + if s.CitationStart != nil { + return visitor.VisitCitationStart(s.CitationStart) + } + if s.CitationEnd != nil { + return visitor.VisitCitationEnd(s.CitationEnd) + } + if s.MessageEnd != nil { + return visitor.VisitMessageEnd(s.MessageEnd) + } + return fmt.Errorf("type %T does not define a non-empty union type", s) } -type NonStreamedChatResponse struct { - // Contents of the reply generated by the model. - Text string `json:"text" url:"text"` - // Unique identifier for the generated reply. Useful for submitting feedback. - GenerationId *string `json:"generation_id,omitempty" url:"generation_id,omitempty"` - // Inline citations for the generated reply. - Citations []*ChatCitation `json:"citations,omitempty" url:"citations,omitempty"` - // Documents seen by the model when generating the reply. - Documents []ChatDocument `json:"documents,omitempty" url:"documents,omitempty"` - // Denotes that a search for documents is required during the RAG flow. - IsSearchRequired *bool `json:"is_search_required,omitempty" url:"is_search_required,omitempty"` - // Generated search queries, meant to be used as part of the RAG flow. - SearchQueries []*ChatSearchQuery `json:"search_queries,omitempty" url:"search_queries,omitempty"` - // Documents retrieved from each of the conducted searches. - SearchResults []*ChatSearchResult `json:"search_results,omitempty" url:"search_results,omitempty"` - FinishReason *FinishReason `json:"finish_reason,omitempty" url:"finish_reason,omitempty"` - ToolCalls []*ToolCall `json:"tool_calls,omitempty" url:"tool_calls,omitempty"` - // A list of previous messages between the user and the model, meant to give the model conversational context for responding to the user's `message`. - ChatHistory []*Message `json:"chat_history,omitempty" url:"chat_history,omitempty"` - // The prompt that was used. Only present when `return_prompt` in the request is set to true. - Prompt *string `json:"prompt,omitempty" url:"prompt,omitempty"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +// One of `low`, `medium`, `high`, or `auto`, defaults to `auto`. Controls how close to the original text the summary is. `high` extractiveness summaries will lean towards reusing sentences verbatim, while `low` extractiveness summaries will tend to paraphrase more. If `auto` is selected, the best option will be picked based on the input text. +type SummarizeRequestExtractiveness string - extraProperties map[string]interface{} - _rawJSON json.RawMessage +const ( + SummarizeRequestExtractivenessLow SummarizeRequestExtractiveness = "low" + SummarizeRequestExtractivenessMedium SummarizeRequestExtractiveness = "medium" + SummarizeRequestExtractivenessHigh SummarizeRequestExtractiveness = "high" +) + +func NewSummarizeRequestExtractivenessFromString(s string) (SummarizeRequestExtractiveness, error) { + switch s { + case "low": + return SummarizeRequestExtractivenessLow, nil + case "medium": + return SummarizeRequestExtractivenessMedium, nil + case "high": + return SummarizeRequestExtractivenessHigh, nil + } + var t SummarizeRequestExtractiveness + return "", fmt.Errorf("%s is not a valid %T", s, t) } -func (n *NonStreamedChatResponse) GetExtraProperties() map[string]interface{} { - return n.extraProperties +func (s SummarizeRequestExtractiveness) Ptr() *SummarizeRequestExtractiveness { + return &s } -func (n *NonStreamedChatResponse) UnmarshalJSON(data []byte) error { - type unmarshaler NonStreamedChatResponse - var value unmarshaler - if err := json.Unmarshal(data, &value); err != nil { - return err - } - *n = NonStreamedChatResponse(value) +// One of `paragraph`, `bullets`, or `auto`, defaults to `auto`. Indicates the style in which the summary will be delivered - in a free form paragraph or in bullet points. If `auto` is selected, the best option will be picked based on the input text. +type SummarizeRequestFormat string - extraProperties, err := core.ExtractExtraProperties(data, *n) - if err != nil { - return err +const ( + SummarizeRequestFormatParagraph SummarizeRequestFormat = "paragraph" + SummarizeRequestFormatBullets SummarizeRequestFormat = "bullets" +) + +func NewSummarizeRequestFormatFromString(s string) (SummarizeRequestFormat, error) { + switch s { + case "paragraph": + return SummarizeRequestFormatParagraph, nil + case "bullets": + return SummarizeRequestFormatBullets, nil } - n.extraProperties = extraProperties + var t SummarizeRequestFormat + return "", fmt.Errorf("%s is not a valid %T", s, t) +} - n._rawJSON = json.RawMessage(data) - return nil +func (s SummarizeRequestFormat) Ptr() *SummarizeRequestFormat { + return &s } -func (n *NonStreamedChatResponse) String() string { - if len(n._rawJSON) > 0 { - if value, err := core.StringifyJSON(n._rawJSON); err == nil { - return value - } - } - if value, err := core.StringifyJSON(n); err == nil { - return value +// One of `short`, `medium`, `long`, or `auto` defaults to `auto`. Indicates the approximate length of the summary. If `auto` is selected, the best option will be picked based on the input text. +type SummarizeRequestLength string + +const ( + SummarizeRequestLengthShort SummarizeRequestLength = "short" + SummarizeRequestLengthMedium SummarizeRequestLength = "medium" + SummarizeRequestLengthLong SummarizeRequestLength = "long" +) + +func NewSummarizeRequestLengthFromString(s string) (SummarizeRequestLength, error) { + switch s { + case "short": + return SummarizeRequestLengthShort, nil + case "medium": + return SummarizeRequestLengthMedium, nil + case "long": + return SummarizeRequestLengthLong, nil } - return fmt.Sprintf("%#v", n) + var t SummarizeRequestLength + return "", fmt.Errorf("%s is not a valid %T", s, t) } -type NotImplementedErrorBody struct { - Data *string `json:"data,omitempty" url:"data,omitempty"` +func (s SummarizeRequestLength) Ptr() *SummarizeRequestLength { + return &s +} + +type SummarizeResponse struct { + // Generated ID for the summary + Id *string `json:"id,omitempty" url:"id,omitempty"` + // Generated summary for the text + Summary *string `json:"summary,omitempty" url:"summary,omitempty"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (n *NotImplementedErrorBody) GetExtraProperties() map[string]interface{} { - return n.extraProperties +func (s *SummarizeResponse) GetExtraProperties() map[string]interface{} { + return s.extraProperties } -func (n *NotImplementedErrorBody) UnmarshalJSON(data []byte) error { - type unmarshaler NotImplementedErrorBody +func (s *SummarizeResponse) UnmarshalJSON(data []byte) error { + type unmarshaler SummarizeResponse var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *n = NotImplementedErrorBody(value) + *s = SummarizeResponse(value) - extraProperties, err := core.ExtractExtraProperties(data, *n) + extraProperties, err := core.ExtractExtraProperties(data, *s) if err != nil { return err } - n.extraProperties = extraProperties + s.extraProperties = extraProperties - n._rawJSON = json.RawMessage(data) + s._rawJSON = json.RawMessage(data) return nil } -func (n *NotImplementedErrorBody) String() string { - if len(n._rawJSON) > 0 { - if value, err := core.StringifyJSON(n._rawJSON); err == nil { +func (s *SummarizeResponse) String() string { + if len(s._rawJSON) > 0 { + if value, err := core.StringifyJSON(s._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(n); err == nil { + if value, err := core.StringifyJSON(s); err == nil { return value } - return fmt.Sprintf("%#v", n) + return fmt.Sprintf("%#v", s) } -type OAuthAuthorizeResponse struct { - // The OAuth 2.0 redirect url. Redirect the user to this url to authorize the connector. - RedirectUrl *string `json:"redirect_url,omitempty" url:"redirect_url,omitempty"` +// A message from the system. +type SystemMessage struct { + Content *SystemMessageContent `json:"content,omitempty" url:"content,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (o *OAuthAuthorizeResponse) GetExtraProperties() map[string]interface{} { - return o.extraProperties +func (s *SystemMessage) GetExtraProperties() map[string]interface{} { + return s.extraProperties } -func (o *OAuthAuthorizeResponse) UnmarshalJSON(data []byte) error { - type unmarshaler OAuthAuthorizeResponse +func (s *SystemMessage) UnmarshalJSON(data []byte) error { + type unmarshaler SystemMessage var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *o = OAuthAuthorizeResponse(value) + *s = SystemMessage(value) - extraProperties, err := core.ExtractExtraProperties(data, *o) + extraProperties, err := core.ExtractExtraProperties(data, *s) if err != nil { return err } - o.extraProperties = extraProperties + s.extraProperties = extraProperties - o._rawJSON = json.RawMessage(data) + s._rawJSON = json.RawMessage(data) return nil } -func (o *OAuthAuthorizeResponse) String() string { - if len(o._rawJSON) > 0 { - if value, err := core.StringifyJSON(o._rawJSON); err == nil { +func (s *SystemMessage) String() string { + if len(s._rawJSON) > 0 { + if value, err := core.StringifyJSON(s._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(o); err == nil { + if value, err := core.StringifyJSON(s); err == nil { return value } - return fmt.Sprintf("%#v", o) + return fmt.Sprintf("%#v", s) } -type ParseInfo struct { - Separator *string `json:"separator,omitempty" url:"separator,omitempty"` - Delimiter *string `json:"delimiter,omitempty" url:"delimiter,omitempty"` - - extraProperties map[string]interface{} - _rawJSON json.RawMessage +type SystemMessageContent struct { + String string + SystemMessageContentItemList []*SystemMessageContentItem } -func (p *ParseInfo) GetExtraProperties() map[string]interface{} { - return p.extraProperties +func (s *SystemMessageContent) UnmarshalJSON(data []byte) error { + var valueString string + if err := json.Unmarshal(data, &valueString); err == nil { + s.String = valueString + return nil + } + var valueSystemMessageContentItemList []*SystemMessageContentItem + if err := json.Unmarshal(data, &valueSystemMessageContentItemList); err == nil { + s.SystemMessageContentItemList = valueSystemMessageContentItemList + return nil + } + return fmt.Errorf("%s cannot be deserialized as a %T", data, s) } -func (p *ParseInfo) UnmarshalJSON(data []byte) error { - type unmarshaler ParseInfo - var value unmarshaler - if err := json.Unmarshal(data, &value); err != nil { - return err +func (s SystemMessageContent) MarshalJSON() ([]byte, error) { + if s.String != "" { + return json.Marshal(s.String) } - *p = ParseInfo(value) - - extraProperties, err := core.ExtractExtraProperties(data, *p) - if err != nil { - return err + if s.SystemMessageContentItemList != nil { + return json.Marshal(s.SystemMessageContentItemList) } - p.extraProperties = extraProperties + return nil, fmt.Errorf("type %T does not include a non-empty union type", s) +} - p._rawJSON = json.RawMessage(data) - return nil +type SystemMessageContentVisitor interface { + VisitString(string) error + VisitSystemMessageContentItemList([]*SystemMessageContentItem) error } -func (p *ParseInfo) String() string { - if len(p._rawJSON) > 0 { - if value, err := core.StringifyJSON(p._rawJSON); err == nil { - return value - } +func (s *SystemMessageContent) Accept(visitor SystemMessageContentVisitor) error { + if s.String != "" { + return visitor.VisitString(s.String) } - if value, err := core.StringifyJSON(p); err == nil { - return value + if s.SystemMessageContentItemList != nil { + return visitor.VisitSystemMessageContentItemList(s.SystemMessageContentItemList) } - return fmt.Sprintf("%#v", p) + return fmt.Errorf("type %T does not include a non-empty union type", s) } -type RerankDocument = map[string]string - -type RerankRequestDocumentsItem struct { - String string - RerankDocument RerankDocument +type SystemMessageContentItem struct { + Type string + Text *TextContent } -func (r *RerankRequestDocumentsItem) UnmarshalJSON(data []byte) error { - var valueString string - if err := json.Unmarshal(data, &valueString); err == nil { - r.String = valueString - return nil +func (s *SystemMessageContentItem) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + Type string `json:"type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err } - var valueRerankDocument RerankDocument - if err := json.Unmarshal(data, &valueRerankDocument); err == nil { - r.RerankDocument = valueRerankDocument - return nil + s.Type = unmarshaler.Type + if unmarshaler.Type == "" { + return fmt.Errorf("%T did not include discriminant type", s) } - return fmt.Errorf("%s cannot be deserialized as a %T", data, r) + switch unmarshaler.Type { + case "text": + value := new(TextContent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + s.Text = value + } + return nil } -func (r RerankRequestDocumentsItem) MarshalJSON() ([]byte, error) { - if r.String != "" { - return json.Marshal(r.String) - } - if r.RerankDocument != nil { - return json.Marshal(r.RerankDocument) +func (s SystemMessageContentItem) MarshalJSON() ([]byte, error) { + if s.Text != nil { + return core.MarshalJSONWithExtraProperty(s.Text, "type", "text") } - return nil, fmt.Errorf("type %T does not include a non-empty union type", r) + return nil, fmt.Errorf("type %T does not define a non-empty union type", s) } -type RerankRequestDocumentsItemVisitor interface { - VisitString(string) error - VisitRerankDocument(RerankDocument) error +type SystemMessageContentItemVisitor interface { + VisitText(*TextContent) error } -func (r *RerankRequestDocumentsItem) Accept(visitor RerankRequestDocumentsItemVisitor) error { - if r.String != "" { - return visitor.VisitString(r.String) - } - if r.RerankDocument != nil { - return visitor.VisitRerankDocument(r.RerankDocument) +func (s *SystemMessageContentItem) Accept(visitor SystemMessageContentItemVisitor) error { + if s.Text != nil { + return visitor.VisitText(s.Text) } - return fmt.Errorf("type %T does not include a non-empty union type", r) + return fmt.Errorf("type %T does not define a non-empty union type", s) } -type RerankResponse struct { - Id *string `json:"id,omitempty" url:"id,omitempty"` - // An ordered list of ranked documents - Results []*RerankResponseResultsItem `json:"results,omitempty" url:"results,omitempty"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +// Text content of the message. +type TextContent struct { + Text string `json:"text" url:"text"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (r *RerankResponse) GetExtraProperties() map[string]interface{} { - return r.extraProperties +func (t *TextContent) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -func (r *RerankResponse) UnmarshalJSON(data []byte) error { - type unmarshaler RerankResponse +func (t *TextContent) UnmarshalJSON(data []byte) error { + type unmarshaler TextContent var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *r = RerankResponse(value) + *t = TextContent(value) - extraProperties, err := core.ExtractExtraProperties(data, *r) + extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { return err } - r.extraProperties = extraProperties + t.extraProperties = extraProperties - r._rawJSON = json.RawMessage(data) + t._rawJSON = json.RawMessage(data) return nil } -func (r *RerankResponse) String() string { - if len(r._rawJSON) > 0 { - if value, err := core.StringifyJSON(r._rawJSON); err == nil { +func (t *TextContent) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(r); err == nil { + if value, err := core.StringifyJSON(t); err == nil { return value } - return fmt.Sprintf("%#v", r) + return fmt.Sprintf("%#v", t) } -type RerankResponseResultsItem struct { - // If `return_documents` is set as `false` this will return none, if `true` it will return the documents passed in - Document *RerankResponseResultsItemDocument `json:"document,omitempty" url:"document,omitempty"` - // Corresponds to the index in the original list of documents to which the ranked document belongs. (i.e. if the first value in the `results` object has an `index` value of 3, it means in the list of documents passed in, the document at `index=3` had the highest relevance) - Index int `json:"index" url:"index"` - // Relevance scores are normalized to be in the range `[0, 1]`. Scores close to `1` indicate a high relevance to the query, and scores closer to `0` indicate low relevance. It is not accurate to assume a score of 0.9 means the document is 2x more relevant than a document with a score of 0.45 - RelevanceScore float64 `json:"relevance_score" url:"relevance_score"` - +type TextResponseFormat struct { extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (r *RerankResponseResultsItem) GetExtraProperties() map[string]interface{} { - return r.extraProperties +func (t *TextResponseFormat) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -func (r *RerankResponseResultsItem) UnmarshalJSON(data []byte) error { - type unmarshaler RerankResponseResultsItem +func (t *TextResponseFormat) UnmarshalJSON(data []byte) error { + type unmarshaler TextResponseFormat var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *r = RerankResponseResultsItem(value) + *t = TextResponseFormat(value) - extraProperties, err := core.ExtractExtraProperties(data, *r) + extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { return err } - r.extraProperties = extraProperties + t.extraProperties = extraProperties - r._rawJSON = json.RawMessage(data) + t._rawJSON = json.RawMessage(data) return nil } -func (r *RerankResponseResultsItem) String() string { - if len(r._rawJSON) > 0 { - if value, err := core.StringifyJSON(r._rawJSON); err == nil { +func (t *TextResponseFormat) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(r); err == nil { + if value, err := core.StringifyJSON(t); err == nil { return value } - return fmt.Sprintf("%#v", r) + return fmt.Sprintf("%#v", t) } -// If `return_documents` is set as `false` this will return none, if `true` it will return the documents passed in -type RerankResponseResultsItemDocument struct { - // The text of the document to rerank - Text string `json:"text" url:"text"` - +type TextResponseFormatV2 struct { extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (r *RerankResponseResultsItemDocument) GetExtraProperties() map[string]interface{} { - return r.extraProperties +func (t *TextResponseFormatV2) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -func (r *RerankResponseResultsItemDocument) UnmarshalJSON(data []byte) error { - type unmarshaler RerankResponseResultsItemDocument +func (t *TextResponseFormatV2) UnmarshalJSON(data []byte) error { + type unmarshaler TextResponseFormatV2 var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *r = RerankResponseResultsItemDocument(value) + *t = TextResponseFormatV2(value) - extraProperties, err := core.ExtractExtraProperties(data, *r) + extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { return err } - r.extraProperties = extraProperties + t.extraProperties = extraProperties - r._rawJSON = json.RawMessage(data) + t._rawJSON = json.RawMessage(data) return nil } -func (r *RerankResponseResultsItemDocument) String() string { - if len(r._rawJSON) > 0 { - if value, err := core.StringifyJSON(r._rawJSON); err == nil { +func (t *TextResponseFormatV2) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(r); err == nil { + if value, err := core.StringifyJSON(t); err == nil { return value } - return fmt.Sprintf("%#v", r) + return fmt.Sprintf("%#v", t) } -type RerankerDataMetrics struct { - // The number of training queries. - NumTrainQueries *int64 `json:"num_train_queries,omitempty" url:"num_train_queries,omitempty"` - // The sum of all relevant passages of valid training examples. - NumTrainRelevantPassages *int64 `json:"num_train_relevant_passages,omitempty" url:"num_train_relevant_passages,omitempty"` - // The sum of all hard negatives of valid training examples. - NumTrainHardNegatives *int64 `json:"num_train_hard_negatives,omitempty" url:"num_train_hard_negatives,omitempty"` - // The number of evaluation queries. - NumEvalQueries *int64 `json:"num_eval_queries,omitempty" url:"num_eval_queries,omitempty"` - // The sum of all relevant passages of valid eval examples. - NumEvalRelevantPassages *int64 `json:"num_eval_relevant_passages,omitempty" url:"num_eval_relevant_passages,omitempty"` - // The sum of all hard negatives of valid eval examples. - NumEvalHardNegatives *int64 `json:"num_eval_hard_negatives,omitempty" url:"num_eval_hard_negatives,omitempty"` +type TokenizeResponse struct { + // An array of tokens, where each token is an integer. + Tokens []int `json:"tokens,omitempty" url:"tokens,omitempty"` + TokenStrings []string `json:"token_strings,omitempty" url:"token_strings,omitempty"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (r *RerankerDataMetrics) GetExtraProperties() map[string]interface{} { - return r.extraProperties +func (t *TokenizeResponse) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -func (r *RerankerDataMetrics) UnmarshalJSON(data []byte) error { - type unmarshaler RerankerDataMetrics +func (t *TokenizeResponse) UnmarshalJSON(data []byte) error { + type unmarshaler TokenizeResponse var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *r = RerankerDataMetrics(value) + *t = TokenizeResponse(value) - extraProperties, err := core.ExtractExtraProperties(data, *r) + extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { return err } - r.extraProperties = extraProperties + t.extraProperties = extraProperties - r._rawJSON = json.RawMessage(data) + t._rawJSON = json.RawMessage(data) return nil } -func (r *RerankerDataMetrics) String() string { - if len(r._rawJSON) > 0 { - if value, err := core.StringifyJSON(r._rawJSON); err == nil { +func (t *TokenizeResponse) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(r); err == nil { + if value, err := core.StringifyJSON(t); err == nil { return value } - return fmt.Sprintf("%#v", r) + return fmt.Sprintf("%#v", t) } -// Configuration for forcing the model output to adhere to the specified format. Supported on [Command R 03-2024](https://docs.cohere.com/docs/command-r), [Command R+ 04-2024](https://docs.cohere.com/docs/command-r-plus) and newer models. -// -// The model can be forced into outputting JSON objects (with up to 5 levels of nesting) by setting `{ "type": "json_object" }`. -// -// A [JSON Schema](https://json-schema.org/) can optionally be provided, to ensure a specific structure. -// -// **Note**: When using `{ "type": "json_object" }` your `message` should always explicitly instruct the model to generate a JSON (eg: _"Generate a JSON ..."_) . Otherwise the model may end up getting stuck generating an infinite stream of characters and eventually run out of context length. -// **Limitation**: The parameter is not supported in RAG mode (when any of `connectors`, `documents`, `tools`, `tool_results` are provided). -type ResponseFormat struct { - Type string - Text *TextResponseFormat - JsonObject *JsonResponseFormat +type TooManyRequestsErrorBody struct { + Data *string `json:"data,omitempty" url:"data,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage } -func (r *ResponseFormat) UnmarshalJSON(data []byte) error { - var unmarshaler struct { - Type string `json:"type"` - } - if err := json.Unmarshal(data, &unmarshaler); err != nil { - return err - } - r.Type = unmarshaler.Type - switch unmarshaler.Type { - case "text": - value := new(TextResponseFormat) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - r.Text = value - case "json_object": - value := new(JsonResponseFormat) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - r.JsonObject = value - } - return nil +func (t *TooManyRequestsErrorBody) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -func (r ResponseFormat) MarshalJSON() ([]byte, error) { - if r.Text != nil { - return core.MarshalJSONWithExtraProperty(r.Text, "type", "text") +func (t *TooManyRequestsErrorBody) UnmarshalJSON(data []byte) error { + type unmarshaler TooManyRequestsErrorBody + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err } - if r.JsonObject != nil { - return core.MarshalJSONWithExtraProperty(r.JsonObject, "type", "json_object") + *t = TooManyRequestsErrorBody(value) + + extraProperties, err := core.ExtractExtraProperties(data, *t) + if err != nil { + return err } - return nil, fmt.Errorf("type %T does not define a non-empty union type", r) -} + t.extraProperties = extraProperties -type ResponseFormatVisitor interface { - VisitText(*TextResponseFormat) error - VisitJsonObject(*JsonResponseFormat) error + t._rawJSON = json.RawMessage(data) + return nil } -func (r *ResponseFormat) Accept(visitor ResponseFormatVisitor) error { - if r.Text != nil { - return visitor.VisitText(r.Text) +func (t *TooManyRequestsErrorBody) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { + return value + } } - if r.JsonObject != nil { - return visitor.VisitJsonObject(r.JsonObject) + if value, err := core.StringifyJSON(t); err == nil { + return value } - return fmt.Errorf("type %T does not define a non-empty union type", r) + return fmt.Sprintf("%#v", t) } -type SingleGeneration struct { - Id string `json:"id" url:"id"` - Text string `json:"text" url:"text"` - // Refers to the nth generation. Only present when `num_generations` is greater than zero. - Index *int `json:"index,omitempty" url:"index,omitempty"` - Likelihood *float64 `json:"likelihood,omitempty" url:"likelihood,omitempty"` - // Only returned if `return_likelihoods` is set to `GENERATION` or `ALL`. The likelihood refers to the average log-likelihood of the entire specified string, which is useful for [evaluating the performance of your model](likelihood-eval), especially if you've created a [custom model](/docs/training-custom-models). Individual token likelihoods provide the log-likelihood of each token. The first token will not have a likelihood. - TokenLikelihoods []*SingleGenerationTokenLikelihoodsItem `json:"token_likelihoods,omitempty" url:"token_likelihoods,omitempty"` +type Tool struct { + // The name of the tool to be called. Valid names contain only the characters `a-z`, `A-Z`, `0-9`, `_` and must not begin with a digit. + Name string `json:"name" url:"name"` + // The description of what the tool does, the model uses the description to choose when and how to call the function. + Description string `json:"description" url:"description"` + // The input parameters of the tool. Accepts a dictionary where the key is the name of the parameter and the value is the parameter spec. Valid parameter names contain only the characters `a-z`, `A-Z`, `0-9`, `_` and must not begin with a digit. + // + // ``` + // + // { + // "my_param": { + // "description": , + // "type": , // any python data type, such as 'str', 'bool' + // "required": + // } + // } + // + // ``` + ParameterDefinitions map[string]*ToolParameterDefinitionsValue `json:"parameter_definitions,omitempty" url:"parameter_definitions,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (s *SingleGeneration) GetExtraProperties() map[string]interface{} { - return s.extraProperties +func (t *Tool) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -func (s *SingleGeneration) UnmarshalJSON(data []byte) error { - type unmarshaler SingleGeneration +func (t *Tool) UnmarshalJSON(data []byte) error { + type unmarshaler Tool var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *s = SingleGeneration(value) + *t = Tool(value) - extraProperties, err := core.ExtractExtraProperties(data, *s) + extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { return err } - s.extraProperties = extraProperties + t.extraProperties = extraProperties - s._rawJSON = json.RawMessage(data) + t._rawJSON = json.RawMessage(data) return nil } -func (s *SingleGeneration) String() string { - if len(s._rawJSON) > 0 { - if value, err := core.StringifyJSON(s._rawJSON); err == nil { +func (t *Tool) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(s); err == nil { + if value, err := core.StringifyJSON(t); err == nil { return value } - return fmt.Sprintf("%#v", s) + return fmt.Sprintf("%#v", t) } -type SingleGenerationInStream struct { - Id string `json:"id" url:"id"` - // Full text of the generation. - Text string `json:"text" url:"text"` - // Refers to the nth generation. Only present when `num_generations` is greater than zero. - Index *int `json:"index,omitempty" url:"index,omitempty"` - FinishReason FinishReason `json:"finish_reason" url:"finish_reason"` +// Contains the tool calls generated by the model. Use it to invoke your tools. +type ToolCall struct { + // Name of the tool to call. + Name string `json:"name" url:"name"` + // The name and value of the parameters to use when invoking a tool. + Parameters map[string]interface{} `json:"parameters,omitempty" url:"parameters,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (s *SingleGenerationInStream) GetExtraProperties() map[string]interface{} { - return s.extraProperties +func (t *ToolCall) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -func (s *SingleGenerationInStream) UnmarshalJSON(data []byte) error { - type unmarshaler SingleGenerationInStream +func (t *ToolCall) UnmarshalJSON(data []byte) error { + type unmarshaler ToolCall var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *s = SingleGenerationInStream(value) + *t = ToolCall(value) - extraProperties, err := core.ExtractExtraProperties(data, *s) + extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { return err } - s.extraProperties = extraProperties + t.extraProperties = extraProperties - s._rawJSON = json.RawMessage(data) + t._rawJSON = json.RawMessage(data) return nil } -func (s *SingleGenerationInStream) String() string { - if len(s._rawJSON) > 0 { - if value, err := core.StringifyJSON(s._rawJSON); err == nil { +func (t *ToolCall) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(s); err == nil { + if value, err := core.StringifyJSON(t); err == nil { return value } - return fmt.Sprintf("%#v", s) + return fmt.Sprintf("%#v", t) } -type SingleGenerationTokenLikelihoodsItem struct { - Token string `json:"token" url:"token"` - Likelihood float64 `json:"likelihood" url:"likelihood"` +// Contains the chunk of the tool call generation in the stream. +type ToolCallDelta struct { + // Name of the tool call + Name *string `json:"name,omitempty" url:"name,omitempty"` + // Index of the tool call generated + Index *float64 `json:"index,omitempty" url:"index,omitempty"` + // Chunk of the tool parameters + Parameters *string `json:"parameters,omitempty" url:"parameters,omitempty"` + // Chunk of the tool plan text + Text *string `json:"text,omitempty" url:"text,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (s *SingleGenerationTokenLikelihoodsItem) GetExtraProperties() map[string]interface{} { - return s.extraProperties +func (t *ToolCallDelta) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -func (s *SingleGenerationTokenLikelihoodsItem) UnmarshalJSON(data []byte) error { - type unmarshaler SingleGenerationTokenLikelihoodsItem +func (t *ToolCallDelta) UnmarshalJSON(data []byte) error { + type unmarshaler ToolCallDelta var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *s = SingleGenerationTokenLikelihoodsItem(value) + *t = ToolCallDelta(value) - extraProperties, err := core.ExtractExtraProperties(data, *s) + extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { return err } - s.extraProperties = extraProperties + t.extraProperties = extraProperties - s._rawJSON = json.RawMessage(data) + t._rawJSON = json.RawMessage(data) return nil } -func (s *SingleGenerationTokenLikelihoodsItem) String() string { - if len(s._rawJSON) > 0 { - if value, err := core.StringifyJSON(s._rawJSON); err == nil { +func (t *ToolCallDelta) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(s); err == nil { + if value, err := core.StringifyJSON(t); err == nil { return value } - return fmt.Sprintf("%#v", s) + return fmt.Sprintf("%#v", t) } -// StreamedChatResponse is returned in streaming mode (specified with `stream=True` in the request). -type StreamedChatResponse struct { - EventType string - StreamStart *ChatStreamStartEvent - SearchQueriesGeneration *ChatSearchQueriesGenerationEvent - SearchResults *ChatSearchResultsEvent - TextGeneration *ChatTextGenerationEvent - CitationGeneration *ChatCitationGenerationEvent - ToolCallsGeneration *ChatToolCallsGenerationEvent - StreamEnd *ChatStreamEndEvent - ToolCallsChunk *ChatToolCallsChunkEvent +// An array of tool calls to be made. +type ToolCallV2 struct { + Id *string `json:"id,omitempty" url:"id,omitempty"` + Type *string `json:"type,omitempty" url:"type,omitempty"` + Function *ToolCallV2Function `json:"function,omitempty" url:"function,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage } -func (s *StreamedChatResponse) UnmarshalJSON(data []byte) error { - var unmarshaler struct { - EventType string `json:"event_type"` - } - if err := json.Unmarshal(data, &unmarshaler); err != nil { - return err - } - s.EventType = unmarshaler.EventType - switch unmarshaler.EventType { - case "stream-start": - value := new(ChatStreamStartEvent) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - s.StreamStart = value - case "search-queries-generation": - value := new(ChatSearchQueriesGenerationEvent) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - s.SearchQueriesGeneration = value - case "search-results": - value := new(ChatSearchResultsEvent) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - s.SearchResults = value - case "text-generation": - value := new(ChatTextGenerationEvent) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - s.TextGeneration = value - case "citation-generation": - value := new(ChatCitationGenerationEvent) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - s.CitationGeneration = value - case "tool-calls-generation": - value := new(ChatToolCallsGenerationEvent) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - s.ToolCallsGeneration = value - case "stream-end": - value := new(ChatStreamEndEvent) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - s.StreamEnd = value - case "tool-calls-chunk": - value := new(ChatToolCallsChunkEvent) - if err := json.Unmarshal(data, &value); err != nil { - return err - } - s.ToolCallsChunk = value - } - return nil +func (t *ToolCallV2) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -func (s StreamedChatResponse) MarshalJSON() ([]byte, error) { - if s.StreamStart != nil { - return core.MarshalJSONWithExtraProperty(s.StreamStart, "event_type", "stream-start") - } - if s.SearchQueriesGeneration != nil { - return core.MarshalJSONWithExtraProperty(s.SearchQueriesGeneration, "event_type", "search-queries-generation") - } - if s.SearchResults != nil { - return core.MarshalJSONWithExtraProperty(s.SearchResults, "event_type", "search-results") - } - if s.TextGeneration != nil { - return core.MarshalJSONWithExtraProperty(s.TextGeneration, "event_type", "text-generation") - } - if s.CitationGeneration != nil { - return core.MarshalJSONWithExtraProperty(s.CitationGeneration, "event_type", "citation-generation") - } - if s.ToolCallsGeneration != nil { - return core.MarshalJSONWithExtraProperty(s.ToolCallsGeneration, "event_type", "tool-calls-generation") - } - if s.StreamEnd != nil { - return core.MarshalJSONWithExtraProperty(s.StreamEnd, "event_type", "stream-end") +func (t *ToolCallV2) UnmarshalJSON(data []byte) error { + type unmarshaler ToolCallV2 + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err } - if s.ToolCallsChunk != nil { - return core.MarshalJSONWithExtraProperty(s.ToolCallsChunk, "event_type", "tool-calls-chunk") + *t = ToolCallV2(value) + + extraProperties, err := core.ExtractExtraProperties(data, *t) + if err != nil { + return err } - return nil, fmt.Errorf("type %T does not define a non-empty union type", s) -} + t.extraProperties = extraProperties -type StreamedChatResponseVisitor interface { - VisitStreamStart(*ChatStreamStartEvent) error - VisitSearchQueriesGeneration(*ChatSearchQueriesGenerationEvent) error - VisitSearchResults(*ChatSearchResultsEvent) error - VisitTextGeneration(*ChatTextGenerationEvent) error - VisitCitationGeneration(*ChatCitationGenerationEvent) error - VisitToolCallsGeneration(*ChatToolCallsGenerationEvent) error - VisitStreamEnd(*ChatStreamEndEvent) error - VisitToolCallsChunk(*ChatToolCallsChunkEvent) error + t._rawJSON = json.RawMessage(data) + return nil } -func (s *StreamedChatResponse) Accept(visitor StreamedChatResponseVisitor) error { - if s.StreamStart != nil { - return visitor.VisitStreamStart(s.StreamStart) - } - if s.SearchQueriesGeneration != nil { - return visitor.VisitSearchQueriesGeneration(s.SearchQueriesGeneration) - } - if s.SearchResults != nil { - return visitor.VisitSearchResults(s.SearchResults) - } - if s.TextGeneration != nil { - return visitor.VisitTextGeneration(s.TextGeneration) - } - if s.CitationGeneration != nil { - return visitor.VisitCitationGeneration(s.CitationGeneration) - } - if s.ToolCallsGeneration != nil { - return visitor.VisitToolCallsGeneration(s.ToolCallsGeneration) - } - if s.StreamEnd != nil { - return visitor.VisitStreamEnd(s.StreamEnd) +func (t *ToolCallV2) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { + return value + } } - if s.ToolCallsChunk != nil { - return visitor.VisitToolCallsChunk(s.ToolCallsChunk) + if value, err := core.StringifyJSON(t); err == nil { + return value } - return fmt.Errorf("type %T does not define a non-empty union type", s) + return fmt.Sprintf("%#v", t) } -// One of `low`, `medium`, `high`, or `auto`, defaults to `auto`. Controls how close to the original text the summary is. `high` extractiveness summaries will lean towards reusing sentences verbatim, while `low` extractiveness summaries will tend to paraphrase more. If `auto` is selected, the best option will be picked based on the input text. -type SummarizeRequestExtractiveness string - -const ( - SummarizeRequestExtractivenessLow SummarizeRequestExtractiveness = "low" - SummarizeRequestExtractivenessMedium SummarizeRequestExtractiveness = "medium" - SummarizeRequestExtractivenessHigh SummarizeRequestExtractiveness = "high" -) +type ToolCallV2Function struct { + Name *string `json:"name,omitempty" url:"name,omitempty"` + Arguments *string `json:"arguments,omitempty" url:"arguments,omitempty"` -func NewSummarizeRequestExtractivenessFromString(s string) (SummarizeRequestExtractiveness, error) { - switch s { - case "low": - return SummarizeRequestExtractivenessLow, nil - case "medium": - return SummarizeRequestExtractivenessMedium, nil - case "high": - return SummarizeRequestExtractivenessHigh, nil - } - var t SummarizeRequestExtractiveness - return "", fmt.Errorf("%s is not a valid %T", s, t) + extraProperties map[string]interface{} + _rawJSON json.RawMessage } -func (s SummarizeRequestExtractiveness) Ptr() *SummarizeRequestExtractiveness { - return &s +func (t *ToolCallV2Function) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -// One of `paragraph`, `bullets`, or `auto`, defaults to `auto`. Indicates the style in which the summary will be delivered - in a free form paragraph or in bullet points. If `auto` is selected, the best option will be picked based on the input text. -type SummarizeRequestFormat string - -const ( - SummarizeRequestFormatParagraph SummarizeRequestFormat = "paragraph" - SummarizeRequestFormatBullets SummarizeRequestFormat = "bullets" -) +func (t *ToolCallV2Function) UnmarshalJSON(data []byte) error { + type unmarshaler ToolCallV2Function + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *t = ToolCallV2Function(value) -func NewSummarizeRequestFormatFromString(s string) (SummarizeRequestFormat, error) { - switch s { - case "paragraph": - return SummarizeRequestFormatParagraph, nil - case "bullets": - return SummarizeRequestFormatBullets, nil + extraProperties, err := core.ExtractExtraProperties(data, *t) + if err != nil { + return err } - var t SummarizeRequestFormat - return "", fmt.Errorf("%s is not a valid %T", s, t) + t.extraProperties = extraProperties + + t._rawJSON = json.RawMessage(data) + return nil } -func (s SummarizeRequestFormat) Ptr() *SummarizeRequestFormat { - return &s +func (t *ToolCallV2Function) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(t); err == nil { + return value + } + return fmt.Sprintf("%#v", t) } -// One of `short`, `medium`, `long`, or `auto` defaults to `auto`. Indicates the approximate length of the summary. If `auto` is selected, the best option will be picked based on the input text. -type SummarizeRequestLength string +// A content block which contains information about the content of a tool result +type ToolContent struct { + Type string + Text *TextContent + Document *DocumentContent +} -const ( - SummarizeRequestLengthShort SummarizeRequestLength = "short" - SummarizeRequestLengthMedium SummarizeRequestLength = "medium" - SummarizeRequestLengthLong SummarizeRequestLength = "long" -) +func (t *ToolContent) UnmarshalJSON(data []byte) error { + var unmarshaler struct { + Type string `json:"type"` + } + if err := json.Unmarshal(data, &unmarshaler); err != nil { + return err + } + t.Type = unmarshaler.Type + if unmarshaler.Type == "" { + return fmt.Errorf("%T did not include discriminant type", t) + } + switch unmarshaler.Type { + case "text": + value := new(TextContent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + t.Text = value + case "document": + value := new(DocumentContent) + if err := json.Unmarshal(data, &value); err != nil { + return err + } + t.Document = value + } + return nil +} -func NewSummarizeRequestLengthFromString(s string) (SummarizeRequestLength, error) { - switch s { - case "short": - return SummarizeRequestLengthShort, nil - case "medium": - return SummarizeRequestLengthMedium, nil - case "long": - return SummarizeRequestLengthLong, nil +func (t ToolContent) MarshalJSON() ([]byte, error) { + if t.Text != nil { + return core.MarshalJSONWithExtraProperty(t.Text, "type", "text") } - var t SummarizeRequestLength - return "", fmt.Errorf("%s is not a valid %T", s, t) + if t.Document != nil { + return core.MarshalJSONWithExtraProperty(t.Document, "type", "document") + } + return nil, fmt.Errorf("type %T does not define a non-empty union type", t) } -func (s SummarizeRequestLength) Ptr() *SummarizeRequestLength { - return &s +type ToolContentVisitor interface { + VisitText(*TextContent) error + VisitDocument(*DocumentContent) error } -type SummarizeResponse struct { - // Generated ID for the summary - Id *string `json:"id,omitempty" url:"id,omitempty"` - // Generated summary for the text - Summary *string `json:"summary,omitempty" url:"summary,omitempty"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +func (t *ToolContent) Accept(visitor ToolContentVisitor) error { + if t.Text != nil { + return visitor.VisitText(t.Text) + } + if t.Document != nil { + return visitor.VisitDocument(t.Document) + } + return fmt.Errorf("type %T does not define a non-empty union type", t) +} + +// Represents tool result in the chat history. +type ToolMessage struct { + ToolResults []*ToolResult `json:"tool_results,omitempty" url:"tool_results,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (s *SummarizeResponse) GetExtraProperties() map[string]interface{} { - return s.extraProperties +func (t *ToolMessage) GetExtraProperties() map[string]interface{} { + return t.extraProperties } -func (s *SummarizeResponse) UnmarshalJSON(data []byte) error { - type unmarshaler SummarizeResponse +func (t *ToolMessage) UnmarshalJSON(data []byte) error { + type unmarshaler ToolMessage var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *s = SummarizeResponse(value) + *t = ToolMessage(value) - extraProperties, err := core.ExtractExtraProperties(data, *s) + extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { return err } - s.extraProperties = extraProperties + t.extraProperties = extraProperties - s._rawJSON = json.RawMessage(data) + t._rawJSON = json.RawMessage(data) return nil } -func (s *SummarizeResponse) String() string { - if len(s._rawJSON) > 0 { - if value, err := core.StringifyJSON(s._rawJSON); err == nil { +func (t *ToolMessage) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(s); err == nil { + if value, err := core.StringifyJSON(t); err == nil { return value } - return fmt.Sprintf("%#v", s) + return fmt.Sprintf("%#v", t) } -type TextResponseFormat struct { +// A message from the system. +type ToolMessageV2 struct { + // The id of the associated tool call that has provided the given content + ToolCallId string `json:"tool_call_id" url:"tool_call_id"` + // A single or list of outputs from a tool. The content should formatted as a JSON object string, or a list of tool content blocks + Content *ToolMessageV2Content `json:"content,omitempty" url:"content,omitempty"` + extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (t *TextResponseFormat) GetExtraProperties() map[string]interface{} { +func (t *ToolMessageV2) GetExtraProperties() map[string]interface{} { return t.extraProperties } -func (t *TextResponseFormat) UnmarshalJSON(data []byte) error { - type unmarshaler TextResponseFormat +func (t *ToolMessageV2) UnmarshalJSON(data []byte) error { + type unmarshaler ToolMessageV2 var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *t = TextResponseFormat(value) + *t = ToolMessageV2(value) extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { @@ -5473,7 +8539,7 @@ func (t *TextResponseFormat) UnmarshalJSON(data []byte) error { return nil } -func (t *TextResponseFormat) String() string { +func (t *ToolMessageV2) String() string { if len(t._rawJSON) > 0 { if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value @@ -5485,27 +8551,74 @@ func (t *TextResponseFormat) String() string { return fmt.Sprintf("%#v", t) } -type TokenizeResponse struct { - // An array of tokens, where each token is an integer. - Tokens []int `json:"tokens,omitempty" url:"tokens,omitempty"` - TokenStrings []string `json:"token_strings,omitempty" url:"token_strings,omitempty"` - Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` +// A single or list of outputs from a tool. The content should formatted as a JSON object string, or a list of tool content blocks +type ToolMessageV2Content struct { + String string + ToolContentList []*ToolContent +} + +func (t *ToolMessageV2Content) UnmarshalJSON(data []byte) error { + var valueString string + if err := json.Unmarshal(data, &valueString); err == nil { + t.String = valueString + return nil + } + var valueToolContentList []*ToolContent + if err := json.Unmarshal(data, &valueToolContentList); err == nil { + t.ToolContentList = valueToolContentList + return nil + } + return fmt.Errorf("%s cannot be deserialized as a %T", data, t) +} + +func (t ToolMessageV2Content) MarshalJSON() ([]byte, error) { + if t.String != "" { + return json.Marshal(t.String) + } + if t.ToolContentList != nil { + return json.Marshal(t.ToolContentList) + } + return nil, fmt.Errorf("type %T does not include a non-empty union type", t) +} + +type ToolMessageV2ContentVisitor interface { + VisitString(string) error + VisitToolContentList([]*ToolContent) error +} + +func (t *ToolMessageV2Content) Accept(visitor ToolMessageV2ContentVisitor) error { + if t.String != "" { + return visitor.VisitString(t.String) + } + if t.ToolContentList != nil { + return visitor.VisitToolContentList(t.ToolContentList) + } + return fmt.Errorf("type %T does not include a non-empty union type", t) +} + +type ToolParameterDefinitionsValue struct { + // The description of the parameter. + Description *string `json:"description,omitempty" url:"description,omitempty"` + // The type of the parameter. Must be a valid Python type. + Type string `json:"type" url:"type"` + // Denotes whether the parameter is always present (required) or not. Defaults to not required. + Required *bool `json:"required,omitempty" url:"required,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (t *TokenizeResponse) GetExtraProperties() map[string]interface{} { +func (t *ToolParameterDefinitionsValue) GetExtraProperties() map[string]interface{} { return t.extraProperties } -func (t *TokenizeResponse) UnmarshalJSON(data []byte) error { - type unmarshaler TokenizeResponse +func (t *ToolParameterDefinitionsValue) UnmarshalJSON(data []byte) error { + type unmarshaler ToolParameterDefinitionsValue var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *t = TokenizeResponse(value) + *t = ToolParameterDefinitionsValue(value) extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { @@ -5517,7 +8630,7 @@ func (t *TokenizeResponse) UnmarshalJSON(data []byte) error { return nil } -func (t *TokenizeResponse) String() string { +func (t *ToolParameterDefinitionsValue) String() string { if len(t._rawJSON) > 0 { if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value @@ -5529,24 +8642,25 @@ func (t *TokenizeResponse) String() string { return fmt.Sprintf("%#v", t) } -type TooManyRequestsErrorBody struct { - Data *string `json:"data,omitempty" url:"data,omitempty"` +type ToolResult struct { + Call *ToolCall `json:"call,omitempty" url:"call,omitempty"` + Outputs []map[string]interface{} `json:"outputs,omitempty" url:"outputs,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (t *TooManyRequestsErrorBody) GetExtraProperties() map[string]interface{} { +func (t *ToolResult) GetExtraProperties() map[string]interface{} { return t.extraProperties } -func (t *TooManyRequestsErrorBody) UnmarshalJSON(data []byte) error { - type unmarshaler TooManyRequestsErrorBody +func (t *ToolResult) UnmarshalJSON(data []byte) error { + type unmarshaler ToolResult var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *t = TooManyRequestsErrorBody(value) + *t = ToolResult(value) extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { @@ -5558,7 +8672,7 @@ func (t *TooManyRequestsErrorBody) UnmarshalJSON(data []byte) error { return nil } -func (t *TooManyRequestsErrorBody) String() string { +func (t *ToolResult) String() string { if len(t._rawJSON) > 0 { if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value @@ -5570,41 +8684,69 @@ func (t *TooManyRequestsErrorBody) String() string { return fmt.Sprintf("%#v", t) } -type Tool struct { - // The name of the tool to be called. Valid names contain only the characters `a-z`, `A-Z`, `0-9`, `_` and must not begin with a digit. - Name string `json:"name" url:"name"` - // The description of what the tool does, the model uses the description to choose when and how to call the function. - Description string `json:"description" url:"description"` - // The input parameters of the tool. Accepts a dictionary where the key is the name of the parameter and the value is the parameter spec. Valid parameter names contain only the characters `a-z`, `A-Z`, `0-9`, `_` and must not begin with a digit. - // - // ``` - // - // { - // "my_param": { - // "description": , - // "type": , // any python data type, such as 'str', 'bool' - // "required": - // } - // } - // - // ``` - ParameterDefinitions map[string]*ToolParameterDefinitionsValue `json:"parameter_definitions,omitempty" url:"parameter_definitions,omitempty"` +type ToolSource struct { + // The unique identifier of the document + Id *string `json:"id,omitempty" url:"id,omitempty"` + ToolOutput map[string]interface{} `json:"tool_output,omitempty" url:"tool_output,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (t *ToolSource) GetExtraProperties() map[string]interface{} { + return t.extraProperties +} + +func (t *ToolSource) UnmarshalJSON(data []byte) error { + type unmarshaler ToolSource + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *t = ToolSource(value) + + extraProperties, err := core.ExtractExtraProperties(data, *t) + if err != nil { + return err + } + t.extraProperties = extraProperties + + t._rawJSON = json.RawMessage(data) + return nil +} + +func (t *ToolSource) String() string { + if len(t._rawJSON) > 0 { + if value, err := core.StringifyJSON(t._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(t); err == nil { + return value + } + return fmt.Sprintf("%#v", t) +} + +type ToolV2 struct { + Type *string `json:"type,omitempty" url:"type,omitempty"` + // The function to be executed. + Function *ToolV2Function `json:"function,omitempty" url:"function,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (t *Tool) GetExtraProperties() map[string]interface{} { +func (t *ToolV2) GetExtraProperties() map[string]interface{} { return t.extraProperties } -func (t *Tool) UnmarshalJSON(data []byte) error { - type unmarshaler Tool +func (t *ToolV2) UnmarshalJSON(data []byte) error { + type unmarshaler ToolV2 var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *t = Tool(value) + *t = ToolV2(value) extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { @@ -5616,7 +8758,7 @@ func (t *Tool) UnmarshalJSON(data []byte) error { return nil } -func (t *Tool) String() string { +func (t *ToolV2) String() string { if len(t._rawJSON) > 0 { if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value @@ -5628,28 +8770,30 @@ func (t *Tool) String() string { return fmt.Sprintf("%#v", t) } -// Contains the tool calls generated by the model. Use it to invoke your tools. -type ToolCall struct { - // Name of the tool to call. - Name string `json:"name" url:"name"` - // The name and value of the parameters to use when invoking a tool. +// The function to be executed. +type ToolV2Function struct { + // The name of the function. + Name *string `json:"name,omitempty" url:"name,omitempty"` + // The description of the function. + Description *string `json:"description,omitempty" url:"description,omitempty"` + // The parameters of the function as a JSON schema. Parameters map[string]interface{} `json:"parameters,omitempty" url:"parameters,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (t *ToolCall) GetExtraProperties() map[string]interface{} { +func (t *ToolV2Function) GetExtraProperties() map[string]interface{} { return t.extraProperties } -func (t *ToolCall) UnmarshalJSON(data []byte) error { - type unmarshaler ToolCall +func (t *ToolV2Function) UnmarshalJSON(data []byte) error { + type unmarshaler ToolV2Function var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *t = ToolCall(value) + *t = ToolV2Function(value) extraProperties, err := core.ExtractExtraProperties(data, *t) if err != nil { @@ -5661,7 +8805,7 @@ func (t *ToolCall) UnmarshalJSON(data []byte) error { return nil } -func (t *ToolCall) String() string { +func (t *ToolV2Function) String() string { if len(t._rawJSON) > 0 { if value, err := core.StringifyJSON(t._rawJSON); err == nil { return value @@ -5673,203 +8817,199 @@ func (t *ToolCall) String() string { return fmt.Sprintf("%#v", t) } -// Contains the chunk of the tool call generation in the stream. -type ToolCallDelta struct { - // Name of the tool call - Name *string `json:"name,omitempty" url:"name,omitempty"` - // Index of the tool call generated - Index *float64 `json:"index,omitempty" url:"index,omitempty"` - // Chunk of the tool parameters - Parameters *string `json:"parameters,omitempty" url:"parameters,omitempty"` - // Chunk of the tool plan text - Text *string `json:"text,omitempty" url:"text,omitempty"` +type UnprocessableEntityErrorBody struct { + Data *string `json:"data,omitempty" url:"data,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (t *ToolCallDelta) GetExtraProperties() map[string]interface{} { - return t.extraProperties +func (u *UnprocessableEntityErrorBody) GetExtraProperties() map[string]interface{} { + return u.extraProperties } -func (t *ToolCallDelta) UnmarshalJSON(data []byte) error { - type unmarshaler ToolCallDelta +func (u *UnprocessableEntityErrorBody) UnmarshalJSON(data []byte) error { + type unmarshaler UnprocessableEntityErrorBody var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *t = ToolCallDelta(value) + *u = UnprocessableEntityErrorBody(value) - extraProperties, err := core.ExtractExtraProperties(data, *t) + extraProperties, err := core.ExtractExtraProperties(data, *u) if err != nil { return err } - t.extraProperties = extraProperties + u.extraProperties = extraProperties - t._rawJSON = json.RawMessage(data) + u._rawJSON = json.RawMessage(data) return nil } -func (t *ToolCallDelta) String() string { - if len(t._rawJSON) > 0 { - if value, err := core.StringifyJSON(t._rawJSON); err == nil { +func (u *UnprocessableEntityErrorBody) String() string { + if len(u._rawJSON) > 0 { + if value, err := core.StringifyJSON(u._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(t); err == nil { + if value, err := core.StringifyJSON(u); err == nil { return value } - return fmt.Sprintf("%#v", t) + return fmt.Sprintf("%#v", u) } -// Represents tool result in the chat history. -type ToolMessage struct { - ToolResults []*ToolResult `json:"tool_results,omitempty" url:"tool_results,omitempty"` +type UpdateConnectorResponse struct { + Connector *Connector `json:"connector,omitempty" url:"connector,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (t *ToolMessage) GetExtraProperties() map[string]interface{} { - return t.extraProperties +func (u *UpdateConnectorResponse) GetExtraProperties() map[string]interface{} { + return u.extraProperties } -func (t *ToolMessage) UnmarshalJSON(data []byte) error { - type unmarshaler ToolMessage +func (u *UpdateConnectorResponse) UnmarshalJSON(data []byte) error { + type unmarshaler UpdateConnectorResponse var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *t = ToolMessage(value) + *u = UpdateConnectorResponse(value) - extraProperties, err := core.ExtractExtraProperties(data, *t) + extraProperties, err := core.ExtractExtraProperties(data, *u) if err != nil { return err } - t.extraProperties = extraProperties + u.extraProperties = extraProperties - t._rawJSON = json.RawMessage(data) + u._rawJSON = json.RawMessage(data) return nil } -func (t *ToolMessage) String() string { - if len(t._rawJSON) > 0 { - if value, err := core.StringifyJSON(t._rawJSON); err == nil { +func (u *UpdateConnectorResponse) String() string { + if len(u._rawJSON) > 0 { + if value, err := core.StringifyJSON(u._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(t); err == nil { + if value, err := core.StringifyJSON(u); err == nil { return value } - return fmt.Sprintf("%#v", t) + return fmt.Sprintf("%#v", u) } -type ToolParameterDefinitionsValue struct { - // The description of the parameter. - Description *string `json:"description,omitempty" url:"description,omitempty"` - // The type of the parameter. Must be a valid Python type. - Type string `json:"type" url:"type"` - // Denotes whether the parameter is always present (required) or not. Defaults to not required. - Required *bool `json:"required,omitempty" url:"required,omitempty"` +type Usage struct { + BilledUnits *UsageBilledUnits `json:"billed_units,omitempty" url:"billed_units,omitempty"` + Tokens *UsageTokens `json:"tokens,omitempty" url:"tokens,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (t *ToolParameterDefinitionsValue) GetExtraProperties() map[string]interface{} { - return t.extraProperties +func (u *Usage) GetExtraProperties() map[string]interface{} { + return u.extraProperties } -func (t *ToolParameterDefinitionsValue) UnmarshalJSON(data []byte) error { - type unmarshaler ToolParameterDefinitionsValue +func (u *Usage) UnmarshalJSON(data []byte) error { + type unmarshaler Usage var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *t = ToolParameterDefinitionsValue(value) + *u = Usage(value) - extraProperties, err := core.ExtractExtraProperties(data, *t) + extraProperties, err := core.ExtractExtraProperties(data, *u) if err != nil { return err } - t.extraProperties = extraProperties + u.extraProperties = extraProperties - t._rawJSON = json.RawMessage(data) + u._rawJSON = json.RawMessage(data) return nil } -func (t *ToolParameterDefinitionsValue) String() string { - if len(t._rawJSON) > 0 { - if value, err := core.StringifyJSON(t._rawJSON); err == nil { +func (u *Usage) String() string { + if len(u._rawJSON) > 0 { + if value, err := core.StringifyJSON(u._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(t); err == nil { + if value, err := core.StringifyJSON(u); err == nil { return value } - return fmt.Sprintf("%#v", t) + return fmt.Sprintf("%#v", u) } -type ToolResult struct { - Call *ToolCall `json:"call,omitempty" url:"call,omitempty"` - Outputs []map[string]interface{} `json:"outputs,omitempty" url:"outputs,omitempty"` +type UsageBilledUnits struct { + // The number of billed input tokens. + InputTokens *float64 `json:"input_tokens,omitempty" url:"input_tokens,omitempty"` + // The number of billed output tokens. + OutputTokens *float64 `json:"output_tokens,omitempty" url:"output_tokens,omitempty"` + // The number of billed search units. + SearchUnits *float64 `json:"search_units,omitempty" url:"search_units,omitempty"` + // The number of billed classifications units. + Classifications *float64 `json:"classifications,omitempty" url:"classifications,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (t *ToolResult) GetExtraProperties() map[string]interface{} { - return t.extraProperties +func (u *UsageBilledUnits) GetExtraProperties() map[string]interface{} { + return u.extraProperties } -func (t *ToolResult) UnmarshalJSON(data []byte) error { - type unmarshaler ToolResult +func (u *UsageBilledUnits) UnmarshalJSON(data []byte) error { + type unmarshaler UsageBilledUnits var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *t = ToolResult(value) + *u = UsageBilledUnits(value) - extraProperties, err := core.ExtractExtraProperties(data, *t) + extraProperties, err := core.ExtractExtraProperties(data, *u) if err != nil { return err } - t.extraProperties = extraProperties + u.extraProperties = extraProperties - t._rawJSON = json.RawMessage(data) + u._rawJSON = json.RawMessage(data) return nil } -func (t *ToolResult) String() string { - if len(t._rawJSON) > 0 { - if value, err := core.StringifyJSON(t._rawJSON); err == nil { +func (u *UsageBilledUnits) String() string { + if len(u._rawJSON) > 0 { + if value, err := core.StringifyJSON(u._rawJSON); err == nil { return value } } - if value, err := core.StringifyJSON(t); err == nil { + if value, err := core.StringifyJSON(u); err == nil { return value } - return fmt.Sprintf("%#v", t) + return fmt.Sprintf("%#v", u) } -type UnprocessableEntityErrorBody struct { - Data *string `json:"data,omitempty" url:"data,omitempty"` +type UsageTokens struct { + // The number of tokens used as input to the model. + InputTokens *float64 `json:"input_tokens,omitempty" url:"input_tokens,omitempty"` + // The number of tokens produced by the model. + OutputTokens *float64 `json:"output_tokens,omitempty" url:"output_tokens,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (u *UnprocessableEntityErrorBody) GetExtraProperties() map[string]interface{} { +func (u *UsageTokens) GetExtraProperties() map[string]interface{} { return u.extraProperties } -func (u *UnprocessableEntityErrorBody) UnmarshalJSON(data []byte) error { - type unmarshaler UnprocessableEntityErrorBody +func (u *UsageTokens) UnmarshalJSON(data []byte) error { + type unmarshaler UsageTokens var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *u = UnprocessableEntityErrorBody(value) + *u = UsageTokens(value) extraProperties, err := core.ExtractExtraProperties(data, *u) if err != nil { @@ -5881,7 +9021,7 @@ func (u *UnprocessableEntityErrorBody) UnmarshalJSON(data []byte) error { return nil } -func (u *UnprocessableEntityErrorBody) String() string { +func (u *UsageTokens) String() string { if len(u._rawJSON) > 0 { if value, err := core.StringifyJSON(u._rawJSON); err == nil { return value @@ -5893,24 +9033,27 @@ func (u *UnprocessableEntityErrorBody) String() string { return fmt.Sprintf("%#v", u) } -type UpdateConnectorResponse struct { - Connector *Connector `json:"connector,omitempty" url:"connector,omitempty"` +// A message from the user. +type UserMessage struct { + // The content of the message. This can be a string or a list of content blocks. + // If a string is provided, it will be treated as a text content block. + Content *UserMessageContent `json:"content,omitempty" url:"content,omitempty"` extraProperties map[string]interface{} _rawJSON json.RawMessage } -func (u *UpdateConnectorResponse) GetExtraProperties() map[string]interface{} { +func (u *UserMessage) GetExtraProperties() map[string]interface{} { return u.extraProperties } -func (u *UpdateConnectorResponse) UnmarshalJSON(data []byte) error { - type unmarshaler UpdateConnectorResponse +func (u *UserMessage) UnmarshalJSON(data []byte) error { + type unmarshaler UserMessage var value unmarshaler if err := json.Unmarshal(data, &value); err != nil { return err } - *u = UpdateConnectorResponse(value) + *u = UserMessage(value) extraProperties, err := core.ExtractExtraProperties(data, *u) if err != nil { @@ -5922,7 +9065,7 @@ func (u *UpdateConnectorResponse) UnmarshalJSON(data []byte) error { return nil } -func (u *UpdateConnectorResponse) String() string { +func (u *UserMessage) String() string { if len(u._rawJSON) > 0 { if value, err := core.StringifyJSON(u._rawJSON); err == nil { return value @@ -5934,6 +9077,52 @@ func (u *UpdateConnectorResponse) String() string { return fmt.Sprintf("%#v", u) } +// The content of the message. This can be a string or a list of content blocks. +// If a string is provided, it will be treated as a text content block. +type UserMessageContent struct { + String string + ContentList []*Content +} + +func (u *UserMessageContent) UnmarshalJSON(data []byte) error { + var valueString string + if err := json.Unmarshal(data, &valueString); err == nil { + u.String = valueString + return nil + } + var valueContentList []*Content + if err := json.Unmarshal(data, &valueContentList); err == nil { + u.ContentList = valueContentList + return nil + } + return fmt.Errorf("%s cannot be deserialized as a %T", data, u) +} + +func (u UserMessageContent) MarshalJSON() ([]byte, error) { + if u.String != "" { + return json.Marshal(u.String) + } + if u.ContentList != nil { + return json.Marshal(u.ContentList) + } + return nil, fmt.Errorf("type %T does not include a non-empty union type", u) +} + +type UserMessageContentVisitor interface { + VisitString(string) error + VisitContentList([]*Content) error +} + +func (u *UserMessageContent) Accept(visitor UserMessageContentVisitor) error { + if u.String != "" { + return visitor.VisitString(u.String) + } + if u.ContentList != nil { + return visitor.VisitContentList(u.ContentList) + } + return fmt.Errorf("type %T does not include a non-empty union type", u) +} + // the underlying files that make up the dataset type DatasetsCreateResponseDatasetPartsItem struct { // the name of the dataset part @@ -5981,3 +9170,92 @@ func (d *DatasetsCreateResponseDatasetPartsItem) String() string { } return fmt.Sprintf("%#v", d) } + +type V2RerankResponseResultsItem struct { + // If `return_documents` is set as `false` this will return none, if `true` it will return the documents passed in + Document *V2RerankResponseResultsItemDocument `json:"document,omitempty" url:"document,omitempty"` + // Corresponds to the index in the original list of documents to which the ranked document belongs. (i.e. if the first value in the `results` object has an `index` value of 3, it means in the list of documents passed in, the document at `index=3` had the highest relevance) + Index int `json:"index" url:"index"` + // Relevance scores are normalized to be in the range `[0, 1]`. Scores close to `1` indicate a high relevance to the query, and scores closer to `0` indicate low relevance. It is not accurate to assume a score of 0.9 means the document is 2x more relevant than a document with a score of 0.45 + RelevanceScore float64 `json:"relevance_score" url:"relevance_score"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (v *V2RerankResponseResultsItem) GetExtraProperties() map[string]interface{} { + return v.extraProperties +} + +func (v *V2RerankResponseResultsItem) UnmarshalJSON(data []byte) error { + type unmarshaler V2RerankResponseResultsItem + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *v = V2RerankResponseResultsItem(value) + + extraProperties, err := core.ExtractExtraProperties(data, *v) + if err != nil { + return err + } + v.extraProperties = extraProperties + + v._rawJSON = json.RawMessage(data) + return nil +} + +func (v *V2RerankResponseResultsItem) String() string { + if len(v._rawJSON) > 0 { + if value, err := core.StringifyJSON(v._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(v); err == nil { + return value + } + return fmt.Sprintf("%#v", v) +} + +// If `return_documents` is set as `false` this will return none, if `true` it will return the documents passed in +type V2RerankResponseResultsItemDocument struct { + // The text of the document to rerank + Text string `json:"text" url:"text"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (v *V2RerankResponseResultsItemDocument) GetExtraProperties() map[string]interface{} { + return v.extraProperties +} + +func (v *V2RerankResponseResultsItemDocument) UnmarshalJSON(data []byte) error { + type unmarshaler V2RerankResponseResultsItemDocument + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *v = V2RerankResponseResultsItemDocument(value) + + extraProperties, err := core.ExtractExtraProperties(data, *v) + if err != nil { + return err + } + v.extraProperties = extraProperties + + v._rawJSON = json.RawMessage(data) + return nil +} + +func (v *V2RerankResponseResultsItemDocument) String() string { + if len(v._rawJSON) > 0 { + if value, err := core.StringifyJSON(v._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(v); err == nil { + return value + } + return fmt.Sprintf("%#v", v) +} diff --git a/v2/client.go b/v2/client.go new file mode 100644 index 0000000..b3f3a9b --- /dev/null +++ b/v2/client.go @@ -0,0 +1,562 @@ +// This file was auto-generated by Fern from our API Definition. + +package v2 + +import ( + bytes "bytes" + context "context" + json "encoding/json" + errors "errors" + v2 "github.com/cohere-ai/cohere-go/v2" + core "github.com/cohere-ai/cohere-go/v2/core" + option "github.com/cohere-ai/cohere-go/v2/option" + io "io" + http "net/http" + os "os" +) + +type Client struct { + baseURL string + caller *core.Caller + header http.Header +} + +func NewClient(opts ...option.RequestOption) *Client { + options := core.NewRequestOptions(opts...) + if options.Token == "" { + options.Token = os.Getenv("CO_API_KEY") + } + return &Client{ + baseURL: options.BaseURL, + caller: core.NewCaller( + &core.CallerParams{ + Client: options.HTTPClient, + MaxAttempts: options.MaxAttempts, + }, + ), + header: options.ToHeader(), + } +} + +// Generates a message from the model in response to a provided conversation. To learn more about the features of the Chat API follow our [Text Generation guides](https://docs.cohere.com/v2/docs/chat-api). +// +// Follow the [Migration Guide](https://docs.cohere.com/v2/docs/migrating-v1-to-v2) for instructions on moving from API v1 to API v2. +func (c *Client) ChatStream( + ctx context.Context, + request *v2.V2ChatStreamRequest, + opts ...option.RequestOption, +) (*core.Stream[v2.StreamedChatResponseV2], error) { + options := core.NewRequestOptions(opts...) + + baseURL := "https://api.cohere.com" + if c.baseURL != "" { + baseURL = c.baseURL + } + if options.BaseURL != "" { + baseURL = options.BaseURL + } + endpointURL := baseURL + "/v2/chat" + + headers := core.MergeHeaders(c.header.Clone(), options.ToHeader()) + headers.Set("Accept", "text/event-stream") + + errorDecoder := func(statusCode int, body io.Reader) error { + raw, err := io.ReadAll(body) + if err != nil { + return err + } + apiError := core.NewAPIError(statusCode, errors.New(string(raw))) + decoder := json.NewDecoder(bytes.NewReader(raw)) + switch statusCode { + case 400: + value := new(v2.BadRequestError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 401: + value := new(v2.UnauthorizedError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 403: + value := new(v2.ForbiddenError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 404: + value := new(v2.NotFoundError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 422: + value := new(v2.UnprocessableEntityError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 429: + value := new(v2.TooManyRequestsError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 499: + value := new(v2.ClientClosedRequestError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 500: + value := new(v2.InternalServerError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 501: + value := new(v2.NotImplementedError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 503: + value := new(v2.ServiceUnavailableError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 504: + value := new(v2.GatewayTimeoutError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + } + return apiError + } + + streamer := core.NewStreamer[v2.StreamedChatResponseV2](c.caller) + return streamer.Stream( + ctx, + &core.StreamParams{ + URL: endpointURL, + Method: http.MethodPost, + Prefix: core.DefaultSSEDataPrefix, + Terminator: core.DefaultSSETerminator, + MaxAttempts: options.MaxAttempts, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Headers: headers, + Client: options.HTTPClient, + Request: request, + ErrorDecoder: errorDecoder, + }, + ) +} + +// Generates a message from the model in response to a provided conversation. To learn more about the features of the Chat API follow our [Text Generation guides](https://docs.cohere.com/v2/docs/chat-api). +// +// Follow the [Migration Guide](https://docs.cohere.com/v2/docs/migrating-v1-to-v2) for instructions on moving from API v1 to API v2. +func (c *Client) Chat( + ctx context.Context, + request *v2.V2ChatRequest, + opts ...option.RequestOption, +) (*v2.ChatResponse, error) { + options := core.NewRequestOptions(opts...) + + baseURL := "https://api.cohere.com" + if c.baseURL != "" { + baseURL = c.baseURL + } + if options.BaseURL != "" { + baseURL = options.BaseURL + } + endpointURL := baseURL + "/v2/chat" + + headers := core.MergeHeaders(c.header.Clone(), options.ToHeader()) + + errorDecoder := func(statusCode int, body io.Reader) error { + raw, err := io.ReadAll(body) + if err != nil { + return err + } + apiError := core.NewAPIError(statusCode, errors.New(string(raw))) + decoder := json.NewDecoder(bytes.NewReader(raw)) + switch statusCode { + case 400: + value := new(v2.BadRequestError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 401: + value := new(v2.UnauthorizedError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 403: + value := new(v2.ForbiddenError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 404: + value := new(v2.NotFoundError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 422: + value := new(v2.UnprocessableEntityError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 429: + value := new(v2.TooManyRequestsError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 499: + value := new(v2.ClientClosedRequestError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 500: + value := new(v2.InternalServerError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 501: + value := new(v2.NotImplementedError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 503: + value := new(v2.ServiceUnavailableError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 504: + value := new(v2.GatewayTimeoutError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + } + return apiError + } + + var response *v2.ChatResponse + if err := c.caller.Call( + ctx, + &core.CallParams{ + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, + }, + ); err != nil { + return nil, err + } + return response, nil +} + +// This endpoint returns text embeddings. An embedding is a list of floating point numbers that captures semantic information about the text that it represents. +// +// Embeddings can be used to create text classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page. +// +// If you want to learn more how to use the embedding model, have a look at the [Semantic Search Guide](/docs/semantic-search). +func (c *Client) Embed( + ctx context.Context, + request *v2.V2EmbedRequest, + opts ...option.RequestOption, +) (*v2.EmbedByTypeResponse, error) { + options := core.NewRequestOptions(opts...) + + baseURL := "https://api.cohere.com" + if c.baseURL != "" { + baseURL = c.baseURL + } + if options.BaseURL != "" { + baseURL = options.BaseURL + } + endpointURL := baseURL + "/v2/embed" + + headers := core.MergeHeaders(c.header.Clone(), options.ToHeader()) + + errorDecoder := func(statusCode int, body io.Reader) error { + raw, err := io.ReadAll(body) + if err != nil { + return err + } + apiError := core.NewAPIError(statusCode, errors.New(string(raw))) + decoder := json.NewDecoder(bytes.NewReader(raw)) + switch statusCode { + case 400: + value := new(v2.BadRequestError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 401: + value := new(v2.UnauthorizedError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 403: + value := new(v2.ForbiddenError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 404: + value := new(v2.NotFoundError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 422: + value := new(v2.UnprocessableEntityError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 429: + value := new(v2.TooManyRequestsError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 499: + value := new(v2.ClientClosedRequestError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 500: + value := new(v2.InternalServerError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 501: + value := new(v2.NotImplementedError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 503: + value := new(v2.ServiceUnavailableError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 504: + value := new(v2.GatewayTimeoutError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + } + return apiError + } + + var response *v2.EmbedByTypeResponse + if err := c.caller.Call( + ctx, + &core.CallParams{ + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, + }, + ); err != nil { + return nil, err + } + return response, nil +} + +// This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score. +func (c *Client) Rerank( + ctx context.Context, + request *v2.V2RerankRequest, + opts ...option.RequestOption, +) (*v2.V2RerankResponse, error) { + options := core.NewRequestOptions(opts...) + + baseURL := "https://api.cohere.com" + if c.baseURL != "" { + baseURL = c.baseURL + } + if options.BaseURL != "" { + baseURL = options.BaseURL + } + endpointURL := baseURL + "/v2/rerank" + + headers := core.MergeHeaders(c.header.Clone(), options.ToHeader()) + + errorDecoder := func(statusCode int, body io.Reader) error { + raw, err := io.ReadAll(body) + if err != nil { + return err + } + apiError := core.NewAPIError(statusCode, errors.New(string(raw))) + decoder := json.NewDecoder(bytes.NewReader(raw)) + switch statusCode { + case 400: + value := new(v2.BadRequestError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 401: + value := new(v2.UnauthorizedError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 403: + value := new(v2.ForbiddenError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 404: + value := new(v2.NotFoundError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 422: + value := new(v2.UnprocessableEntityError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 429: + value := new(v2.TooManyRequestsError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 499: + value := new(v2.ClientClosedRequestError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 500: + value := new(v2.InternalServerError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 501: + value := new(v2.NotImplementedError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 503: + value := new(v2.ServiceUnavailableError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + case 504: + value := new(v2.GatewayTimeoutError) + value.APIError = apiError + if err := decoder.Decode(value); err != nil { + return apiError + } + return value + } + return apiError + } + + var response *v2.V2RerankResponse + if err := c.caller.Call( + ctx, + &core.CallParams{ + URL: endpointURL, + Method: http.MethodPost, + MaxAttempts: options.MaxAttempts, + Headers: headers, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: errorDecoder, + }, + ); err != nil { + return nil, err + } + return response, nil +} diff --git a/v_2.go b/v_2.go new file mode 100644 index 0000000..d9f629d --- /dev/null +++ b/v_2.go @@ -0,0 +1,499 @@ +// This file was auto-generated by Fern from our API Definition. + +package api + +import ( + json "encoding/json" + fmt "fmt" + core "github.com/cohere-ai/cohere-go/v2/core" +) + +type V2ChatRequest struct { + // The name of a compatible [Cohere model](https://docs.cohere.com/v2/docs/models) (such as command-r or command-r-plus) or the ID of a [fine-tuned](https://docs.cohere.com/v2/docs/chat-fine-tuning) model. + Model string `json:"model" url:"-"` + Messages ChatMessages `json:"messages,omitempty" url:"-"` + // A list of available tools (functions) that the model may suggest invoking before producing a text response. + // + // When `tools` is passed (without `tool_results`), the `text` content in the response will be empty and the `tool_calls` field in the response will be populated with a list of tool calls that need to be made. If no calls need to be made, the `tool_calls` array will be empty. + Tools []*ToolV2 `json:"tools,omitempty" url:"-"` + // A list of relevant documents that the model can cite to generate a more accurate reply. Each document is either a string or document object with content and metadata. + Documents []*V2ChatRequestDocumentsItem `json:"documents,omitempty" url:"-"` + CitationOptions *CitationOptions `json:"citation_options,omitempty" url:"-"` + ResponseFormat *ResponseFormatV2 `json:"response_format,omitempty" url:"-"` + // Used to select the [safety instruction](https://docs.cohere.com/v2/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`. + // When `OFF` is specified, the safety instruction will be omitted. + // + // Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters. + // + // **Note**: This parameter is only compatible with models [Command R 08-2024](https://docs.cohere.com/v2/docs/command-r#august-2024-release), [Command R+ 08-2024](https://docs.cohere.com/v2/docs/command-r-plus#august-2024-release) and newer. + SafetyMode *V2ChatRequestSafetyMode `json:"safety_mode,omitempty" url:"-"` + // The maximum number of tokens the model will generate as part of the response. + // + // **Note**: Setting a low value may result in incomplete generations. + MaxTokens *int `json:"max_tokens,omitempty" url:"-"` + // A list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence. + StopSequences []string `json:"stop_sequences,omitempty" url:"-"` + // Defaults to `0.3`. + // + // A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations. + // + // Randomness can be further maximized by increasing the value of the `p` parameter. + Temperature *float64 `json:"temperature,omitempty" url:"-"` + // If specified, the backend will make a best effort to sample tokens + // deterministically, such that repeated requests with the same + // seed and parameters should return the same result. However, + // determinism cannot be totally guaranteed. + Seed *int `json:"seed,omitempty" url:"-"` + // Defaults to `0.0`, min value of `0.0`, max value of `1.0`. + // Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation. + FrequencyPenalty *float64 `json:"frequency_penalty,omitempty" url:"-"` + // Defaults to `0.0`, min value of `0.0`, max value of `1.0`. + // Used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies. + PresencePenalty *float64 `json:"presence_penalty,omitempty" url:"-"` + // Ensures only the top `k` most likely tokens are considered for generation at each step. + // Defaults to `0`, min value of `0`, max value of `500`. + K *float64 `json:"k,omitempty" url:"-"` + // Ensures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`. + // Defaults to `0.75`. min value of `0.01`, max value of `0.99`. + P *float64 `json:"p,omitempty" url:"-"` + // Whether to return the prompt in the response. + ReturnPrompt *bool `json:"return_prompt,omitempty" url:"-"` + stream bool +} + +func (v *V2ChatRequest) Stream() bool { + return v.stream +} + +func (v *V2ChatRequest) UnmarshalJSON(data []byte) error { + type unmarshaler V2ChatRequest + var body unmarshaler + if err := json.Unmarshal(data, &body); err != nil { + return err + } + *v = V2ChatRequest(body) + v.stream = false + return nil +} + +func (v *V2ChatRequest) MarshalJSON() ([]byte, error) { + type embed V2ChatRequest + var marshaler = struct { + embed + Stream bool `json:"stream"` + }{ + embed: embed(*v), + Stream: false, + } + return json.Marshal(marshaler) +} + +type V2ChatStreamRequest struct { + // The name of a compatible [Cohere model](https://docs.cohere.com/v2/docs/models) (such as command-r or command-r-plus) or the ID of a [fine-tuned](https://docs.cohere.com/v2/docs/chat-fine-tuning) model. + Model string `json:"model" url:"-"` + Messages ChatMessages `json:"messages,omitempty" url:"-"` + // A list of available tools (functions) that the model may suggest invoking before producing a text response. + // + // When `tools` is passed (without `tool_results`), the `text` content in the response will be empty and the `tool_calls` field in the response will be populated with a list of tool calls that need to be made. If no calls need to be made, the `tool_calls` array will be empty. + Tools []*ToolV2 `json:"tools,omitempty" url:"-"` + // A list of relevant documents that the model can cite to generate a more accurate reply. Each document is either a string or document object with content and metadata. + Documents []*V2ChatStreamRequestDocumentsItem `json:"documents,omitempty" url:"-"` + CitationOptions *CitationOptions `json:"citation_options,omitempty" url:"-"` + ResponseFormat *ResponseFormatV2 `json:"response_format,omitempty" url:"-"` + // Used to select the [safety instruction](https://docs.cohere.com/v2/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`. + // When `OFF` is specified, the safety instruction will be omitted. + // + // Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters. + // + // **Note**: This parameter is only compatible with models [Command R 08-2024](https://docs.cohere.com/v2/docs/command-r#august-2024-release), [Command R+ 08-2024](https://docs.cohere.com/v2/docs/command-r-plus#august-2024-release) and newer. + SafetyMode *V2ChatStreamRequestSafetyMode `json:"safety_mode,omitempty" url:"-"` + // The maximum number of tokens the model will generate as part of the response. + // + // **Note**: Setting a low value may result in incomplete generations. + MaxTokens *int `json:"max_tokens,omitempty" url:"-"` + // A list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence. + StopSequences []string `json:"stop_sequences,omitempty" url:"-"` + // Defaults to `0.3`. + // + // A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations. + // + // Randomness can be further maximized by increasing the value of the `p` parameter. + Temperature *float64 `json:"temperature,omitempty" url:"-"` + // If specified, the backend will make a best effort to sample tokens + // deterministically, such that repeated requests with the same + // seed and parameters should return the same result. However, + // determinism cannot be totally guaranteed. + Seed *int `json:"seed,omitempty" url:"-"` + // Defaults to `0.0`, min value of `0.0`, max value of `1.0`. + // Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation. + FrequencyPenalty *float64 `json:"frequency_penalty,omitempty" url:"-"` + // Defaults to `0.0`, min value of `0.0`, max value of `1.0`. + // Used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies. + PresencePenalty *float64 `json:"presence_penalty,omitempty" url:"-"` + // Ensures only the top `k` most likely tokens are considered for generation at each step. + // Defaults to `0`, min value of `0`, max value of `500`. + K *float64 `json:"k,omitempty" url:"-"` + // Ensures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`. + // Defaults to `0.75`. min value of `0.01`, max value of `0.99`. + P *float64 `json:"p,omitempty" url:"-"` + // Whether to return the prompt in the response. + ReturnPrompt *bool `json:"return_prompt,omitempty" url:"-"` + stream bool +} + +func (v *V2ChatStreamRequest) Stream() bool { + return v.stream +} + +func (v *V2ChatStreamRequest) UnmarshalJSON(data []byte) error { + type unmarshaler V2ChatStreamRequest + var body unmarshaler + if err := json.Unmarshal(data, &body); err != nil { + return err + } + *v = V2ChatStreamRequest(body) + v.stream = true + return nil +} + +func (v *V2ChatStreamRequest) MarshalJSON() ([]byte, error) { + type embed V2ChatStreamRequest + var marshaler = struct { + embed + Stream bool `json:"stream"` + }{ + embed: embed(*v), + Stream: true, + } + return json.Marshal(marshaler) +} + +type V2EmbedRequest struct { + // An array of strings for the model to embed. Maximum number of texts per call is `96`. We recommend reducing the length of each text to be under `512` tokens for optimal quality. + Texts []string `json:"texts,omitempty" url:"-"` + // An array of image data URIs for the model to embed. Maximum number of images per call is `1`. + // + // The image must be a valid [data URI](https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/data). The image must be in either `image/jpeg` or `image/png` format and has a maximum size of 5MB. + Images []string `json:"images,omitempty" url:"-"` + // Defaults to embed-english-v2.0 + // + // The identifier of the model. Smaller "light" models are faster, while larger models will perform better. [Custom models](/docs/training-custom-models) can also be supplied with their full ID. + // + // Available models and corresponding embedding dimensions: + // + // * `embed-english-v3.0` 1024 + // * `embed-multilingual-v3.0` 1024 + // * `embed-english-light-v3.0` 384 + // * `embed-multilingual-light-v3.0` 384 + // + // * `embed-english-v2.0` 4096 + // * `embed-english-light-v2.0` 1024 + // * `embed-multilingual-v2.0` 768 + Model string `json:"model" url:"-"` + InputType EmbedInputType `json:"input_type" url:"-"` + // Specifies the types of embeddings you want to get back. Not required and default is None, which returns the Embed Floats response type. Can be one or more of the following types. + // + // * `"float"`: Use this when you want to get back the default float embeddings. Valid for all models. + // * `"int8"`: Use this when you want to get back signed int8 embeddings. Valid for only v3 models. + // * `"uint8"`: Use this when you want to get back unsigned int8 embeddings. Valid for only v3 models. + // * `"binary"`: Use this when you want to get back signed binary embeddings. Valid for only v3 models. + // * `"ubinary"`: Use this when you want to get back unsigned binary embeddings. Valid for only v3 models. + EmbeddingTypes []EmbeddingType `json:"embedding_types,omitempty" url:"-"` + // One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length. + // + // Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model. + // + // If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned. + Truncate *V2EmbedRequestTruncate `json:"truncate,omitempty" url:"-"` +} + +type V2RerankRequest struct { + // The identifier of the model to use, one of : `rerank-english-v3.0`, `rerank-multilingual-v3.0`, `rerank-english-v2.0`, `rerank-multilingual-v2.0` + Model string `json:"model" url:"-"` + // The search query + Query string `json:"query" url:"-"` + // A list of document objects or strings to rerank. + // If a document is provided the text fields is required and all other fields will be preserved in the response. + // + // The total max chunks (length of documents * max_chunks_per_doc) must be less than 10000. + // + // We recommend a maximum of 1,000 documents for optimal endpoint performance. + Documents []*V2RerankRequestDocumentsItem `json:"documents,omitempty" url:"-"` + // The number of most relevant documents or indices to return, defaults to the length of the documents + TopN *int `json:"top_n,omitempty" url:"-"` + // If a JSON object is provided, you can specify which keys you would like to have considered for reranking. The model will rerank based on order of the fields passed in (i.e. rank_fields=['title','author','text'] will rerank using the values in title, author, text sequentially. If the length of title, author, and text exceeds the context length of the model, the chunking will not re-consider earlier fields). If not provided, the model will use the default text field for ranking. + RankFields []string `json:"rank_fields,omitempty" url:"-"` + // - If false, returns results without the doc text - the api will return a list of {index, relevance score} where index is inferred from the list passed into the request. + // - If true, returns results with the doc text passed in - the api will return an ordered list of {index, text, relevance score} where index + text refers to the list passed into the request. + ReturnDocuments *bool `json:"return_documents,omitempty" url:"-"` + // The maximum number of chunks to produce internally from a document + MaxChunksPerDoc *int `json:"max_chunks_per_doc,omitempty" url:"-"` +} + +type V2ChatRequestDocumentsItem struct { + String string + Document *Document +} + +func (v *V2ChatRequestDocumentsItem) UnmarshalJSON(data []byte) error { + var valueString string + if err := json.Unmarshal(data, &valueString); err == nil { + v.String = valueString + return nil + } + valueDocument := new(Document) + if err := json.Unmarshal(data, &valueDocument); err == nil { + v.Document = valueDocument + return nil + } + return fmt.Errorf("%s cannot be deserialized as a %T", data, v) +} + +func (v V2ChatRequestDocumentsItem) MarshalJSON() ([]byte, error) { + if v.String != "" { + return json.Marshal(v.String) + } + if v.Document != nil { + return json.Marshal(v.Document) + } + return nil, fmt.Errorf("type %T does not include a non-empty union type", v) +} + +type V2ChatRequestDocumentsItemVisitor interface { + VisitString(string) error + VisitDocument(*Document) error +} + +func (v *V2ChatRequestDocumentsItem) Accept(visitor V2ChatRequestDocumentsItemVisitor) error { + if v.String != "" { + return visitor.VisitString(v.String) + } + if v.Document != nil { + return visitor.VisitDocument(v.Document) + } + return fmt.Errorf("type %T does not include a non-empty union type", v) +} + +// Used to select the [safety instruction](https://docs.cohere.com/v2/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`. +// When `OFF` is specified, the safety instruction will be omitted. +// +// Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters. +// +// **Note**: This parameter is only compatible with models [Command R 08-2024](https://docs.cohere.com/v2/docs/command-r#august-2024-release), [Command R+ 08-2024](https://docs.cohere.com/v2/docs/command-r-plus#august-2024-release) and newer. +type V2ChatRequestSafetyMode string + +const ( + V2ChatRequestSafetyModeContextual V2ChatRequestSafetyMode = "CONTEXTUAL" + V2ChatRequestSafetyModeStrict V2ChatRequestSafetyMode = "STRICT" + V2ChatRequestSafetyModeOff V2ChatRequestSafetyMode = "OFF" +) + +func NewV2ChatRequestSafetyModeFromString(s string) (V2ChatRequestSafetyMode, error) { + switch s { + case "CONTEXTUAL": + return V2ChatRequestSafetyModeContextual, nil + case "STRICT": + return V2ChatRequestSafetyModeStrict, nil + case "OFF": + return V2ChatRequestSafetyModeOff, nil + } + var t V2ChatRequestSafetyMode + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (v V2ChatRequestSafetyMode) Ptr() *V2ChatRequestSafetyMode { + return &v +} + +type V2ChatStreamRequestDocumentsItem struct { + String string + Document *Document +} + +func (v *V2ChatStreamRequestDocumentsItem) UnmarshalJSON(data []byte) error { + var valueString string + if err := json.Unmarshal(data, &valueString); err == nil { + v.String = valueString + return nil + } + valueDocument := new(Document) + if err := json.Unmarshal(data, &valueDocument); err == nil { + v.Document = valueDocument + return nil + } + return fmt.Errorf("%s cannot be deserialized as a %T", data, v) +} + +func (v V2ChatStreamRequestDocumentsItem) MarshalJSON() ([]byte, error) { + if v.String != "" { + return json.Marshal(v.String) + } + if v.Document != nil { + return json.Marshal(v.Document) + } + return nil, fmt.Errorf("type %T does not include a non-empty union type", v) +} + +type V2ChatStreamRequestDocumentsItemVisitor interface { + VisitString(string) error + VisitDocument(*Document) error +} + +func (v *V2ChatStreamRequestDocumentsItem) Accept(visitor V2ChatStreamRequestDocumentsItemVisitor) error { + if v.String != "" { + return visitor.VisitString(v.String) + } + if v.Document != nil { + return visitor.VisitDocument(v.Document) + } + return fmt.Errorf("type %T does not include a non-empty union type", v) +} + +// Used to select the [safety instruction](https://docs.cohere.com/v2/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`. +// When `OFF` is specified, the safety instruction will be omitted. +// +// Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters. +// +// **Note**: This parameter is only compatible with models [Command R 08-2024](https://docs.cohere.com/v2/docs/command-r#august-2024-release), [Command R+ 08-2024](https://docs.cohere.com/v2/docs/command-r-plus#august-2024-release) and newer. +type V2ChatStreamRequestSafetyMode string + +const ( + V2ChatStreamRequestSafetyModeContextual V2ChatStreamRequestSafetyMode = "CONTEXTUAL" + V2ChatStreamRequestSafetyModeStrict V2ChatStreamRequestSafetyMode = "STRICT" + V2ChatStreamRequestSafetyModeOff V2ChatStreamRequestSafetyMode = "OFF" +) + +func NewV2ChatStreamRequestSafetyModeFromString(s string) (V2ChatStreamRequestSafetyMode, error) { + switch s { + case "CONTEXTUAL": + return V2ChatStreamRequestSafetyModeContextual, nil + case "STRICT": + return V2ChatStreamRequestSafetyModeStrict, nil + case "OFF": + return V2ChatStreamRequestSafetyModeOff, nil + } + var t V2ChatStreamRequestSafetyMode + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (v V2ChatStreamRequestSafetyMode) Ptr() *V2ChatStreamRequestSafetyMode { + return &v +} + +// One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length. +// +// Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model. +// +// If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned. +type V2EmbedRequestTruncate string + +const ( + V2EmbedRequestTruncateNone V2EmbedRequestTruncate = "NONE" + V2EmbedRequestTruncateStart V2EmbedRequestTruncate = "START" + V2EmbedRequestTruncateEnd V2EmbedRequestTruncate = "END" +) + +func NewV2EmbedRequestTruncateFromString(s string) (V2EmbedRequestTruncate, error) { + switch s { + case "NONE": + return V2EmbedRequestTruncateNone, nil + case "START": + return V2EmbedRequestTruncateStart, nil + case "END": + return V2EmbedRequestTruncateEnd, nil + } + var t V2EmbedRequestTruncate + return "", fmt.Errorf("%s is not a valid %T", s, t) +} + +func (v V2EmbedRequestTruncate) Ptr() *V2EmbedRequestTruncate { + return &v +} + +type V2RerankRequestDocumentsItem struct { + String string + RerankDocument RerankDocument +} + +func (v *V2RerankRequestDocumentsItem) UnmarshalJSON(data []byte) error { + var valueString string + if err := json.Unmarshal(data, &valueString); err == nil { + v.String = valueString + return nil + } + var valueRerankDocument RerankDocument + if err := json.Unmarshal(data, &valueRerankDocument); err == nil { + v.RerankDocument = valueRerankDocument + return nil + } + return fmt.Errorf("%s cannot be deserialized as a %T", data, v) +} + +func (v V2RerankRequestDocumentsItem) MarshalJSON() ([]byte, error) { + if v.String != "" { + return json.Marshal(v.String) + } + if v.RerankDocument != nil { + return json.Marshal(v.RerankDocument) + } + return nil, fmt.Errorf("type %T does not include a non-empty union type", v) +} + +type V2RerankRequestDocumentsItemVisitor interface { + VisitString(string) error + VisitRerankDocument(RerankDocument) error +} + +func (v *V2RerankRequestDocumentsItem) Accept(visitor V2RerankRequestDocumentsItemVisitor) error { + if v.String != "" { + return visitor.VisitString(v.String) + } + if v.RerankDocument != nil { + return visitor.VisitRerankDocument(v.RerankDocument) + } + return fmt.Errorf("type %T does not include a non-empty union type", v) +} + +type V2RerankResponse struct { + Id *string `json:"id,omitempty" url:"id,omitempty"` + // An ordered list of ranked documents + Results []*V2RerankResponseResultsItem `json:"results,omitempty" url:"results,omitempty"` + Meta *ApiMeta `json:"meta,omitempty" url:"meta,omitempty"` + + extraProperties map[string]interface{} + _rawJSON json.RawMessage +} + +func (v *V2RerankResponse) GetExtraProperties() map[string]interface{} { + return v.extraProperties +} + +func (v *V2RerankResponse) UnmarshalJSON(data []byte) error { + type unmarshaler V2RerankResponse + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *v = V2RerankResponse(value) + + extraProperties, err := core.ExtractExtraProperties(data, *v) + if err != nil { + return err + } + v.extraProperties = extraProperties + + v._rawJSON = json.RawMessage(data) + return nil +} + +func (v *V2RerankResponse) String() string { + if len(v._rawJSON) > 0 { + if value, err := core.StringifyJSON(v._rawJSON); err == nil { + return value + } + } + if value, err := core.StringifyJSON(v); err == nil { + return value + } + return fmt.Sprintf("%#v", v) +}