File size: 867 Bytes
eea761e
 
 
 
 
 
178081b
 
 
eea761e
 
 
 
 
178081b
eea761e
 
 
 
178081b
 
 
 
 
 
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
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)