-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.py
36 lines (35 loc) · 1.58 KB
/
connect.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
29
30
31
32
33
34
35
36
import json
import requests
def connect(provider, model, version, api, proxy = False):
response = requests.get("https://genai.gd.edu.kg/model.json")
json_data = response.json()
if provider == "google":
version = next(arg for arg in [version] if arg in json_data["google"]["version"])
model = next(arg for arg in [model] if arg in json_data["google"]["model"])
api_url = (
f"https://api.genai.gd.edu.kg/google/{version}/models/{model}?key={api}"
if proxy
else f"https://generativelanguage.googleapis.com/{version}/models/{model}?key={api}"
)
response = requests.get(api_url, headers={"Content-Type": "application/json"})
elif provider == "openai":
version = next(arg for arg in [version] if arg in json_data["openai"]["version"])
model = next(arg for arg in [model] if arg in json_data["openai"]["model"])
api_url = (
f"https://api.genai.gd.edu.kg/openai/{version}/models"
if proxy
else f"https://api.openai.com/{version}/models"
)
response = requests.get(api_url, headers={"Content-Type": "application/json", "Authorization": f"Bearer {api}"})
else:
raise ValueError("Invalid provider")
response_json = response.json()
if "error" in response_json and response_json["error"] is not None:
raise ValueError(response_json["error"]["message"])
return {
"provider": provider,
"model": model,
"version": version,
"api": api,
"proxy": proxy
}