From 8864b70ee4bd405d9405263a63e3ada793b30ac1 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Tue, 23 Jan 2024 23:48:11 +0100 Subject: Add aiohttp_socks support --- g4f/Provider/HuggingChat.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'g4f/Provider/HuggingChat.py') diff --git a/g4f/Provider/HuggingChat.py b/g4f/Provider/HuggingChat.py index d493da8f..a481ce7c 100644 --- a/g4f/Provider/HuggingChat.py +++ b/g4f/Provider/HuggingChat.py @@ -2,7 +2,7 @@ from __future__ import annotations import json, uuid -from aiohttp import ClientSession +from aiohttp import ClientSession, BaseConnector from ..typing import AsyncResult, Messages from .base_provider import AsyncGeneratorProvider, ProviderModelMixin @@ -33,6 +33,7 @@ class HuggingChat(AsyncGeneratorProvider, ProviderModelMixin): messages: Messages, stream: bool = True, proxy: str = None, + connector: BaseConnector = None, web_search: bool = False, cookies: dict = None, **kwargs @@ -43,9 +44,16 @@ class HuggingChat(AsyncGeneratorProvider, ProviderModelMixin): 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', } + if proxy and not connector: + try: + from aiohttp_socks import ProxyConnector + connector = ProxyConnector.from_url(proxy) + except ImportError: + raise RuntimeError('Install "aiohttp_socks" package for proxy support') async with ClientSession( cookies=cookies, - headers=headers + headers=headers, + connector=connector ) as session: async with session.post(f"{cls.url}/conversation", json={"model": cls.get_model(model)}, proxy=proxy) as response: conversation_id = (await response.json())["conversationId"] -- cgit v1.2.3