Skip to content

Commit 7f2deb1

Browse files
committed
Add every API Keys supported by LiteLLM
1 parent ef3d91b commit 7f2deb1

File tree

11 files changed

+468
-90
lines changed

11 files changed

+468
-90
lines changed

pyqt_openai/__init__.py

Lines changed: 409 additions & 16 deletions
Large diffs are not rendered by default.

pyqt_openai/chat_widget/center/textEditPromptGroup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
PROMPT_END_KEY_NAME,
1111
PROMPT_JSON_KEY_NAME,
1212
CONTEXT_DELIMITER,
13-
IMAGE_FILE_EXT_LIST, TEXT_FILE_EXT_LIST,
13+
IMAGE_FILE_EXT_LIST,
14+
TEXT_FILE_EXT_LIST,
1415
)
1516
from pyqt_openai.chat_widget.center.textEditPrompt import TextEditPrompt
1617
from pyqt_openai.lang.translations import LangClass

pyqt_openai/config_loader.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
get_config_directory,
1111
ROOT_DIR,
1212
SRC_DIR,
13+
DEFAULT_API_CONFIGS,
1314
)
1415

1516
_config_cache = None
@@ -130,13 +131,34 @@ def update_api_key(yaml_file_path):
130131
with open(yaml_file_path, "r") as file:
131132
data = yaml.safe_load(file)
132133

134+
# Rename API_KEY to OPENAI_API_KEY
133135
if "General" in data and "API_KEY" in data["General"]:
134136
data["General"]["OPENAI_API_KEY"] = data["General"].pop("API_KEY")
135137

138+
# Rename CLAUDE_API_KEY to ANTHROPIC_API_KEY
139+
if "General" in data and "CLAUDE_API_KEY" in data["General"]:
140+
data["General"]["ANTHROPIC_API_KEY"] = data["General"].pop("CLAUDE_API_KEY")
141+
os.environ["ANTHROPIC_API_KEY"] = data["General"]["ANTHROPIC_API_KEY"]
142+
143+
# REPLICATE_API_TOKEN IS USED IN REPLICATE PACKAGE.
144+
# REPLICATE_API_KEY IS USED IN LITELLM.
145+
if "REPLICATE" in data and "REPLICATE_API_TOKEN" in data["REPLICATE"]:
146+
os.environ["REPLICATE_API_KEY"] = data["REPLICATE"]["REPLICATE_API_TOKEN"]
147+
136148
with open(yaml_file_path, "w") as file:
137149
yaml.safe_dump(data, file)
138150

139151

140-
init_yaml()
152+
def load_api_keys():
153+
env_vars = [item["env_var_name"] for item in DEFAULT_API_CONFIGS]
141154

155+
# Set API keys
156+
for key, value in CONFIG_MANAGER.get_general().items():
157+
if key in env_vars:
158+
os.environ[key] = value
159+
160+
161+
init_yaml()
162+
update_api_key(INI_FILE_NAME)
142163
CONFIG_MANAGER = ConfigManager()
164+
load_api_keys()

pyqt_openai/g4f_image_widget/g4fImageHome.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __initUi(self):
2323
description.setFont(QFont(*MEDIUM_LABEL_PARAM))
2424
description.setAlignment(Qt.AlignmentFlag.AlignCenter)
2525

26-
# TODO v1.7.0 "how does this work?" or "What is GPT4Free?" link (maybe)
26+
# TODO v2.x.0 "how does this work?" or "What is GPT4Free?" link (maybe)
2727

2828
lay = QVBoxLayout()
2929
lay.addWidget(title)

pyqt_openai/globals.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from openai import OpenAI
88
from g4f.client import Client
99

10-
from pyqt_openai import DEFAULT_GEMINI_MODEL, LLAMA_REQUEST_URL
10+
from pyqt_openai import DEFAULT_GEMINI_MODEL
1111
from pyqt_openai.sqlite import SqliteDatabase
1212
from pyqt_openai.util.llamapage_script import GPTLLamaIndexWrapper
1313
from pyqt_openai.util.replicate_script import ReplicateWrapper
@@ -20,6 +20,6 @@
2020

2121
OPENAI_CLIENT = OpenAI(api_key="")
2222
GEMINI_CLIENT = genai.GenerativeModel(DEFAULT_GEMINI_MODEL)
23-
CLAUDE_CLIENT = anthropic.Anthropic(api_key="")
23+
ANTHROPIC_CLIENT = anthropic.Anthropic(api_key="")
2424

2525
REPLICATE_CLIENT = ReplicateWrapper(api_key="")

pyqt_openai/mainWindow.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,21 +341,22 @@ def __setToolBar(self):
341341
)
342342

343343
def __loadApiKeyInConf(self):
344+
"""
345+
This function is used to utilize the existing OpenAI, Gemini, Anthropic, and Replicate modules. While these four modules are also available in LiteLLM, this function is additionally used to leverage various features such as image generation provided by these modules.
346+
"""
344347
set_api_key(
345348
"OPENAI_API_KEY", CONFIG_MANAGER.get_general_property("OPENAI_API_KEY")
346349
)
347350
set_api_key(
348351
"GEMINI_API_KEY", CONFIG_MANAGER.get_general_property("GEMINI_API_KEY")
349352
)
350353
set_api_key(
351-
"CLAUDE_API_KEY", CONFIG_MANAGER.get_general_property("CLAUDE_API_KEY")
352-
)
353-
set_api_key(
354-
"LLAMA_API_KEY", CONFIG_MANAGER.get_general_property("LLAMA_API_KEY")
354+
"ANTHROPIC_API_KEY",
355+
CONFIG_MANAGER.get_general_property("ANTHROPIC_API_KEY"),
355356
)
356357
set_api_key(
357-
"REPLICATE_API_TOKEN",
358-
CONFIG_MANAGER.get_replicate_property("REPLICATE_API_TOKEN"),
358+
"REPLICATE_API_KEY",
359+
CONFIG_MANAGER.get_replicate_property("REPLICATE_API_KEY"),
359360
)
360361

361362
# Set llama index directory if it exists

pyqt_openai/settings_dialog/apiWidget.py

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,13 @@
1111
)
1212

1313
from pyqt_openai import (
14-
HOW_TO_GET_OPENAI_API_KEY_URL,
15-
HOW_TO_GET_CLAUDE_API_KEY_URL,
16-
HOW_TO_GET_GEMINI_API_KEY_URL,
17-
HOW_TO_GET_LLAMA_API_KEY_URL,
1814
DEFAULT_API_CONFIGS,
19-
HOW_TO_REPLICATE,
2015
)
2116
from pyqt_openai.config_loader import CONFIG_MANAGER
2217
from pyqt_openai.util.script import set_api_key
2318
from pyqt_openai.widgets.linkLabel import LinkLabel
2419

2520

26-
# FIXME Are there any ways to get authentification from the claude, gemini?
27-
# def is_api_key_valid(endpoint, api_key):
28-
# response = requests.get(endpoint, headers={'Authorization': f'Bearer {api_key}'})
29-
# f = response.status_code == 200
30-
# print(f)
31-
32-
3321
class ApiWidget(QWidget):
3422
def __init__(self, parent=None):
3523
super().__init__(parent)
@@ -44,33 +32,10 @@ def __initVal(self):
4432
"display_name": conf["display_name"],
4533
"env_var_name": conf["env_var_name"],
4634
"api_key": CONFIG_MANAGER.get_general_property(conf["env_var_name"]),
35+
"manual_url": conf["manual_url"],
4736
}
4837
self.__api_keys.append(_conf)
4938

50-
# Add REPLICATE API
51-
self.__api_keys.append(
52-
{
53-
"display_name": "Replicate",
54-
"env_var_name": "REPLICATE_API_TOKEN",
55-
"api_key": CONFIG_MANAGER.get_replicate_property("REPLICATE_API_TOKEN"),
56-
}
57-
)
58-
59-
# Set "get api key" to here
60-
for i, obj in enumerate(self.__api_keys):
61-
obj["get_api_key"] = {
62-
"OpenAI": HOW_TO_GET_OPENAI_API_KEY_URL,
63-
"Claude": HOW_TO_GET_CLAUDE_API_KEY_URL,
64-
"Gemini": HOW_TO_GET_GEMINI_API_KEY_URL,
65-
"Llama": HOW_TO_GET_LLAMA_API_KEY_URL,
66-
"Replicate": HOW_TO_REPLICATE,
67-
"DeepInfra": "",
68-
"Groq": "",
69-
"HuggingFace": "",
70-
"OpenRouter": "",
71-
"Perplexity API": "",
72-
}[obj["display_name"]]
73-
7439
def __initUi(self):
7540
self.setWindowTitle("API Key")
7641

@@ -95,7 +60,7 @@ def __initUi(self):
9560
getApiKeyLbl = LinkLabel()
9661
getApiKeyLbl.setText("Link")
9762
getApiKeyLbl.setAlignment(Qt.AlignmentFlag.AlignCenter)
98-
getApiKeyLbl.setUrl(obj["get_api_key"])
63+
getApiKeyLbl.setUrl(obj["manual_url"])
9964
self.__tableWidget.setCellWidget(i, 2, getApiKeyLbl)
10065

10166
saveBtn = QPushButton("Save")
@@ -123,8 +88,5 @@ def setApiKeys(self):
12388
}
12489
# Save the api keys to the conf file
12590
for k, v in api_keys.items():
126-
if k == "REPLICATE_API_TOKEN":
127-
CONFIG_MANAGER.set_replicate_property(k, v)
128-
else:
129-
CONFIG_MANAGER.set_general_property(k, v)
91+
CONFIG_MANAGER.set_general_property(k, v)
13092
set_api_key(k, v)

pyqt_openai/settings_dialog/voiceSettingsWidget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __initUi(self):
7171
"Auto-Play Voice when Response is Received (Work in Progress)"
7272
)
7373
self.__autoPlayChkBox.setChecked(self.auto_play)
74-
# TODO implement auto-play voice in v1.7.0
74+
# TODO implement auto-play voice in v1.8.0
7575
self.__autoPlayChkBox.setEnabled(False)
7676

7777
lay = QFormLayout()
@@ -93,7 +93,7 @@ def __initUi(self):
9393
self.__autoStopSilenceDurationSpinBox = QSpinBox()
9494
self.__autoStopSilenceDurationSpinBox.setRange(3, 10)
9595
self.__autoStopSilenceDurationSpinBox.setValue(self.auto_stop_silence_duration)
96-
# TODO implement auto-play voice in v1.7.0
96+
# TODO implement auto-play voice in v1.8.0
9797
self.__autoStopSilenceDurationSpinBox.setEnabled(False)
9898

9999
lay = QFormLayout()

pyqt_openai/util/replicate_script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def api_key(self):
2525
@api_key.setter
2626
def api_key(self, value):
2727
self.__api_key = value
28-
os.environ["REPLICATE_API_TOKEN"] = self.__api_key
28+
os.environ["REPLICATE_API_KEY"] = self.__api_key
2929

3030
def is_available(self):
3131
return True if self.__api_key is not None else False

pyqt_openai/util/script.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
from pyqt_openai.globals import (
7474
DB,
7575
OPENAI_CLIENT,
76-
CLAUDE_CLIENT,
76+
ANTHROPIC_CLIENT,
7777
G4F_CLIENT,
7878
LLAMAINDEX_WRAPPER,
7979
REPLICATE_CLIENT,
@@ -716,18 +716,22 @@ def get_claude_argument(model, system, messages, cur_text, stream, images):
716716

717717

718718
def set_api_key(env_var_name, api_key):
719+
api_key = api_key.strip() if api_key else ""
719720
if env_var_name == "OPENAI_API_KEY":
720721
OPENAI_CLIENT.api_key = api_key
721-
os.environ['OPENAI_API_KEY'] = api_key
722+
os.environ["OPENAI_API_KEY"] = api_key
722723
if env_var_name == "GEMINI_API_KEY":
723724
genai.configure(api_key=api_key)
724725
os.environ["GEMINI_API_KEY"] = api_key
725726
if env_var_name == "CLAUDE_API_KEY":
726-
CLAUDE_CLIENT.api_key = api_key
727-
os.environ['ANTHROPIC_API_KEY'] = api_key
728-
if env_var_name == "REPLICATE_API_TOKEN":
727+
ANTHROPIC_CLIENT.api_key = api_key
728+
os.environ["ANTHROPIC_API_KEY"] = api_key
729+
if env_var_name == "REPLICATE_API_KEY":
729730
REPLICATE_CLIENT.api_key = api_key
730-
os.environ["REPLICATE_API_TOKEN"] = api_key
731+
os.environ["REPLICATE_API_KEY"] = api_key
732+
733+
# Set environment variables dynamically
734+
os.environ[env_var_name] = api_key
731735

732736

733737
def get_openai_model_endpoint(model):
@@ -918,18 +922,10 @@ def get_api_argument(
918922
model, system, messages, cur_text, stream, images
919923
)
920924

921-
elif provider == "Claude":
925+
elif provider == "Anthropic":
922926
args = get_claude_argument(
923927
model, system, messages, cur_text, stream, images
924928
)
925-
elif provider == "Llama":
926-
args = {
927-
"model": model,
928-
"messages": messages,
929-
"stream": stream,
930-
"max_tokens": DEFAULT_TOKEN_CHUNK_SIZE,
931-
}
932-
args["messages"].append({"role": "user", "content": cur_text})
933929
else:
934930
raise Exception(f"Provider not found for model {model}")
935931
return args
@@ -993,7 +989,7 @@ def stream_response(provider, response, is_g4f=False, get_content_only=True):
993989
yield chunk
994990
else:
995991
for part in response:
996-
yield part.choices[0].delta.content or ''
992+
yield part.choices[0].delta.content or ""
997993

998994

999995
def get_api_response(args, get_content_only=True):

0 commit comments

Comments
 (0)