File size: 6,283 Bytes
4cf1107 |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import requests
import json
from openai import OpenAI
from groq import Groq
class OllamaClient:
def __init__(
self,
api_key=None,
model=None,
):
self.base_url = "http://localhost:11434"
self.headers = {"Content-Type": "application/json"}
self.api_key = api_key
self.model = model
def list_models(self):
url = f"{self.base_url}/api/tags"
try:
response = requests.get(url)
response.raise_for_status() # Raise an exception for HTTP errors (status codes 4xx and 5xx)
return response.json() # returns the response is in JSON format
except requests.exceptions.HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
def api_chat_completion(self,prompt,**options):
url = f"{self.base_url}/api/chat"
options = options if options is not None else {"max_tokens":1024,"top_p":0.7,"temperature":0.7}
payload = json.dumps(
{
"model": self.model,
"messages": [{"role": "user", "content": prompt}],
"option": {
"num_ctx": self.options["max_tokens"],
"top_p": self.options["top_p"],
"temperature": self.options["temperature"],
# stop_sequences=["<|prompter|>","<|assistant|>","</s>"]
},
"stream": False,
}
)
response = requests.request("POST", url, headers=self.headers, data=payload)
return response.json()["message"]["content"]
def client_chat_completion(self,prompt,**options):
options = options if options is not None else {"max_tokens":1024,"top_p":0.7,"temperature":0.7}
client = OpenAI(
base_url=self.base_url,
api_key=self.api_key,
)
completion = client.chat.completions.create(
model=self.model,
messages=[{"role": "user", "content": prompt}],
temperature=options["temperature"],
top_p=options["top_p"],
max_tokens=options["max_tokens"],
stream=False,
)
return completion.choices[0].message.content
class NvidiaClient:
def __init__(self, api_key=None, model=None):
self.base_url = "https://integrate.api.nvidia.com/v1"
self.api_key = api_key
self.headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}",
}
self.model = model
def list_models(self):
url = f"{self.base_url}/models"
response = requests.request("GET", url) # api_key is not needed to list the available models
return response.json()
def api_chat_completion(self,prompt,**options):
url = f"{self.base_url}/chat/completions"
options = options if options is not None else {"max_tokens":1024,"top_p":0.7,"temperature":0.7}
payload = json.dumps(
{
"model": self.model,
"messages": [{"role": "user", "content": prompt}],
"temperature": options["temperature"],
"top_p": options["top_p"],
"max_tokens": options["max_tokens"],
"stream": False,
}
)
response = requests.request("POST", url, headers=self.headers, data=payload)
return response.json()["choices"][0]["message"]["content"]
def client_chat_completion(self,prompt,**options):
options = options if options is not None else {"max_tokens":1024,"top_p":0.7,"temperature":0.7}
client = OpenAI(
base_url=self.base_url,
api_key=self.api_key,
)
completion = client.chat.completions.create(
model=self.model,
messages=[{"role": "user", "content": prompt}],
temperature=self.options["temperature"],
top_p=self.options["top_p"],
max_tokens=self.options["max_tokens"],
stream=False,
)
return completion.choices[0].message.content
class GroqClient:
def __init__(self, api_key=None, model=None):
self.base_url = "https://api.groq.com/openai/v1"
self.api_key = api_key
self.headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}",
}
self.model = model
def list_models(self):
url = f"{self.base_url}/models"
response = requests.request("GET", url, headers=self.headers)
return response.json()
def api_chat_completion(self,prompt,**options):
url = f"{self.base_url}/chat/completions"
options = options if options is not None else {"max_tokens":1024,"top_p":0.7,"temperature":0.7}
payload = json.dumps(
{
"model": self.model,
"messages": [{"role": "user", "content": prompt}],
"temperature": options["temperature"],
"top_p": options["top_p"],
"max_tokens": options["max_tokens"],
"stream": False,
}
)
response = requests.request("POST", url, headers=self.headers, data=payload)
return response.json()["choices"][0]["message"]["content"]
def client_chat_completion(self,prompt,**options):
options = options if options is not None else {"max_tokens":1024,"top_p":0.7,"temperature":0.7}
client = Groq(
api_key=self.api_key,
)
completion = client.chat.completions.create(
model=self.model,
messages=[
{"role": "system", "content": "you are a helpful assistant."},
{"role": "user", "content": prompt},
],
temperature=self.options["temperature"],
top_p=self.options["top_p"],
max_tokens=self.options["max_tokens"],
stream=False,
)
return completion.choices[0].message.content
|