-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat Add redis as vector database (#145)
- Loading branch information
Showing
8 changed files
with
372 additions
and
17 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
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,107 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/RediSearch/redisearch-go/v2/redisearch" | ||
openaiembedder "github.com/henomis/lingoose/embedder/openai" | ||
"github.com/henomis/lingoose/index" | ||
indexoption "github.com/henomis/lingoose/index/option" | ||
"github.com/henomis/lingoose/index/vectordb/redis" | ||
"github.com/henomis/lingoose/llm/openai" | ||
"github.com/henomis/lingoose/loader" | ||
"github.com/henomis/lingoose/prompt" | ||
"github.com/henomis/lingoose/textsplitter" | ||
) | ||
|
||
// download https://raw.githubusercontent.com/hwchase17/chat-your-data/master/state_of_the_union.txt | ||
|
||
func main() { | ||
index := index.New( | ||
redis.New( | ||
redis.Options{ | ||
RedisearchClient: redisearch.NewClient("localhost:6379", "test"), | ||
CreateIndex: &redis.CreateIndexOptions{ | ||
Dimension: 1536, | ||
Distance: redis.DistanceCosine, | ||
}, | ||
}, | ||
), | ||
openaiembedder.New(openaiembedder.AdaEmbeddingV2), | ||
).WithIncludeContents(true) | ||
|
||
indexIsEmpty, err := index.IsEmpty(context.Background()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if indexIsEmpty { | ||
err = ingestData(index) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
query := "What is the purpose of the NATO Alliance?" | ||
similarities, err := index.Query( | ||
context.Background(), | ||
query, | ||
indexoption.WithTopK(3), | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
content := "" | ||
for _, similarity := range similarities { | ||
fmt.Printf("Similarity: %f\n", similarity.Score) | ||
fmt.Printf("Document: %s\n", similarity.Content()) | ||
fmt.Println("Metadata: ", similarity.Metadata) | ||
fmt.Println("ID: ", similarity.ID) | ||
fmt.Println("----------") | ||
content += similarity.Content() + "\n" | ||
} | ||
|
||
llmOpenAI := openai.NewCompletion().WithVerbose(true) | ||
|
||
prompt1 := prompt.NewPromptTemplate( | ||
"Based on the following context answer to the question.\n\nContext:\n{{.context}}\n\nQuestion: {{.query}}"). | ||
WithInputs( | ||
map[string]string{ | ||
"query": query, | ||
"context": content, | ||
}, | ||
) | ||
|
||
err = prompt1.Format(nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = llmOpenAI.Completion(context.Background(), prompt1.String()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func ingestData(redisIndex *index.Index) error { | ||
documents, err := loader.NewDirectoryLoader(".", ".txt").Load(context.Background()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
textSplitter := textsplitter.NewRecursiveCharacterTextSplitter(1000, 20) | ||
|
||
documentChunks := textSplitter.SplitDocuments(documents) | ||
|
||
for _, doc := range documentChunks { | ||
fmt.Println(doc.Content) | ||
fmt.Println("----------") | ||
fmt.Println(doc.Metadata) | ||
fmt.Println("----------") | ||
fmt.Println() | ||
} | ||
|
||
return redisIndex.LoadFromDocuments(context.Background(), documentChunks) | ||
} |
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
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
Oops, something went wrong.