Spaces:
Running
Running
Update rag_engine.py
Browse files- rag_engine.py +33 -11
rag_engine.py
CHANGED
@@ -5,9 +5,10 @@ import json
|
|
5 |
import time
|
6 |
import faiss
|
7 |
import numpy as np
|
8 |
-
from sentence_transformers import SentenceTransformer
|
9 |
import requests
|
10 |
from dotenv import load_dotenv
|
|
|
|
|
11 |
|
12 |
# Load environment variables
|
13 |
load_dotenv()
|
@@ -33,7 +34,7 @@ class RAGEngine:
|
|
33 |
data_folder = "data/"
|
34 |
for file_name in os.listdir(data_folder):
|
35 |
if file_name.endswith(".json"):
|
36 |
-
with open(os.path.join(data_folder, 'r', encoding='utf-8') as f:
|
37 |
data = json.load(f)
|
38 |
docs.extend(self.flatten_data(data))
|
39 |
return docs
|
@@ -42,13 +43,29 @@ class RAGEngine:
|
|
42 |
flattened = []
|
43 |
if isinstance(data, list):
|
44 |
for item in data:
|
45 |
-
|
46 |
-
flattened.append({"text": text})
|
47 |
elif isinstance(data, dict):
|
48 |
-
|
49 |
-
flattened.append({"text": text})
|
50 |
return flattened
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
def create_vector_store(self):
|
53 |
embeddings = []
|
54 |
for doc in self.documents:
|
@@ -66,7 +83,6 @@ class RAGEngine:
|
|
66 |
return results
|
67 |
|
68 |
def ask_deepseek(self, context, query, retries=3, wait_time=5):
|
69 |
-
# 🔥 More detailed prompt
|
70 |
prompt = (
|
71 |
"You are an expert Honkai Star Rail Build Advisor.\n"
|
72 |
"You specialize in optimizing character performance based on Light Cones, Relics, Stats, Eidolons, and Team Synergies.\n"
|
@@ -75,7 +91,7 @@ class RAGEngine:
|
|
75 |
"Format your answer like this:\n"
|
76 |
"- Best Light Cones (Top 3)\n"
|
77 |
"- Recommended Relic Sets and Main Stats\n"
|
78 |
-
- Important Substats to Prioritize\n"
|
79 |
"- Optimal Eidolon Level (if necessary)\n"
|
80 |
"- Best Team Compositions (Synergies and Playstyle)\n"
|
81 |
"- Any Special Notes\n\n"
|
@@ -92,7 +108,6 @@ class RAGEngine:
|
|
92 |
"parameters": {"temperature": 0.7, "max_new_tokens": 800}
|
93 |
}
|
94 |
|
95 |
-
# 🚀 Retry logic
|
96 |
for attempt in range(retries):
|
97 |
response = requests.post(self.model_url, headers=headers, json=payload)
|
98 |
if response.status_code == 200:
|
@@ -101,11 +116,18 @@ class RAGEngine:
|
|
101 |
else:
|
102 |
print(f"Request failed (attempt {attempt+1}/{retries}): {response.status_code}")
|
103 |
if attempt < retries - 1:
|
104 |
-
time.sleep(wait_time)
|
105 |
return f"Error: Could not get a valid response after {retries} attempts."
|
106 |
|
107 |
def answer_query(self, query):
|
108 |
relevant_docs = self.search_documents(query)
|
109 |
context = "\n".join(relevant_docs)
|
110 |
answer = self.ask_deepseek(context, query)
|
111 |
-
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import time
|
6 |
import faiss
|
7 |
import numpy as np
|
|
|
8 |
import requests
|
9 |
from dotenv import load_dotenv
|
10 |
+
from sentence_transformers import SentenceTransformer
|
11 |
+
import streamlit as st
|
12 |
|
13 |
# Load environment variables
|
14 |
load_dotenv()
|
|
|
34 |
data_folder = "data/"
|
35 |
for file_name in os.listdir(data_folder):
|
36 |
if file_name.endswith(".json"):
|
37 |
+
with open(os.path.join(data_folder, file_name), 'r', encoding='utf-8') as f:
|
38 |
data = json.load(f)
|
39 |
docs.extend(self.flatten_data(data))
|
40 |
return docs
|
|
|
43 |
flattened = []
|
44 |
if isinstance(data, list):
|
45 |
for item in data:
|
46 |
+
flattened.extend(self.extract_fields(item))
|
|
|
47 |
elif isinstance(data, dict):
|
48 |
+
flattened.extend(self.extract_fields(data))
|
|
|
49 |
return flattened
|
50 |
|
51 |
+
def extract_fields(self, item):
|
52 |
+
"""Smart chunking: extract key fields instead of dumping full JSON."""
|
53 |
+
chunks = []
|
54 |
+
if isinstance(item, dict):
|
55 |
+
for key, value in item.items():
|
56 |
+
if isinstance(value, (str, int, float)):
|
57 |
+
text = f"{key}: {value}"
|
58 |
+
chunks.append({"text": text})
|
59 |
+
elif isinstance(value, dict):
|
60 |
+
for sub_key, sub_value in value.items():
|
61 |
+
text = f"{key} -> {sub_key}: {sub_value}"
|
62 |
+
chunks.append({"text": text})
|
63 |
+
elif isinstance(value, list):
|
64 |
+
for idx, sub_item in enumerate(value):
|
65 |
+
text = f"{key}[{idx}]: {sub_item}"
|
66 |
+
chunks.append({"text": text})
|
67 |
+
return chunks
|
68 |
+
|
69 |
def create_vector_store(self):
|
70 |
embeddings = []
|
71 |
for doc in self.documents:
|
|
|
83 |
return results
|
84 |
|
85 |
def ask_deepseek(self, context, query, retries=3, wait_time=5):
|
|
|
86 |
prompt = (
|
87 |
"You are an expert Honkai Star Rail Build Advisor.\n"
|
88 |
"You specialize in optimizing character performance based on Light Cones, Relics, Stats, Eidolons, and Team Synergies.\n"
|
|
|
91 |
"Format your answer like this:\n"
|
92 |
"- Best Light Cones (Top 3)\n"
|
93 |
"- Recommended Relic Sets and Main Stats\n"
|
94 |
+
"- Important Substats to Prioritize\n"
|
95 |
"- Optimal Eidolon Level (if necessary)\n"
|
96 |
"- Best Team Compositions (Synergies and Playstyle)\n"
|
97 |
"- Any Special Notes\n\n"
|
|
|
108 |
"parameters": {"temperature": 0.7, "max_new_tokens": 800}
|
109 |
}
|
110 |
|
|
|
111 |
for attempt in range(retries):
|
112 |
response = requests.post(self.model_url, headers=headers, json=payload)
|
113 |
if response.status_code == 200:
|
|
|
116 |
else:
|
117 |
print(f"Request failed (attempt {attempt+1}/{retries}): {response.status_code}")
|
118 |
if attempt < retries - 1:
|
119 |
+
time.sleep(wait_time)
|
120 |
return f"Error: Could not get a valid response after {retries} attempts."
|
121 |
|
122 |
def answer_query(self, query):
|
123 |
relevant_docs = self.search_documents(query)
|
124 |
context = "\n".join(relevant_docs)
|
125 |
answer = self.ask_deepseek(context, query)
|
126 |
+
return answer
|
127 |
+
|
128 |
+
def stream_answer(self, query):
|
129 |
+
"""Streamed generation for Streamlit."""
|
130 |
+
answer = self.answer_query(query)
|
131 |
+
for word in answer.split():
|
132 |
+
yield word + " "
|
133 |
+
time.sleep(0.02) # Feel free to tweak typing speed
|