Update app.py
Browse files
app.py
CHANGED
@@ -135,20 +135,21 @@ Search Query: transformer architectures
|
|
135 |
User Question: What is the history of the transformer model?
|
136 |
Search Query: transformer model history
|
137 |
"""
|
|
|
|
|
138 |
def clean_text(text):
|
139 |
cleaned = re.sub(r'[^\x00-\x7F]+', '', text) # Remove non-ASCII chars
|
140 |
cleaned = re.sub(r'[^a-zA-Z0-9_\- ]', '', cleaned) #Then your original rule
|
141 |
return cleaned
|
|
|
142 |
def to_query(provider,question):
|
143 |
-
|
144 |
-
print(f"<query sytem> = {query_system}")
|
145 |
try:
|
146 |
query_agent = LlamaCppAgent(
|
147 |
provider,
|
148 |
system_prompt=f"{query_system}",
|
149 |
-
#system_prompt="you are kind assistant",
|
150 |
predefined_messages_formatter_type=MessagesFormatterType.GEMMA_2,
|
151 |
-
debug_output=
|
152 |
)
|
153 |
|
154 |
message="""
|
@@ -157,12 +158,10 @@ User Question: %s
|
|
157 |
Search Query:
|
158 |
"""%question
|
159 |
|
160 |
-
|
161 |
-
print(message)
|
162 |
settings = provider.get_provider_default_settings()
|
163 |
messages = BasicChatHistory()
|
164 |
result = query_agent.get_chat_response(
|
165 |
-
#query_system+message,
|
166 |
message,
|
167 |
llm_sampling_settings=settings,
|
168 |
chat_history=messages,
|
@@ -202,6 +201,9 @@ def respond(
|
|
202 |
Returns:
|
203 |
str: The response to the message.
|
204 |
"""
|
|
|
|
|
|
|
205 |
try:
|
206 |
# Load the global variables
|
207 |
global llm
|
@@ -222,17 +224,10 @@ def respond(
|
|
222 |
provider = LlamaCppPythonProvider(llm)
|
223 |
|
224 |
query = to_query(provider,message)
|
225 |
-
print("<query>")
|
226 |
-
print(f"from {message} to {query}")
|
227 |
text = retriever_tool(query=f"{query}")
|
228 |
|
229 |
-
retriever_system="""
|
230 |
-
You are an AI assistant that answers questions based on documents provided by the user. Wait for the user to send a document. Once you receive the document, carefully read its contents and then answer the following question:
|
231 |
-
|
232 |
-
Question: %s
|
233 |
|
234 |
-
|
235 |
-
""" % message
|
236 |
retriever_system="""
|
237 |
You are an AI assistant that answers questions based on below retrievered documents.
|
238 |
|
@@ -243,7 +238,6 @@ Documents:
|
|
243 |
Question: %s
|
244 |
Answer:
|
245 |
""" % (text,message)
|
246 |
-
#[Wait for user's document]
|
247 |
|
248 |
# Create the agent
|
249 |
agent = LlamaCppAgent(
|
@@ -251,7 +245,7 @@ Answer:
|
|
251 |
#system_prompt=f"{retriever_system}",
|
252 |
system_prompt="you are kind assistant",
|
253 |
predefined_messages_formatter_type=MessagesFormatterType.GEMMA_2,
|
254 |
-
debug_output=
|
255 |
)
|
256 |
|
257 |
# Set the settings like temperature, top-k, top-p, max tokens, etc.
|
@@ -261,7 +255,7 @@ Answer:
|
|
261 |
settings.top_p = top_p
|
262 |
settings.max_tokens = max_tokens
|
263 |
settings.repeat_penalty = repeat_penalty
|
264 |
-
settings.stream =
|
265 |
|
266 |
messages = BasicChatHistory()
|
267 |
|
|
|
135 |
User Question: What is the history of the transformer model?
|
136 |
Search Query: transformer model history
|
137 |
"""
|
138 |
+
|
139 |
+
# remove strange char like *,/
|
140 |
def clean_text(text):
|
141 |
cleaned = re.sub(r'[^\x00-\x7F]+', '', text) # Remove non-ASCII chars
|
142 |
cleaned = re.sub(r'[^a-zA-Z0-9_\- ]', '', cleaned) #Then your original rule
|
143 |
return cleaned
|
144 |
+
|
145 |
def to_query(provider,question):
|
146 |
+
|
|
|
147 |
try:
|
148 |
query_agent = LlamaCppAgent(
|
149 |
provider,
|
150 |
system_prompt=f"{query_system}",
|
|
|
151 |
predefined_messages_formatter_type=MessagesFormatterType.GEMMA_2,
|
152 |
+
debug_output=False,
|
153 |
)
|
154 |
|
155 |
message="""
|
|
|
158 |
Search Query:
|
159 |
"""%question
|
160 |
|
161 |
+
|
|
|
162 |
settings = provider.get_provider_default_settings()
|
163 |
messages = BasicChatHistory()
|
164 |
result = query_agent.get_chat_response(
|
|
|
165 |
message,
|
166 |
llm_sampling_settings=settings,
|
167 |
chat_history=messages,
|
|
|
201 |
Returns:
|
202 |
str: The response to the message.
|
203 |
"""
|
204 |
+
if model is None:#
|
205 |
+
return
|
206 |
+
|
207 |
try:
|
208 |
# Load the global variables
|
209 |
global llm
|
|
|
224 |
provider = LlamaCppPythonProvider(llm)
|
225 |
|
226 |
query = to_query(provider,message)
|
|
|
|
|
227 |
text = retriever_tool(query=f"{query}")
|
228 |
|
|
|
|
|
|
|
|
|
229 |
|
230 |
+
#very sensitive against prompt
|
|
|
231 |
retriever_system="""
|
232 |
You are an AI assistant that answers questions based on below retrievered documents.
|
233 |
|
|
|
238 |
Question: %s
|
239 |
Answer:
|
240 |
""" % (text,message)
|
|
|
241 |
|
242 |
# Create the agent
|
243 |
agent = LlamaCppAgent(
|
|
|
245 |
#system_prompt=f"{retriever_system}",
|
246 |
system_prompt="you are kind assistant",
|
247 |
predefined_messages_formatter_type=MessagesFormatterType.GEMMA_2,
|
248 |
+
debug_output=False,
|
249 |
)
|
250 |
|
251 |
# Set the settings like temperature, top-k, top-p, max tokens, etc.
|
|
|
255 |
settings.top_p = top_p
|
256 |
settings.max_tokens = max_tokens
|
257 |
settings.repeat_penalty = repeat_penalty
|
258 |
+
settings.stream = False
|
259 |
|
260 |
messages = BasicChatHistory()
|
261 |
|