From 5ca47b44b2b42abb4f48163c17500b5ee67ab28f Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Tue, 5 Sep 2023 17:27:24 +0200 Subject: Add to many provider async and stream support, Fix Ails, AItianhu, ChatgptAi, ChatgptLogin Provider, Add fallback cookies to Bing, Improve OpenaiChat Provider --- g4f/Provider/Liaobots.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'g4f/Provider/Liaobots.py') diff --git a/g4f/Provider/Liaobots.py b/g4f/Provider/Liaobots.py index 2360c8a5..2cc5ff99 100644 --- a/g4f/Provider/Liaobots.py +++ b/g4f/Provider/Liaobots.py @@ -32,7 +32,6 @@ models = { class Liaobots(AsyncGeneratorProvider): url = "https://liaobots.com" working = True - supports_stream = True supports_gpt_35_turbo = True supports_gpt_4 = True _auth_code = None @@ -46,24 +45,24 @@ class Liaobots(AsyncGeneratorProvider): proxy: str = None, **kwargs ) -> AsyncGenerator: + model = model if model in models else "gpt-3.5-turbo" if proxy and "://" not in proxy: proxy = f"http://{proxy}" headers = { "authority": "liaobots.com", "content-type": "application/json", - "origin": "https://liaobots.com", - "referer": "https://liaobots.com/", + "origin": cls.url, + "referer": cls.url + "/", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36", } async with ClientSession( headers=headers ) as session: - model = model if model in models else "gpt-3.5-turbo" auth_code = auth if isinstance(auth, str) else cls._auth_code if not auth_code: - async with session.post("https://liaobots.com/api/user", proxy=proxy, json={"authcode": ""}) as response: + async with session.post(cls.url + "/api/user", proxy=proxy, json={"authcode": ""}) as response: response.raise_for_status() - auth_code = cls._auth_code = json.loads((await response.text()))["authCode"] + auth_code = cls._auth_code = json.loads(await response.text())["authCode"] data = { "conversationId": str(uuid.uuid4()), "model": models[model], @@ -71,10 +70,11 @@ class Liaobots(AsyncGeneratorProvider): "key": "", "prompt": "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully.", } - async with session.post("https://liaobots.com/api/chat", proxy=proxy, json=data, headers={"x-auth-code": auth_code}) as response: + async with session.post(cls.url + "/api/chat", proxy=proxy, json=data, headers={"x-auth-code": auth_code}) as response: response.raise_for_status() - async for line in response.content: - yield line.decode("utf-8") + async for stream in response.content.iter_any(): + if stream: + yield stream.decode() @classmethod -- cgit v1.2.3