Update app.py
Browse files
app.py
CHANGED
@@ -34,10 +34,13 @@ def user(message, history, system_message=None):
|
|
34 |
return history
|
35 |
|
36 |
|
37 |
-
def call_nvidia_api(history, max_tokens, temperature, top_p):
|
38 |
"""Calls the NVIDIA API to generate a response."""
|
|
|
|
|
|
|
39 |
payload = {
|
40 |
-
"messages":
|
41 |
"temperature": temperature,
|
42 |
"top_p": top_p,
|
43 |
"max_tokens": max_tokens,
|
@@ -53,23 +56,23 @@ def call_nvidia_api(history, max_tokens, temperature, top_p):
|
|
53 |
response.raise_for_status()
|
54 |
response_body = response.json()
|
55 |
print(f"Payload recebido: {response_body}")
|
56 |
-
if response_body
|
57 |
assistant_message = response_body["choices"][0]["message"]["content"]
|
58 |
-
history.append(
|
59 |
return history
|
60 |
-
|
61 |
def chatbot_submit(message, chat_history, system_message, max_tokens_val, temperature_val, top_p_val):
|
62 |
"""Submits the user message to the chatbot and updates the chat history."""
|
63 |
print("Updating chatbot...")
|
64 |
|
65 |
-
#
|
66 |
-
chat_history.append([message, ""])
|
67 |
|
68 |
# Chama a API da NVIDIA para gerar uma resposta
|
69 |
-
chat_history = call_nvidia_api(chat_history, max_tokens_val, temperature_val, top_p_val)
|
70 |
|
71 |
# Extrai apenas a mensagem do assistente da resposta
|
72 |
-
if chat_history and chat_history[-1][1]:
|
73 |
assistant_message = chat_history[-1][1]
|
74 |
else:
|
75 |
assistant_message = "Desculpe, ocorreu um erro ao gerar a resposta."
|
|
|
34 |
return history
|
35 |
|
36 |
|
37 |
+
def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
|
38 |
"""Calls the NVIDIA API to generate a response."""
|
39 |
+
messages = [{"role": "system", "content": system_message}]
|
40 |
+
messages.extend([{"role": "user", "content": h[0]} for h in history])
|
41 |
+
|
42 |
payload = {
|
43 |
+
"messages": messages,
|
44 |
"temperature": temperature,
|
45 |
"top_p": top_p,
|
46 |
"max_tokens": max_tokens,
|
|
|
56 |
response.raise_for_status()
|
57 |
response_body = response.json()
|
58 |
print(f"Payload recebido: {response_body}")
|
59 |
+
if response_body.get("choices"):
|
60 |
assistant_message = response_body["choices"][0]["message"]["content"]
|
61 |
+
history.append(["", assistant_message])
|
62 |
return history
|
63 |
+
|
64 |
def chatbot_submit(message, chat_history, system_message, max_tokens_val, temperature_val, top_p_val):
|
65 |
"""Submits the user message to the chatbot and updates the chat history."""
|
66 |
print("Updating chatbot...")
|
67 |
|
68 |
+
# Adiciona a mensagem do usu谩rio ao hist贸rico
|
69 |
+
chat_history.append([message, ""])
|
70 |
|
71 |
# Chama a API da NVIDIA para gerar uma resposta
|
72 |
+
chat_history = call_nvidia_api(chat_history, system_message, max_tokens_val, temperature_val, top_p_val)
|
73 |
|
74 |
# Extrai apenas a mensagem do assistente da resposta
|
75 |
+
if chat_history and chat_history[-1][1]:
|
76 |
assistant_message = chat_history[-1][1]
|
77 |
else:
|
78 |
assistant_message = "Desculpe, ocorreu um erro ao gerar a resposta."
|