Skip to content

Commit b5e2ec0

Browse files
committed
Reorder parameters in OpenAILangChain constructor
1 parent 31311da commit b5e2ec0

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

libs/openai_langchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class OpenAILangChain:
1515
lite_llm = None # Change from open_ai_llm to lite_llm
1616
memory = None
1717

18-
def __init__(self,code_language="python",temprature:float=0.3,max_tokens=1000,model="gpt-3.5-turbo",api_key=None):
18+
def __init__(self,api_key=None,code_language="python",temprature:float=0.3,max_tokens=1000,model="gpt-3.5-turbo"):
1919
code_prompt = st.session_state.code_prompt
2020
code_language = st.session_state.code_language
2121
self.utils = libs.general_utils.GeneralUtils()

script.py

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,27 @@ def main():
8787
with st.expander("Open AI Settings"):
8888
try:
8989
# Settings for Open AI model.
90-
model_options_openai = ["gpt-4", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0613", "gpt-3.5-turbo", "gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k-0613", "gpt-3.5-turbo-0301", "text-davinci-003"]
90+
model_options_openai = ["gpt-4", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0613", "gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k-0613", "gpt-3.5-turbo-0301", "text-davinci-003"]
9191
st.session_state["openai"]["model_name"] = st.selectbox("Model name", model_options_openai, index=model_options_openai.index(st.session_state["openai"]["model_name"]))
9292
st.session_state["openai"]["temperature"] = st.slider("Temperature", min_value=0.0, max_value=2.0, value=st.session_state["openai"]["temperature"], step=0.1)
9393
st.session_state["openai"]["max_tokens"] = st.slider("Maximum Tokens", min_value=1, max_value=4096, value=st.session_state["openai"]["max_tokens"], step=1)
9494

95-
# Check if the API key is in App secrets.
96-
if st.secrets["OPENAI_API_KEY"]:
97-
api_key = st.secrets["OPENAI_API_KEY"]
98-
logger.info("Gemini AI API key is initialized from App secrets.")
99-
else:
95+
try:
96+
# Check if the API key is in App secrets.
97+
if st.secrets["OPENAI_API_KEY"]:
98+
api_key = st.secrets["OPENAI_API_KEY"]
99+
logger.info("OpenAI API key is initialized from App secrets.")
100+
101+
except Exception as exception:
102+
logger.error(f"Error loading : {str(exception)}")
103+
st.toast(f"Error loading : {str(exception)}", icon="❌")
104+
105+
# Create API key input box on error.
100106
api_key = st.text_input("API Key", value="", key="api_key", type="password")
107+
logger.info("OpenAI API key is initialized from user input.")
101108

102109
st.session_state.proxy_api = st.text_input("Proxy API", value="",placeholder="http://myproxy-api.replit.co/")
103-
st.session_state.openai_langchain = OpenAILangChain(st.session_state.code_language, st.session_state["openai"]["temperature"], st.session_state["openai"]["max_tokens"], st.session_state["openai"]["model_name"], api_key)
110+
st.session_state.openai_langchain = OpenAILangChain(api_key,st.session_state.code_language, st.session_state["openai"]["temperature"], st.session_state["openai"]["max_tokens"], st.session_state["openai"]["model_name"])
104111
st.toast("Open AI initialized successfully.", icon="✅")
105112
except Exception as exception:
106113
st.toast(f"Error loading Open AI: {str(exception)}", icon="❌")
@@ -179,13 +186,19 @@ def main():
179186
st.session_state["palm"]["temperature"] = st.slider("Temperature", min_value=0.0, max_value=1.0, value=st.session_state["palm"]["temperature"], step=0.1)
180187
st.session_state["palm"]["max_tokens"] = st.slider("Maximum Tokens", min_value=1, max_value=8196, value=st.session_state["palm"]["max_tokens"], step=1)
181188

182-
# Check if the API key is in App secrets.
183-
if st.secrets["PALM_API_KEY"]:
184-
api_key = st.secrets["PALM_API_KEY"]
185-
logger.info("Gemini AI API key is initialized from App secrets.")
186-
else:
187-
# Add password option for getting API key
188-
api_key = st.text_input("API Key", type="password")
189+
try:
190+
# Check if the API key is in App secrets.
191+
if st.secrets["PALM_API_KEY"]:
192+
api_key = st.secrets["PALM_API_KEY"]
193+
logger.info("Palm AI API key is initialized from App secrets.")
194+
except Exception as exception:
195+
logger.error(f"Error loading : {str(exception)}")
196+
st.toast(f"Error loading : {str(exception)}", icon="❌")
197+
198+
# Create API key input box on error.
199+
api_key = st.text_input("API Key", value="", key="api_key", type="password")
200+
logger.info("Palm API key is initialized from user input.")
201+
189202
try:
190203
st.session_state.palm_langchain = PalmAI(api_key, model=st.session_state["palm"]["model_name"], temperature=st.session_state["palm"]["temperature"], max_output_tokens=st.session_state["palm"]["max_tokens"])
191204
except Exception as exception:
@@ -204,16 +217,21 @@ def main():
204217
model_options_gemini = ["gemini-pro","gemini-pro-vision"]
205218
st.session_state["gemini"]["model_name"] = st.selectbox("Model name", model_options_gemini, index=model_options_gemini.index(st.session_state["gemini"]["model_name"]))
206219
st.session_state["gemini"]["temperature"] = st.slider("Temperature", min_value=0.0, max_value=1.0, value=st.session_state["gemini"]["temperature"], step=0.1)
207-
st.session_state["gemini"]["max_tokens"] = st.slider("Maximum Tokens", min_value=1, max_value=8196, value=st.session_state["gemini"]["max_tokens"], step=1)
220+
st.session_state["gemini"]["max_tokens"] = st.slider("Maximum Tokens", min_value=1, max_value=30720, value=st.session_state["gemini"]["max_tokens"], step=1)
221+
222+
try:
223+
# Check if the API key is in App secrets.
224+
if st.secrets["GEMINI_API_KEY"]:
225+
api_key = st.secrets["GEMINI_API_KEY"]
226+
logger.info("Gemini AI API key is initialized from App secrets.")
227+
except Exception as exception:
228+
logger.error(f"Error loading : {str(exception)}")
229+
st.toast(f"Error loading : {str(exception)}", icon="❌")
230+
231+
# Create API key input box on error.
232+
api_key = st.text_input("API Key", value="", key="api_key", type="password")
233+
logger.info("Gemini API key is initialized from user input.")
208234

209-
# Check if the API key is in App secrets.
210-
if st.secrets["GEMINI_API_KEY"]:
211-
api_key = st.secrets["GEMINI_API_KEY"]
212-
logger.info("Gemini AI API key is initialized from App secrets.")
213-
else:
214-
# Add password option for getting API key
215-
api_key = st.text_input("API Key", type="password")
216-
logger.info("Gemini AI API key is initialized from user input.")
217235
try:
218236
st.session_state.gemini_langchain = GeminiAI(api_key, model=st.session_state["gemini"]["model_name"], temperature=st.session_state["gemini"]["temperature"], max_output_tokens=st.session_state["gemini"]["max_tokens"])
219237
except Exception as exception:
@@ -282,11 +300,11 @@ def main():
282300
if st.session_state.openai_langchain:
283301
st.session_state.generated_code = st.session_state.openai_langchain.generate_code(st.session_state.code_prompt, code_language)
284302
else:# Reinitialize the chain
285-
if not api_key:
303+
if api_key == None:
286304
st.toast("Open AI API key is not initialized.", icon="❌")
287305
logger.error("Open AI API key is not initialized.")
288306
else:
289-
st.session_state.openai_langchain = OpenAILangChain(st.session_state.code_language,st.session_state["openai"]["temperature"],st.session_state["openai"]["max_tokens"],st.session_state["openai"]["model_name"],api_key)
307+
st.session_state.openai_langchain = OpenAILangChain(api_key,st.session_state.code_language,st.session_state["openai"]["temperature"],st.session_state["openai"]["max_tokens"],st.session_state["openai"]["model_name"])
290308
st.session_state.generated_code = st.session_state.openai_langchain.generate_code(st.session_state.code_prompt, code_language)
291309
elif st.session_state.ai_option == "Vertex AI":
292310
if st.session_state.vertexai_langchain:
@@ -307,7 +325,7 @@ def main():
307325
if st.session_state.palm_langchain:
308326
st.session_state.generated_code = st.session_state.palm_langchain.generate_code(st.session_state.code_prompt, code_language)
309327
else:# Reinitialize the chain
310-
if not api_key:
328+
if api_key == None:
311329
st.toast("Palm AI API key is not initialized.", icon="❌")
312330
logger.error("Palm AI API key is not initialized.")
313331
else:
@@ -318,7 +336,7 @@ def main():
318336
if st.session_state.gemini_langchain:
319337
st.session_state.generated_code = st.session_state.gemini_langchain.generate_code(st.session_state.code_prompt, code_language)
320338
else:# Reinitialize the chain
321-
if not api_key:
339+
if api_key == None:
322340
st.toast("Gemini AI API key is not initialized.", icon="❌")
323341
logger.error("Gemini AI API key is not initialized.")
324342
else:

0 commit comments

Comments
 (0)