ana-solo commited on
Commit
23bf6ca
·
verified ·
1 Parent(s): 8f1ec25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -20,23 +20,28 @@ Settings.llm = OpenRouter(
20
 
21
  # === Функция получения ответа ===
22
  def get_facts(system_prompt: str, user_question: str) -> str:
23
- # 1. Загружаем индекс и делаем retrieval
24
- storage_context = StorageContext.from_defaults(persist_dir="./storage")
25
- index = load_index_from_storage(storage_context)
26
- retriever = index.as_retriever(similarity_top_k=4)
27
- nodes = retriever.retrieve(user_question) # ← только по пользовательскому вопросу!
28
 
29
- # 2. Формируем контекст из найденных документов
30
- context = "\n\n".join([node.get_content() for node in nodes])
 
 
31
 
32
- # 3. Подставляем контекст ВНУТРЬ системного промпта
33
- full_system_prompt = system_prompt + f"Контекст: {context}"
34
 
35
- # 4. Отправляем в LLM
36
- messages = [
37
- ChatMessage(role="system", content=full_system_prompt),
38
- ChatMessage(role="user", content=user_question)
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