import aiohttp import json import traceback HUGGINGFACE_TOKEN = 'MY_API_KEY' MODEL = "HuggingFaceH4/zephyr-7b-beta" async def call_huggingface_api(messages, temperature=0.95, top_k=50, top_p=0.9, num_return_sequences=1000): url = f"https://api-inference.huggingface.co/models/{MODEL}" headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"} data = { "inputs": "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages]), "options": { "temperature": temperature, "top_k": top_k, "top_p": top_p, "max_length": 1000, "no_repeat_ngram_size": 3, "do_sample": True, "num_return_sequences": num_return_sequences, "no_repeat_ngram_size": 3, "repetition_penalty": 1.2, "length_penalty": 1.0, "early_stopping": True, } } try: async with aiohttp.ClientSession() as session: async with session.post(url, headers=headers, json=data) as resp: if resp.status == 200: response = await resp.json() return response else: print(f"Error calling Hugging Face API: {resp.status}, {await resp.text()}") return None except Exception as e: print(f"Error calling Hugging Face API: {e}") print(traceback.format_exc()) return None