-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathconfig.py
187 lines (164 loc) · 6.44 KB
/
config.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import sys
import google.generativeai as genai
from dotenv import load_dotenv
from ollama import Client
from openai import OpenAI
import anthropic
from prompt_toolkit.shortcuts import input_dialog
class Config:
"""
Configuration class for managing settings.
Attributes:
verbose (bool): Flag indicating whether verbose mode is enabled.
openai_api_key (str): API key for OpenAI.
google_api_key (str): API key for Google.
ollama_host (str): url to ollama running remotely.
"""
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(Config, cls).__new__(cls)
# Put any initialization here
return cls._instance
def __init__(self):
load_dotenv()
self.verbose = False
self.openai_api_key = (
None # instance variables are backups in case saving to a `.env` fails
)
self.google_api_key = (
None # instance variables are backups in case saving to a `.env` fails
)
self.ollama_host = (
None # instance variables are backups in case savint to a `.env` fails
)
self.anthropic_api_key = (
None # instance variables are backups in case saving to a `.env` fails
)
self.qwen_api_key = (
None # instance variables are backups in case saving to a `.env` fails
)
def initialize_openai(self):
if self.verbose:
print("[Config][initialize_openai]")
if self.openai_api_key:
if self.verbose:
print("[Config][initialize_openai] using cached openai_api_key")
api_key = self.openai_api_key
else:
if self.verbose:
print(
"[Config][initialize_openai] no cached openai_api_key, try to get from env."
)
api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI(
api_key=api_key,
)
client.api_key = api_key
client.base_url = os.getenv("OPENAI_API_BASE_URL", client.base_url)
return client
def initialize_qwen(self):
if self.verbose:
print("[Config][initialize_qwen]")
if self.qwen_api_key:
if self.verbose:
print("[Config][initialize_qwen] using cached qwen_api_key")
api_key = self.qwen_api_key
else:
if self.verbose:
print(
"[Config][initialize_qwen] no cached qwen_api_key, try to get from env."
)
api_key = os.getenv("QWEN_API_KEY")
client = OpenAI(
api_key=api_key,
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
client.api_key = api_key
client.base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"
return client
def initialize_google(self):
if self.google_api_key:
if self.verbose:
print("[Config][initialize_google] using cached google_api_key")
api_key = self.google_api_key
else:
if self.verbose:
print(
"[Config][initialize_google] no cached google_api_key, try to get from env."
)
api_key = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=api_key, transport="rest")
model = genai.GenerativeModel("gemini-pro-vision")
return model
def initialize_ollama(self):
if self.ollama_host:
if self.verbose:
print("[Config][initialize_ollama] using cached ollama host")
else:
if self.verbose:
print(
"[Config][initialize_ollama] no cached ollama host. Assuming ollama running locally."
)
self.ollama_host = os.getenv("OLLAMA_HOST", None)
model = Client(host=self.ollama_host)
return model
def initialize_anthropic(self):
if self.anthropic_api_key:
api_key = self.anthropic_api_key
else:
api_key = os.getenv("ANTHROPIC_API_KEY")
return anthropic.Anthropic(api_key=api_key)
def validation(self, model, voice_mode):
"""
Validate the input parameters for the dialog operation.
"""
self.require_api_key(
"OPENAI_API_KEY",
"OpenAI API key",
model == "gpt-4"
or voice_mode
or model == "gpt-4-with-som"
or model == "gpt-4-with-ocr"
or model == "o1-with-ocr",
)
self.require_api_key(
"GOOGLE_API_KEY", "Google API key", model == "gemini-pro-vision"
)
self.require_api_key(
"ANTHROPIC_API_KEY", "Anthropic API key", model == "claude-3"
)
self.require_api_key("QWEN_API_KEY", "Qwen API key", model == "qwen-vl"
or model == "qwen-vl-with-omniparser")
def require_api_key(self, key_name, key_description, is_required):
key_exists = bool(os.environ.get(key_name))
if self.verbose:
print("[Config] require_api_key")
print("[Config] key_name", key_name)
print("[Config] key_description", key_description)
print("[Config] key_exists", key_exists)
if is_required and not key_exists:
self.prompt_and_save_api_key(key_name, key_description)
def prompt_and_save_api_key(self, key_name, key_description):
key_value = input_dialog(
title="API Key Required", text=f"Please enter your {key_description}:"
).run()
if key_value is None: # User pressed cancel or closed the dialog
sys.exit("Operation cancelled by user.")
if key_value:
if key_name == "OPENAI_API_KEY":
self.openai_api_key = key_value
elif key_name == "GOOGLE_API_KEY":
self.google_api_key = key_value
elif key_name == "ANTHROPIC_API_KEY":
self.anthropic_api_key = key_value
elif key_name == "QWEN_API_KEY":
self.qwen_api_key = key_value
self.save_api_key_to_env(key_name, key_value)
load_dotenv() # Reload environment variables
# Update the instance attribute with the new key
@staticmethod
def save_api_key_to_env(key_name, key_value):
with open(".env", "a") as file:
file.write(f"\n{key_name}='{key_value}'")