generated from The-Swarm-Corporation/Multi-Agent-Template-App
-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Kye Gomez
authored and
Kye Gomez
committed
Jul 8, 2024
1 parent
5ce2102
commit 8eb3908
Showing
9 changed files
with
430 additions
and
3 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
File renamed without changes.
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,78 @@ | ||
from typing import List, Dict, Any | ||
from swarms_memory.faiss_wrapper import FAISSDB | ||
|
||
|
||
from transformers import AutoTokenizer, AutoModel | ||
import torch | ||
|
||
|
||
# Custom embedding function using a HuggingFace model | ||
def custom_embedding_function(text: str) -> List[float]: | ||
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") | ||
model = AutoModel.from_pretrained("bert-base-uncased") | ||
inputs = tokenizer( | ||
text, | ||
return_tensors="pt", | ||
padding=True, | ||
truncation=True, | ||
max_length=512, | ||
) | ||
with torch.no_grad(): | ||
outputs = model(**inputs) | ||
embeddings = ( | ||
outputs.last_hidden_state.mean(dim=1).squeeze().tolist() | ||
) | ||
return embeddings | ||
|
||
|
||
# Custom preprocessing function | ||
def custom_preprocess(text: str) -> str: | ||
return text.lower().strip() | ||
|
||
|
||
# Custom postprocessing function | ||
def custom_postprocess( | ||
results: List[Dict[str, Any]], | ||
) -> List[Dict[str, Any]]: | ||
for result in results: | ||
result["custom_score"] = ( | ||
result["score"] * 2 | ||
) # Example modification | ||
return results | ||
|
||
|
||
# Initialize the wrapper with custom functions | ||
wrapper = FAISSDB( | ||
dimension=768, | ||
index_type="Flat", | ||
embedding_function=custom_embedding_function, | ||
preprocess_function=custom_preprocess, | ||
postprocess_function=custom_postprocess, | ||
metric="cosine", | ||
logger_config={ | ||
"handlers": [ | ||
{ | ||
"sink": "custom_faiss_rag_wrapper.log", | ||
"rotation": "1 GB", | ||
}, | ||
{"sink": lambda msg: print(f"Custom log: {msg}", end="")}, | ||
], | ||
}, | ||
) | ||
|
||
# Adding documents | ||
wrapper.add( | ||
"This is a sample document about artificial intelligence.", | ||
{"category": "AI"}, | ||
) | ||
wrapper.add( | ||
"Python is a popular programming language for data science.", | ||
{"category": "Programming"}, | ||
) | ||
|
||
# Querying | ||
results = wrapper.query("What is AI?") | ||
for result in results: | ||
print( | ||
f"Score: {result['score']}, Custom Score: {result['custom_score']}, Text: {result['metadata']['text']}" | ||
) |
File renamed without changes.
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from swarms_memory.chroma_db_wrapper import ChromaDB | ||
from swarms_memory.pinecone_wrapper import PineconeMemory | ||
from swarms_memory.faiss_wrapper import FAISSDB | ||
|
||
__all__ = ["ChromaDB", "PineconeMemory"] | ||
__all__ = ["ChromaDB", "PineconeMemory", "FAISSDB"] |
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.