5 Commits

Author SHA1 Message Date
Joakim Persson a90b3e5f2d Mer parametrisering från konfguration och rensning av utkommenterad kod 2024-07-18 17:14:28 +02:00
Joakim Persson c4e4d8ded0 Lagt till konfigurering från fil samt rensat ut oanvänd kod 2024-07-18 17:13:02 +02:00
Joakim Persson fd31d583af Lagt till några modellnamn samt api-endpoint 2024-07-18 17:11:57 +02:00
Joakim Persson 89b1d16bfd Lade till krav på PyYAML 2024-07-18 17:11:00 +02:00
Joakim Persson cfb6b6a9bd Introducerar yaml-baserad konfiguration för smidigare hantering 2024-07-18 13:43:23 +02:00
4 changed files with 76 additions and 35 deletions
+26
View File
@@ -0,0 +1,26 @@
# Frontend Configuration
frontend:
url: "http://localhost:8000"
# Backend Configuration
backend:
url: "http://localhost:5005"
api: "/api/chat"
# Ollama Server Configuration
ollama:
url: "http://localhost:11434"
api_key: "${OLLAMA_API_KEY}" # Refer to environment variable
model: "phi3:mini" # Select a model supported by the Ollama server
# model: "llama3:70b" # Select a model supported by the Ollama server
# model: "llama3:latest" # Select a model supported by the Ollama server
# Additional Configuration Options (Optional ignored for now)
logging:
level: DEBUG # Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
# Cache Settings (Optional)
cache:
enabled: true
timeout: 60 # Seconds
+1
View File
@@ -7,3 +7,4 @@ ollama
Flask Flask
flask_cors flask_cors
requests requests
PyYAML
+5 -20
View File
@@ -17,13 +17,8 @@ CORS(app, resources={
} }
}) })
# @app.route('/api/chat', methods=['POST'])
# @cross_origin(origin='http://localhost:8000', headers=['Content-Type']) # Allow cross-origin requests from localhost:8000
# @cross_origin(origin='*', headers=['Content-Type']) # Enable CORS for this route. Probably not necessary.
# @cross_origin(origin='*', headers=['Content-Type', 'Accept'])
@app.route('/api/chat', methods=['POST']) @app.route('/api/chat', methods=['POST'])
def chat(): def chat(url_server = "http://localhost:11434/api/generate", model = "phi3:mini"):
# Get the message from the JSON in the request body # Get the message from the JSON in the request body
data = request.get_json() data = request.get_json()
message = data.get('query') message = data.get('query')
@@ -32,8 +27,8 @@ def chat():
try: try:
# Alternative LLM: "model": "mannix/llama3-8b-ablitered-v3:latest", # Alternative LLM: "model": "mannix/llama3-8b-ablitered-v3:latest",
url = "http://localhost:11434/api/generate" url = url_server
model_to_use = "phi3:mini" model_to_use = model
data = { data = {
"model": model_to_use, "model": model_to_use,
'prompt': message, 'prompt': message,
@@ -85,22 +80,12 @@ def get_response(user_query):
# Return the generated response # Return the generated response
return response return response
def run_flask(): def run_flask(fport = 5005):
# Flask endpoint for user interaction # Flask endpoint for user interaction
app.run(port=5005, debug=True) app.run(str(fport), debug=True)
# app.run(port=5000, debug=True, use_reloader=False) # app.run(port=5000, debug=True, use_reloader=False)
# @app.route('/api/chat', methods=['POST'])
# def chat():
# data = request.get_json()
# query = data['query']
# # response = requests.post('http://ollama-server/api/v1/chat', json={'prompt': query})
# response = requests.post('localhost::11434', json={"model": "llama3",'prompt': query})
# return jsonify({'response': response.json().get('result')})
if __name__ == '__main__': if __name__ == '__main__':
# Run the Flask application # Run the Flask application
run_flask() run_flask()
+43 -14
View File
@@ -1,21 +1,45 @@
# Start all services # Start all services
import subprocess import subprocess
import threading import os
import yaml
from backend import run_flask from backend import run_flask
import socket import socket
import urllib.parse
def start_frontend():
host = 'localhost' # or the address of your server def configure():
port = 8000 # change to your server's port # Load configuration file that defines parameters for services
with open('./smartassist/config/smartassist.yaml') as f:
config = yaml.safe_load(f)
# 1. Find the API Key environment variable name
api_key_env = None
for key, value in config['ollama'].items():
if isinstance(value, str) and value.startswith("${") and value.endswith("}"):
api_key_env = value[2:-1] # Extract name between ${}
# 2. Obtain API Key if an environment variable was found
api_key = None
if api_key_env:
api_key = os.getenv(api_key_env)
else:
print("Warning: Environment variable reference not found in YAML configuration.")
# Update the config dictionary with the actual API key (or None if not found)
config['ollama']['api_key'] = api_key
return config
def start_frontend(config):
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
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((host, port)) s.bind((hostname, port))
print("No server is running on this host and port.") print("No server is running on this host and port.")
# 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)])
@@ -28,15 +52,20 @@ def start_frontend():
print(f"Failed to start frontend: {e}") print(f"Failed to start frontend: {e}")
def start_backend(): def start_backend(config):
parsed_url = urllib.parse.urlparse(config['backend']['url'])
# 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
print(f"{port}")
try: try:
# Start backend as a separate thread run_flask(port = port)
# threading.Thread(target=run_flask).start() # Flask's built-in server doesn't support running in a separate thread.
run_flask()
except Exception as e: except Exception as e:
print(f"Failed to start backend: {e}") print(f"Failed to start backend: {e}")
if __name__ == '__main__': if __name__ == '__main__':
start_frontend() # No need for this as the backend starts a web server on its own. conf = configure() # Read config from file and set up config dict
start_backend() print(conf)
start_frontend(config=conf) # No need for this as the backend starts a web server on its own.
start_backend(config=conf)