Spaces:
Build error
Build error
Update services/chat_service.py
Browse files- services/chat_service.py +71 -70
services/chat_service.py
CHANGED
|
@@ -67,29 +67,78 @@ class ChatService:
|
|
| 67 |
)
|
| 68 |
|
| 69 |
def construct_prompt(
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
async def chat(
|
| 95 |
self,
|
|
@@ -174,54 +223,6 @@ class ChatService:
|
|
| 174 |
logger.error(f"Error searching sources: {e}")
|
| 175 |
return {'products': [], 'documents': [], 'faqs': []}
|
| 176 |
|
| 177 |
-
def build_context(
|
| 178 |
-
self,
|
| 179 |
-
search_results: Dict[str, List[Dict[str, Any]]],
|
| 180 |
-
chat_history: List[Dict[str, Any]]
|
| 181 |
-
) -> str:
|
| 182 |
-
"""Build context for the model from search results and chat history"""
|
| 183 |
-
context_parts = []
|
| 184 |
-
|
| 185 |
-
# Add relevant products
|
| 186 |
-
if search_results.get('products'):
|
| 187 |
-
products = search_results['products'][:2] # Limit to top 2 products
|
| 188 |
-
for product in products:
|
| 189 |
-
context_parts.append(
|
| 190 |
-
f"Produkt: {product['Name']}\n"
|
| 191 |
-
f"Beschreibung: {product['Description']}\n"
|
| 192 |
-
f"Preis: {product['Price']}€\n"
|
| 193 |
-
f"Kategorie: {product['ProductCategory']}"
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
# Add relevant PDF content
|
| 197 |
-
if search_results.get('documents'):
|
| 198 |
-
docs = search_results['documents'][:2]
|
| 199 |
-
for doc in docs:
|
| 200 |
-
context_parts.append(
|
| 201 |
-
f"Aus Dokument '{doc['source']}' (Seite {doc['page']}):\n"
|
| 202 |
-
f"{doc['text']}"
|
| 203 |
-
)
|
| 204 |
-
|
| 205 |
-
# Add relevant FAQs
|
| 206 |
-
if search_results.get('faqs'):
|
| 207 |
-
faqs = search_results['faqs'][:2]
|
| 208 |
-
for faq in faqs:
|
| 209 |
-
context_parts.append(
|
| 210 |
-
f"FAQ:\n"
|
| 211 |
-
f"Frage: {faq['question']}\n"
|
| 212 |
-
f"Antwort: {faq['answer']}"
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
# Add recent chat history
|
| 216 |
-
if chat_history:
|
| 217 |
-
recent_history = chat_history[-3:] # Last 3 interactions
|
| 218 |
-
history_text = "\n".join(
|
| 219 |
-
f"User: {h['user_input']}\nAssistant: {h['response']}"
|
| 220 |
-
for h in recent_history
|
| 221 |
-
)
|
| 222 |
-
context_parts.append(f"Letzte Interaktionen:\n{history_text}")
|
| 223 |
-
|
| 224 |
-
return "\n\n".join(context_parts)
|
| 225 |
|
| 226 |
async def generate_response(
|
| 227 |
self,
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
def construct_prompt(
|
| 70 |
+
self,
|
| 71 |
+
user_input: str,
|
| 72 |
+
context: str,
|
| 73 |
+
chat_history: List[Tuple[str, str]],
|
| 74 |
+
max_history_turns: int = 1
|
| 75 |
+
) -> str:
|
| 76 |
+
"""Constructs the full prompt."""
|
| 77 |
+
# System message
|
| 78 |
+
system_message = self.construct_system_prompt(context)
|
| 79 |
+
|
| 80 |
+
# Start with system message
|
| 81 |
+
prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system_message}<|eot_id|>"
|
| 82 |
+
|
| 83 |
+
# Add chat history (limit to last `max_history_turns` interactions)
|
| 84 |
+
for user_msg, assistant_msg in chat_history[-max_history_turns:]:
|
| 85 |
+
prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|>"
|
| 86 |
+
prompt += f"<|start_header_id|>assistant<|end_header_id|>\n\n{assistant_msg}<|eot_id|>"
|
| 87 |
+
|
| 88 |
+
# Add the current user input
|
| 89 |
+
prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{user_input}<|eot_id|>"
|
| 90 |
+
prompt += "<|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 91 |
+
|
| 92 |
+
return prompt
|
| 93 |
|
| 94 |
+
def build_context(
|
| 95 |
+
self,
|
| 96 |
+
search_results: Dict[str, List[Dict[str, Any]]],
|
| 97 |
+
chat_history: List[Dict[str, Any]]
|
| 98 |
+
) -> str:
|
| 99 |
+
"""Build context for the model from search results and chat history"""
|
| 100 |
+
context_parts = []
|
| 101 |
+
|
| 102 |
+
# Add relevant products
|
| 103 |
+
if search_results.get('products'):
|
| 104 |
+
products = search_results['products'][:2] # Limit to top 2 products
|
| 105 |
+
for product in products:
|
| 106 |
+
context_parts.append(
|
| 107 |
+
f"Produkt: {product['Name']}\n"
|
| 108 |
+
f"Beschreibung: {product['Description']}\n"
|
| 109 |
+
f"Preis: {product['Price']}€\n"
|
| 110 |
+
f"Kategorie: {product['ProductCategory']}"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Add relevant PDF content
|
| 114 |
+
if search_results.get('documents'):
|
| 115 |
+
docs = search_results['documents'][:2]
|
| 116 |
+
for doc in docs:
|
| 117 |
+
context_parts.append(
|
| 118 |
+
f"Aus Dokument '{doc['source']}' (Seite {doc['page']}):\n"
|
| 119 |
+
f"{doc['text']}"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# Add relevant FAQs
|
| 123 |
+
if search_results.get('faqs'):
|
| 124 |
+
faqs = search_results['faqs'][:2]
|
| 125 |
+
for faq in faqs:
|
| 126 |
+
context_parts.append(
|
| 127 |
+
f"FAQ:\n"
|
| 128 |
+
f"Frage: {faq['question']}\n"
|
| 129 |
+
f"Antwort: {faq['answer']}"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# Add recent chat history
|
| 133 |
+
if chat_history:
|
| 134 |
+
recent_history = chat_history[-3:] # Last 3 interactions
|
| 135 |
+
history_text = "\n".join(
|
| 136 |
+
f"User: {h['user_input']}\nAssistant: {h['response']}"
|
| 137 |
+
for h in recent_history
|
| 138 |
+
)
|
| 139 |
+
context_parts.append(f"Letzte Interaktionen:\n{history_text}")
|
| 140 |
+
|
| 141 |
+
return "\n\n".join(context_parts)
|
| 142 |
|
| 143 |
async def chat(
|
| 144 |
self,
|
|
|
|
| 223 |
logger.error(f"Error searching sources: {e}")
|
| 224 |
return {'products': [], 'documents': [], 'faqs': []}
|
| 225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
async def generate_response(
|
| 228 |
self,
|