artificialguybr commited on
Commit
a9af4d7
1 Parent(s): 020a962

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -22,10 +22,13 @@ def clear_chat():
22
  chat_history_state.value = []
23
  chatbot.textbox.value = ""
24
 
25
- def call_nvidia_api(api_history, max_tokens, temperature, top_p):
26
  """Calls the NVIDIA API to generate a response."""
 
 
 
27
  payload = {
28
- "messages": api_history,
29
  "temperature": temperature,
30
  "top_p": top_p,
31
  "max_tokens": max_tokens,
@@ -47,24 +50,20 @@ def call_nvidia_api(api_history, max_tokens, temperature, top_p):
47
  else:
48
  return "Desculpe, ocorreu um erro ao gerar a resposta."
49
 
50
- def chatbot_submit(message, chat_history, api_history, system_message, max_tokens_val, temperature_val, top_p_val):
51
- """Submits the user message and updates both histories."""
52
- # Update Gradio history
53
- chat_history.append([message, ""])
54
 
55
- # Update API history
56
- api_history.append({"role": "user", "content": message})
57
 
58
- # Call NVIDIA API
59
- assistant_message = call_nvidia_api(api_history, max_tokens_val, temperature_val, top_p_val)
60
 
61
- # Update Gradio history with response
62
  chat_history[-1][1] = assistant_message
63
 
64
- # Update API history with response
65
- api_history.append({"role": "assistant", "content": assistant_message})
66
-
67
- return assistant_message, chat_history, api_history
68
 
69
  chat_history_state = gr.State([])
70
  system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
 
22
  chat_history_state.value = []
23
  chatbot.textbox.value = ""
24
 
25
+ def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
26
  """Calls the NVIDIA API to generate a response."""
27
+ messages = [{"role": "system", "content": system_message}]
28
+ messages.extend([{"role": "user", "content": h[0]} for h in history])
29
+
30
  payload = {
31
+ "messages": messages,
32
  "temperature": temperature,
33
  "top_p": top_p,
34
  "max_tokens": max_tokens,
 
50
  else:
51
  return "Desculpe, ocorreu um erro ao gerar a resposta."
52
 
53
+ def chatbot_submit(message, chat_history, system_message, max_tokens_val, temperature_val, top_p_val):
54
+ """Submits the user message to the chatbot and updates the chat history."""
55
+ print("Updating chatbot...")
 
56
 
57
+ # Adiciona a mensagem do usuário ao histórico para exibição
58
+ chat_history.append([message, ""])
59
 
60
+ # Chama a API da NVIDIA para gerar uma resposta
61
+ assistant_message = call_nvidia_api(chat_history, system_message, max_tokens_val, temperature_val, top_p_val)
62
 
63
+ # Atualiza o histórico com a resposta do assistente
64
  chat_history[-1][1] = assistant_message
65
 
66
+ return assistant_message, chat_history
 
 
 
67
 
68
  chat_history_state = gr.State([])
69
  system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)