Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,23 +20,28 @@ Settings.llm = OpenRouter(
|
|
| 20 |
|
| 21 |
# === Функция получения ответа ===
|
| 22 |
def get_facts(system_prompt: str, user_question: str) -> str:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
retriever = index.as_retriever(similarity_top_k=4)
|
| 27 |
-
nodes = retriever.retrieve(user_question) # ← только по пользовательскому вопросу!
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
response = Settings.llm.chat(messages)
|
| 42 |
return response.message.content
|
|
|
|
| 20 |
|
| 21 |
# === Функция получения ответа ===
|
| 22 |
def get_facts(system_prompt: str, user_question: str) -> str:
|
| 23 |
+
try:
|
| 24 |
+
if not user_question.strip():
|
| 25 |
+
return "Ошибка: пожалуйста, введите ваш запрос."
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
storage_context = StorageContext.from_defaults(persist_dir="./storage")
|
| 28 |
+
index = load_index_from_storage(storage_context)
|
| 29 |
+
retriever = index.as_retriever(similarity_top_k=4)
|
| 30 |
+
nodes = retriever.retrieve(user_question)
|
| 31 |
|
| 32 |
+
context = "\n\n".join([node.get_content() for node in nodes]) if nodes else "Не найдено релевантных документов."
|
| 33 |
+
full_system_prompt = system_prompt + f"\n\nКонтекст:\n{context}"
|
| 34 |
|
| 35 |
+
messages = [
|
| 36 |
+
ChatMessage(role="system", content=full_system_prompt),
|
| 37 |
+
ChatMessage(role="user", content=user_question)
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
response = Settings.llm.chat(messages)
|
| 41 |
+
return response.message.content
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"❌ Ошибка:\n{str(e)}"
|
| 45 |
|
| 46 |
response = Settings.llm.chat(messages)
|
| 47 |
return response.message.content
|