File size: 1,453 Bytes
212d543
 
 
 
e355ce1
212d543
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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