Skip to content

Commit

Permalink
SDK regeneration
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-api[bot] committed Jun 7, 2024
1 parent 38fd3f2 commit f292152
Show file tree
Hide file tree
Showing 16 changed files with 1,819 additions and 423 deletions.
26 changes: 13 additions & 13 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewClient(opts ...option.RequestOption) *Client {
}

// Generates a text response to a user message.
// To learn how to use Chat with Streaming and RAG follow [this guide](https://docs.cohere.com/docs/cochat-beta#various-ways-of-using-the-chat-endpoint).
// To learn how to use the Chat API with Streaming and RAG follow our [Text Generation guides](https://docs.cohere.com/docs/chat-api).
func (c *Client) ChatStream(
ctx context.Context,
request *v2.ChatStreamRequest,
Expand All @@ -70,7 +70,7 @@ func (c *Client) ChatStream(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "chat"
endpointURL := baseURL + "/chat"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -109,7 +109,7 @@ func (c *Client) ChatStream(
}

// Generates a text response to a user message.
// To learn how to use Chat with Streaming and RAG follow [this guide](https://docs.cohere.com/docs/cochat-beta#various-ways-of-using-the-chat-endpoint).
// To learn how to use the Chat API with Streaming and RAG follow our [Text Generation guides](https://docs.cohere.com/docs/chat-api).
func (c *Client) Chat(
ctx context.Context,
request *v2.ChatRequest,
Expand All @@ -124,7 +124,7 @@ func (c *Client) Chat(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "chat"
endpointURL := baseURL + "/chat"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -185,7 +185,7 @@ func (c *Client) GenerateStream(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "generate"
endpointURL := baseURL + "/generate"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -256,7 +256,7 @@ func (c *Client) Generate(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "generate"
endpointURL := baseURL + "/generate"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -331,7 +331,7 @@ func (c *Client) Embed(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "embed"
endpointURL := baseURL + "/embed"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -402,7 +402,7 @@ func (c *Client) Rerank(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "rerank"
endpointURL := baseURL + "/rerank"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -460,7 +460,7 @@ func (c *Client) Classify(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "classify"
endpointURL := baseURL + "/classify"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -535,7 +535,7 @@ func (c *Client) Summarize(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "summarize"
endpointURL := baseURL + "/summarize"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -592,7 +592,7 @@ func (c *Client) Tokenize(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "tokenize"
endpointURL := baseURL + "/tokenize"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -663,7 +663,7 @@ func (c *Client) Detokenize(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "detokenize"
endpointURL := baseURL + "/detokenize"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -719,7 +719,7 @@ func (c *Client) CheckApiKey(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "check-api-key"
endpointURL := baseURL + "/check-api-key"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down
30 changes: 15 additions & 15 deletions connectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ package api

type CreateConnectorRequest struct {
// A human-readable name for the connector.
Name string `json:"name" url:"name"`
Name string `json:"name" url:"-"`
// A description of the connector.
Description *string `json:"description,omitempty" url:"description,omitempty"`
Description *string `json:"description,omitempty" url:"-"`
// The URL of the connector that will be used to search for documents.
Url string `json:"url" url:"url"`
Url string `json:"url" url:"-"`
// A list of fields to exclude from the prompt (fields remain in the document).
Excludes []string `json:"excludes,omitempty" url:"excludes,omitempty"`
Excludes []string `json:"excludes,omitempty" url:"-"`
// The OAuth 2.0 configuration for the connector. Cannot be specified if service_auth is specified.
Oauth *CreateConnectorOAuth `json:"oauth,omitempty" url:"oauth,omitempty"`
Oauth *CreateConnectorOAuth `json:"oauth,omitempty" url:"-"`
// Whether the connector is active or not.
Active *bool `json:"active,omitempty" url:"active,omitempty"`
Active *bool `json:"active,omitempty" url:"-"`
// 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"`
ContinueOnFailure *bool `json:"continue_on_failure,omitempty" url:"-"`
// The service to service authentication configuration for the connector. Cannot be specified if oauth is specified.
ServiceAuth *CreateConnectorServiceAuth `json:"service_auth,omitempty" url:"service_auth,omitempty"`
ServiceAuth *CreateConnectorServiceAuth `json:"service_auth,omitempty" url:"-"`
}

type ConnectorsListRequest struct {
Expand All @@ -35,15 +35,15 @@ type ConnectorsOAuthAuthorizeRequest struct {

type UpdateConnectorRequest struct {
// A human-readable name for the connector.
Name *string `json:"name,omitempty" url:"name,omitempty"`
Name *string `json:"name,omitempty" url:"-"`
// The URL of the connector that will be used to search for documents.
Url *string `json:"url,omitempty" url:"url,omitempty"`
Url *string `json:"url,omitempty" url:"-"`
// A list of fields to exclude from the prompt (fields remain in the document).
Excludes []string `json:"excludes,omitempty" url:"excludes,omitempty"`
Excludes []string `json:"excludes,omitempty" url:"-"`
// The OAuth 2.0 configuration for the connector. Cannot be specified if service_auth is specified.
Oauth *CreateConnectorOAuth `json:"oauth,omitempty" url:"oauth,omitempty"`
Active *bool `json:"active,omitempty" url:"active,omitempty"`
ContinueOnFailure *bool `json:"continue_on_failure,omitempty" url:"continue_on_failure,omitempty"`
Oauth *CreateConnectorOAuth `json:"oauth,omitempty" url:"-"`
Active *bool `json:"active,omitempty" url:"-"`
ContinueOnFailure *bool `json:"continue_on_failure,omitempty" url:"-"`
// The service to service authentication configuration for the connector. Cannot be specified if oauth is specified.
ServiceAuth *CreateConnectorServiceAuth `json:"service_auth,omitempty" url:"service_auth,omitempty"`
ServiceAuth *CreateConnectorServiceAuth `json:"service_auth,omitempty" url:"-"`
}
13 changes: 6 additions & 7 deletions connectors/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
context "context"
json "encoding/json"
errors "errors"
fmt "fmt"
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"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (c *Client) List(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "connectors"
endpointURL := baseURL + "/connectors"

queryParams, err := core.QueryValues(request)
if err != nil {
Expand Down Expand Up @@ -132,7 +131,7 @@ func (c *Client) Create(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := baseURL + "/" + "connectors"
endpointURL := baseURL + "/connectors"

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -211,7 +210,7 @@ func (c *Client) Get(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := fmt.Sprintf(baseURL+"/"+"connectors/%v", id)
endpointURL := core.EncodeURL(baseURL+"/connectors/%v", id)

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -289,7 +288,7 @@ func (c *Client) Delete(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := fmt.Sprintf(baseURL+"/"+"connectors/%v", id)
endpointURL := core.EncodeURL(baseURL+"/connectors/%v", id)

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -375,7 +374,7 @@ func (c *Client) Update(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := fmt.Sprintf(baseURL+"/"+"connectors/%v", id)
endpointURL := core.EncodeURL(baseURL+"/connectors/%v", id)

headers := core.MergeHeaders(c.header.Clone(), options.ToHeader())

Expand Down Expand Up @@ -462,7 +461,7 @@ func (c *Client) OAuthAuthorize(
if options.BaseURL != "" {
baseURL = options.BaseURL
}
endpointURL := fmt.Sprintf(baseURL+"/"+"connectors/%v/oauth/authorize", id)
endpointURL := core.EncodeURL(baseURL+"/connectors/%v/oauth/authorize", id)

queryParams, err := core.QueryValues(request)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"mime/multipart"
"net/http"
"net/url"
)

const (
Expand All @@ -22,6 +23,16 @@ type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}

// EncodeURL encodes the given arguments into the URL, escaping
// values as needed.
func EncodeURL(urlFormat string, args ...interface{}) string {
escapedArgs := make([]interface{}, 0, len(args))
for _, arg := range args {
escapedArgs = append(escapedArgs, url.PathEscape(fmt.Sprintf("%v", arg)))
}
return fmt.Sprintf(urlFormat, escapedArgs...)
}

// MergeHeaders merges the given headers together, where the right
// takes precedence over the left.
func MergeHeaders(left, right http.Header) http.Header {
Expand Down
141 changes: 141 additions & 0 deletions core/extra_properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package core

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
)

// MarshalJSONWithExtraProperty marshals the given value to JSON, including the extra property.
func MarshalJSONWithExtraProperty(marshaler interface{}, key string, value interface{}) ([]byte, error) {
return MarshalJSONWithExtraProperties(marshaler, map[string]interface{}{key: value})
}

// MarshalJSONWithExtraProperties marshals the given value to JSON, including any extra properties.
func MarshalJSONWithExtraProperties(marshaler interface{}, extraProperties map[string]interface{}) ([]byte, error) {
bytes, err := json.Marshal(marshaler)
if err != nil {
return nil, err
}
if len(extraProperties) == 0 {
return bytes, nil
}
keys, err := getKeys(marshaler)
if err != nil {
return nil, err
}
for _, key := range keys {
if _, ok := extraProperties[key]; ok {
return nil, fmt.Errorf("cannot add extra property %q because it is already defined on the type", key)
}
}
extraBytes, err := json.Marshal(extraProperties)
if err != nil {
return nil, err
}
if isEmptyJSON(bytes) {
if isEmptyJSON(extraBytes) {
return bytes, nil
}
return extraBytes, nil
}
result := bytes[:len(bytes)-1]
result = append(result, ',')
result = append(result, extraBytes[1:len(extraBytes)-1]...)
result = append(result, '}')
return result, nil
}

// ExtractExtraProperties extracts any extra properties from the given value.
func ExtractExtraProperties(bytes []byte, value interface{}, exclude ...string) (map[string]interface{}, error) {
val := reflect.ValueOf(value)
for val.Kind() == reflect.Ptr {
if val.IsNil() {
return nil, fmt.Errorf("value must be non-nil to extract extra properties")
}
val = val.Elem()
}
if err := json.Unmarshal(bytes, &value); err != nil {
return nil, err
}
var extraProperties map[string]interface{}
if err := json.Unmarshal(bytes, &extraProperties); err != nil {
return nil, err
}
for i := 0; i < val.Type().NumField(); i++ {
key := jsonKey(val.Type().Field(i))
if key == "" || key == "-" {
continue
}
delete(extraProperties, key)
}
for _, key := range exclude {
delete(extraProperties, key)
}
if len(extraProperties) == 0 {
return nil, nil
}
return extraProperties, nil
}

// getKeys returns the keys associated with the given value. The value must be a
// a struct or a map with string keys.
func getKeys(value interface{}) ([]string, error) {
val := reflect.ValueOf(value)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if !val.IsValid() {
return nil, nil
}
switch val.Kind() {
case reflect.Struct:
return getKeysForStructType(val.Type()), nil
case reflect.Map:
var keys []string
if val.Type().Key().Kind() != reflect.String {
return nil, fmt.Errorf("cannot extract keys from %T; only structs and maps with string keys are supported", value)
}
for _, key := range val.MapKeys() {
keys = append(keys, key.String())
}
return keys, nil
default:
return nil, fmt.Errorf("cannot extract keys from %T; only structs and maps with string keys are supported", value)
}
}

// getKeysForStructType returns all the keys associated with the given struct type,
// visiting embedded fields recursively.
func getKeysForStructType(structType reflect.Type) []string {
if structType.Kind() == reflect.Pointer {
structType = structType.Elem()
}
if structType.Kind() != reflect.Struct {
return nil
}
var keys []string
for i := 0; i < structType.NumField(); i++ {
field := structType.Field(i)
if field.Anonymous {
keys = append(keys, getKeysForStructType(field.Type)...)
continue
}
keys = append(keys, jsonKey(field))
}
return keys
}

// jsonKey returns the JSON key from the struct tag of the given field,
// excluding the omitempty flag (if any).
func jsonKey(field reflect.StructField) string {
return strings.TrimSuffix(field.Tag.Get("json"), ",omitempty")
}

// isEmptyJSON returns true if the given data is empty, the empty JSON object, or
// an explicit null.
func isEmptyJSON(data []byte) bool {
return len(data) <= 2 || bytes.Equal(data, []byte("null"))
}
Loading

0 comments on commit f292152

Please sign in to comment.