IMHamza101 commited on
Commit
c7c159f
·
verified ·
1 Parent(s): bf9f844

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py CHANGED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.document_loaders import PyPDFLoader
2
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
3
+ from langchain_huggingface import HuggingFaceEmbeddings
4
+ from langchain_milvus import Milvus
5
+ from langchain.chat_models import init_chat_model
6
+ from typing import List
7
+ from langchain.agents.middleware import dynamic_prompt, ModelRequest
8
+ from langchain.agents import create_agent
9
+ from langchain_core.documents import Document
10
+ from langchain_core.runnables import chain
11
+
12
+ import gradio as gr
13
+ import os
14
+ import shutil # Import shutil for directory removal
15
+ import tempfile # Import tempfile for temporary directory creation
16
+
17
+ #loading data
18
+ file_path = "PIE_Service_Rules_&_Policies.pdf"
19
+ loader = PyPDFLoader(file_path)
20
+
21
+ docs = loader.load()
22
+
23
+ #splitting it
24
+ text_splitter = RecursiveCharacterTextSplitter(
25
+ chunk_size=1000, chunk_overlap=200, add_start_index=True
26
+ )
27
+ all_splits = text_splitter.split_documents(docs)
28
+
29
+ #performing embeddings and storing in milvus
30
+ embeddings = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
31
+
32
+ # Create a temporary directory for Milvus Lite
33
+ temp_dir = tempfile.mkdtemp()
34
+ URI = os.path.join(temp_dir, "milvus_data.db")
35
+
36
+ # Explicitly remove the Milvus Lite data to ensure a clean start
37
+ # This block is no longer needed as tempfile.mkdtemp() provides a clean directory
38
+ # if os.path.exists(URI):
39
+ # if os.path.isdir(URI):
40
+ # shutil.rmtree(URI)
41
+ # print(f"Removed existing Milvus Lite data directory: {URI}")
42
+ # elif os.path.isfile(URI):
43
+ # os.remove(URI)
44
+ # print(f"Removed existing Milvus Lite data file: {URI}")
45
+
46
+ vector_store = Milvus(
47
+ embedding_function=embeddings,
48
+ connection_args={"uri": URI},
49
+ index_params={"index_type": "FLAT", "metric_type": "L2"},
50
+ drop_old=True
51
+ )
52
+
53
+ ids = vector_store.add_documents(documents=all_splits)
54
+
55
+ #Retriever
56
+ @chain
57
+ def retriever(query: str) -> List[Document]:
58
+ return vector_store.similarity_search(query, k=2)
59
+
60
+
61
+ #model
62
+ # from google.colab import userdata
63
+ # key = userdata.get('Groq_Key')
64
+ key = os.getenv('Groq_key2')
65
+ os.environ["GROQ_API_KEY"] = key
66
+
67
+ model = init_chat_model(
68
+ "llama-3.1-8b-instant",
69
+ model_provider="groq"
70
+ )
71
+
72
+ #using langchain middleware for dynamic prompts
73
+ @dynamic_prompt
74
+ def prompt_with_context(request: ModelRequest) -> str:
75
+ """Inject context into state messages."""
76
+ last_query = request.state["messages"][-1].text
77
+ retrieved_docs = vector_store.similarity_search(last_query)
78
+
79
+ docs_content = "\n\n".join(doc.page_content for doc in retrieved_docs)
80
+
81
+ system_message = (
82
+ "You are a helpful assistant who explain company policies to company employees. Use the following context in your response:"
83
+ f"\n\n{docs_content}"
84
+ )
85
+
86
+ return system_message
87
+
88
+
89
+ agent = create_agent(model, tools=[], middleware=[prompt_with_context])
90
+
91
+ def chat(message, history):
92
+
93
+ results = []
94
+ for step in agent.stream(
95
+ {"messages": [{"role": "user", "content": message}]},
96
+ stream_mode="values",
97
+ ):
98
+ # Grab the last message in the stream
99
+ last_message = step["messages"][-1]
100
+
101
+ # Append it to results instead of printing
102
+ results.append(last_message)
103
+ return results[1].content
104
+
105
+ demo = gr.ChatInterface(fn=chat, title="PI Invent Help Assistant")
106
+ demo.launch(debug = True)