ernieclient

package
v0.1.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultCompletionModelPath = "completions"
)

DefaultCompletionModelPath default model.

Variables

View Source
var (
	ErrNotSetAuth      = errors.New("both accessToken and apiKey secretKey are not set")
	ErrCompletionCode  = errors.New("completion API returned unexpected status code")
	ErrAccessTokenCode = errors.New("get access_token API returned unexpected status code")
	ErrEmbeddingCode   = errors.New("embedding API returned unexpected status code")
	ErrEmptyResponse   = errors.New("empty response")
)

Functions

This section is empty.

Types

type ChatChoice

type ChatChoice struct {
	Index        int         `json:"index"`
	Message      ChatMessage `json:"message"`
	FinishReason string      `json:"finish_reason"`
}

ChatChoice is a choice in a chat response.

type ChatMessage

type ChatMessage struct {
	// The role of the author of this message. One of system, user, or assistant.
	Role string `json:"role"`
	// The content of the message.
	Content string `json:"content"`
	// The name of the author of this message. May contain a-z, A-Z, 0-9, and underscores,
	// with a maximum length of 64 characters.
	Name string `json:"name,omitempty"`

	// FunctionCall represents a function call to be made in the message.
	FunctionCall *llms.FunctionCall `json:"function_call,omitempty"`
}

ChatMessage is a message in a chat request.

type ChatRequest

type ChatRequest struct {
	Model            string         `json:"model,omitempty"`
	Messages         []*ChatMessage `json:"messages"`
	Temperature      float64        `json:"temperature"`
	TopP             float64        `json:"top_p,omitempty"`
	MaxTokens        int            `json:"max_tokens,omitempty"`
	N                int            `json:"n,omitempty"`
	StopWords        []string       `json:"stop,omitempty"`
	Stream           bool           `json:"stream,omitempty"`
	FrequencyPenalty float64        `json:"frequency_penalty,omitempty"`
	PresencePenalty  float64        `json:"presence_penalty,omitempty"`

	// If the 'functions' parameter is set, setting the 'system' parameter is not supported.
	System string `json:"system,omitempty"`

	// Function definitions to include in the request.
	Functions []FunctionDefinition `json:"functions,omitempty"`
	// FunctionCallBehavior is the behavior to use when calling functions.
	//
	// If a specific function should be invoked, use the format:
	// `{"name": "my_function"}`
	FunctionCallBehavior FunctionCallBehavior `json:"function_call,omitempty"`

	// StreamingFunc is a function to be called for each chunk of a streaming response.
	// Return an error to stop streaming early.
	StreamingFunc func(ctx context.Context, chunk []byte) error `json:"-"`
}

ChatRequest is a request to complete a chat completion..

type ChatResponse

type ChatResponse struct {
	ID               string           `json:"id"`
	Object           string           `json:"object"`
	Created          int              `json:"created"`
	Result           string           `json:"result"`
	IsTruncated      bool             `json:"is_truncated"`
	NeedClearHistory bool             `json:"need_clear_history"`
	FunctionCall     *FunctionCallRes `json:"function_call,omitempty"`
	Usage            struct {
		PromptTokens     int `json:"prompt_tokens"`
		CompletionTokens int `json:"completion_tokens"`
		TotalTokens      int `json:"total_tokens"`
	} `json:"usage"`
}

type ChatUsage

type ChatUsage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

ChatUsage is the usage of a chat completion request.

type Client

type Client struct {
	Model     string
	ModelPath ModelPath
	// contains filtered or unexported fields
}

Client is a client for the ERNIE API.

func New

func New(opts ...Option) (*Client, error)

New returns a new ERNIE client.

func (*Client) CreateChat

func (c *Client) CreateChat(ctx context.Context, r *ChatRequest) (*ChatResponse, error)

CreateChat creates chat request.

func (*Client) CreateCompletion

func (c *Client) CreateCompletion(ctx context.Context, modelPath ModelPath, r *CompletionRequest) (*Completion, error)

CreateCompletion creates a completion.

func (*Client) CreateEmbedding

func (c *Client) CreateEmbedding(ctx context.Context, texts []string) (*EmbeddingResponse, error)

CreateEmbedding use ernie Embedding-V1.

type Completion

type Completion struct {
	ID               string `json:"id"`
	Object           string `json:"object"`
	Created          int    `json:"created"`
	SentenceID       int    `json:"sentence_id"`
	IsEnd            bool   `json:"is_end"`
	IsTruncated      bool   `json:"is_truncated"`
	Result           string `json:"result"`
	NeedClearHistory bool   `json:"need_clear_history"`
	Usage            struct {
		PromptTokens     int `json:"prompt_tokens"`
		CompletionTokens int `json:"completion_tokens"`
		TotalTokens      int `json:"total_tokens"`
	} `json:"usage"`
	// for error
	ErrorCode int    `json:"error_code,omitempty"`
	ErrorMsg  string `json:"error_msg,omitempty"`
}

Completion is a completion.

type CompletionRequest

type CompletionRequest struct {
	Messages      []Message                                     `json:"messages"`
	Temperature   float64                                       `json:"temperature"`
	TopP          float64                                       `json:"top_p,omitempty"`
	PenaltyScore  float64                                       `json:"penalty_score,omitempty"`
	Stream        bool                                          `json:"stream,omitempty"`
	UserID        string                                        `json:"user_id,omitempty"`
	StreamingFunc func(ctx context.Context, chunk []byte) error `json:"-"`
}

CompletionRequest is a request to create a completion.

type Doer

type Doer interface {
	Do(req *http.Request) (*http.Response, error)
}

Doer performs a HTTP request.

type EmbeddingResponse

type EmbeddingResponse struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int    `json:"created"`
	Data    []struct {
		Object    string    `json:"object"`
		Embedding []float32 `json:"embedding"`
		Index     int       `json:"index"`
	} `json:"data"`
	Usage struct {
		PromptTokens int `json:"prompt_tokens"`
		TotalTokens  int `json:"total_tokens"`
	} `json:"usage"`
	// for error
	ErrorCode int    `json:"error_code,omitempty"`
	ErrorMsg  string `json:"error_msg,omitempty"`
}

type FunctionCall

type FunctionCall struct {
	// Name is the name of the function to call.
	Name string `json:"name"`
	// Arguments is the set of arguments to pass to the function.
	Arguments string `json:"arguments"`
}

FunctionCall is a call to a function.

type FunctionCallBehavior

type FunctionCallBehavior string

FunctionCallBehavior is the behavior to use when calling functions.

const (
	// FunctionCallBehaviorUnspecified is the empty string.
	FunctionCallBehaviorUnspecified FunctionCallBehavior = ""
	// FunctionCallBehaviorNone will not call any functions.
	FunctionCallBehaviorNone FunctionCallBehavior = "none"
	// FunctionCallBehaviorAuto will call functions automatically.
	FunctionCallBehaviorAuto FunctionCallBehavior = "auto"
)

type FunctionCallRes

type FunctionCallRes struct {
	Name      string `json:"name"`
	Thoughts  string `json:"thoughts"`
	Arguments string `json:"arguments"`
}

type FunctionDefinition

type FunctionDefinition struct {
	// Name is the name of the function.
	Name string `json:"name"`
	// Description is a description of the function.
	Description string `json:"description"`
	// Parameters is a list of parameters for the function.
	Parameters any `json:"parameters"`
}

FunctionDefinition is a definition of a function that can be called by the model.

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type ModelPath

type ModelPath string

ModelPath ERNIE API URL path suffix distinguish models.

type Option

type Option func(*Client) error

Option is an option for the ERNIE client.

func WithAKSK

func WithAKSK(apiKey, secretKey string) Option

WithAKSK allows setting apiKey, secretKey.

func WithAccessToken

func WithAccessToken(accessToken string) Option

Usually used for dev, Prod env recommend use WithAKSK.

func WithHTTPClient

func WithHTTPClient(client Doer) Option

WithHTTPClient allows setting a custom HTTP client.

type StreamedChatResponsePayload

type StreamedChatResponsePayload struct {
	ID               string           `json:"id"`
	Object           string           `json:"object"`
	Created          int              `json:"created"`
	SentenceID       int              `json:"sentence_id"`
	IsEnd            bool             `json:"is_end"`
	IsTruncated      bool             `json:"is_truncated"`
	Result           string           `json:"result"`
	NeedClearHistory bool             `json:"need_clear_history"`
	FunctionCall     *FunctionCallRes `json:"function_call,omitempty"`
	Usage            struct {
		PromptTokens     int `json:"prompt_tokens"`
		CompletionTokens int `json:"completion_tokens"`
		TotalTokens      int `json:"total_tokens"`
	} `json:"usage"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL