feat: impl. multi-model / attachments, images

This commit is contained in:
Eugen Eisler
2024-10-29 20:38:15 +01:00
parent 0a2ae30034
commit 69aefc16f6
13 changed files with 136 additions and 140 deletions

View File

@@ -66,7 +66,7 @@ func (an *Client) ListModels() (ret []string, err error) {
}
func (an *Client) SendStream(
msgs []*common.Message, opts *common.ChatOptions, channel chan string,
msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions, channel chan string,
) (err error) {
ctx := context.Background()
req := an.buildMessagesRequest(msgs, opts)
@@ -91,7 +91,7 @@ func (an *Client) SendStream(
return
}
func (an *Client) Send(ctx context.Context, msgs []*common.Message, opts *common.ChatOptions) (ret string, err error) {
func (an *Client) Send(ctx context.Context, msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions) (ret string, err error) {
req := an.buildMessagesRequest(msgs, opts)
req.Stream = false
@@ -109,7 +109,7 @@ func (an *Client) Send(ctx context.Context, msgs []*common.Message, opts *common
return
}
func (an *Client) buildMessagesRequest(msgs []*common.Message, opts *common.ChatOptions) (ret anthropic.MessagesRequest) {
func (an *Client) buildMessagesRequest(msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions) (ret anthropic.MessagesRequest) {
temperature := float32(opts.Temperature)
topP := float32(opts.TopP)
@@ -125,7 +125,7 @@ func (an *Client) buildMessagesRequest(msgs []*common.Message, opts *common.Chat
return
}
func (an *Client) toMessages(msgs []*common.Message) (ret []anthropic.Message) {
func (an *Client) toMessages(msgs []*goopenai.ChatCompletionMessage) (ret []anthropic.Message) {
// we could call the method before calling the specific vendor
normalizedMessages := common.NormalizeMessages(msgs, an.defaultRequiredUserMessage)

View File

@@ -22,7 +22,7 @@ func (c *Client) ListModels() ([]string, error) {
return []string{"dry-run-model"}, nil
}
func (c *Client) SendStream(msgs []*common.Message, opts *common.ChatOptions, channel chan string) error {
func (c *Client) SendStream(msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions, channel chan string) error {
output := "Dry run: Would send the following request:\n\n"
for _, msg := range msgs {
@@ -50,7 +50,7 @@ func (c *Client) SendStream(msgs []*common.Message, opts *common.ChatOptions, ch
return nil
}
func (c *Client) Send(_ context.Context, msgs []*common.Message, opts *common.ChatOptions) (string, error) {
func (c *Client) Send(_ context.Context, msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions) (string, error) {
fmt.Println("Dry run: Would send the following request:")
for _, msg := range msgs {

View File

@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/danielmiessler/fabric/plugins"
goopenai "github.com/sashabaranov/go-openai"
"strings"
"github.com/danielmiessler/fabric/common"
@@ -58,7 +59,7 @@ func (o *Client) ListModels() (ret []string, err error) {
return
}
func (o *Client) Send(ctx context.Context, msgs []*common.Message, opts *common.ChatOptions) (ret string, err error) {
func (o *Client) Send(ctx context.Context, msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions) (ret string, err error) {
systemInstruction, messages := toMessages(msgs)
var client *genai.Client
@@ -89,7 +90,7 @@ func (o *Client) buildModelNameFull(modelName string) string {
return fmt.Sprintf("%v%v", modelsNamePrefix, modelName)
}
func (o *Client) SendStream(msgs []*common.Message, opts *common.ChatOptions, channel chan string) (err error) {
func (o *Client) SendStream(msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions, channel chan string) (err error) {
ctx := context.Background()
var client *genai.Client
if client, err = genai.NewClient(ctx, option.WithAPIKey(o.ApiKey.Value)); err != nil {
@@ -141,7 +142,7 @@ func (o *Client) extractText(response *genai.GenerateContentResponse) (ret strin
return
}
func toMessages(msgs []*common.Message) (systemInstruction *genai.Content, messages []genai.Part) {
func toMessages(msgs []*goopenai.ChatCompletionMessage) (systemInstruction *genai.Content, messages []genai.Part) {
if len(msgs) >= 2 {
systemInstruction = &genai.Content{
Parts: []genai.Part{

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/danielmiessler/fabric/plugins"
goopenai "github.com/sashabaranov/go-openai"
"net/http"
"net/url"
"time"
@@ -62,7 +63,7 @@ func (o *Client) ListModels() (ret []string, err error) {
return
}
func (o *Client) SendStream(msgs []*common.Message, opts *common.ChatOptions, channel chan string) (err error) {
func (o *Client) SendStream(msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions, channel chan string) (err error) {
req := o.createChatRequest(msgs, opts)
respFunc := func(resp ollamaapi.ChatResponse) (streamErr error) {
@@ -80,7 +81,7 @@ func (o *Client) SendStream(msgs []*common.Message, opts *common.ChatOptions, ch
return
}
func (o *Client) Send(ctx context.Context, msgs []*common.Message, opts *common.ChatOptions) (ret string, err error) {
func (o *Client) Send(ctx context.Context, msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions) (ret string, err error) {
bf := false
req := o.createChatRequest(msgs, opts)
@@ -97,8 +98,8 @@ func (o *Client) Send(ctx context.Context, msgs []*common.Message, opts *common.
return
}
func (o *Client) createChatRequest(msgs []*common.Message, opts *common.ChatOptions) (ret ollamaapi.ChatRequest) {
messages := lo.Map(msgs, func(message *common.Message, _ int) (ret ollamaapi.Message) {
func (o *Client) createChatRequest(msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions) (ret ollamaapi.ChatRequest) {
messages := lo.Map(msgs, func(message *goopenai.ChatCompletionMessage, _ int) (ret ollamaapi.Message) {
return ollamaapi.Message{Role: message.Role, Content: message.Content}
})

View File

@@ -68,7 +68,7 @@ func (o *Client) ListModels() (ret []string, err error) {
}
func (o *Client) SendStream(
msgs []*common.Message, opts *common.ChatOptions, channel chan string,
msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions, channel chan string,
) (err error) {
req := o.buildChatCompletionRequest(msgs, opts)
req.Stream = true
@@ -104,7 +104,7 @@ func (o *Client) SendStream(
return
}
func (o *Client) Send(ctx context.Context, msgs []*common.Message, opts *common.ChatOptions) (ret string, err error) {
func (o *Client) Send(ctx context.Context, msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions) (ret string, err error) {
req := o.buildChatCompletionRequest(msgs, opts)
var resp goopenai.ChatCompletionResponse
@@ -119,9 +119,9 @@ func (o *Client) Send(ctx context.Context, msgs []*common.Message, opts *common.
}
func (o *Client) buildChatCompletionRequest(
msgs []*common.Message, opts *common.ChatOptions,
msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions,
) (ret goopenai.ChatCompletionRequest) {
messages := lo.Map(msgs, func(message *common.Message, _ int) goopenai.ChatCompletionMessage {
messages := lo.Map(msgs, func(message *goopenai.ChatCompletionMessage, _ int) goopenai.ChatCompletionMessage {
return goopenai.ChatCompletionMessage{Role: message.Role, Content: message.Content}
})

View File

@@ -11,10 +11,10 @@ import (
func TestBuildChatCompletionRequestPinSeed(t *testing.T) {
var msgs []*common.Message
var msgs []*goopenai.ChatCompletionMessage
for i := 0; i < 2; i++ {
msgs = append(msgs, &common.Message{
msgs = append(msgs, &goopenai.ChatCompletionMessage{
Role: "User",
Content: "My msg",
})
@@ -57,10 +57,10 @@ func TestBuildChatCompletionRequestPinSeed(t *testing.T) {
func TestBuildChatCompletionRequestNilSeed(t *testing.T) {
var msgs []*common.Message
var msgs []*goopenai.ChatCompletionMessage
for i := 0; i < 2; i++ {
msgs = append(msgs, &common.Message{
msgs = append(msgs, &goopenai.ChatCompletionMessage{
Role: "User",
Content: "My msg",
})

View File

@@ -3,6 +3,7 @@ package ai
import (
"context"
"github.com/danielmiessler/fabric/plugins"
goopenai "github.com/sashabaranov/go-openai"
"github.com/danielmiessler/fabric/common"
)
@@ -10,6 +11,6 @@ import (
type Vendor interface {
plugins.Plugin
ListModels() ([]string, error)
SendStream([]*common.Message, *common.ChatOptions, chan string) error
Send(context.Context, []*common.Message, *common.ChatOptions) (string, error)
SendStream([]*goopenai.ChatCompletionMessage, *common.ChatOptions, chan string) error
Send(context.Context, []*goopenai.ChatCompletionMessage, *common.ChatOptions) (string, error)
}

View File

@@ -3,6 +3,7 @@ package fsdb
import (
"fmt"
"github.com/danielmiessler/fabric/common"
goopenai "github.com/sashabaranov/go-openai"
)
type SessionsEntity struct {
@@ -36,16 +37,16 @@ func (o *SessionsEntity) SaveSession(session *Session) (err error) {
type Session struct {
Name string
Messages []*common.Message
Messages []*goopenai.ChatCompletionMessage
vendorMessages []*common.Message
vendorMessages []*goopenai.ChatCompletionMessage
}
func (o *Session) IsEmpty() bool {
return len(o.Messages) == 0
}
func (o *Session) Append(messages ...*common.Message) {
func (o *Session) Append(messages ...*goopenai.ChatCompletionMessage) {
if o.vendorMessages != nil {
for _, message := range messages {
o.Messages = append(o.Messages, message)
@@ -56,9 +57,9 @@ func (o *Session) Append(messages ...*common.Message) {
}
}
func (o *Session) GetVendorMessages() (ret []*common.Message) {
func (o *Session) GetVendorMessages() (ret []*goopenai.ChatCompletionMessage) {
if o.vendorMessages == nil {
o.vendorMessages = []*common.Message{}
o.vendorMessages = []*goopenai.ChatCompletionMessage{}
for _, message := range o.Messages {
o.appendVendorMessage(message)
}
@@ -67,13 +68,13 @@ func (o *Session) GetVendorMessages() (ret []*common.Message) {
return
}
func (o *Session) appendVendorMessage(message *common.Message) {
func (o *Session) appendVendorMessage(message *goopenai.ChatCompletionMessage) {
if message.Role != common.ChatMessageRoleMeta {
o.vendorMessages = append(o.vendorMessages, message)
}
}
func (o *Session) GetLastMessage() (ret *common.Message) {
func (o *Session) GetLastMessage() (ret *goopenai.ChatCompletionMessage) {
if len(o.Messages) > 0 {
ret = o.Messages[len(o.Messages)-1]
}

View File

@@ -2,8 +2,6 @@ package fsdb
import (
"testing"
"github.com/danielmiessler/fabric/common"
)
func TestSessions_GetOrCreateSession(t *testing.T) {
@@ -27,7 +25,7 @@ func TestSessions_SaveSession(t *testing.T) {
StorageEntity: &StorageEntity{Dir: dir, FileExtension: ".json"},
}
sessionName := "testSession"
session := &Session{Name: sessionName, Messages: []*common.Message{{Content: "message1"}}}
session := &Session{Name: sessionName, Messages: []*goopenai.ChatCompletionMessage{{Content: "message1"}}}
err := sessions.SaveSession(session)
if err != nil {
t.Fatalf("failed to save session: %v", err)