Lagt till vidarebefordran av LLM från backen.py till frontend (index.html, client.html, frontend.js

This commit is contained in:
2024-07-29 00:47:42 +02:00
parent 534874e810
commit 941b426574
4 changed files with 20 additions and 18 deletions
+12 -8
View File
@@ -7,11 +7,16 @@ import requests
import json import json
import logging import logging
import os import os
# from utils import set_local_logger import utils
from utils import GlobalState
# Create a logger for this module # Create a logger for this module
logger = logging.getLogger(__name__) # This logger will be used to log messages from this module # logger = logging.getLogger(__name__) # This logger will be used to log messages from this module
logger.debug("Logging level of backend logger has been configured") # logger.debug("Logging level of backend logger has been configured")
global_state = GlobalState() # Import the singleton that holds global states (e.g., logger)
logger = global_state.getLogger(__name__) # Logger for this module, inherit properties of the root logger
# llm = global_state.get_llm()
# Find out the path to current directory according to the Python interpreter (venv) # Find out the path to current directory according to the Python interpreter (venv)
logger.debug("Current working directory: %s", os.getcwd()) logger.debug("Current working directory: %s", os.getcwd())
@@ -31,6 +36,7 @@ def index():
logger.debug("Entering route '/'") logger.debug("Entering route '/'")
api_endpoint = os.environ['BE_API_ENDPOINT'] # Retrieve the environment variable api_endpoint = os.environ['BE_API_ENDPOINT'] # Retrieve the environment variable
logger.debug("API endpoint: %s", api_endpoint) logger.debug("API endpoint: %s", api_endpoint)
use_model = global_state.get_llm()
with open('smartassist/src/html/client.html', 'r') as f: with open('smartassist/src/html/client.html', 'r') as f:
client_html = f.read() client_html = f.read()
logger.debug("Client HTML (first few characters): %s", client_html[:50]) # Print to see if it's loading logger.debug("Client HTML (first few characters): %s", client_html[:50]) # Print to see if it's loading
@@ -114,13 +120,11 @@ def smartassist():
return jsonify({"response": response}) return jsonify({"response": response})
def get_response(user_query): def get_response(user_query):
# Create a client object for interacting with OLLAMA API client = Client() # Create a client object for interacting with OLLAMA API
client = Client() response = client.generate_response(user_query) # Generate and retrieve the response based on user's query
# Generate and retrieve the response based on user's query
response = client.generate_response(user_query)
# Return the generated response
return response return response
def run_flask(fport=5005): def run_flask(fport=5005):
""" """
Starts the Flask server Starts the Flask server
+3 -8
View File
@@ -17,16 +17,11 @@
<!-- Get the apiEndpoint --> <!-- Get the apiEndpoint -->
<script> <script>
const apiEndpoint = window.apiEndpoint; const apiEndpoint = window.apiEndpoint;
// Debugging log messages const useModel = window.useModel;
// if (apiEndpoint) {
// console.log("client.html - apiEndpoint: ", apiEndpoint);
// }
// else {
// console.log("client.html - cannot find apiEndpoint");
// }
</script> </script>
<!-- Get the javascript handling communication with the backene --> <!-- Get the javascript handling communication with the backend -->
<script src="/js/frontend.js"></script> <script src="/js/frontend.js"></script>
<script> <script>
+1 -1
View File
@@ -14,7 +14,7 @@ function sendMessage() {
fetch(`${apiEndpoint}`, { fetch(`${apiEndpoint}`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, model: "phi3:mini" }), // Add these parameters here body: JSON.stringify({ query, model: `${useModel}` }), // Add these parameters here
// body: JSON.stringify({ query, url_server: "http://your-custom-url", model: "phi3:mini" }), // Add these parameters here // body: JSON.stringify({ query, url_server: "http://your-custom-url", model: "phi3:mini" }), // Add these parameters here
}) })
.then(response => response.json()) .then(response => response.json())
+3
View File
@@ -18,9 +18,12 @@
<script> <script>
// Extract apiEndpoint for use in your frontend code... // Extract apiEndpoint for use in your frontend code...
const apiEndpoint = '{{ api_endpoint }}'; // Templating syntax (Jinja2) const apiEndpoint = '{{ api_endpoint }}'; // Templating syntax (Jinja2)
const useModel = '{{ use_model }}'; // Templating syntax (Jinja2)
// Tell the iframe about the apiEndpoing // Tell the iframe about the apiEndpoing
document.getElementById('client-frame').contentWindow.apiEndpoint = apiEndpoint; document.getElementById('client-frame').contentWindow.apiEndpoint = apiEndpoint;
document.getElementById('client-frame').contentWindow.useModel = useModel;
console.log("index.html - API Endpoint: ", apiEndpoint); console.log("index.html - API Endpoint: ", apiEndpoint);
console.log("index.html - use model: ", useModel);
</script> </script>
<!-- Responsive scaling and some padding --> <!-- Responsive scaling and some padding -->