Documentation
¶
Index ¶
- Variables
- type ChatCompletion
- type ChatCompletionChunk
- type ChatRequest
- type ChatStream
- type Citation
- type Client
- func (c *Client) Chat(ctx context.Context, params *schemas.ChatParams) (*schemas.ChatResponse, error)
- func (c *Client) ChatStream(ctx context.Context, params *schemas.ChatParams) (clients.ChatStream, error)
- func (c *Client) ModelName() string
- func (c *Client) Provider() string
- func (c *Client) SupportChatStream() bool
- type Config
- type Connectors
- type ConnectorsResponse
- type Documents
- type ErrorMapper
- type FinalResponse
- type FinishReasonMapper
- type Meta
- type Params
- type SearchQuery
- type SearchQueryObject
- type SearchResults
- type StreamReader
- type SupportedEventType
- type TokenCount
Constants ¶
This section is empty.
Variables ¶
var ( // Reference: https://platform.openai.com/docs/api-reference/chat/object CompleteReason = "complete" MaxTokensReason = "max_tokens" FilteredReason = "error_toxic" )
Functions ¶
This section is empty.
Types ¶
type ChatCompletion ¶
type ChatCompletion struct { Text string `json:"text"` GenerationID string `json:"generation_id"` ResponseID string `json:"response_id"` TokenCount TokenCount `json:"token_count"` Citations []Citation `json:"citations"` Documents []Documents `json:"documents"` SearchQueries []SearchQuery `json:"search_queries"` SearchResults []SearchResults `json:"search_results"` Meta Meta `json:"meta"` ToolInputs map[string]interface{} `json:"tool_inputs"` }
Cohere Chat Response
type ChatCompletionChunk ¶
type ChatCompletionChunk struct { IsFinished bool `json:"is_finished"` EventType string `json:"event_type"` GenerationID *string `json:"generation_id"` Text string `json:"text"` Response *FinalResponse `json:"response,omitempty"` FinishReason *string `json:"finish_reason,omitempty"` }
ChatCompletionChunk represents SSEvent a chat response is broken down on chat streaming Ref: https://docs.cohere.com/reference/about
type ChatRequest ¶
type ChatRequest struct { Model string `json:"model"` Message string `json:"message"` ChatHistory []schemas.ChatMessage `json:"chat_history"` Temperature float64 `json:"temperature,omitempty"` Preamble string `json:"preamble,omitempty"` PromptTruncation *string `json:"prompt_truncation,omitempty"` Connectors []string `json:"connectors,omitempty"` SearchQueriesOnly bool `json:"search_queries_only,omitempty"` Stream bool `json:"stream,omitempty"` Seed *int `json:"seed,omitempty"` MaxTokens *int `json:"max_tokens,omitempty"` K int `json:"k"` P float32 `json:"p"` FrequencyPenalty float32 `json:"frequency_penalty"` PresencePenalty float32 `json:"presence_penalty"` StopSequences []string `json:"stop_sequences"` }
ChatRequest is a request to complete a chat completion Ref: https://docs.cohere.com/reference/chat
func NewChatRequestFromConfig ¶
func NewChatRequestFromConfig(cfg *Config) *ChatRequest
NewChatRequestFromConfig fills the struct from the config. Not using reflection because of performance penalty it gives
func (*ChatRequest) ApplyParams ¶
func (r *ChatRequest) ApplyParams(params *schemas.ChatParams)
type ChatStream ¶
type ChatStream struct {
// contains filtered or unexported fields
}
ChatStream represents cohere chat stream for a specific request
func NewChatStream ¶
func NewChatStream( tel *telemetry.Telemetry, client *http.Client, req *http.Request, modelName string, errMapper *ErrorMapper, finishReasonMapper *FinishReasonMapper, ) *ChatStream
func (*ChatStream) Close ¶
func (s *ChatStream) Close() error
func (*ChatStream) Open ¶
func (s *ChatStream) Open() error
func (*ChatStream) Recv ¶
func (s *ChatStream) Recv() (*schemas.ChatStreamChunk, error)
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a client for accessing Cohere API
func NewClient ¶
func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *telemetry.Telemetry) (*Client, error)
NewClient creates a new Cohere client for the Cohere API.
func (*Client) Chat ¶
func (c *Client) Chat(ctx context.Context, params *schemas.ChatParams) (*schemas.ChatResponse, error)
Chat sends a chat request to the specified cohere model.
func (*Client) ChatStream ¶
func (c *Client) ChatStream(ctx context.Context, params *schemas.ChatParams) (clients.ChatStream, error)
func (*Client) SupportChatStream ¶
type Config ¶
type Config struct { BaseURL string `yaml:"base_url" json:"base_url" validate:"required,http_url"` ChatEndpoint string `yaml:"chat_endpoint" json:"chat_endpoint" validate:"required"` ModelName string `yaml:"model" json:"model" validate:"required"` // https://docs.cohere.com/docs/models#command APIKey fields.Secret `yaml:"api_key" json:"-" validate:"required"` DefaultParams *Params `yaml:"default_params,omitempty" json:"defaultParams"` }
func (*Config) UnmarshalYAML ¶
type Connectors ¶
type ConnectorsResponse ¶
type ErrorMapper ¶
type ErrorMapper struct {
// contains filtered or unexported fields
}
func NewErrorMapper ¶
func NewErrorMapper(tel *telemetry.Telemetry) *ErrorMapper
type FinalResponse ¶
type FinalResponse struct { ResponseID string `json:"response_id"` Text string `json:"text"` GenerationID string `json:"generation_id"` TokenCount TokenCount `json:"token_count"` Meta Meta `json:"meta"` }
type FinishReasonMapper ¶
type FinishReasonMapper struct {
// contains filtered or unexported fields
}
func NewFinishReasonMapper ¶
func NewFinishReasonMapper(tel *telemetry.Telemetry) *FinishReasonMapper
func (*FinishReasonMapper) Map ¶
func (m *FinishReasonMapper) Map(finishReason *string) *schemas.FinishReason
type Params ¶
type Params struct { Seed *int `yaml:"seed,omitempty" json:"seed,omitempty" validate:"omitempty,number"` Temperature float64 `yaml:"temperature,omitempty" json:"temperature" validate:"required,number"` MaxTokens *int `yaml:"max_tokens,omitempty" json:"max_tokens,omitempty" validate:"omitempty,number"` K int `yaml:"k,omitempty" json:"k" validate:"number,gte=0,lte=500"` P float32 `yaml:"p,omitempty" json:"p" validate:"number,gte=0.01,lte=0.99"` FrequencyPenalty float32 `yaml:"frequency_penalty,omitempty" json:"frequency_penalty" validate:"gte=0.0,lte=1.0"` PresencePenalty float32 `yaml:"presence_penalty,omitempty" json:"presence_penalty" validate:"gte=0.0,lte=1.0"` Preamble string `yaml:"preamble,omitempty" json:"preamble,omitempty"` StopSequences []string `yaml:"stop_sequences,omitempty" json:"stop_sequences" validate:"max=5"` PromptTruncation *string `yaml:"prompt_truncation,omitempty" json:"prompt_truncation,omitempty"` Connectors []string `yaml:"connectors,omitempty" json:"connectors,omitempty"` SearchQueriesOnly bool `yaml:"search_queries_only,omitempty" json:"search_queries_only,omitempty"` }
Params defines Cohere-specific model params with the specific validation of values TODO: Add validations
func DefaultParams ¶
func DefaultParams() Params
func (*Params) UnmarshalYAML ¶
type SearchQuery ¶
type SearchQueryObject ¶
type SearchResults ¶
type SearchResults struct { SearchQuery []SearchQueryObject `json:"search_query"` Connectors []ConnectorsResponse `json:"connectors"` DocumentID []string `json:"documentId"` }
type StreamReader ¶
type StreamReader struct {
// contains filtered or unexported fields
}
StreamReader reads Cohere streaming chat chunks that are formated as serializer chunk json per line (a.k.a. application/stream+json)
func NewStreamReader ¶
func NewStreamReader(stream io.Reader, maxBufferSize int) *StreamReader
NewStreamReader creates an instance of StreamReader
func (*StreamReader) ReadEvent ¶
func (r *StreamReader) ReadEvent() ([]byte, error)
ReadEvent scans the EventStream for events.
type SupportedEventType ¶
type SupportedEventType = string
SupportedEventType Cohere has other types too: Ref: https://docs.cohere.com/reference/chat (see Chat -> Responses -> StreamedChatResponse)
var ( StreamStartEvent SupportedEventType = "stream-start" TextGenEvent SupportedEventType = "text-generation" StreamEndEvent SupportedEventType = "stream-end" )