llm

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ChatMessageRoleSystem    = "system"
	ChatMessageRoleUser      = "user"
	ChatMessageRoleAssistant = "assistant"
	ChatMessageRoleFunction  = "function"
)

Chat message role defined by the OpenAI API.

Variables

View Source
var DefaultDao = NewMemoryDao()
View Source
var NotImplementError = errors.New("the method not implement")

Functions

This section is empty.

Types

type ChatCompletionChoice

type ChatCompletionChoice struct {
	Index   int                   `json:"index"`
	Message ChatCompletionMessage `json:"message"`
	// FinishReason
	// stop: API returned complete message,
	// or a message terminated by one of the stop sequences provided via the stop parameter
	// length: Incomplete model output due to max_tokens parameter or token limit
	// function_call: The model decided to call a function
	// content_filter: Omitted content due to a flag from our content filters
	// null: API response still in progress or incomplete
	FinishReason FinishReason `json:"finish_reason"`
}

type ChatCompletionMessage

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

	// This property isn't in the official documentation, but it's in
	// the documentation for the official library for python:
	// - https://github.com/openai/openai-python/blob/main/chatml.md
	// - https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
	Name string `json:"name,omitempty"`

	FunctionCall *FunctionCall `json:"function_call,omitempty"`
}

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model            string                  `json:"model"`
	Messages         []ChatCompletionMessage `json:"messages"`
	MaxTokens        int                     `json:"max_tokens,omitempty"`
	Temperature      float32                 `json:"temperature,omitempty"`
	TopP             float32                 `json:"top_p,omitempty"`
	N                int                     `json:"n,omitempty"`
	Stream           bool                    `json:"stream,omitempty"`
	Stop             []string                `json:"stop,omitempty"`
	PresencePenalty  float32                 `json:"presence_penalty,omitempty"`
	FrequencyPenalty float32                 `json:"frequency_penalty,omitempty"`
	// LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string.
	// incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}`
	// refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
	LogitBias    map[string]int       `json:"logit_bias,omitempty"`
	User         string               `json:"user,omitempty"`
	Functions    []FunctionDefinition `json:"functions,omitempty"`
	FunctionCall any                  `json:"function_call,omitempty"`
}

ChatCompletionRequest represents a request structure for chat completion API.

func (*ChatCompletionRequest) FromPrompt

func (r *ChatCompletionRequest) FromPrompt(model, prompt string)

func (*ChatCompletionRequest) ToPrompt

func (r *ChatCompletionRequest) ToPrompt() string

func (*ChatCompletionRequest) ToPromptWithoutRole

func (r *ChatCompletionRequest) ToPromptWithoutRole() string

type ChatCompletionResponse

type ChatCompletionResponse struct {
	ID      string                 `json:"id"`
	Object  string                 `json:"object"`
	Created int64                  `json:"created"`
	Model   string                 `json:"model"`
	Choices []ChatCompletionChoice `json:"choices"`
	Usage   Usage                  `json:"usage"`
}

ChatCompletionResponse represents a response structure for chat completion API.

type ChatCompletionStreamChoice

type ChatCompletionStreamChoice struct {
	Index                int                             `json:"index"`
	Delta                ChatCompletionStreamChoiceDelta `json:"delta"`
	FinishReason         FinishReason                    `json:"finish_reason"`
	ContentFilterResults ContentFilterResults            `json:"content_filter_results,omitempty"`
}

type ChatCompletionStreamChoiceDelta

type ChatCompletionStreamChoiceDelta struct {
	Content      string        `json:"content,omitempty"`
	Role         string        `json:"role,omitempty"`
	FunctionCall *FunctionCall `json:"function_call,omitempty"`
}

type ChatCompletionStreamResponse

type ChatCompletionStreamResponse struct {
	ID                string                       `json:"id"`
	Object            string                       `json:"object"`
	Created           int64                        `json:"created"`
	Model             string                       `json:"model"`
	Choices           []ChatCompletionStreamChoice `json:"choices"`
	PromptAnnotations []PromptAnnotation           `json:"prompt_annotations,omitempty"`
}

func (*ChatCompletionStreamResponse) ToChatCompletionResponse

func (r *ChatCompletionStreamResponse) ToChatCompletionResponse() ChatCompletionResponse

type ContentFilterResults

type ContentFilterResults struct {
	Hate     Hate     `json:"hate,omitempty"`
	SelfHarm SelfHarm `json:"self_harm,omitempty"`
	Sexual   Sexual   `json:"sexual,omitempty"`
	Violence Violence `json:"violence,omitempty"`
}

type Conversation

type Conversation struct {
	Id        string    `json:"id"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	Deleted   bool      `json:"deleted"`
	UserId    string    `json:"user_id"`
	Name      string    `json:"name"`
	Model     string    `json:"model"`
	Summary   string    `json:"summary"`
	ExtraInfo string    `json:"extra_info"`
}

type Dao

type Dao interface {
	SaveConversation(ctx context.Context, conversation Conversation) (Conversation, error)
	GetConversation(ctx context.Context, id string) (Conversation, error)
	ListConversations(ctx context.Context) ([]Conversation, error)
	DeleteConversation(ctx context.Context, id string) error

	SaveMessage(ctx context.Context, message Message) (Message, error)
	GetMessage(ctx context.Context, id string) (Message, error)
	ListMessages(ctx context.Context, conversationId string) ([]Message, error)
	DeleteMessage(ctx context.Context, id string) error

	GetConversationLastMessage(ctx context.Context, id string) (Message, error)
}

type FinishReason

type FinishReason string
const (
	FinishReasonStop          FinishReason = "stop"
	FinishReasonLength        FinishReason = "length"
	FinishReasonFunctionCall  FinishReason = "function_call"
	FinishReasonContentFilter FinishReason = "content_filter"
	FinishReasonNull          FinishReason = "null"
)

func (FinishReason) MarshalJSON

func (r FinishReason) MarshalJSON() ([]byte, error)

type FunctionCall

type FunctionCall struct {
	Name string `json:"name,omitempty"`
	// call function with arguments in JSON format
	Arguments string `json:"arguments,omitempty"`
}

type FunctionDefine deprecated

type FunctionDefine = FunctionDefinition

Deprecated: use FunctionDefinition instead.

type FunctionDefinition

type FunctionDefinition struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	// Parameters is an object describing the function.
	// You can pass json.RawMessage to describe the schema,
	// or you can pass in a struct which serializes to the proper JSON schema.
	// The jsonschema package is provided for convenience, but you should
	// consider another specialized library if you require more complex schemas.
	Parameters any `json:"parameters"`
}

type Hate

type Hate struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

type LLM

type LLM struct {
	// contains filtered or unexported fields
}

func New

func New(dao Dao, c client) *LLM

func (*LLM) CreateConversation

func (l *LLM) CreateConversation(ctx context.Context, name string) (Conversation, error)

func (*LLM) CreateMessage

func (l *LLM) CreateMessage(ctx context.Context, conversationId string, req ChatCompletionRequest) (Message, error)

func (*LLM) CreateMessageStream

func (l *LLM) CreateMessageStream(ctx context.Context, conversationId string, req ChatCompletionRequest, respChan chan ChatCompletionStreamResponse, errChan chan error)

func (*LLM) DeleteConversation

func (l *LLM) DeleteConversation(ctx context.Context, id string) error

func (*LLM) DeleteMessage

func (l *LLM) DeleteMessage(ctx context.Context, id string) error

func (*LLM) GetConversation

func (l *LLM) GetConversation(ctx context.Context, id string) (Conversation, error)

func (*LLM) GetMessage

func (l *LLM) GetMessage(ctx context.Context, id string) (Message, error)

func (*LLM) ListConversations

func (l *LLM) ListConversations(ctx context.Context) ([]Conversation, error)

func (*LLM) ListMessages

func (l *LLM) ListMessages(ctx context.Context, conversationId string) ([]Message, error)

type MemoryDao

type MemoryDao struct {
	// contains filtered or unexported fields
}

func NewMemoryDao

func NewMemoryDao() *MemoryDao

func (*MemoryDao) DeleteConversation

func (d *MemoryDao) DeleteConversation(ctx context.Context, id string) error

func (*MemoryDao) DeleteMessage

func (d *MemoryDao) DeleteMessage(ctx context.Context, id string) error

func (*MemoryDao) GetConversation

func (d *MemoryDao) GetConversation(ctx context.Context, id string) (Conversation, error)

func (*MemoryDao) GetConversationLastMessage

func (d *MemoryDao) GetConversationLastMessage(ctx context.Context, id string) (Message, error)

func (*MemoryDao) GetMessage

func (d *MemoryDao) GetMessage(ctx context.Context, id string) (Message, error)

func (*MemoryDao) ListConversations

func (d *MemoryDao) ListConversations(ctx context.Context) ([]Conversation, error)

func (*MemoryDao) ListMessages

func (d *MemoryDao) ListMessages(ctx context.Context, conversationId string) ([]Message, error)

func (*MemoryDao) SaveConversation

func (d *MemoryDao) SaveConversation(ctx context.Context, conversation Conversation) (Conversation, error)

func (*MemoryDao) SaveMessage

func (d *MemoryDao) SaveMessage(ctx context.Context, message Message) (Message, error)

type Message

type Message struct {
	Id              string                 `json:"id"`
	CreatedAt       time.Time              `json:"created_at"`
	UpdatedAt       time.Time              `json:"updated_at"`
	Deleted         bool                   `json:"deleted"`
	UserId          string                 `json:"user_id"`
	ConversationId  string                 `json:"conversation_id"`
	Model           string                 `json:"model"`
	PromptToken     int                    `json:"prompt_token"`
	CompletionToken int                    `json:"completion_token"`
	Description     string                 `json:"description"`
	Request         ChatCompletionRequest  `json:"request"`
	Response        ChatCompletionResponse `json:"response"`
	RawResponse     []byte                 `json:"raw_response"`
}

type PromptAnnotation

type PromptAnnotation struct {
	PromptIndex          int                  `json:"prompt_index,omitempty"`
	ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"`
}

type SelfHarm

type SelfHarm struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

type Sexual

type Sexual struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

type Usage

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

type Violence

type Violence struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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