artificialguybr commited on
Commit
6782564
1 Parent(s): 314bafa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -32,7 +32,7 @@ def user(message, history):
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([{"role": item["role"], "content": item["content"]} for item in history])
36
 
37
  payload = {
38
  "messages": messages,
@@ -41,7 +41,7 @@ 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: {payload}")
45
  session = requests.Session()
46
  response = session.post(INVOKE_URL, headers=headers, json=payload)
47
  while response.status_code == 202:
@@ -50,7 +50,7 @@ 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: {response_body}")
54
  if response_body.get("choices"):
55
  assistant_message = response_body["choices"][0]["message"]["content"]
56
  history.append({"role": "assistant", "content": assistant_message})
@@ -61,13 +61,13 @@ def chatbot_submit(message, chat_history, system_message, max_tokens_val, temper
61
  print("Updating chatbot...")
62
 
63
  # Adiciona a mensagem do usuário ao histórico
64
- chat_history = user(message, chat_history)
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 chat_history[-1]["role"] == "assistant":
71
  assistant_message = chat_history[-1]["content"]
72
  else:
73
  assistant_message = "Desculpe, ocorreu um erro ao gerar a resposta."
 
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 já deve ser uma lista de dicionários com as chaves "role" e "content"
36
 
37
  payload = {
38
  "messages": messages,
 
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
  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})
 
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."