File size: 1,735 Bytes
a33458e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
from langchain.llms import HuggingFaceHub
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
import sys
import os
# Add project root to path for imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from app.config import HF_API_KEY, LLM_MODEL, EMBEDDING_MODEL, DEFAULT_TEMPERATURE, MAX_TOKENS
def get_llm():
"""Initialize and return the language model."""
if not HF_API_KEY:
# Can still work without API key but with rate limits
print("Warning: Hugging Face API key not set. Using models without authentication.")
llm = HuggingFaceHub(
huggingfacehub_api_token=HF_API_KEY,
repo_id=LLM_MODEL,
model_kwargs={
"temperature": DEFAULT_TEMPERATURE,
"max_length": MAX_TOKENS
}
)
return llm
def get_embeddings():
"""Initialize and return the embeddings model."""
# SentenceTransformers can be used locally without an API key
return HuggingFaceEmbeddings(
model_name=EMBEDDING_MODEL
)
def get_chat_model():
"""
Create a chat-like interface using a regular LLM.
This is necessary because many free HF models don't have chat interfaces.
"""
llm = get_llm()
# Create a chat-like prompt template
chat_template = """
Context: {context}
Chat History:
{chat_history}
User: {question}
AI Assistant:
"""
prompt = PromptTemplate(
input_variables=["context", "chat_history", "question"],
template=chat_template
)
# Create a chain
return LLMChain(llm=llm, prompt=prompt) |