artificialguybr commited on
Commit
db1061f
1 Parent(s): 62cbda2

Refactor API headers and update NVIDIA API call

Browse files
Files changed (1) hide show
  1. app.py +32 -36
app.py CHANGED
@@ -3,14 +3,14 @@ import requests
3
  import json
4
  import os
5
 
6
- # Definir vari谩veis de ambiente ou substituir com sua chave de API real
7
- API_KEY = os.getenv('API_KEY')
8
  INVOKE_URL = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/df2bee43-fb69-42b9-9ee5-f4eabbeaf3a8"
 
9
 
10
  headers = {
11
  "Authorization": f"Bearer {API_KEY}",
12
- "accept": "text/event-stream",
13
- "content-type": "application/json",
14
  }
15
 
16
  BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
@@ -27,45 +27,41 @@ def user(message, history):
27
  history.append({"role": "user", "content": message})
28
  return history
29
 
30
- def call_nvidia_api(history, max_tokens, temperature, top_p, seed=42):
31
  # Preparar o payload com o hist贸rico de chat formatado
32
- messages = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg} for i, msg in enumerate(history)]
33
  payload = {
34
- "messages": messages,
35
- "temperature": temperature,
36
- "top_p": top_p,
37
- "max_tokens": max_tokens,
38
- "seed": seed,
39
- "stream": True
40
  }
41
 
42
- response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
43
- full_response = ""
44
- for line in response.iter_lines():
45
- if line:
46
- decoded_line = line.decode("utf-8").strip()
47
- if decoded_line.startswith("data:"):
48
- try:
49
- json_data = json.loads(decoded_line[5:])
50
- # Processar a resposta da API aqui
51
- # Supondo que a resposta da API seja diretamente o texto a ser adicionado ao chat
52
- full_response += json_data.get("content", "")
53
- except json.JSONDecodeError:
54
- print(f"Invalid JSON: {decoded_line[5:]}")
55
- return full_response
 
 
 
 
56
 
57
- def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
58
  print("Starting chat...")
59
- # Chamar a API da NVIDIA aqui com o hist贸rico formatado
60
- assistant_response = call_nvidia_api(history, max_tokens, temperature, top_p)
61
  # Atualizar o hist贸rico com a resposta do assistente
62
- if history:
63
- history[-1][1] += assistant_response
64
- else:
65
- history.append(["", assistant_response])
66
- return history, history, ""
67
-
68
-
69
  # Gradio interface setup
70
  with gr.Blocks() as demo:
71
  with gr.Row():
 
3
  import json
4
  import os
5
 
6
+ API_KEY = os.getenv('API_KEY')
 
7
  INVOKE_URL = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/df2bee43-fb69-42b9-9ee5-f4eabbeaf3a8"
8
+ FETCH_URL_FORMAT = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/"
9
 
10
  headers = {
11
  "Authorization": f"Bearer {API_KEY}",
12
+ "Accept": "application/json",
13
+ "Content-Type": "application/json",
14
  }
15
 
16
  BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
 
27
  history.append({"role": "user", "content": message})
28
  return history
29
 
30
+ def call_nvidia_api(history):
31
  # Preparar o payload com o hist贸rico de chat formatado
 
32
  payload = {
33
+ "messages": history,
34
+ "temperature": 0.7,
35
+ "top_p": 0.95,
36
+ "max_tokens": 500,
37
+ "seed": 42,
38
+ "stream": False # Ajustado para False conforme nova especifica莽茫o
39
  }
40
 
41
+ session = requests.Session()
42
+ response = session.post(INVOKE_URL, headers=headers, json=payload)
43
+
44
+ # Novo m茅todo de polling para verificar o status da resposta
45
+ while response.status_code == 202:
46
+ request_id = response.headers.get("NVCF-REQID")
47
+ fetch_url = FETCH_URL_FORMAT + request_id
48
+ response = session.get(fetch_url, headers=headers)
49
+
50
+ response.raise_for_status()
51
+ response_body = response.json()
52
+
53
+ # Processar a resposta da API aqui
54
+ if response_body["choices"]:
55
+ assistant_message = response_body["choices"][0]["message"]["content"]
56
+ history.append({"role": "assistant", "content": assistant_message})
57
+
58
+ return history
59
 
60
+ def chat(history, system_message):
61
  print("Starting chat...")
 
 
62
  # Atualizar o hist贸rico com a resposta do assistente
63
+ updated_history = call_nvidia_api(history)
64
+ return updated_history, ""
 
 
 
 
 
65
  # Gradio interface setup
66
  with gr.Blocks() as demo:
67
  with gr.Row():