import requests | |
import os | |
from dotenv import load_dotenv | |
# Cargar variables de entorno | |
load_dotenv() | |
class Llama2Chatbot: | |
def __init__(self): | |
self.API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-hf" | |
api_key = os.getenv("HF_API_KEY") | |
if not api_key: | |
raise ValueError("No se encontró la clave de API de Hugging Face. Asegúrate de configurar la variable de entorno HF_API_KEY.") | |
self.headers = {"Authorization": f"Bearer {api_key}"} | |
def generate_response(self, prompt): | |
payload = {"inputs": prompt} | |
response = requests.post(self.API_URL, headers=self.headers, json=payload) | |
return response.json()[0]['generated_text'] | |
def initialize_chatbot(): | |
return Llama2Chatbot() | |
def get_chatbot_response(chatbot, prompt): | |
return chatbot.generate_response(prompt) |