prabalGaur commited on
Commit
edb1632
·
verified ·
1 Parent(s): 1fd8e8f

Upload community_contributions/a3_igniters_sodiq/rag.py with huggingface_hub

Browse files
community_contributions/a3_igniters_sodiq/rag.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import chromadb
3
+ from chromadb.utils import embedding_functions
4
+ from dotenv import load_dotenv
5
+ load_dotenv(override=True)
6
+
7
+ DB_PATH = "./chroma_db"
8
+
9
+ def get_collection():
10
+ client = chromadb.PersistentClient(path=DB_PATH)
11
+
12
+ embedding = embedding_functions.OpenAIEmbeddingFunction(
13
+ api_key=os.getenv("OPENAI_API_KEY"),
14
+ model_name="text-embedding-3-small"
15
+ )
16
+
17
+ collection = client.get_or_create_collection(
18
+ name="support_knowledge_base",
19
+ embedding_function=embedding
20
+ )
21
+ return collection
22
+
23
+ def populate_knowledge_base():
24
+ collection = get_collection()
25
+
26
+ if collection.count() > 0:
27
+ return
28
+
29
+ documents = [
30
+ "To reset your password, go to Settings > Security > Reset Password. You will receive a reset link via email.",
31
+ "We accept the following payment methods: Visa, MasterCard, American Express, and PayPal.",
32
+ "Refunds are processed within 5-10 business days. To request a refund, contact billing support with your order ID.",
33
+ "Our standard support hours are Monday to Friday, 9 AM to 5 PM EST. Premium support is available 24/7.",
34
+ "To update your billing address, navigate to the Billing section in your account dashboard.",
35
+ "The API documentation can be found at https://api.example.com/docs. API keys are managed in the Developer settings.",
36
+ "Two-factor authentication (2FA) can be enabled in the Security settings for added account protection.",
37
+ "If you encounter a '500 Internal Server Error', please try clearing your browser cache or contact technical support."
38
+ ]
39
+
40
+ ids = [f"doc_{i}" for i in range(len(documents))]
41
+
42
+ collection.add(
43
+ documents=documents,
44
+ ids=ids
45
+ )
46
+ print("Knowledge base populated with synthetic data.")
47
+
48
+ def search_knowledge_base(query, n_results=3):
49
+ populate_knowledge_base()
50
+
51
+ collection = get_collection()
52
+ results = collection.query(
53
+ query_texts=[query],
54
+ n_results=n_results
55
+ )
56
+ print(f"RAG search results for query: '{query}'")
57
+ for i, doc in enumerate(results['documents'][0]):
58
+ print(f"{i+1}. {doc}")
59
+
60
+ if results and results['documents']:
61
+ return results['documents'][0]
62
+
63
+ return []