-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatBot6.py
125 lines (104 loc) · 4.05 KB
/
ChatBot6.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import cohere
import uuid
import hnswlib
from typing import List, Dict
from unstructured.partition.html import partition_html
from unstructured.chunking.title import chunk_by_title
co = cohere.Client("D14bT4Bm9SoiXE5ioVryf2DGOyIw1yjm1ccR0giQ")
class Vectorstore:
def __init__(self, raw_documents: List[Dict[str, str]]):
self.raw_documents = raw_documents
self.docs = []
self.docs_embs = []
self.retrieve_top_k = 10
self.rerank_top_k = 3
self.load_and_chunk()
self.embed()
self.index()
raw_documents = [
{
"title": "Text Embeddings",
"url": "https://docs.cohere.com/docs/text-embeddings"},
{
"title": "Similarity Between Words and Sentences",
"url": "https://docs.cohere.com/docs/similarity-between-words-and-sentences"},
{
"title": "The Attention Mechanism",
"url": "https://docs.cohere.com/docs/the-attention-mechanism"},
{
"title": "Transformer Models",
"url": "https://docs.cohere.com/docs/transformer-models"}
]
def load_and_chunk(self) -> None:
"""
Loads the text from the sources and chunks the HTML content.
"""
print("Loading documents...")
for raw_document in self.raw_documents:
elements = partition_html(url=raw_document["url"])
chunks = chunk_by_title(elements)
for chunk in chunks:
self.docs.append(
{
"title": raw_document["title"],
"text": str(chunk),
"url": raw_document["url"],
}
)
def embed(self) -> None:
"""
Embeds the document chunks using the Cohere API.
"""
print("Embedding document chunks...")
batch_size = 90
self.docs_len = len(self.docs)
for i in range(0, self.docs_len, batch_size):
batch = self.docs[i : min(i + batch_size, self.docs_len)]
texts = [item["text"] for item in batch]
docs_embs_batch = co.embed(
texts=texts, model="embed-english-v3.0", input_type="search_document"
).embeddings
self.docs_embs.extend(docs_embs_batch)
def index(self) -> None:
"""
Indexes the documents for efficient retrieval.
"""
print("Indexing documents...")
self.idx = hnswlib.Index(space="ip", dim=1024)
self.idx.init_index(max_elements=self.docs_len, ef_construction=512, M=64)
self.idx.add_items(self.docs_embs, list(range(len(self.docs_embs))))
print(f"Indexing complete with {self.idx.get_current_count()} documents.")
def retrieve(self, query: str) -> List[Dict[str, str]]:
"""
Retrieves document chunks based on the given query.
Parameters:
query (str): The query to retrieve document chunks for.
Returns:
List[Dict[str, str]]: A list of dictionaries representing the retrieved document chunks, with 'title', 'text', and 'url' keys.
"""
# Dense retrieval
query_emb = co.embed(
texts=[query], model="embed-english-v3.0", input_type="search_query"
).embeddings
doc_ids = self.idx.knn_query(query_emb, k=self.retrieve_top_k)[0][0]
# Reranking
rank_fields = ["title", "text"] # We'll use the title and text fields for reranking
docs_to_rerank = [self.docs[doc_id] for doc_id in doc_ids]
rerank_results = co.rerank(
query=query,
documents=docs_to_rerank,
top_n=self.rerank_top_k,
model="rerank-english-v3.0",
rank_fields=rank_fields
)
docs_retrieved = []
for doc_id in doc_ids_reranked:
docs_retrieved.append(
{
"title": self.docs[doc_id]["title"],
"text": self.docs[doc_id]["text"],
"url": self.docs[doc_id]["url"],
}
)
return docs_retrieved
vectorstore = Vectorstore(raw_documents)