ernieclient

package
v0.0.0-...-25d0b29 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 10 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")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// 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) 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,omitempty"`
	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 []float64 `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 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.

Jump to

Keyboard shortcuts

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