chatbot

package
v0.0.0-...-ac3c55a Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 13 Imported by: 6

Documentation

Index

Constants

View Source
const (
	ChatRoleUser = "user"
	ChatRoleAI   = "ai"

	AIName_OpenAI = "openai"
	AIName_Gemini = "gemini"
	AIName_Claude = "claude"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Chatbot

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

Chatbot 是聊天机器人结构体

func MustChatbot

func MustChatbot() *Chatbot

func NewChatbot

func NewChatbot(config *Config) *Chatbot

NewChatbot 返回一个新的Chatbot实例

func (*Chatbot) AddChatSessionCtx

func (c *Chatbot) AddChatSessionCtx(userID string, content string, role, aiName string)

func (*Chatbot) ClaudeRequest

func (c *Chatbot) ClaudeRequest(cache *chatResponseCache, userID string, input string) (string, error)

func (*Chatbot) GeminiRequest

func (c *Chatbot) GeminiRequest(cache *chatResponseCache, userID string, input string) (string, error)

func (*Chatbot) GetChatMessageFromDB

func (c *Chatbot) GetChatMessageFromDB(userID, aiName string) []chatMessage

func (*Chatbot) GetChatSessionCtx

func (c *Chatbot) GetChatSessionCtx(userID string, ctxs []openai.ChatMessage) []openai.ChatMessage

func (*Chatbot) GetClaudeChatSessionCtx

func (c *Chatbot) GetClaudeChatSessionCtx(userID string, ctxs []claude.Message) []claude.Message

func (*Chatbot) GetGeminiChatSessionCtx

func (c *Chatbot) GetGeminiChatSessionCtx(userID string) []*genai.Content

func (*Chatbot) GetResponse

func (c *Chatbot) GetResponse(userID string, input string) (string, error)

GetResponse 调用聊天机器人API获取响应

func (*Chatbot) OpenAIRequest

func (c *Chatbot) OpenAIRequest(cache *chatResponseCache, userID string, input string) (string, error)

func (*Chatbot) RegsiterMessagePublish

func (c *Chatbot) RegsiterMessagePublish(publisher func(string, string) error)

注册聊天消息的异步推送回调 其实这里比较好的设计应该是调用ChatBot的调用方,在发起聊天请求中注册一下异步推送的回调,这样就可以支持不同的pusher了

func (*Chatbot) WaitChatResponse

func (c *Chatbot) WaitChatResponse(userID string)

type ClaudeConfig

type ClaudeConfig struct {
	ApiKey string `json:"api_key"`
	Enable bool   `json:"enable"`
}

claude配置

type Config

type Config struct {
	OpenAI OpenAIConfig `json:"open_ai"`
	Gemini GeminiConfig `json:"gemini"`
	Claude ClaudeConfig `json:"claude"`
	Redis  RedisConfig  `json:"redis"`
}

type GeminiConfig

type GeminiConfig struct {
	ApiKey string `json:"api_key"`
	Enable bool   `json:"enable"`
}

gemini配置

type OpenAIConfig

type OpenAIConfig struct {
	ApiKey string `json:"api_key"`
	Enable bool   `json:"enable"`
}

openai配置

type RedisConfig

type RedisConfig struct {
	Addr     string `json:"addr"`
	Username string `json:"username"`
	Password string `json:"password"`
	DB       int    `json:"db"`
	Enable   bool   `json:"enable"`
}

Jump to

Keyboard shortcuts

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