Update vdb.py
Browse files
vdb.py
CHANGED
|
@@ -1,129 +1,138 @@
|
|
| 1 |
-
import hashlib
|
| 2 |
-
import json
|
| 3 |
-
import re
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
|
| 6 |
-
from dotenv import load_dotenv
|
| 7 |
-
from llama_index.core import (QueryBundle)
|
| 8 |
-
from llama_index.core.postprocessor import LLMRerank
|
| 9 |
-
from nest_asyncio import apply
|
| 10 |
-
from openai import OpenAI
|
| 11 |
-
from tqdm import tqdm
|
| 12 |
-
|
| 13 |
-
from llama_index.core import VectorStoreIndex
|
| 14 |
-
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 15 |
-
|
| 16 |
-
from llama_index.
|
| 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 |
-
async def
|
| 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 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hashlib
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from llama_index.core import (QueryBundle)
|
| 8 |
+
from llama_index.core.postprocessor import LLMRerank
|
| 9 |
+
from nest_asyncio import apply
|
| 10 |
+
from openai import OpenAI
|
| 11 |
+
from tqdm import tqdm
|
| 12 |
+
|
| 13 |
+
from llama_index.core import VectorStoreIndex, Settings
|
| 14 |
+
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 15 |
+
|
| 16 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 17 |
+
|
| 18 |
+
from llama_index.core import Document
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
embed_model = HuggingFaceEmbedding(
|
| 22 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 23 |
+
)
|
| 24 |
+
Settings.embed_model = embed_model
|
| 25 |
+
|
| 26 |
+
# Load variables from .env
|
| 27 |
+
load_dotenv()
|
| 28 |
+
|
| 29 |
+
def build_documents(sections):
|
| 30 |
+
docs = []
|
| 31 |
+
for s in sections:
|
| 32 |
+
metadata = {"section_title": s["title"]}
|
| 33 |
+
docs.append(Document(text=s["content"], metadata=metadata))
|
| 34 |
+
return docs
|
| 35 |
+
|
| 36 |
+
def create_vector_index(docs):
|
| 37 |
+
# embed_model = OpenAIEmbedding()
|
| 38 |
+
# index = VectorStoreIndex.from_documents(docs, embed_model=embed_model)
|
| 39 |
+
index = VectorStoreIndex.from_documents(docs)
|
| 40 |
+
return index
|
| 41 |
+
|
| 42 |
+
def split_markdown_by_section(md_path: str):
|
| 43 |
+
text = Path(md_path).read_text(encoding="utf-8")
|
| 44 |
+
sections = re.split(r"(?m)^# ", text)
|
| 45 |
+
chunks = []
|
| 46 |
+
for section in sections:
|
| 47 |
+
if not section.strip():
|
| 48 |
+
continue
|
| 49 |
+
title, *content = section.split("\n", 1)
|
| 50 |
+
body = content[0].strip() if content else ""
|
| 51 |
+
chunks.append({"title": title.strip(), "content": body})
|
| 52 |
+
return chunks
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
client = OpenAI()
|
| 57 |
+
|
| 58 |
+
apply()
|
| 59 |
+
|
| 60 |
+
tqdm.pandas()
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def hash_data(data):
|
| 64 |
+
json_str = json.dumps(data, sort_keys=True)
|
| 65 |
+
|
| 66 |
+
json_bytes = json_str.encode('utf-8')
|
| 67 |
+
|
| 68 |
+
hash_hex = hashlib.sha256(json_bytes).hexdigest()
|
| 69 |
+
|
| 70 |
+
return hash_hex
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def get_retrieved_nodes(query, index, vector_top_k=10, reranker_top_n=3, with_reranker=True):
|
| 74 |
+
query_bundle = QueryBundle(query)
|
| 75 |
+
retriever = index.as_retriever(similarity_top_k=vector_top_k)
|
| 76 |
+
retrieved_nodes = retriever.retrieve(query_bundle)
|
| 77 |
+
|
| 78 |
+
if with_reranker:
|
| 79 |
+
reranker = LLMRerank(choice_batch_size=5, top_n=reranker_top_n)
|
| 80 |
+
retrieved_nodes = reranker.postprocess_nodes(retrieved_nodes, query_bundle)
|
| 81 |
+
|
| 82 |
+
return retrieved_nodes
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def get_all_text(nodes):
|
| 86 |
+
return ' '.join(f"\n- {node.get_text()}" for node in nodes)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
async def further_retrieve(query, index, messages):
|
| 90 |
+
try:
|
| 91 |
+
retrieved_nodes = get_retrieved_nodes(query, index, vector_top_k=10, reranker_top_n=3, with_reranker=False)
|
| 92 |
+
return completion(query, get_all_text(retrieved_nodes), messages)
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print(e)
|
| 95 |
+
return None
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
async def completion(query, docs, messages):
|
| 99 |
+
messages.extend([
|
| 100 |
+
{
|
| 101 |
+
"role": "system",
|
| 102 |
+
"content": f"""
|
| 103 |
+
Given tone and voice guidelines and customer support help documents, act as a customer support bot.
|
| 104 |
+
Answer any further questions as if you are customer support bot.
|
| 105 |
+
TONE AND VOICE:
|
| 106 |
+
promote the society, be gentle, be kind always positive.
|
| 107 |
+
|
| 108 |
+
DOCUMENT:
|
| 109 |
+
{docs}
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
INSTRUCTIONS:
|
| 114 |
+
|
| 115 |
+
- Answer the users QUESTION using the DOCUMENT text above.
|
| 116 |
+
- Format formula into latex format between $...$ or \[...\]
|
| 117 |
+
- Keep your answer ground in the facts of the DOCUMENT or chat history.
|
| 118 |
+
- If document has an image markdown ,use it in your answer
|
| 119 |
+
- Respond in same language as user Question
|
| 120 |
+
- Use Markdown Structure
|
| 121 |
+
- DOCUMENT can have images with there descriptions
|
| 122 |
+
- if a text is followed by an image dont skip the image
|
| 123 |
+
QUESTION:
|
| 124 |
+
"""
|
| 125 |
+
},
|
| 126 |
+
{
|
| 127 |
+
"role": "system",
|
| 128 |
+
"content": query
|
| 129 |
+
}
|
| 130 |
+
])
|
| 131 |
+
completion = client.chat.completions.create(
|
| 132 |
+
model="gpt-4o-mini",
|
| 133 |
+
messages=messages,
|
| 134 |
+
stream=True
|
| 135 |
+
)
|
| 136 |
+
for chunk in completion:
|
| 137 |
+
if chunk.choices[0].delta.content:
|
| 138 |
+
yield chunk.choices[0].delta.content
|