multi_ai_client

package module
v0.0.0-...-5c71440 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 11 Imported by: 0

README

multi-ai-client

Go wrapper to interact with and select between multiple AI services simultaneously.

Example usage is provided in cmd/example/main.go.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APISettings

type APISettings struct {
	APIKey      string
	APIEndpoint string
	APIType     APIType
}

APISettings is a struct representing the settings needed to interact with an API. It contains the API key, the API endpoint, and the API type.

type APIType

type APIType int

APIType is an enum representing the type of API. It determines how the client interacts with the API, and which settings are required or supported.

const (
	OpenAI APIType = iota
	Mistral
	Anthropic
)

type AnthropicMetadata

type AnthropicMetadata struct {
	UserID string `json:"user_id"`
}

type Chat

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

Chat is a struct representing a chat between a user and an assistant.

func NewChatFromMessages

func NewChatFromMessages(messages []Message) (*Chat, error)

NewChatFromMessages creates a new Chat from a list of messages. The messages are added to the chat in the order they are provided. There may only be one system message in the list of messages, and it must be the first message.

func (*Chat) AddAssistantMessage

func (c *Chat) AddAssistantMessage(s string)

AddAssistantMessage Adds an assistant message to the chat.

func (*Chat) AddUserMessage

func (c *Chat) AddUserMessage(s string)

AddUserMessage Adds a user message to the chat.

func (*Chat) ClearMessages

func (c *Chat) ClearMessages()

ClearMessages Removes all non-system messages from the chat.

func (*Chat) ClearSystemMessage

func (c *Chat) ClearSystemMessage()

ClearSystemMessage Removes the system message from the chat.

func (*Chat) GetMessages

func (c *Chat) GetMessages() []Message

GetMessages returns all messages in the chat.

func (*Chat) GetMessagesWithoutSystemMessage

func (c *Chat) GetMessagesWithoutSystemMessage() []Message

GetMessagesWithoutSystemMessage returns all messages in the chat except the system message.

func (*Chat) GetSystemMessage

func (c *Chat) GetSystemMessage() string

GetSystemMessage returns the system message in the chat.

func (*Chat) ReplaceLastAssistantMessage

func (c *Chat) ReplaceLastAssistantMessage(s string)

ReplaceLastAssistantMessage Replaces the last assistant message in the chat with a new message. If the last message is not an assistant message, this function does nothing.

func (*Chat) SetSystemMessage

func (c *Chat) SetSystemMessage(s string)

SetSystemMessage Adds a system message to the chat.

func (*Chat) String

func (c *Chat) String() string

type Client

type Client struct {
	Chat Chat
	// contains filtered or unexported fields
}

func (*Client) AddModelDefinition

func (c *Client) AddModelDefinition(modelDefinition ModelDefinition)

AddModelDefinition adds a model definition to the client.

func (*Client) CreateResponse

func (c *Client) CreateResponse() (int, chan MessageChunk, error)

CreateResponse creates a response to a user prompt using the model definitions added to the client. It returns the total amount of responses initiated, a channel to receive message chunks, and an error if one occurred.

func (*Client) CreateResponseWithPrompt

func (c *Client) CreateResponseWithPrompt(usrPrompt string, assistantResponse string) (int, chan MessageChunk, error)

CreateResponseWithPrompt creates a response to a user prompt using the model definitions added to the client. If functions like CreateResponse, but allows for a user prompt and assistant response to be passed in first.

func (*Client) ResetChat

func (c *Client) ResetChat()

ResetChat resets the chat history of the client.

func (Client) String

func (c Client) String() string

type JsonMessage

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

func NewJsonMessageFromMessage

func NewJsonMessageFromMessage(message Message) JsonMessage

type Message

type Message struct {
	Type MessageType
	Text string
}

Message is a struct representing a message in a chat. It has a type and a text.

func NewAssistantMessage

func NewAssistantMessage(text string) *Message

NewAssistantMessage Creates a new Message with the given text of type AssistantMessage.

func NewMessage

func NewMessage(t MessageType, text string) *Message

NewMessage Creates a new Message with the given type and text.

func NewSystemMessage

func NewSystemMessage(text string) *Message

NewSystemMessage Creates a new Message with the given text of type SystemMessage.

func NewUserMessage

func NewUserMessage(text string) *Message

NewUserMessage Creates a new Message with the given text of type UserMessage.

type MessageChunk

type MessageChunk struct {
	Index int
	Delta string
}

type MessageType

type MessageType int

MessageType is an enum representing the type of a message in a chat. It can be a SystemMessage, UserMessage, or AssistantMessage. Chats usually start with a SystemMessage, followed by an alternating sequence of UserMessage and AssistantMessage.

const (
	SystemMessage MessageType = iota
	UserMessage
	AssistantMessage
)

type MistralResponseFormat

type MistralResponseFormat struct {
	Type string `json:"type"`
}

type ModelDefinition

type ModelDefinition struct {
	Name          string
	APISettings   APISettings
	ModelSettings ModelSettings
}

ModelDefinition is a struct representing a model definition. It contains a friendly name for the model and all the settings needed to interact with the model.

func NewModelDefinition

func NewModelDefinition(name string, apiType APIType, apiKey string, modelName string) ModelDefinition

NewModelDefinition creates a new ModelDefinition with the given name, API type, API key, and model name. By default, the API endpoint is left as the default value and only the model name is set in the settings.

func (*ModelDefinition) CreateRequest

func (m *ModelDefinition) CreateRequest(chat Chat) (*http.Request, error)

type ModelSettings

type ModelSettings interface {
	// MakeBody creates the body of the request to the model API.
	MakeBody(chat Chat) []byte

	Set(key string, value interface{}) error
}

ModelSettings is an interface representing the settings needed to interact with a model.

func NewModelSettings

func NewModelSettings(apiType APIType, modelName string) ModelSettings

type ModelSettingsAnthropic

type ModelSettingsAnthropic struct {
	ModelSettings `json:"-"`
	Model         string             `json:"model"`
	MaxTokens     int                `json:"max_tokens"`
	Metadata      *AnthropicMetadata `json:"metadata,omitempty"`
	StopSequences []string           `json:"stop_sequences,omitempty"`
	Stream        null.Bool          `json:"stream,omitempty"`
	Temperature   null.Float         `json:"temperature,omitempty"`
	TopK          null.Int           `json:"top_k,omitempty"`
	TopP          null.Float         `json:"top_p,omitempty"`
	Messages      []JsonMessage      `json:"messages"`
	System        null.String        `json:"system,omitempty"`
}

func (ModelSettingsAnthropic) MakeBody

func (m ModelSettingsAnthropic) MakeBody(chat Chat) []byte

func (ModelSettingsAnthropic) Set

func (m ModelSettingsAnthropic) Set(key string, value interface{}) error

type ModelSettingsMistral

type ModelSettingsMistral struct {
	ModelSettings  `json:"-"`
	Model          string                 `json:"model"`
	ResponseFormat *MistralResponseFormat `json:"response_format,omitempty"`
	Temperature    null.Float             `json:"temperature,omitempty"`
	TopP           null.Float             `json:"top_p,omitempty"`
	MaxTokens      null.Int               `json:"max_tokens,omitempty"`
	Stream         null.Bool              `json:"stream,omitempty"`
	SafePrompt     null.Bool              `json:"safe_prompt,omitempty"`
	RandomSeed     null.Int               `json:"random_seed,omitempty"`
	Messages       []JsonMessage          `json:"messages"`
}

func (ModelSettingsMistral) MakeBody

func (m ModelSettingsMistral) MakeBody(chat Chat) []byte

func (ModelSettingsMistral) Set

func (m ModelSettingsMistral) Set(key string, value interface{}) error

type ModelSettingsOpenAI

type ModelSettingsOpenAI struct {
	ModelSettings    `json:"-"`
	Model            string                `json:"model"`
	FrequencyPenalty null.Float            `json:"frequency_penalty,omitempty"`
	LogitBias        map[string]int        `json:"logit_bias,omitempty"`
	Logprobs         null.Bool             `json:"logprobs,omitempty"`
	TopLogprobs      null.Int              `json:"top_logprobs,omitempty"`
	MaxTokens        null.Int              `json:"max_tokens,omitempty"`
	PresencePenalty  null.Float            `json:"presence_penalty,omitempty"`
	ResponseFormat   *OpenAIResponseFormat `json:"response_format,omitempty"`
	Seed             null.Int              `json:"seed,omitempty"`
	Stop             []string              `json:"stop,omitempty"`
	Stream           null.Bool             `json:"stream,omitempty"`
	Temperature      null.Float            `json:"temperature,omitempty"`
	TopP             null.Float            `json:"top_p,omitempty"`
	User             null.String           `json:"user,omitempty"`
	Messages         []JsonMessage         `json:"messages"`
}

func (ModelSettingsOpenAI) MakeBody

func (m ModelSettingsOpenAI) MakeBody(chat Chat) []byte

func (ModelSettingsOpenAI) Set

func (m ModelSettingsOpenAI) Set(key string, value interface{}) error

type OpenAIResponseFormat

type OpenAIResponseFormat struct {
	Type string `json:"type"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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