Spaces:
Sleeping
Sleeping
Update src/RAG.py
Browse files- src/RAG.py +225 -227
src/RAG.py
CHANGED
@@ -1,228 +1,226 @@
|
|
1 |
-
#from texts import *
|
2 |
-
from langgraph.graph import StateGraph
|
3 |
-
from langchain_core.runnables import RunnableLambda
|
4 |
-
from openai import OpenAI
|
5 |
-
import os
|
6 |
-
import openai
|
7 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
-
import torch
|
9 |
-
from typing import TypedDict
|
10 |
-
import requests
|
11 |
-
import json
|
12 |
-
from typing import List, Dict, Any
|
13 |
-
from sklearn.cluster import KMeans
|
14 |
-
import numpy as np
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
)
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
#
|
28 |
-
# #
|
29 |
-
# #
|
30 |
-
#
|
31 |
-
#
|
32 |
-
# embeddings
|
33 |
-
#
|
34 |
-
#
|
35 |
-
#
|
36 |
-
#
|
37 |
-
#
|
38 |
-
#
|
39 |
-
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
#
|
44 |
-
|
45 |
-
# embeddings
|
46 |
-
# embeddings
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
embeddings_tensor=
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
#
|
70 |
-
#
|
71 |
-
# #
|
72 |
-
#
|
73 |
-
# #
|
74 |
-
#
|
75 |
-
#
|
76 |
-
#
|
77 |
-
#
|
78 |
-
# top_k
|
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 |
-
|
126 |
-
#
|
127 |
-
#
|
128 |
-
#
|
129 |
-
# ##
|
130 |
-
#
|
131 |
-
#
|
132 |
-
#
|
133 |
-
#
|
134 |
-
|
135 |
-
#
|
136 |
-
|
137 |
-
#
|
138 |
-
#
|
139 |
-
#
|
140 |
-
#
|
141 |
-
#
|
142 |
-
#
|
143 |
-
#
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
##
|
157 |
-
{}
|
158 |
-
|
159 |
-
|
160 |
-
""
|
161 |
-
url=
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
#
|
166 |
-
#
|
167 |
-
#
|
168 |
-
#
|
169 |
-
#
|
170 |
-
#
|
171 |
-
#
|
172 |
-
#
|
173 |
-
#
|
174 |
-
# "
|
175 |
-
#
|
176 |
-
|
177 |
-
# }
|
178 |
-
|
179 |
-
#
|
180 |
-
|
181 |
-
#
|
182 |
-
|
183 |
-
#
|
184 |
-
#
|
185 |
-
#
|
186 |
-
#
|
187 |
-
#
|
188 |
-
#
|
189 |
-
#
|
190 |
-
#
|
191 |
-
#
|
192 |
-
#
|
193 |
-
#
|
194 |
-
#
|
195 |
-
#
|
196 |
-
#
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
graph_builder.
|
214 |
-
graph_builder.add_node(
|
215 |
-
graph_builder.add_node("
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
#
|
221 |
-
graph_builder.add_edge("
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
# Compile the graph
|
227 |
-
# graph = graph_builder.compile()
|
228 |
return graph_builder
|
|
|
1 |
+
#from texts import *
|
2 |
+
from langgraph.graph import StateGraph
|
3 |
+
from langchain_core.runnables import RunnableLambda
|
4 |
+
from openai import OpenAI
|
5 |
+
import os
|
6 |
+
import openai
|
7 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
+
import torch
|
9 |
+
from typing import TypedDict
|
10 |
+
import requests
|
11 |
+
import json
|
12 |
+
from typing import List, Dict, Any
|
13 |
+
from sklearn.cluster import KMeans
|
14 |
+
import numpy as np
|
15 |
+
file_text=''
|
16 |
+
|
17 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
18 |
+
chunk_size=1024,
|
19 |
+
chunk_overlap=256,
|
20 |
+
length_function=len
|
21 |
+
)
|
22 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
23 |
+
client=OpenAI()
|
24 |
+
|
25 |
+
# def get_embedding(text, model="text-embedding-3-small"):
|
26 |
+
# # response = openai.embeddings.create(
|
27 |
+
# # input=[text],
|
28 |
+
# # model=model
|
29 |
+
# # )
|
30 |
+
# embeddings = encoder.encode([text], convert_to_tensor=True, show_progress_bar=True)
|
31 |
+
# embeddings = embeddings.cpu().numpy()
|
32 |
+
# return embeddings
|
33 |
+
# text=chuncked_text+Focusing_text+planning_text+Focusing_text2+Evoking_text
|
34 |
+
# chunks=text_splitter.split_text(text)
|
35 |
+
# embeddings=[get_embedding(chunk) for chunk in chunks]
|
36 |
+
# embedds=np.array(embeddings)
|
37 |
+
# kmeans=KMeans(n_clusters=3,max_iter=1000)
|
38 |
+
# kmeans.fit(embedds)
|
39 |
+
|
40 |
+
# def embed_document(state,file_text):
|
41 |
+
# chunks=text_splitter.split_text(file_text)
|
42 |
+
|
43 |
+
# embeddings=encoder.encode(chunks, convert_to_tensor=True, show_progress_bar=False)
|
44 |
+
# embeddings=embeddings.cpu().numpy()
|
45 |
+
# print(len(embeddings))
|
46 |
+
# return {'single_query':state['single_query'],'embeddings':embeddings,'chunks':chunks}
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
def get_knowledge(state,embeddings,chunks,method='cosine'):
|
51 |
+
query_embedding = state['embedded_query']
|
52 |
+
if method=='cosine':
|
53 |
+
# Convert to tensor
|
54 |
+
query_tensor = torch.tensor(query_embedding, dtype=torch.float32).to('cpu') # shape: [embedding_dim]
|
55 |
+
#embeddings_tensor = torch.tensor(embeddings, dtype=torch.float32).to('cuda') # shape: [num_chunks, embedding_dim]
|
56 |
+
# Normalize
|
57 |
+
embeddings_tensor=torch.from_numpy(embeddings).to('cpu')
|
58 |
+
query_tensor = query_tensor / query_tensor.norm()
|
59 |
+
embeddings_tensor = embeddings_tensor / embeddings_tensor.norm(dim=1, keepdim=True)
|
60 |
+
# Compute cosine similarity
|
61 |
+
similarities = torch.matmul(query_tensor, embeddings_tensor.T)
|
62 |
+
print(similarities.shape)
|
63 |
+
top_k = 5
|
64 |
+
top_k_indices = torch.topk(similarities, k=top_k).indices
|
65 |
+
print(top_k_indices)
|
66 |
+
return {'embedded_query':state['embedded_query'],'knowledge':[chunks[i] for i in top_k_indices.squeeze(0)]}
|
67 |
+
# elif method=='Kmeans':
|
68 |
+
# query_emb = np.array(get_embedding(state['single_query']))
|
69 |
+
# # Predict the closest cluster
|
70 |
+
# cluster_idx = kmeans.predict([query_emb])[0]
|
71 |
+
# # Find indices of documents in this cluster
|
72 |
+
# cluster_doc_indices = np.where(kmeans.labels_ == cluster_idx)[0]
|
73 |
+
# # Compute L2 (Euclidean) distance within the cluster
|
74 |
+
# cluster_embs = np.array(embeddings)[cluster_doc_indices]
|
75 |
+
# distances = np.linalg.norm(cluster_embs - query_emb, axis=1)
|
76 |
+
# top_k=5
|
77 |
+
# # Get top_k most similar documents (smallest distances)
|
78 |
+
# top_indices = distances.argsort()[:top_k]
|
79 |
+
# return {'single_query':state['single_query'],'knowledge':[chunks[cluster_doc_indices[i]] for i in top_indices]}
|
80 |
+
|
81 |
+
|
82 |
+
def summerise_knowledge(state):
|
83 |
+
prompt="""
|
84 |
+
[system]
|
85 |
+
## Instructions:
|
86 |
+
You are skillfull text analysist. Summerise the extracted information from uploaded file by the user. Make your summary as concice as possible.
|
87 |
+
### Inputs:
|
88 |
+
Extracted Knowledge:
|
89 |
+
{}
|
90 |
+
### Output:
|
91 |
+
"""
|
92 |
+
text=""
|
93 |
+
chunks=state['knowledge']
|
94 |
+
for chunk in chunks:
|
95 |
+
text+=chunk
|
96 |
+
response=client.chat.completions.create(
|
97 |
+
model="gpt-5-mini",
|
98 |
+
messages =[{'role':'user','content':prompt.format(text)}])
|
99 |
+
# url = "https://11434-dep-01k080agynagw33vkkb9xfxpkb-d.cloudspaces.litng.ai/api/chat"
|
100 |
+
# s = requests.Session()
|
101 |
+
# s.headers.update({"Authorization": "Bearer bf54d08f-e88a-4a4a-bd14-444c984eaa6e"})
|
102 |
+
# response = s.post(url, json={
|
103 |
+
# "model": "hf.co/alibidaran/LLAMA3-instructive_reasoning-GGUF:Q8_0",
|
104 |
+
# "messages":[{'role':'user','content':prompt.format(text)}] ,
|
105 |
+
# "options": {
|
106 |
+
# "temperature": 0.7,
|
107 |
+
# "top_p": 0.95 # Set your desired top_p here
|
108 |
+
# }
|
109 |
+
|
110 |
+
# })
|
111 |
+
# full_response = ""
|
112 |
+
# for line in response.iter_lines():
|
113 |
+
# if line:
|
114 |
+
# data = json.loads(line.decode("utf-8"))
|
115 |
+
# message = data.get("message", {})
|
116 |
+
# content = message.get("content", "")
|
117 |
+
# full_response += content
|
118 |
+
# if data.get("done", Falsurl = "https://800-01jy9pekct42qjmqxcap35g81s.cloudspaces.litng.ai/predict"):
|
119 |
+
# break
|
120 |
+
print(response.choices[0].message)
|
121 |
+
return {'embedded_query':state['embedded_query'],'summary':response.choices[0].message.content}
|
122 |
+
#return {'single_query':state['single_query'],'summary':full_response}
|
123 |
+
|
124 |
+
# def making_instructions(state):
|
125 |
+
# prompt="""
|
126 |
+
# [system]
|
127 |
+
# ## Instructions:
|
128 |
+
# You are skill full certificated psychologist. Create an instruction for the practitioner to help them how to behaive and respond to the client effectively.
|
129 |
+
# ## Input:
|
130 |
+
# [summary]
|
131 |
+
# {}
|
132 |
+
# ### Output:
|
133 |
+
# """
|
134 |
+
|
135 |
+
# response=client.chat.completions.create(
|
136 |
+
# model="gpt-4o-mini",
|
137 |
+
# # api_base="https://litellm.llemma.net/",
|
138 |
+
# # api_key="sk-ZsStrG5lPoGnCHZl4NgcOA",
|
139 |
+
# messages =[{'role':'user','content':prompt.format(state['summary'])}],
|
140 |
+
# temperature=0.7,
|
141 |
+
# top_p=0.95,
|
142 |
+
# max_tokens=800)
|
143 |
+
# return {'query':state['query'],'instruction':response.choices[0].message}
|
144 |
+
|
145 |
+
def respond(state):
|
146 |
+
system_prompt="""
|
147 |
+
You are a reasonable expert who thinks and answer the users question.
|
148 |
+
Before respond first think and create a chain of thoughts in your mind.
|
149 |
+
Then respond to the client. Also follow the retrived information in the ##Summary section.
|
150 |
+
Your chain of thought and reflection must be in <thinking>..</thinking> format and your respond
|
151 |
+
should be in the <output>..</output> format.
|
152 |
+
"""
|
153 |
+
user_prompt="""
|
154 |
+
## Instructions:
|
155 |
+
{}
|
156 |
+
## Summary:
|
157 |
+
{}
|
158 |
+
"""
|
159 |
+
url="https://8000-01jy9pekct42qjmqxcap35g81s.cloudspaces.litng.ai/predict"
|
160 |
+
payload = { "user_prompt":user_prompt.format(state['single_query'],state['summary'])}
|
161 |
+
response = requests.post(url, data=payload)
|
162 |
+
return {'final_response':response.json()['output'][0]}
|
163 |
+
# messages=[
|
164 |
+
# {'role':'system', 'content':system_prompt},
|
165 |
+
# ]
|
166 |
+
# messages+=[{'role':'user','content':user_prompt.format(state['single_query'],state['summary'])}]
|
167 |
+
# url = "https://11434-dep-01jx2gzqqspsvcvtgmabz6jkkz-d.cloudspaces.litng.ai/api/chat"
|
168 |
+
# s = requests.Session()
|
169 |
+
# s.headers.update({"Authorization": "Bearer bf54d08f-e88a-4a4a-bd14-444c984eaa6e"})
|
170 |
+
# response = s.post(url, json={
|
171 |
+
# "model": "hf.co/alibidaran/LLAMA3-intructive_reasoning_GGUF:Q8_0",
|
172 |
+
# "messages": messages,
|
173 |
+
# "options": {
|
174 |
+
# "temperature": 0.7 # Set your desired temperature here
|
175 |
+
# }
|
176 |
+
|
177 |
+
# })
|
178 |
+
# # Collect the assistant's output
|
179 |
+
# full_response = ""
|
180 |
+
|
181 |
+
# for line in response.iter_lines():
|
182 |
+
# if line:
|
183 |
+
# data = json.loads(line.decode("utf-8"))
|
184 |
+
# message = data.get("message", {})
|
185 |
+
# content = message.get("content", "")
|
186 |
+
# full_response += content
|
187 |
+
# if data.get("done", False):
|
188 |
+
# break
|
189 |
+
# response=client.chat.completions.create(
|
190 |
+
# model="gpt-4o-mini",
|
191 |
+
# # api_base="https://litellm.llemma.net/",
|
192 |
+
# # api_key="sk-ZsStrG5lPoGnCHZl4NgcOA",
|
193 |
+
# messages =messages,
|
194 |
+
# temperature=0.7,
|
195 |
+
# top_p=0.95,)
|
196 |
+
#return {'final_response':full_response}
|
197 |
+
|
198 |
+
def end_node(state):
|
199 |
+
#print("Response:\n", state["final_response"])
|
200 |
+
return {'knowledge':state["summary"]}
|
201 |
+
class MyState(TypedDict):
|
202 |
+
#query: List[Dict[str, Any]]
|
203 |
+
embedded_query:list
|
204 |
+
knowledge: list
|
205 |
+
summary: str
|
206 |
+
|
207 |
+
def load_graph(embeddings,chunks):
|
208 |
+
graph_builder = StateGraph(state_schema=MyState)
|
209 |
+
|
210 |
+
# Add nodes
|
211 |
+
graph_builder.set_entry_point('get_knowledge')
|
212 |
+
graph_builder.add_node("get_knowledge", RunnableLambda(lambda state: get_knowledge(state,embeddings,chunks,method='cosine')))
|
213 |
+
graph_builder.add_node("summarise", RunnableLambda(summerise_knowledge))
|
214 |
+
#graph_builder.add_node('instructuons', RunnableLambda(making_instructions))
|
215 |
+
#graph_builder.add_node("respond", RunnableLambda(respond))
|
216 |
+
graph_builder.add_node("end", RunnableLambda(end_node))
|
217 |
+
|
218 |
+
# Add edges
|
219 |
+
graph_builder.add_edge("get_knowledge", "summarise")
|
220 |
+
#graph_builder.add_edge("summarise", "respond")
|
221 |
+
#graph_builder.add_edge("instructuons",'respond')
|
222 |
+
graph_builder.add_edge("summarise", "end")
|
223 |
+
|
224 |
+
# Compile the graph
|
225 |
+
# graph = graph_builder.compile()
|
|
|
|
|
226 |
return graph_builder
|