Commit
路
db1061f
1
Parent(s):
62cbda2
Refactor API headers and update NVIDIA API call
Browse files
app.py
CHANGED
@@ -3,14 +3,14 @@ import requests
|
|
3 |
import json
|
4 |
import os
|
5 |
|
6 |
-
|
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 |
-
"
|
13 |
-
"
|
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
|
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":
|
35 |
-
"temperature":
|
36 |
-
"top_p":
|
37 |
-
"max_tokens":
|
38 |
-
"seed":
|
39 |
-
"stream":
|
40 |
}
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
def chat(history, system_message
|
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 |
-
|
63 |
-
|
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():
|