gufett0 commited on
Commit
b8c06a5
·
1 Parent(s): 0a7d821

added introductory prompt

Browse files
Files changed (3) hide show
  1. app.py +2 -4
  2. backend.py +46 -9
  3. data/chiarimento.txt +1 -0
app.py CHANGED
@@ -12,11 +12,9 @@ DESCRIPTION = """\
12
 
13
  chat_interface =gr.ChatInterface(
14
  fn=handle_query,
15
- chatbot=gr.Chatbot(height=650),
16
  textbox=gr.Textbox(placeholder="Chiedimi qualasiasi cosa relativa agli Osservatori", container=False, scale=7),
17
- examples=[
18
- ["Ciao, in cosa puoi aiutarmi?"],
19
- ["Dimmi i risultati e le modalità di conduzione del censimento per favore"]]
20
  )
21
 
22
 
 
12
 
13
  chat_interface =gr.ChatInterface(
14
  fn=handle_query,
15
+ chatbot=gr.Chatbot(height=600),
16
  textbox=gr.Textbox(placeholder="Chiedimi qualasiasi cosa relativa agli Osservatori", container=False, scale=7),
17
+ #examples=[["Ciao, in cosa puoi aiutarmi?"],["Dimmi i risultati e le modalità di conduzione del censimento per favore"]]
 
 
18
  )
19
 
20
 
backend.py CHANGED
@@ -13,7 +13,7 @@ from llama_cpp import Llama
13
  import spaces
14
  from huggingface_hub import login
15
  from llama_index.core.memory import ChatMemoryBuffer
16
- from typing import Iterator, List
17
  from llama_index.core.chat_engine import CondensePlusContextChatEngine
18
  from llama_index.core.llms import ChatMessage, MessageRole
19
 
@@ -40,6 +40,17 @@ model.eval()
40
  Settings.embed_model = InstructorEmbedding(model_name="hkunlp/instructor-base")
41
  Settings.llm = GemmaLLMInterface()
42
 
 
 
 
 
 
 
 
 
 
 
 
43
  ############################---------------------------------
44
 
45
  # Get the parser
@@ -47,9 +58,9 @@ parser = SentenceSplitter.from_defaults(
47
  chunk_size=256, chunk_overlap=64, paragraph_separator="\n\n"
48
  )
49
 
50
- def build_index():
51
  # Load documents from a file
52
- documents = SimpleDirectoryReader(input_files=["data/blockchainprova.txt"]).load_data()
53
  # Parse the documents into nodes
54
  nodes = parser.get_nodes_from_documents(documents)
55
  # Build the vector store index from the nodes
@@ -59,11 +70,39 @@ def build_index():
59
 
60
 
61
  @spaces.GPU(duration=20)
62
- def handle_query(query_str: str,
63
- chat_history: list[tuple[str, str]],) -> Iterator[str]:
 
 
 
64
 
65
- index = build_index()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
 
 
 
 
67
  try:
68
 
69
  memory = ChatMemoryBuffer.from_defaults(token_limit=None)
@@ -82,7 +121,7 @@ def handle_query(query_str: str,
82
  chat_engine = index.as_chat_engine(
83
  chat_mode="condense_plus_context",
84
  memory=memory,
85
- similarity_top_k=3,
86
  response_mode="tree_summarize", #Good for summarization purposes
87
 
88
  context_prompt = (
@@ -92,9 +131,7 @@ def handle_query(query_str: str,
92
  "{context_str}"
93
  "\nIstruzione: Usa la cronologia delle chat precedenti, o il contesto sopra, per interagire e aiutare l'utente a rispondere alla sua domanda."
94
  ),
95
-
96
  verbose=False,
97
-
98
  )
99
 
100
 
 
13
  import spaces
14
  from huggingface_hub import login
15
  from llama_index.core.memory import ChatMemoryBuffer
16
+ from typing import Iterator, List, Any
17
  from llama_index.core.chat_engine import CondensePlusContextChatEngine
18
  from llama_index.core.llms import ChatMessage, MessageRole
19
 
 
40
  Settings.embed_model = InstructorEmbedding(model_name="hkunlp/instructor-base")
41
  Settings.llm = GemmaLLMInterface()
42
 
43
+ documents_paths = {
44
+ 'blockchain': 'data/blockchainprova.txt',
45
+ 'metaverse': 'data/metaverso',
46
+ 'payment': 'data/payment'
47
+ }
48
+
49
+ session_state = {"documents_loaded": False,
50
+ "document_db": None,
51
+ "original_message": None,
52
+ "clarification": False}
53
+
54
  ############################---------------------------------
55
 
56
  # Get the parser
 
58
  chunk_size=256, chunk_overlap=64, paragraph_separator="\n\n"
59
  )
60
 
61
+ def build_index(path: str):
62
  # Load documents from a file
63
+ documents = SimpleDirectoryReader(input_files=[path]).load_data()
64
  # Parse the documents into nodes
65
  nodes = parser.get_nodes_from_documents(documents)
66
  # Build the vector store index from the nodes
 
70
 
71
 
72
  @spaces.GPU(duration=20)
73
+ def handle_query(query_str: str,
74
+ chat_history: list[tuple[str, str]],
75
+ session: dict[str, Any]) -> Iterator[str]:
76
+
77
+ global index
78
 
79
+ if not session["index"]:
80
+ matched_path = None
81
+ words = query_str.lower()
82
+ for key, path in documents_paths.items():
83
+ if key in words:
84
+ matched_path = path
85
+ break
86
+ if matched_path:
87
+ index = build_index(matched_path)
88
+ session["index"] = True
89
+
90
+ else: ## CHIEDI CHIARIMENTO
91
+ conversation: List[ChatMessage] = []
92
+ for user, assistant in chat_history:
93
+ conversation.extend(
94
+ [
95
+ ChatMessage(role=MessageRole.USER, content=user),
96
+
97
+ ChatMessage(role=MessageRole.ASSISTANT, content=assistant),
98
+ ]
99
+ )
100
+ index = build_index("data/chiarimento.txt")
101
 
102
+ else:
103
+ # The index is already built, no need to rebuild it.
104
+ pass
105
+
106
  try:
107
 
108
  memory = ChatMemoryBuffer.from_defaults(token_limit=None)
 
121
  chat_engine = index.as_chat_engine(
122
  chat_mode="condense_plus_context",
123
  memory=memory,
124
+ similarity_top_k=4,
125
  response_mode="tree_summarize", #Good for summarization purposes
126
 
127
  context_prompt = (
 
131
  "{context_str}"
132
  "\nIstruzione: Usa la cronologia delle chat precedenti, o il contesto sopra, per interagire e aiutare l'utente a rispondere alla sua domanda."
133
  ),
 
134
  verbose=False,
 
135
  )
136
 
137
 
data/chiarimento.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ In italiano, chiedi molto brevemente se la domanda si riferisce agli "Osservatori Blockchain", "Osservatori Payment" oppure "Osservatori Metaverse".