mirror of https://github.com/velour/catbase.git
llm: failover locally
This commit is contained in:
parent
ed4136aa60
commit
276f6c188e
|
@ -38,6 +38,12 @@ func New(b bot.Bot) *LLMPlugin {
|
||||||
|
|
||||||
func (p *LLMPlugin) register() {
|
func (p *LLMPlugin) register() {
|
||||||
p.h = bot.HandlerTable{
|
p.h = bot.HandlerTable{
|
||||||
|
{
|
||||||
|
Kind: bot.Message, IsCmd: true,
|
||||||
|
Regex: regexp.MustCompile(`(?is)^llm (?P<text>.*)`),
|
||||||
|
HelpText: "chat completion",
|
||||||
|
Handler: p.chatMessageForce,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Kind: bot.Message, IsCmd: true,
|
Kind: bot.Message, IsCmd: true,
|
||||||
Regex: regexp.MustCompile(`(?is)^gpt (?P<text>.*)`),
|
Regex: regexp.MustCompile(`(?is)^gpt (?P<text>.*)`),
|
||||||
|
|
|
@ -11,16 +11,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var InstanceNotFoundError = errors.New("instance not found")
|
var InstanceNotFoundError = errors.New("instance not found")
|
||||||
var empty = chatEntry{}
|
var empty = llamaResponse{}
|
||||||
|
|
||||||
func (g *LLMPlugin) llama() (chatEntry, error) {
|
func (g *LLMPlugin) llama() (chatEntry, error) {
|
||||||
llamaURL := g.c.Get("gpt.llamaurl", "")
|
llamaURL := g.c.GetArray("gpt.llamaurls", []string{})
|
||||||
if llamaURL == "" {
|
if len(llamaURL) == 0 {
|
||||||
return empty, fmt.Errorf("could not find llama url")
|
return chatEntry{}, fmt.Errorf("could not find llama url")
|
||||||
}
|
}
|
||||||
llamaModel := g.c.Get("gpt.llamamodel", "")
|
llamaModel := g.c.Get("gpt.llamamodel", "")
|
||||||
if llamaModel == "" {
|
if llamaModel == "" {
|
||||||
return empty, fmt.Errorf("could not find llama model")
|
return chatEntry{}, fmt.Errorf("could not find llama model")
|
||||||
}
|
}
|
||||||
|
|
||||||
req := llamaRequest{
|
req := llamaRequest{
|
||||||
|
@ -29,6 +29,19 @@ func (g *LLMPlugin) llama() (chatEntry, error) {
|
||||||
Stream: false,
|
Stream: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, u := range llamaURL {
|
||||||
|
llamaResp, err := mkRequest(u, req)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return llamaResp.Message, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return chatEntry{}, InstanceNotFoundError
|
||||||
|
}
|
||||||
|
|
||||||
|
func mkRequest(llamaURL string, req llamaRequest) (llamaResponse, error) {
|
||||||
body, err := json.Marshal(req)
|
body, err := json.Marshal(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return empty, fmt.Errorf("could not marshal llama request: %w", err)
|
return empty, fmt.Errorf("could not marshal llama request: %w", err)
|
||||||
|
@ -50,7 +63,7 @@ func (g *LLMPlugin) llama() (chatEntry, error) {
|
||||||
return empty, fmt.Errorf("could not unmarshal llama response: %w, raw: %s", err, string(body))
|
return empty, fmt.Errorf("could not unmarshal llama response: %w, raw: %s", err, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
return llamaResp.Message, nil
|
return llamaResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type llamaRequest struct {
|
type llamaRequest struct {
|
||||||
|
|
Loading…
Reference in New Issue