Fix : server script error & readme not up to date

This commit is contained in:
martin legrand 2025-03-15 18:16:43 +01:00
parent 01e225a890
commit 07a43d75ab
2 changed files with 11 additions and 11 deletions

View File

@ -133,7 +133,7 @@ Note: For Windows or macOS, use ipconfig or ifconfig respectively to find the IP
Clone the repository and then, run the script `stream_llm.py` in `server/`
```sh
python3 stream_llm.py
python3 server_ollama.py
```
### 2**Run it**

View File

@ -10,10 +10,9 @@ log.setLevel(logging.ERROR)
app = Flask(__name__)
# Shared state with thread-safe locks
class Config:
def __init__(self):
self.model = None
self.model = None
self.known_models = []
self.allowed_models = []
self.model_name = None
@ -23,7 +22,7 @@ class Config:
data = json.load(f)
self.known_models = data['known_models']
self.model_name = data['model_name']
def validate_model(self, model):
if model not in self.known_models:
raise ValueError(f"Model {model} is not known")
@ -37,8 +36,8 @@ class GenerationState:
self.model = None
state = GenerationState()
def generate_response(history):
def generate_response(history): # Only takes history as an argument
global state
try:
with state.lock:
@ -47,7 +46,7 @@ def generate_response(history):
state.current_buffer = ""
stream = ollama.chat(
model=state.model,
model=state.model, # Access state.model directly
messages=history,
stream=True,
)
@ -70,13 +69,14 @@ def generate_response(history):
def start_generation():
global state
data = request.get_json()
with state.lock:
if state.is_generating:
return jsonify({"error": "Generation already in progress"}), 400
history = data.get('messages', [])
threading.Thread(target=generate_response, args=(history, state.model)).start()
# Pass only history to the thread
threading.Thread(target=generate_response, args=(history,)).start() # Note the comma to make it a single-element tuple
return jsonify({"message": "Generation started"}), 202
@app.route('/get_updated_sentence')
@ -93,4 +93,4 @@ if __name__ == '__main__':
config.load()
config.validate_model(config.model_name)
state.model = config.model_name
app.run(host='0.0.0.0', port=5000, debug=False, threaded=True)
app.run(host='0.0.0.0', port=5001, debug=False, threaded=True)