Läser nu ut Ollama-modell från yaml-fil

This commit is contained in:
2024-07-29 00:48:21 +02:00
parent 941b426574
commit 1757542077
+30 -22
View File
@@ -56,38 +56,46 @@ def configure():
# envrionment variable # envrionment variable
#################################### ####################################
backend_api_ep = 'http://localhost:5005/api/chat' # Default API endpoint backend_api_ep = 'http://localhost:5005/api/chat' # Default API endpoint
if isinstance(updated_config.get('backend'), dict): # Look for 'backend' key in config file if isinstance(updated_config.get('backend'), dict): # Look for 'backend' key
if isinstance(updated_config['backend'].get('url'), str): # Look for 'url' key in config file if isinstance(updated_config['backend'].get('url'), str): # Look for 'url' key
url = updated_config['backend'].get('url') url = updated_config['backend'].get('url')
if isinstance(updated_config['backend'].get('api'), str): # Look for 'api' key in config file if isinstance(updated_config['backend'].get('api'), str): # Look for 'api' key
api = updated_config['backend'].get('api') api = updated_config['backend'].get('api')
backend_api_ep = url+api # Extract API endpoint if defined backend_api_ep = url+api # Extract API endpoint if defined
logger.debug("BE_API_ENDPOINT is set to '{}'".format(backend_api_ep)) logger.debug("BE_API_ENDPOINT is set to '{}'".format(backend_api_ep))
os.environ['BE_API_ENDPOINT'] = backend_api_ep # Look into alternative way to share this with backend.py os.environ['BE_API_ENDPOINT'] = backend_api_ep # Look into alternative way to share this with backend.py
####################################
# Extract Ollama parameters (url, api_key, model)
####################################
if isinstance(updated_config.get('ollama'), dict): # Look for 'ollama' key
if isinstance(updated_config['ollama'].get('model'), str): # Look for 'model' key
model_to_use = updated_config['ollama'].get('model')
global_state.set_llm(model_to_use)
return updated_config return updated_config
def start_frontend(config): # def start_frontend(config):
parsed_url = urllib.parse.urlparse(config['frontend']['url']) # parsed_url = urllib.parse.urlparse(config['frontend']['url'])
hostname = parsed_url.netloc.split(':')[0] # Split by ':' and take the first part, i.e., 'localhost', IP, or domain name # hostname = parsed_url.netloc.split(':')[0] # Split by ':' and take the first part, i.e., 'localhost', IP, or domain name
port = parsed_url.port # This is the server port # port = parsed_url.port # This is the server port
# Use the socket module in Python to check whether a port is in use, # # Use the socket module in Python to check whether a port is in use,
# which would indicate that a server is already running on that port. # # which would indicate that a server is already running on that port.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: # with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try: # try:
s.bind((hostname, port)) # s.bind((hostname, port))
logger.debug("No server is running on %s -— starting one.", parsed_url.netloc) # logger.debug("No server is running on %s -— starting one.", parsed_url.netloc)
# Start frontend (web server) as a separate process # # Start frontend (web server) as a separate process
subprocess.Popen(["python", "-m", "http.server", str(port)]) # subprocess.Popen(["python", "-m", "http.server", str(port)])
except socket.error as e: # except socket.error as e:
if e.errno == 48: # if e.errno == 48:
logger.debug("A server is already running on %s -— will use this.", parsed_url.netloc) # logger.debug("A server is already running on %s -— will use this.", parsed_url.netloc)
else: # else:
raise # Unexpected error, re-raise it so we can see the traceback # raise # Unexpected error, re-raise it so we can see the traceback
except Exception as e: # except Exception as e:
logger.error("Failed to start frontend: %s", str(e)) # Corresponds to print(f"Failed to start frontend: {e}") # logger.error("Failed to start frontend: %s", str(e)) # Corresponds to print(f"Failed to start frontend: {e}")