Documentation
¶
Index ¶
- func WithContext[Req, Res, Stream any](ctx context.Context, binding ChatBinding[Req, Res, Stream]) context.Context
- type AssistantMessage
- type Chat
- type ChatBase
- type ChatBinding
- type ChatHistory
- func (chatHistory *ChatHistory) GetHistory() ChatHistory
- func (chatHistory *ChatHistory) PushHistory(messages ...MessageWithRole)
- func (chatHistory *ChatHistory) SetHistory(history ChatHistory)
- func (chatHistory *ChatHistory) SetSystem(system *SystemMessage)
- func (chatHistory *ChatHistory) String() string
- type ChatRaw
- type ChatWithBinding
- func (chat *ChatWithBinding[RawRequest, RawResponse, StreamResponse]) Completion(ctx context.Context, message UserMessage, options CompletionParams) (*AssistantMessage, error)
- func (chat *ChatWithBinding[RawRequest, RawResponse, StreamResponse]) CompletionJSON(ctx context.Context, message UserMessage, options CompletionParams, dest any) error
- func (chat *ChatWithBinding[RawRequest, RawResponse, StreamResponse]) CompletionStream(ctx context.Context, message UserMessage, options CompletionParams) (<-chan string, utils.StreamWaitFn)
- func (chat *ChatWithBinding[RawRequest, RawResponse, StreamResponse]) RawQuery(ctx context.Context, request RawRequest) (RawResponse, error)
- type CompletionParams
- type MessageRole
- type MessageWithRole
- type SystemMessage
- type UserMessage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithContext ¶ added in v0.1.1
func WithContext[Req, Res, Stream any](ctx context.Context, binding ChatBinding[Req, Res, Stream]) context.Context
WithContext associates a new Chat instance with the current context, from the provided binding.
Types ¶
type AssistantMessage ¶
type AssistantMessage struct {
Content string
}
func NewAssistantMessage ¶
func NewAssistantMessage(content string) AssistantMessage
func NewAssistantMessageF ¶ added in v0.1.4
func NewAssistantMessageF(format string, args ...interface{}) AssistantMessage
func NewAssistantMessageT ¶ added in v0.1.4
func (AssistantMessage) GetContent ¶
func (message AssistantMessage) GetContent() string
func (AssistantMessage) Role ¶
func (message AssistantMessage) Role() MessageRole
func (AssistantMessage) String ¶
func (message AssistantMessage) String() string
type Chat ¶
func ContextWithRaw ¶ added in v0.1.1
func NewChat ¶
func NewChat[Req, Res, Stream any](binding ChatBinding[Req, Res, Stream]) Chat[Req, Res]
NewChat returns a new Chat instance based on the provided binding.
Since a chat instance maintains its own history, it is recommended to create a new chat instance for each new process.
type ChatBase ¶
type ChatBase interface {
// SetHistory replaces the current history with the provided one.
SetHistory(history ChatHistory)
// GetHistory returns the current history.
//
// The returned history is a copy of the one used internally, so you can modify it without affecting the interna
// state.
GetHistory() ChatHistory
// PushHistory adds the provided messages to the history.
PushHistory(messages ...MessageWithRole)
// SetSystem replaces the current system message with the provided one.
SetSystem(system *SystemMessage)
// Completion returns the LLM response to the provided message, based on user history.
//
// The LLM response is automatically added to history.
Completion(
ctx context.Context, message UserMessage, options CompletionParams,
) (response *AssistantMessage, err error)
// CompletionJSON ets the LLM response to the provided message, and attempts to parse it into the desired
// destination.
//
// You might need to instruct your model to return JSON depending on the selected binding.
//
// The LLM response is automatically added to history.
CompletionJSON(ctx context.Context, message UserMessage, options CompletionParams, dest any) (err error)
// CompletionStream returns a channel that will be filled with the LLM response to the provided message, as
// data bits are received from the LLM. The channel must be automatically closed by the implementation, so you
// just have to worry about reading from it.
//
// The LLM response is automatically added to history. If the stream is closed early, then the already read bits
// might form an incomplete response in the history.
CompletionStream(
ctx context.Context, message UserMessage, options CompletionParams,
) (response <-chan string, wait utils.StreamWaitFn)
}
type ChatBinding ¶
type ChatBinding[RawRequest, RawResponse, StreamResponse any] interface { RawQuery(ctx context.Context, request RawRequest, history ChatHistory) (response RawResponse, err error) Completion( ctx context.Context, message UserMessage, options CompletionParams, history ChatHistory, ) (response *AssistantMessage, err error) CompletionStream( ctx context.Context, message UserMessage, options CompletionParams, history ChatHistory, ) (response <-chan StreamResponse, wait utils.StreamWaitFn) StreamResponseToMessage(response StreamResponse) (message string) }
ChatBinding represents the minimum set of methods that a binding should implement, for it to be turned into a Chat instance.
type ChatHistory ¶
type ChatHistory struct {
System *SystemMessage
History []MessageWithRole
}
func (*ChatHistory) GetHistory ¶
func (chatHistory *ChatHistory) GetHistory() ChatHistory
func (*ChatHistory) PushHistory ¶
func (chatHistory *ChatHistory) PushHistory(messages ...MessageWithRole)
func (*ChatHistory) SetHistory ¶
func (chatHistory *ChatHistory) SetHistory(history ChatHistory)
func (*ChatHistory) SetSystem ¶
func (chatHistory *ChatHistory) SetSystem(system *SystemMessage)
func (*ChatHistory) String ¶
func (chatHistory *ChatHistory) String() string
type ChatRaw ¶
type ChatRaw[RawRequest, RawResponse any] interface { RawQuery(ctx context.Context, request RawRequest) (response RawResponse, err error) }
ChatRaw is an interface used to access the raw implementation of a LLM binding. It lets you tinkle with all the options offered by the binding, with the downside of letting you in charge of parsing the response and managing messages history.
type ChatWithBinding ¶
type ChatWithBinding[RawRequest, RawResponse, StreamResponse any] struct { ChatHistory // contains filtered or unexported fields }
func (*ChatWithBinding[RawRequest, RawResponse, StreamResponse]) Completion ¶
func (chat *ChatWithBinding[RawRequest, RawResponse, StreamResponse]) Completion( ctx context.Context, message UserMessage, options CompletionParams, ) (*AssistantMessage, error)
func (*ChatWithBinding[RawRequest, RawResponse, StreamResponse]) CompletionJSON ¶
func (chat *ChatWithBinding[RawRequest, RawResponse, StreamResponse]) CompletionJSON( ctx context.Context, message UserMessage, options CompletionParams, dest any, ) error
func (*ChatWithBinding[RawRequest, RawResponse, StreamResponse]) CompletionStream ¶
func (chat *ChatWithBinding[RawRequest, RawResponse, StreamResponse]) CompletionStream( ctx context.Context, message UserMessage, options CompletionParams, ) (<-chan string, utils.StreamWaitFn)
type CompletionParams ¶
type MessageRole ¶
type MessageRole string
const ( MessageRoleUser MessageRole = "user" MessageRoleAssistant MessageRole = "assistant" )
type MessageWithRole ¶
type MessageWithRole interface {
Role() MessageRole
GetContent() string
fmt.Stringer
}
type SystemMessage ¶
type SystemMessage struct {
Content string
}
func NewSystemMessage ¶
func NewSystemMessage(content string) *SystemMessage
func NewSystemMessageF ¶ added in v0.1.4
func NewSystemMessageF(format string, args ...interface{}) *SystemMessage
func NewSystemMessageT ¶ added in v0.1.4
func (SystemMessage) String ¶
func (message SystemMessage) String() string
type UserMessage ¶
type UserMessage struct {
Content string
}
func NewUserMessage ¶
func NewUserMessage(content string) UserMessage
func NewUserMessageF ¶ added in v0.1.4
func NewUserMessageF(format string, args ...interface{}) UserMessage
func NewUserMessageT ¶ added in v0.1.4
func (UserMessage) GetContent ¶
func (message UserMessage) GetContent() string
func (UserMessage) Role ¶
func (message UserMessage) Role() MessageRole
func (UserMessage) String ¶
func (message UserMessage) String() string
Source Files
¶
Click to show internal directories.
Click to hide internal directories.