-
Notifications
You must be signed in to change notification settings - Fork 3
/
MilvusCrewAITool.py
32 lines (29 loc) · 1.36 KB
/
MilvusCrewAITool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.vectorstores import Milvus
from langchain_community.embeddings import OllamaEmbeddings
from langchain.agents import Tool
class MilvusTool(Tool):
def __init__(self, connection_args=None, collection_name="default"):
"""Initialize vector database"""
super().__init__(name="MilvusTool", func=self.__init__, description="Initialize the vector database")
def store_documents(self, documents):
"""Store documents into the vector database"""
embeddings = OllamaEmbeddings(model="llama-pro:8b-instruct-fp16")
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents([documents])
vector_db = Milvusfrom_documents(
docs,
embeddings,
connection_args={"host": "127.0.0.1", "port": "19530"},
collection_name="default"
)
def retrieve_documents(self, query):
"""Retrieve documents from the vector database"""
embeddings = OllamaEmbeddings(model="llama-pro:8b-instruct-fp16")
vector_db = Milvus(
embeddings,
connection_args={"host": "127.0.0.1", "port": "19530"},
collection_name="default"
)
fl = vector_db.similarity_search(query)
return [doc["data"] for doc in fl]