Skip to content

Commit

Permalink
added beta_models capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mmartial committed Dec 5, 2024
1 parent 57ce464 commit 6f7cf54
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 20 deletions.
37 changes: 31 additions & 6 deletions OpenAI_GPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@


#####
def simpler_gpt_call(apikey, messages, model_engine, max_tokens, temperature, **kwargs):
def simpler_gpt_call(apikey, messages, model_engine, max_tokens, temperature, beta_model=False, **kwargs):
client = OpenAI(api_key=apikey)

# beta models limitation: https://platform.openai.com/docs/guides/reasoning
# o1 will may not provide an answer if the max_completion_tokens is lower than 2000

# with open("msg.json", 'w') as f:
# json.dump(messages, f, indent=4)

if beta_model is True:
kwargs['max_completion_tokens'] = max_tokens
else:
kwargs['max_tokens'] = max_tokens
kwargs['temperature'] = temperature

# Generate a response (20231108: Fixed for new API version)
try:
completion = client.chat.completions.create(
response = client.chat.completions.create(
model=model_engine,
messages = messages,
max_tokens=max_tokens,
temperature=temperature,
**kwargs
)
# using list from venv/lib/python3.11/site-packages/openai/_exceptions.py
Expand All @@ -35,7 +45,9 @@ def simpler_gpt_call(apikey, messages, model_engine, max_tokens, temperature, **
except openai.OpenAIError as e:
return(f"OpenAI API request failed: {e}", "")

return "", completion.choices[0].message.content
# with open("response.txt", 'w') as f:
# f.write(f"{response}")
return "", response.choices[0].message.content

##########
class OAI_GPT:
Expand Down Expand Up @@ -63,6 +75,7 @@ def __init__(self, apikey, base_save_location, username):
self.gpt_roles = {}
self.gpt_roles_help = ""
self.model_capability = {}
self.beta_models = {}


#####
Expand Down Expand Up @@ -96,6 +109,10 @@ def get_gpt_roles_help(self):
def get_save_location(self):
return self.save_location

def get_beta_models(self):
return self.beta_models


#####
# https://platform.openai.com/docs/models/continuous-model-upgrades
def set_parameters(self, models_list, av_models_list):
Expand Down Expand Up @@ -131,6 +148,10 @@ def set_parameters(self, models_list, av_models_list):
self.model_capability[key] = models[key]["capability"]
else:
self.model_capability[key] = "None"
if 'beta_model' in models[key]:
self.beta_models[key] = models[key]['beta_model']
else:
self.beta_models[key] = False
if cf.isNotBlank(models[key]["status_details"]):
per_model_help += " NOTE: " + models[key]["status_details"]
self.per_model_help[key] = per_model_help
Expand Down Expand Up @@ -310,7 +331,11 @@ def chatgpt_it(self, model_engine, prompt, max_tokens, temperature, clear_chat,
msg_file = f"{dest_dir}/msg.json"
with open(msg_file, 'w') as f:
json.dump(clean_messages, f, indent=4)
err, response = simpler_gpt_call(self.apikey, clean_messages, model_engine, max_tokens, temperature, **kwargs)

beta_model = False
if model_engine in self.beta_models:
beta_model = True
err, response = simpler_gpt_call(self.apikey, clean_messages, model_engine, max_tokens, temperature, beta_model=beta_model, **kwargs)

if cf.isNotBlank(err):
return err, ""
Expand Down
33 changes: 26 additions & 7 deletions OpenAI_GPT_WUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self, oai_gpt: OAI_GPT, enable_vision: bool = True, prompt_presets_
self.gpt_presets = oai_gpt.get_gpt_presets()
self.gpt_presets_help = oai_gpt.get_gpt_presets_help()
self.per_model_help = oai_gpt.get_per_model_help()
self.beta_models = oai_gpt.get_beta_models()

self.enable_vision = enable_vision

Expand Down Expand Up @@ -193,6 +194,7 @@ def set_ui(self):
clear_chat = False
prompt_preset = None
msg_extra = None
beta_model = False

if 'gpt_last_prompt' in st.session_state:
if st.session_state['gpt_last_prompt'] != "":
Expand Down Expand Up @@ -221,6 +223,8 @@ def set_ui(self):
st.info(f"{model}: {self.models_status[model]}")
if self.model_capability[model] == "vision":
vision_capable = True
if model in self.beta_models and self.beta_models[model] is True:
beta_model = True
m_token = self.models[model]['max_token']

# vision mode bypass
Expand All @@ -236,12 +240,22 @@ def set_ui(self):

role = list(self.gpt_roles.keys())[0]
if vision_mode is False:
role = st.selectbox("Role", options=self.gpt_roles, index=0, key="input_role", help = "Role of the input text\n\n" + self.gpt_roles_help)
tmp_roles = self.gpt_roles.copy()
if beta_model is True:
tmp_roles.pop("system")
tmp_txt = "" if beta_model is False else "\n\nBeta models do not support the 'system' role"
role = st.selectbox("Role", options=tmp_roles, index=0, key="input_role", help = "Role of the input text\n\n" + self.gpt_roles_help + tmp_txt)

if beta_model is False:
max_tokens = st.slider('max_tokens', 0, m_token, 1000, 100, "%i", "max_tokens", "The maximum number of tokens to generate in the completion. The token count of your prompt plus max_tokens cannot exceed the model\'s context length.")
else:
max_tokens = st.slider('max_completion_tokens', 0, m_token, 4000, 100, "%i", "max_completion_tokens", "For beta models: control the total number of tokens generated by the model, including both reasoning and visible completion tokens. If the token value is too low, the answer might be empty")

max_tokens = st.slider('max_tokens', 0, m_token, 1000, 100, "%i", "max_tokens", "The maximum number of tokens to generate in the completion. The token count of your prompt plus max_tokens cannot exceed the model\'s context length.")
temperature = st.slider('temperature', 0.0, 1.0, 0.5, 0.01, "%0.2f", "temperature", "The temperature of the model. Higher temperature results in more surprising text.")
temperature = 0.5
if beta_model is False:
temperature = st.slider('temperature', 0.0, 1.0, 0.5, 0.01, "%0.2f", "temperature", "The temperature of the model. Higher temperature results in more surprising text.")

if vision_mode is False:
if vision_mode is False and beta_model is False:
presets = st.selectbox("GPT Task", options=list(self.gpt_presets.keys()), index=0, key="presets", help=self.gpt_presets_help)
else:
presets = list(self.gpt_presets.keys())[0]
Expand Down Expand Up @@ -295,10 +309,13 @@ def set_ui(self):
if 'gpt_last_prompt' not in st.session_state:
st.session_state['gpt_last_prompt'] = ''

prompt_value=f"GPT ({model}) Input (role: {role}) [max_tokens: {max_tokens} | temperature: {temperature}"
prompt_value=f"GPT ({model}) Input (role: {role}) [max_tokens: {max_tokens} "
if beta_model is False:
prompt_value += f"| temperature: {temperature}"
if vision_mode:
prompt_value += f" | vision details: {vision_details}"
prompt_value += f" | preset: {presets}"
if beta_model is False:
prompt_value += f" | preset: {presets}"
if prompt_preset is not None:
prompt_value += f" | prompt preset: {prompt_preset}"
if 'gpt_clear_chat' in st.session_state or clear_chat is True:
Expand Down Expand Up @@ -351,7 +368,9 @@ def set_ui(self):
st.warning("You requested an estimated %i tokens, which might exceed the model's context window of %i tokens. We are still proceeding with the request, but an error return is possible." % (requested_token_count, self.models[model]["context_token"]))

if max_tokens > 0:
with st.spinner(f"Asking OpenAI ({model} for {max_tokens} tokens with temperature {temperature}. Prompt est. tokens : {prompt_token_count})"):
tmp_txt = "" if beta_model is True else f" (temperature: {temperature})"
st.toast(f"Requesting OpenAI ({model} for {max_tokens} tokens{tmp_txt})")
with st.spinner(f"Asking OpenAI ({model} for {max_tokens} tokens{tmp_txt}. Prompt est. tokens : {prompt_token_count})"):
if 'gpt_clear_chat' in st.session_state:
clear_chat = True
del st.session_state['gpt_clear_chat']
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ The following table shows the [models](https://platform.openai.com/docs/models/)
| GPT | gpt-4o-2024-08-06 | active | vision | | 0.9.8 |
| GPT | gpt-4o-mini | active | vision | | 0.9.7 |
| GPT | gpt-4o-mini-2024-07-18 | active | vision | | 0.9.7 |
| GPT | o1-preview | active | vision | untested | 0.9.8 |
| GPT | o1-mini | active | vision | untested | 0.9.8 |
| GPT | o1-preview | active | | untested | 0.9.8 |
| GPT | o1-mini | active | | untested | 0.9.8 |

(MM) Note on "untested": we do not yet have access to the model (`... or you do not have access to it`)

Expand Down
10 changes: 6 additions & 4 deletions models.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@
"context_token": 128000,
"data": "Up to Oct 2023 (as of 20240912)",
"status": "active",
"status_details": "",
"capability": "vision"
"status_details": "beta model: limited control of features and capabilities. For details, see https://platform.openai.com/docs/guides/reasoning",
"capability": "",
"beta_model": true
},
"o1-mini":
{
Expand All @@ -68,8 +69,9 @@
"context_token": 128000,
"data": "Up to Oct 2023 (as of 20240912)",
"status": "active",
"status_details": "",
"capability": "vision"
"status_details": "beta model: limited control of features and capabilities. For details, see https://platform.openai.com/docs/guides/reasoning",
"capability": "",
"beta_model": true
},
"gpt-4-turbo":
{
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry.dependencies]
python = "^3.10"
openai = "==1.51.0"
openai = "==1.56.1"
streamlit = "^1.39.0"
extra-streamlit-components = "^0.1.71"
streamlit-extras = "^0.4.7"
Expand Down

0 comments on commit 6f7cf54

Please sign in to comment.