From a01cc8600dd49824cf87c2e120e33e53fb56cf3f Mon Sep 17 00:00:00 2001 From: Kalki Date: Mon, 28 Aug 2023 18:41:40 +0530 Subject: [PATCH] backend compatibility --- gui/pages/Content/Agents/AgentCreate.js | 14 +-- superagi/models/models.py | 117 +++++++++++++++--------- 2 files changed, 79 insertions(+), 52 deletions(-) diff --git a/gui/pages/Content/Agents/AgentCreate.js b/gui/pages/Content/Agents/AgentCreate.js index 3a37607c9..17729cddf 100644 --- a/gui/pages/Content/Agents/AgentCreate.js +++ b/gui/pages/Content/Agents/AgentCreate.js @@ -56,7 +56,7 @@ export default function AgentCreate({ const [searchValue, setSearchValue] = useState(''); const [showButton, setShowButton] = useState(false); const [showPlaceholder, setShowPlaceholder] = useState(true); - const [modelsArray, setModelsArray] = useState(['gpt-4', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-4-32k']); + const [modelsArray, setModelsArray] = useState(['gpt-4', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k']); const constraintsArray = [ "If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.", @@ -69,7 +69,7 @@ export default function AgentCreate({ const [goals, setGoals] = useState(['Describe the agent goals here']); const [instructions, setInstructions] = useState(['']); - const models = ['gpt-4', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-4-32k'] + const models = ['gpt-4', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k'] const [model, setModel] = useState(models[1]); const modelRef = useRef(null); const [modelDropdown, setModelDropdown] = useState(false); @@ -155,7 +155,7 @@ export default function AgentCreate({ .then((response) => { const models = response.data.map(model => model.name) || []; const selected_model = localStorage.getItem("agent_model_" + String(internalId)) || ''; - setModelsArray(prevModels => Array.from(new Set([...prevModels, ...models]))); + setModelsArray(models); if (models.length > 0 && !selected_model) { setLocalStorageValue("agent_model_" + String(internalId), models[0], setModel); } else { @@ -504,10 +504,10 @@ export default function AgentCreate({ } const handleAddAgent = async () => { - if(env === 'DEV' && models.includes(model)) { - const bool = await validateModel() - if(!bool) return; - } + // if(env === 'DEV' && models.includes(model)) { + // const bool = await validateModel() + // if(!bool) return; + // } if (!validateAgentData(true)) { return; diff --git a/superagi/models/models.py b/superagi/models/models.py index 59a9b5fd6..8e8c2a680 100644 --- a/superagi/models/models.py +++ b/superagi/models/models.py @@ -2,7 +2,8 @@ from sqlalchemy.sql import func from typing import List, Dict, Union from superagi.models.base_model import DBBaseModel -from superagi.helper.encyption_helper import encrypt_data, decrypt_data +from superagi.llms.openai import OpenAi +from superagi.helper.encyption_helper import decrypt_data import requests, logging # marketplace_url = "https://app.superagi.com/api" @@ -154,6 +155,31 @@ def store_model_details(cls, session, organisation_id, model_name, description, def fetch_models(cls, session, organisation_id) -> Union[Dict[str, str], List[Dict[str, Union[str, int]]]]: try: from superagi.models.models_config import ModelsConfig + from superagi.models.configuration import Configuration + + model_provider = session.query(ModelsConfig).filter(ModelsConfig.provider == "OpenAI", + ModelsConfig.org_id == organisation_id).first() + if model_provider is None: + configurations = session.query(Configuration).filter(Configuration.key == 'model_api_key', + Configuration.organisation_id == organisation_id).first() + + if configurations is None: + return {"error": "API Key is Missing"} + else: + default_models = {"gpt-3.5-turbo": 4032, "gpt-4": 8092, "gpt-3.5-turbo-16k": 16184} + model_api_key = decrypt_data(configurations.value) + + model_details = ModelsConfig.store_api_key(session, organisation_id, "OpenAI", model_api_key) + model_provider_id = model_details.get('model_provider_id') + models = OpenAi(api_key=model_api_key).get_models() + + installed_models = [model[0] for model in session.query(Models.model_name).filter(Models.org_id == organisation_id).all()] + + for model in models: + if model not in installed_models and model in default_models: + result = cls.store_model_details(session, organisation_id, model, model, '', + model_provider_id, default_models[model], 'Custom', '') + models = session.query(Models.id, Models.model_name, Models.description, ModelsConfig.provider).join( ModelsConfig, Models.model_provider_id == ModelsConfig.id).filter( Models.org_id == organisation_id).all() @@ -203,47 +229,48 @@ def fetch_model_details(cls, session, organisation_id, model_id: int) -> Dict[st logging.error(f"Unexpected Error Occured: {e}") return {"error": "Unexpected Error Occured"} - @classmethod - def validate_model_in_db(cls, session, organisation_id, model): - try: - from superagi.models.models_config import ModelsConfig - from superagi.models.configuration import Configuration - - models = {"gpt-3.5-turbo-0301": 4032, "gpt-4-0314": 8092, "gpt-3.5-turbo": 4032, - "gpt-4": 8092, "gpt-3.5-turbo-16k": 16184, "gpt-4-32k": 32768} - - model_config = session.query(Models).filter(Models.model_name == model, - Models.org_id == organisation_id).first() - if model_config is None: - model_provider = session.query(ModelsConfig).filter(ModelsConfig.provider == "OpenAI", - ModelsConfig.org_id == organisation_id).first() - - if model_provider is None: - configurations = session.query(Configuration).filter(Configuration.key == 'model_api_key', - Configuration.organisation_id == organisation_id).first() - model_api_key = decrypt_data(configurations.value) - - if configurations is None: - return {"error": "Model not found and the API Key is missing"} - - model_details = ModelsConfig.store_api_key(session, organisation_id, "OpenAI", model_api_key) - - # Get 'model_provider_id' - model_provider_id = model_details.get('model_provider_id') - - result = cls.store_model_details(session, organisation_id, model, model, '', - model_provider_id, models[model], 'Custom', '') - if result is not None: - return {"success": "The Model has been Successfully installed as it was not previously set up"} - - else: - result = cls.store_model_details(session, organisation_id, model, model, '', - model_provider.id, models[model], 'Custom', '') - if result is not None: - return {"success": "The Model has been Successfully installed as it was not previously set up"} - - else: - return {"success": "Model is found"} - - except Exception as e: - logging.error(f"Unexpected Error occurred while Validating GPT Models: {e}") + # @classmethod + # def validate_model_in_db(cls, session, organisation_id, model): + # try: + # from superagi.models.models_config import ModelsConfig + # from superagi.models.configuration import Configuration + # + # models = {"gpt-3.5-turbo": 4032, "gpt-4": 8092, "gpt-3.5-turbo-16k": 16184} + # + # model_config = session.query(Models).filter(Models.model_name == model, + # Models.org_id == organisation_id).first() + # if model_config is None: + # model_provider = session.query(ModelsConfig).filter(ModelsConfig.provider == "OpenAI", + # ModelsConfig.org_id == organisation_id).first() + # + # if model_provider is None: + # configurations = session.query(Configuration).filter(Configuration.key == 'model_api_key', + # Configuration.organisation_id == organisation_id).first() + # model_api_key = decrypt_data(configurations.value) + # + # if configurations is None: + # return {"error": "Model not found and the API Key is missing"} + # + # model_details = ModelsConfig.store_api_key(session, organisation_id, "OpenAI", model_api_key) + # print("///////////////////////////////////////1") + # models = OpenAi(api_key=model_api_key).get_models() + # print("////////////////////////////////////////2") + # print(models) + # model_provider_id = model_details.get('model_provider_id') + # + # result = cls.store_model_details(session, organisation_id, model, model, '', + # model_provider_id, models[model], 'Custom', '') + # if result is not None: + # return {"success": "The Model has been Successfully installed as it was not previously set up"} + # + # else: + # result = cls.store_model_details(session, organisation_id, model, model, '', + # model_provider.id, models[model], 'Custom', '') + # if result is not None: + # return {"success": "The Model has been Successfully installed as it was not previously set up"} + # + # else: + # return {"success": "Model is found"} + # + # except Exception as e: + # logging.error(f"Unexpected Error occurred while Validating GPT Models: {e}")