2023-09-03 08:26:26 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-01 04:38:11 +00:00
|
|
|
import json, uuid
|
2023-09-03 08:26:26 +00:00
|
|
|
|
2023-08-27 23:43:45 +00:00
|
|
|
from aiohttp import ClientSession
|
|
|
|
|
2023-10-04 05:20:51 +00:00
|
|
|
from ...typing import AsyncGenerator
|
|
|
|
from ..base_provider import AsyncGeneratorProvider, format_prompt, get_cookies
|
2023-08-27 23:43:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HuggingChat(AsyncGeneratorProvider):
|
2023-09-23 09:42:30 +00:00
|
|
|
url = "https://huggingface.co/chat"
|
2023-08-27 23:43:45 +00:00
|
|
|
needs_auth = True
|
|
|
|
working = True
|
2023-10-01 04:38:11 +00:00
|
|
|
model = "meta-llama/Llama-2-70b-chat-hf"
|
2023-08-27 23:43:45 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: list[dict[str, str]],
|
|
|
|
stream: bool = True,
|
|
|
|
proxy: str = None,
|
|
|
|
cookies: dict = None,
|
|
|
|
**kwargs
|
|
|
|
) -> AsyncGenerator:
|
2023-09-05 15:27:24 +00:00
|
|
|
model = model if model else cls.model
|
2023-08-27 23:43:45 +00:00
|
|
|
if proxy and "://" not in proxy:
|
|
|
|
proxy = f"http://{proxy}"
|
2023-09-20 04:12:34 +00:00
|
|
|
if not cookies:
|
|
|
|
cookies = get_cookies(".huggingface.co")
|
2023-08-27 23:43:45 +00:00
|
|
|
|
|
|
|
headers = {
|
|
|
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
|
|
|
|
}
|
|
|
|
async with ClientSession(
|
|
|
|
cookies=cookies,
|
|
|
|
headers=headers
|
|
|
|
) as session:
|
2023-10-01 04:38:11 +00:00
|
|
|
async with session.post(f"{cls.url}/conversation", json={"model": model}, proxy=proxy) as response:
|
2023-08-27 23:43:45 +00:00
|
|
|
conversation_id = (await response.json())["conversationId"]
|
|
|
|
|
|
|
|
send = {
|
2023-10-01 04:38:11 +00:00
|
|
|
"id": str(uuid.uuid4()),
|
2023-08-27 23:43:45 +00:00
|
|
|
"inputs": format_prompt(messages),
|
2023-10-01 04:38:11 +00:00
|
|
|
"is_retry": False,
|
|
|
|
"response_id": str(uuid.uuid4()),
|
|
|
|
"web_search": False
|
2023-08-27 23:43:45 +00:00
|
|
|
}
|
2023-10-01 04:38:11 +00:00
|
|
|
async with session.post(f"{cls.url}/conversation/{conversation_id}", json=send, proxy=proxy) as response:
|
|
|
|
async for line in response.content:
|
|
|
|
line = json.loads(line[:-1])
|
|
|
|
if "type" not in line:
|
|
|
|
raise RuntimeError(f"Response: {line}")
|
|
|
|
elif line["type"] == "stream":
|
|
|
|
yield line["token"]
|
|
|
|
elif line["type"] == "finalAnswer":
|
|
|
|
break
|
2023-08-27 23:43:45 +00:00
|
|
|
|
2023-09-20 04:12:34 +00:00
|
|
|
async with session.delete(f"{cls.url}/conversation/{conversation_id}", proxy=proxy) as response:
|
2023-08-27 23:43:45 +00:00
|
|
|
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@property
|
|
|
|
def params(cls):
|
|
|
|
params = [
|
|
|
|
("model", "str"),
|
|
|
|
("messages", "list[dict[str, str]]"),
|
|
|
|
("stream", "bool"),
|
|
|
|
("proxy", "str"),
|
|
|
|
]
|
|
|
|
param = ", ".join([": ".join(p) for p in params])
|
2023-09-17 21:23:54 +00:00
|
|
|
return f"g4f.provider.{cls.__name__} supports: ({param})"
|