Skip to content

Commit

Permalink
backend compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jedan2506 committed Aug 28, 2023
1 parent a68a7f5 commit a01cc86
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 52 deletions.
14 changes: 7 additions & 7 deletions gui/pages/Content/Agents/AgentCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
117 changes: 72 additions & 45 deletions superagi/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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',

Check warning on line 163 in superagi/models/models.py

View check run for this annotation

Codecov / codecov/patch

superagi/models/models.py#L163

Added line #L163 was not covered by tests
Configuration.organisation_id == organisation_id).first()

if configurations is None:
return {"error": "API Key is Missing"}

Check warning on line 167 in superagi/models/models.py

View check run for this annotation

Codecov / codecov/patch

superagi/models/models.py#L167

Added line #L167 was not covered by tests
else:
default_models = {"gpt-3.5-turbo": 4032, "gpt-4": 8092, "gpt-3.5-turbo-16k": 16184}
model_api_key = decrypt_data(configurations.value)

Check warning on line 170 in superagi/models/models.py

View check run for this annotation

Codecov / codecov/patch

superagi/models/models.py#L169-L170

Added lines #L169 - L170 were not covered by tests

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()

Check warning on line 174 in superagi/models/models.py

View check run for this annotation

Codecov / codecov/patch

superagi/models/models.py#L172-L174

Added lines #L172 - L174 were not covered by tests

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, '',

Check warning on line 180 in superagi/models/models.py

View check run for this annotation

Codecov / codecov/patch

superagi/models/models.py#L180

Added line #L180 was not covered by tests
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()
Expand Down Expand Up @@ -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}")

0 comments on commit a01cc86

Please sign in to comment.