-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiris.py
More file actions
75 lines (67 loc) · 2.37 KB
/
iris.py
File metadata and controls
75 lines (67 loc) · 2.37 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
from dotenv import load_dotenv
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_loaders import JSONLoader
import json
from pathlib import Path
from langchain_openai import OpenAIEmbeddings
from langchain_community.document_loaders import PyPDFLoader
from langchain_iris import IRISVector
load_dotenv()
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
embeddings = OpenAIEmbeddings()
def load_docs(folder_path, name):
username = 'demo'
password = 'demo'
hostname = os.getenv('IRIS_HOSTNAME', 'localhost')
port = '1972'
namespace = 'USER'
CONNECTION_STRING = f"iris://{username}:{password}@{hostname}:{port}/{namespace}"
COLLECTION_NAME = name
count=0
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
loader =PyPDFLoader(file_path)
documents= loader.load()
text_splitter = CharacterTextSplitter(chunk_size=250, chunk_overlap=50)
data= text_splitter.split_documents(documents)
if count==0:
db = IRISVector.from_documents(
embedding=embeddings,
documents=data,
collection_name=COLLECTION_NAME,
connection_string=CONNECTION_STRING,
)
else:
db = IRISVector(
embedding_function=embeddings,
dimension=1536,
collection_name=COLLECTION_NAME,
connection_string=CONNECTION_STRING,
)
db.add_documents(data)
print("done")
ret= db.similarity_search("hello")
print(ret)
def search_q(query, coll="test"):
embeddings = OpenAIEmbeddings()
username = 'demo'
password = 'demo'
hostname = os.getenv('IRIS_HOSTNAME', 'localhost')
port = '1972'
namespace = 'USER'
CONNECTION_STRING = f"iris://{username}:{password}@{hostname}:{port}/{namespace}"
COLLECTION_NAME = "main"
db = IRISVector(
embedding_function=embeddings,
dimension=1536,
# collection_name=COLLECTION_NAME,
collection_name=coll,
connection_string=CONNECTION_STRING,
)
ret= db.similarity_search(query)
print(ret)
# print(f"Number of docs in vector store: {len(db.get()['ids'])}")
return ret
# load_docs("../docs", "test")
# search_q("what")