-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_handler.py
29 lines (25 loc) · 904 Bytes
/
api_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from openai import OpenAI
class NvidiaAPI:
def __init__(self, api_key, model, temperature, top_p, max_tokens):
self.client = OpenAI(
base_url="https://integrate.api.nvidia.com/v1",
api_key=api_key
)
self.model = model
self.temperature = temperature
self.top_p = top_p
self.max_tokens = max_tokens
def send_request(self, prompt):
completion = self.client.chat.completions.create(
model=self.model,
messages=[{"role": "user", "content": prompt}],
temperature=self.temperature,
top_p=self.top_p,
max_tokens=self.max_tokens,
stream=True
)
response = ""
for chunk in completion:
if chunk.choices[0].delta.content is not None:
response += chunk.choices[0].delta.content
return response