Compare commits
3 Commits
41c0c86d82
...
f160092b2a
| Author | SHA1 | Date | |
|---|---|---|---|
| f160092b2a | |||
| 06628d5c19 | |||
| 2e24be1e44 |
@@ -22,6 +22,10 @@ logger.debug("Current working directory: %s", os.getcwd())
|
||||
app = Flask(__name__)
|
||||
app.config['STATIC_FOLDER'] = 'static' # Adjust if needed
|
||||
|
||||
# Increase the maximum cookie size
|
||||
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
|
||||
app.config['SESSION_COOKIE_SIZE_LIMIT'] = 4096 * 2 # Allow up to 8KB cookies
|
||||
|
||||
# Set the secret key for session management
|
||||
secret_key = os.urandom(24)
|
||||
app.config['SECRET_KEY'] = secret_key # When do I need this. How is it retained between sessions?
|
||||
|
||||
@@ -9,7 +9,7 @@ from backend import run_flask, tag
|
||||
import logging
|
||||
import requests
|
||||
import utils
|
||||
from utils import GlobalState
|
||||
from utils import GlobalState, fetch_models_from_endpoints
|
||||
|
||||
global_state = GlobalState() # Configure root logger. The level will be adjusted later based on config file
|
||||
logger = global_state.getLogger(__name__) # Logger for this module, inherit properties of the root logger
|
||||
@@ -70,34 +70,7 @@ def configure():
|
||||
if isinstance(updated_config.get('endpoints'), list): # Extract info on endpoint, model, url, provider et cetera from list
|
||||
global_state.set_endpoints(endpoints=updated_config.get('endpoints')) # Extract and set list of endpoints
|
||||
logger.debug("endpoints = \n{}".format(json.dumps(global_state.get_endpoints(), indent=4)))
|
||||
for endpoint in global_state.get_endpoints():
|
||||
if endpoint["provider"] == "ollama":
|
||||
if "requestOptions" in endpoint: # Check if authentication is needed
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": endpoint["requestOptions"]["headers"]["Authorization"]
|
||||
}
|
||||
# headers = {
|
||||
# "Authorization": endpoint["requestOptions"]["headers"]["Authorization"]
|
||||
# }
|
||||
else: # otherwise proceed without authentication
|
||||
headers = {"Content-Type": "application/json"}
|
||||
# headers = None
|
||||
# models = tag(url = endpoint["url"], headers = headers) # Ask for models (LLMs) available at endpoint
|
||||
try:
|
||||
models = requests.get(endpoint["url"] + "/api/tags", headers=headers).json()
|
||||
# models = requests.get(endpoint["url"] + "/api/tags", headers=headers)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
if isinstance(models, dict) and 'error' in models:
|
||||
logger.error('Error fetching models from backend: %s', models['error'])
|
||||
else:
|
||||
endpoint["models"] = models.get("models", []) # get the list of models directly
|
||||
logger.debug("models = \n{}".format(json.dumps(models, indent=4)))
|
||||
if endpoint["model"] is not "AUTODETECT": # Check if specified model is available
|
||||
# do something
|
||||
logger.debug("Asking for specific model")
|
||||
fetch_models_from_endpoints(global_state.get_endpoints(), global_state) # Call the new function
|
||||
|
||||
|
||||
# TODO: Remove this section when not needed anymore
|
||||
|
||||
@@ -2,6 +2,49 @@
|
||||
# imported to more than one other module. The rational for defining these things here
|
||||
# is that it is easier to avoid circular imports when they are defined in a central location.
|
||||
import logging
|
||||
import json
|
||||
import requests
|
||||
#from backend import GlobalState # Assuming GlobalState is defined there
|
||||
|
||||
def fetch_models_from_endpoints(endpoints, global_state):
|
||||
"""
|
||||
Fetch models from endpoints and update the endpoint dictionaries.
|
||||
|
||||
Args:
|
||||
endpoints (list): List of endpoint dictionaries.
|
||||
global_state: The global state object.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
for endpoint in endpoints:
|
||||
if endpoint["provider"] == "ollama":
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
if "requestOptions" in endpoint: # Check if authentication is needed
|
||||
headers.update({
|
||||
"Authorization": endpoint["requestOptions"]["headers"]["Authorization"]
|
||||
})
|
||||
|
||||
try:
|
||||
models_response = requests.get(endpoint["url"] + "/api/tags", headers=headers)
|
||||
models_response.raise_for_status() # Raise an exception for HTTP errors
|
||||
models = models_response.json()
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error("Error fetching models from backend: %s", str(e))
|
||||
continue
|
||||
|
||||
if isinstance(models, dict) and 'error' in models:
|
||||
logger.error('Error fetching models from backend: %s', models['error'])
|
||||
else:
|
||||
endpoint["models"] = models.get("models", []) # get the list of models directly
|
||||
logger.debug("models = \n{}".format(json.dumps(models, indent=4)))
|
||||
if endpoint["model"] is not "AUTODETECT": # Check if specified model is available
|
||||
logger.debug("Asking for specific model")
|
||||
|
||||
|
||||
class GlobalState:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user