-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
187 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
openaiembedder "github.com/henomis/lingoose/embedder/openai" | ||
simplevectorindex "github.com/henomis/lingoose/index/simpleVectorIndex" | ||
"github.com/henomis/lingoose/llm/cache" | ||
"github.com/henomis/lingoose/llm/openai" | ||
) | ||
|
||
func main() { | ||
|
||
embedder := openaiembedder.New(openaiembedder.AdaEmbeddingV2) | ||
index := simplevectorindex.New("db", ".", embedder) | ||
llm := openai.NewCompletion().WithCompletionCache(cache.New(embedder, index).WithTopK(3)) | ||
|
||
for { | ||
text := askUserInput("What is your question?") | ||
|
||
response, err := llm.Completion(context.Background(), text) | ||
if err != nil { | ||
fmt.Println(err) | ||
continue | ||
} | ||
|
||
fmt.Println(response) | ||
} | ||
} | ||
|
||
func askUserInput(question string) string { | ||
fmt.Printf("%s > ", question) | ||
reader := bufio.NewReader(os.Stdin) | ||
name, _ := reader.ReadString('\n') | ||
name = strings.TrimSuffix(name, "\n") | ||
return name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/henomis/lingoose/embedder" | ||
"github.com/henomis/lingoose/index" | ||
indexoption "github.com/henomis/lingoose/index/option" | ||
"github.com/henomis/lingoose/types" | ||
) | ||
|
||
type Embedder interface { | ||
Embed(ctx context.Context, texts []string) ([]embedder.Embedding, error) | ||
} | ||
|
||
type Index interface { | ||
Search(context.Context, []float64, ...indexoption.Option) (index.SearchResults, error) | ||
Add(context.Context, *index.Data) error | ||
} | ||
|
||
var ErrCacheMiss = fmt.Errorf("cache miss") | ||
|
||
const ( | ||
defaultTopK = 1 | ||
defaultScoreThreshold = 0.9 | ||
cacheAnswerMetadataKey = "cache-answer" | ||
) | ||
|
||
type Cache struct { | ||
embedder Embedder | ||
index Index | ||
topK int | ||
scoreThreshold float64 | ||
} | ||
|
||
type CacheResult struct { | ||
Answer []string | ||
Embedding []float64 | ||
} | ||
|
||
func New(embedder Embedder, index Index) *Cache { | ||
return &Cache{ | ||
embedder: embedder, | ||
index: index, | ||
topK: defaultTopK, | ||
scoreThreshold: defaultScoreThreshold, | ||
} | ||
} | ||
|
||
func (c *Cache) WithTopK(topK int) *Cache { | ||
c.topK = topK | ||
return c | ||
} | ||
|
||
func (c *Cache) WithScoreThreshold(scoreThreshold float64) *Cache { | ||
c.scoreThreshold = scoreThreshold | ||
return c | ||
} | ||
|
||
func (c *Cache) Get(ctx context.Context, query string) (*CacheResult, error) { | ||
|
||
embedding, err := c.embedder.Embed(ctx, []string{query}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
results, err := c.index.Search(ctx, embedding[0], indexoption.WithTopK(c.topK)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
answers, cacheHit := c.extractResults(results) | ||
if cacheHit { | ||
return &CacheResult{ | ||
Answer: answers, | ||
Embedding: embedding[0], | ||
}, nil | ||
} | ||
|
||
return nil, ErrCacheMiss | ||
} | ||
|
||
func (c *Cache) Set(ctx context.Context, embedding []float64, answer string) error { | ||
return c.index.Add(ctx, &index.Data{ | ||
Values: embedding, | ||
Metadata: types.Meta{ | ||
cacheAnswerMetadataKey: answer, | ||
}, | ||
}) | ||
} | ||
|
||
func (c *Cache) extractResults(results index.SearchResults) ([]string, bool) { | ||
var output []string | ||
|
||
for _, result := range results { | ||
if result.Score > c.scoreThreshold { | ||
answer, ok := result.Metadata[cacheAnswerMetadataKey] | ||
if !ok { | ||
continue | ||
} | ||
|
||
output = append(output, answer.(string)) | ||
} | ||
} | ||
|
||
return output, len(output) > 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters