AdalAbilbekov commited on
Commit
da29922
1 Parent(s): 15603b4
Files changed (6) hide show
  1. .gitattributes +2 -0
  2. app.py +96 -0
  3. faiss_document_store.db +3 -0
  4. faiss_index +3 -0
  5. faiss_index.json +1 -0
  6. requirements.txt +7 -0
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ faiss_index filter=lfs diff=lfs merge=lfs -text
37
+ faiss_document_store.db filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+
4
+ """
5
+ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
+ """
7
+ # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
+
9
+ import os
10
+ import time
11
+ from haystack.document_stores import FAISSDocumentStore
12
+ from haystack.nodes import EmbeddingRetriever
13
+ from haystack.pipelines import Pipeline
14
+ from groq import Groq
15
+
16
+ api_key = os.getenv("groq_api")
17
+
18
+ # Load the FAISS index from the saved file
19
+ faiss_index_path = "./faiss_index" # Replace with your actual path
20
+ document_store = FAISSDocumentStore.load(faiss_index_path)
21
+
22
+ # Initialize the Retriever
23
+ retriever = EmbeddingRetriever(
24
+ document_store=document_store,
25
+ embedding_model="intfloat/multilingual-e5-large-instruct",
26
+ model_format="sentence_transformers"
27
+ )
28
+
29
+ def get_detailed_instruct(task_description: str, query: str) -> str:
30
+ return f'Instruct: {task_description}\nQuery: {query}'
31
+
32
+ # task = 'Given a web search query, retrieve relevant passages that answer the query'
33
+
34
+ def respond(text):
35
+ # Set up the query
36
+ # query = text
37
+ task = 'Given a web search query, retrieve relevant passages that answer the query'
38
+ query = get_detailed_instruct(task, text)
39
+ # Step 1: Measure retrieval time and retrieve top documents
40
+ start_time = time.time()
41
+ retrieval_results = retriever.retrieve(query=query, top_k=10)
42
+ print(f"Document retrieval completed in {time.time() - start_time:.2f} seconds")
43
+
44
+ # Step 2: Extract context from the retrieved documents
45
+ context = "\n\n".join([doc.content for doc in retrieval_results])
46
+
47
+ # Step 3: Initialize Groq Client for LLM (Ensure API key is stored securely)
48
+ os.environ['GROQ_API_KEY'] = api_key # Ensure this key is valid
49
+ client = Groq(api_key=os.environ["GROQ_API_KEY"])
50
+
51
+ # Step 4: Send the query and retrieved context to LLM for completion
52
+ chat_completion = client.chat.completions.create(
53
+ messages=[
54
+ {
55
+ "role": "system",
56
+ "content": (
57
+ "You are a retrieval-augmented generation system (RAG) designed to answer complex and general questions. "
58
+ "Do not say that you are RAG and the retrieved documents"
59
+ "Please use the retrieved documents to generate accurate and contextually relevant responses. "
60
+ "You should find the answer to the query from the retrieved documents."
61
+ )
62
+ },
63
+ {
64
+ "role": "user",
65
+ "content": f"Here are the retrieved documents:\n\n{context}\n\nUsing this information, answer the question: {query}",
66
+ }
67
+ ],
68
+ model="llama-3.1-70b-versatile", # Specify the model you want to use
69
+ temperature=0
70
+ )
71
+
72
+ # Step 5: Output the LLM's response
73
+ # print("Response from LLM:")
74
+ return chat_completion.choices[0].message.content
75
+
76
+
77
+
78
+ title = "Powered by Kazuk"
79
+ description = "Kazuk Team"
80
+
81
+ css = """
82
+ body {
83
+ background-color: #690088 !important;
84
+ }
85
+ """
86
+
87
+ demo = gr.Interface(fn=respond,
88
+ inputs="text",
89
+ outputs="text",
90
+ title=title,
91
+ description=description,
92
+ css=css
93
+ )
94
+
95
+ if __name__ == "__main__":
96
+ demo.launch(share=True)
faiss_document_store.db ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a489f7187154252a6938c15d471387d56099bbb97d0a552050c15ab2a8c9acc2
3
+ size 1765376
faiss_index ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3139bdcd2edb189177297a484b2752a9e975f02f6398c9b10ea68f69615bd334
3
+ size 2056237
faiss_index.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"similarity": "cosine", "faiss_index_factory_str": "Flat", "embedding_dim": 1024}
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ huggingface_hub
2
+ haystack
3
+ groq
4
+ Flask
5
+ farm-haystack
6
+ faiss-cpu
7
+ groq