Spaces:
Sleeping
Sleeping
Upload llm_handler.py
Browse files- llm_handler.py +65 -0
llm_handler.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
|
| 4 |
+
# --- Global Variables from main app ---
|
| 5 |
+
encoder = None
|
| 6 |
+
chroma_collection = None
|
| 7 |
+
openrouter_client = None
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def initialize_llm():
|
| 11 |
+
"""Initializes the OpenRouter client."""
|
| 12 |
+
global openrouter_client
|
| 13 |
+
|
| 14 |
+
# Get the API key from Hugging Face secrets
|
| 15 |
+
api_key = os.getenv("OPENROUTER_API_KEY")
|
| 16 |
+
|
| 17 |
+
if not api_key:
|
| 18 |
+
print("❌ OPENROUTER_API_KEY secret not found.")
|
| 19 |
+
return
|
| 20 |
+
|
| 21 |
+
openrouter_client = OpenAI(
|
| 22 |
+
base_url="https://openrouter.ai/api/v1",
|
| 23 |
+
api_key=api_key,
|
| 24 |
+
)
|
| 25 |
+
print("✅ OpenRouter client initialized successfully.")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def get_rag_response(query: str) -> str:
|
| 29 |
+
"""Generates a response using Retrieval-Augmented Generation with OpenRouter."""
|
| 30 |
+
if not all([encoder, chroma_collection, openrouter_client]):
|
| 31 |
+
return "Chatbot is not ready. Models or clients are not loaded."
|
| 32 |
+
|
| 33 |
+
# 1. Retrieve relevant documents from ChromaDB
|
| 34 |
+
query_embedding = encoder.encode([query])[0].tolist()
|
| 35 |
+
results = chroma_collection.query(
|
| 36 |
+
query_embeddings=[query_embedding],
|
| 37 |
+
n_results=3,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Get the 'metadatas' which contain the full internship details
|
| 41 |
+
retrieved_docs = results.get('metadatas', [[]])[0]
|
| 42 |
+
context = "\n".join([str(doc) for doc in retrieved_docs])
|
| 43 |
+
|
| 44 |
+
# 2. Generate a response using the LLM with the retrieved context
|
| 45 |
+
system_prompt = """
|
| 46 |
+
You are a helpful and friendly assistant for the PM Internship Scheme.
|
| 47 |
+
First, try to answer the user's question based on the provided context about specific internships.
|
| 48 |
+
If the context doesn't have the answer, use your own general knowledge to provide a helpful response.
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
user_prompt = f"Context:\n{context}\n\nQuestion: {query}"
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
completion = openrouter_client.chat.completions.create(
|
| 55 |
+
model="x-ai/grok-4-fast", # The specific model ID for Grok-4 Fast
|
| 56 |
+
messages=[
|
| 57 |
+
{"role": "system", "content": system_prompt},
|
| 58 |
+
{"role": "user", "content": user_prompt},
|
| 59 |
+
],
|
| 60 |
+
)
|
| 61 |
+
answer = completion.choices[0].message.content
|
| 62 |
+
return answer
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"❌ Error calling OpenRouter API: {e}")
|
| 65 |
+
return "Sorry, I encountered an error while processing your request."
|