artificialguybr commited on
Commit
d617b8e
1 Parent(s): 6782564

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -31,8 +31,12 @@ def user(message, history):
31
 
32
  def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
33
  """Calls the NVIDIA API to generate a response."""
 
34
  messages = [{"role": "system", "content": system_message}]
35
- messages.extend(history) # history deve ser uma lista de dicionários com as chaves "role" e "content"
 
 
 
36
 
37
  payload = {
38
  "messages": messages,
@@ -41,7 +45,6 @@ def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
41
  "max_tokens": max_tokens,
42
  "stream": False
43
  }
44
- print(f"Payload enviado: {json.dumps(payload, indent=2)}")
45
  session = requests.Session()
46
  response = session.post(INVOKE_URL, headers=headers, json=payload)
47
  while response.status_code == 202:
@@ -50,29 +53,26 @@ def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
50
  response = session.get(fetch_url, headers=headers)
51
  response.raise_for_status()
52
  response_body = response.json()
53
- print(f"Payload recebido: {json.dumps(response_body, indent=2)}")
54
  if response_body.get("choices"):
55
  assistant_message = response_body["choices"][0]["message"]["content"]
56
- history.append({"role": "assistant", "content": assistant_message})
57
  return history
58
 
59
  def chatbot_submit(message, chat_history, system_message, max_tokens_val, temperature_val, top_p_val):
60
  """Submits the user message to the chatbot and updates the chat history."""
61
- print("Updating chatbot...")
62
-
63
  # Adiciona a mensagem do usuário ao histórico
64
- chat_history.append({"role": "user", "content": message})
65
 
66
  # Chama a API da NVIDIA para gerar uma resposta
67
- chat_history = call_nvidia_api(chat_history, system_message, max_tokens_val, temperature_val, top_p_val)
68
 
69
- # Extrai apenas a mensagem do assistente da resposta
70
- if chat_history and "content" in chat_history[-1]:
71
- assistant_message = chat_history[-1]["content"]
72
  else:
73
  assistant_message = "Desculpe, ocorreu um erro ao gerar a resposta."
74
 
75
- return assistant_message, chat_history
76
 
77
  chat_history_state = gr.State([])
78
  system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
 
31
 
32
  def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
33
  """Calls the NVIDIA API to generate a response."""
34
+ # Transforma o histórico de listas de listas para o formato esperado pela API
35
  messages = [{"role": "system", "content": system_message}]
36
+ for msg in history:
37
+ messages.append({"role": "user", "content": msg[0]})
38
+ if msg[1]: # Garante que não adicionamos mensagens vazias do assistente
39
+ messages.append({"role": "assistant", "content": msg[1]})
40
 
41
  payload = {
42
  "messages": messages,
 
45
  "max_tokens": max_tokens,
46
  "stream": False
47
  }
 
48
  session = requests.Session()
49
  response = session.post(INVOKE_URL, headers=headers, json=payload)
50
  while response.status_code == 202:
 
53
  response = session.get(fetch_url, headers=headers)
54
  response.raise_for_status()
55
  response_body = response.json()
 
56
  if response_body.get("choices"):
57
  assistant_message = response_body["choices"][0]["message"]["content"]
58
+ history.append(["", assistant_message]) # Adiciona a nova mensagem do assistente ao histórico
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
  # Adiciona a mensagem do usuário ao histórico
64
+ updated_history = chat_history + [[message, ""]]
65
 
66
  # Chama a API da NVIDIA para gerar uma resposta
67
+ updated_history = call_nvidia_api(updated_history, system_message, max_tokens_val, temperature_val, top_p_val)
68
 
69
+ # A última entrada no histórico atualizado contém a última mensagem do usuário e a resposta do assistente
70
+ if updated_history and updated_history[-1][1]:
71
+ assistant_message = updated_history[-1][1]
72
  else:
73
  assistant_message = "Desculpe, ocorreu um erro ao gerar a resposta."
74
 
75
+ return assistant_message, updated_history
76
 
77
  chat_history_state = gr.State([])
78
  system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)