Skip to content

Commit

Permalink
chore: better nil handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stillmatic committed Oct 8, 2024
1 parent 8d6e66d commit 18b8ac3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/llm/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type InferMessage struct {
Content string
Role string
Image []byte
Audio []byte

ShouldCache bool
}
Expand Down
28 changes: 23 additions & 5 deletions packages/llm/providers/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package google

import (
"context"
"github.com/cespare/xxhash/v2"
"github.com/stillmatic/gollum/packages/llm"
"google.golang.org/api/option"
"log/slog"
"strings"
"time"

"github.com/cespare/xxhash/v2"
"github.com/stillmatic/gollum/packages/llm"
"google.golang.org/api/option"

"github.com/google/generative-ai-go/genai"
"github.com/pkg/errors"
"google.golang.org/api/iterator"
Expand Down Expand Up @@ -74,6 +75,10 @@ func getHash(value string) string {
return string(xxhash.New().Sum([]byte(value)))
}

func getHashBytes(value []byte) string {
return string(xxhash.New().Sum(value))
}

func (p *Provider) uploadFile(ctx context.Context, key string, value string) (*genai.File, error) {
// check if the file is already cached
if _, ok := p.cachedFileMap[key]; ok {
Expand Down Expand Up @@ -153,6 +158,7 @@ func (p *Provider) GenerateResponse(ctx context.Context, req llm.InferRequest) (
// it is slightly better to build a trie, indexed on hashes of each message
// since we can quickly get based on the prefix (i.e. existing messages)
// but ... your number of messages is probably not THAT high to justify the complexity.
// NB: trie also not useful for images/audio
messagesToCache := make([]llm.InferMessage, 0)
for _, message := range req.Messages {
if message.ShouldCache {
Expand All @@ -169,6 +175,12 @@ func (p *Provider) GenerateResponse(ctx context.Context, req llm.InferRequest) (
// it is possible to have collision between user + assistant content being identical
// this feels like a rare case especially given that we are ordering sensitive in the hash.
hashKeys = append(hashKeys, getHash(message.Content))
if len(message.Image) > 0 {
hashKeys = append(hashKeys, getHashBytes(message.Image))
}
if len(message.Audio) > 0 {
hashKeys = append(hashKeys, getHashBytes(message.Audio))
}
}
joinedKey := strings.Join(hashKeys, "/")
var cachedContent *genai.CachedContent
Expand Down Expand Up @@ -199,9 +211,15 @@ func (p *Provider) GenerateResponse(ctx context.Context, req llm.InferRequest) (

func singleTurnMessageToParts(message llm.InferMessage) []genai.Part {
parts := []genai.Part{genai.Text(message.Content)}
if message.Image != nil && len(message.Image) > 0 {
if len(message.Image) > 0 {
// TODO: set the image type based on the actual image type
parts = append(parts, genai.ImageData("png", message.Image))
}
// if len(message.Audio) > 0 {
// // TODO: set the audio type based on the actual audio type
// parts = append(parts, genai.("wav", message.Audio))
// }

return parts
}

Expand All @@ -210,7 +228,7 @@ func multiTurnMessageToParts(messages []llm.InferMessage) ([]*genai.Content, *ge
hist := make([]*genai.Content, 0, len(messages))
for _, message := range messages {
parts := []genai.Part{genai.Text(message.Content)}
if message.Image != nil && len(message.Image) > 0 {
if len(message.Image) > 0 {
parts = append(parts, genai.ImageData("png", message.Image))
}
if message.Role == "system" {
Expand Down

0 comments on commit 18b8ac3

Please sign in to comment.