Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -37,7 +37,68 @@ def respond(
|
|
37 |
repeat_penalty,
|
38 |
selected_topic
|
39 |
):
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
def select_topic(topic):
|
43 |
return gr.update(visible=True), topic
|
|
|
37 |
repeat_penalty,
|
38 |
selected_topic
|
39 |
):
|
40 |
+
chat_template = MessagesFormatterType.GEMMA_2
|
41 |
+
|
42 |
+
print("HISTORY SO FAR ", history)
|
43 |
+
print("Selected topic:", selected_topic)
|
44 |
+
|
45 |
+
if selected_topic:
|
46 |
+
query_engine = backend.create_index_for_query_engine(documents_paths[selected_topic])
|
47 |
+
message = backend.generate_prompt(query_engine, message)
|
48 |
+
gr.Info(f"Relevant context indexed from {selected_topic} docs...")
|
49 |
+
else:
|
50 |
+
query_engine = backend.load_index_for_query_engine()
|
51 |
+
message = backend.generate_prompt(query_engine, message)
|
52 |
+
gr.Info("Relevant context extracted from db...")
|
53 |
+
|
54 |
+
# Load model only if it's not already loaded or if a new model is selected
|
55 |
+
if backend.llm is None or backend.llm_model != model:
|
56 |
+
try:
|
57 |
+
backend.load_model(model)
|
58 |
+
except Exception as e:
|
59 |
+
return f"Error loading model: {str(e)}"
|
60 |
+
|
61 |
+
provider = LlamaCppPythonProvider(backend.llm)
|
62 |
+
|
63 |
+
agent = LlamaCppAgent(
|
64 |
+
provider,
|
65 |
+
system_prompt=f"{system_message}",
|
66 |
+
predefined_messages_formatter_type=chat_template,
|
67 |
+
debug_output=True
|
68 |
+
)
|
69 |
+
|
70 |
+
settings = provider.get_provider_default_settings()
|
71 |
+
settings.temperature = temperature
|
72 |
+
settings.top_k = top_k
|
73 |
+
settings.top_p = top_p
|
74 |
+
settings.max_tokens = max_tokens
|
75 |
+
settings.repeat_penalty = repeat_penalty
|
76 |
+
settings.stream = True
|
77 |
+
|
78 |
+
messages = BasicChatHistory()
|
79 |
+
|
80 |
+
# add user and assistant messages to the history
|
81 |
+
for msn in history:
|
82 |
+
user = {'role': Roles.user, 'content': msn[0]}
|
83 |
+
assistant = {'role': Roles.assistant, 'content': msn[1]}
|
84 |
+
messages.add_message(user)
|
85 |
+
messages.add_message(assistant)
|
86 |
+
|
87 |
+
try:
|
88 |
+
stream = agent.get_chat_response(
|
89 |
+
message,
|
90 |
+
llm_sampling_settings=settings,
|
91 |
+
chat_history=messages,
|
92 |
+
returns_streaming_generator=True,
|
93 |
+
print_output=False
|
94 |
+
)
|
95 |
+
|
96 |
+
outputs = ""
|
97 |
+
for output in stream:
|
98 |
+
outputs += output
|
99 |
+
yield outputs
|
100 |
+
except Exception as e:
|
101 |
+
yield f"Error during response generation: {str(e)}"
|
102 |
|
103 |
def select_topic(topic):
|
104 |
return gr.update(visible=True), topic
|