husseinelsaadi commited on
Commit
763e3e7
·
1 Parent(s): fb236cf
Files changed (1) hide show
  1. app.py +29 -15
app.py CHANGED
@@ -44,23 +44,37 @@ import json
44
  from backend.services.codingo_chatbot import get_response as _codingo_get_response
45
 
46
 
47
- def get_chatbot_response(query: str) -> str:
48
- """Proxy to the codingo_chatbot implementation.
49
-
50
- This function exists to preserve the original public API of
51
- ``app.get_chatbot_response`` while redirecting calls to the new
52
- implementation. It catches any exceptions and returns a user
53
- friendly message, ensuring the Flask route never raises.
54
- """
55
- try:
56
- return _codingo_get_response(query)
57
- except Exception as exc:
58
- print(f"Chatbot error: {exc}", file=sys.stderr)
59
- return (
60
- "I'm having trouble processing your request. Please try again or ask "
61
- "about Codingo's features, job matching, or how to use the platform."
62
  )
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  # Initialize Flask app
65
  app = Flask(
66
  __name__,
 
44
  from backend.services.codingo_chatbot import get_response as _codingo_get_response
45
 
46
 
47
+ def get_chatbot_response(user_input: str) -> str:
48
+ from llama_cpp import Llama
49
+
50
+ # Load model once
51
+ global llm
52
+ if 'llm' not in globals():
53
+ llm = Llama(
54
+ model_path="/tmp/llama_models/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf",
55
+ n_ctx=2048,
56
+ n_threads=8,
57
+ n_gpu_layers=20 # GPU acceleration if available
 
 
 
 
58
  )
59
 
60
+ # Format prompt in TinyLlama's chat style
61
+ prompt = f"<|system|>\nYou are LUNA, a helpful assistant for the Codingo recruitment platform.\n<|user|>\n{user_input}\n<|assistant|>\n"
62
+
63
+ # Generate response with safe parameters
64
+ output = llm(
65
+ prompt,
66
+ max_tokens=256,
67
+ temperature=0.3, # lower temperature for stability
68
+ top_p=0.9,
69
+ repeat_penalty=1.1,
70
+ stop=["</s>"]
71
+ )
72
+
73
+ reply = output["choices"][0]["text"].strip()
74
+ if not reply:
75
+ reply = "I'm here to help you with Codingo. Could you please rephrase your question?"
76
+ return reply
77
+
78
  # Initialize Flask app
79
  app = Flask(
80
  __name__,