From 6d0d975c71ac3f1b2d3d42d0d9e8e1c58e82e5dc Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Wed, 27 Dec 2023 06:46:42 +0100 Subject: Improve Chatxyz Provider --- g4f/Provider/Chatxyz.py | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) (limited to 'g4f/Provider') diff --git a/g4f/Provider/Chatxyz.py b/g4f/Provider/Chatxyz.py index 610cd45f..feb09be9 100644 --- a/g4f/Provider/Chatxyz.py +++ b/g4f/Provider/Chatxyz.py @@ -1,12 +1,16 @@ from __future__ import annotations + +import json from aiohttp import ClientSession + from ..typing import AsyncResult, Messages from .base_provider import AsyncGeneratorProvider -import json + class Chatxyz(AsyncGeneratorProvider): - url = "https://chat.3211000.xyz" - working = True + url = "https://chat.3211000.xyz" + working = True supports_gpt_35_turbo = True + supports_message_history = True @classmethod async def create_async_generator( @@ -16,8 +20,6 @@ class Chatxyz(AsyncGeneratorProvider): proxy: str = None, **kwargs ) -> AsyncResult: - if not model: - model = "gpt-3.5-turbo" headers = { 'Accept': 'text/event-stream', 'Accept-Encoding': 'gzip, deflate, br', @@ -35,28 +37,17 @@ class Chatxyz(AsyncGeneratorProvider): 'x-requested-with': 'XMLHttpRequest' } async with ClientSession(headers=headers) as session: - - system_message = "" - user_messages=[] - for message in messages: - if message["role"] == "system": - system_message+=f'{message["content"]}\n' - else: - user_messages.append(message) - new_message = [{'role':'system','content':system_message}] - for i in user_messages: - new_message.append(i) - data = { - "messages": new_message, + "messages": messages, "stream": True, "model": "gpt-3.5-turbo", "temperature": 0.5, "presence_penalty": 0, "frequency_penalty": 0, - "top_p": 1 + "top_p": 1, + **kwargs } - async with session.post(f'{cls.url}/api/openai/v1/chat/completions',json=data) as response: + async with session.post(f'{cls.url}/api/openai/v1/chat/completions', json=data, proxy=proxy) as response: response.raise_for_status() async for chunk in response.content: line = chunk.decode() @@ -64,5 +55,6 @@ class Chatxyz(AsyncGeneratorProvider): break elif line.startswith("data: "): line = json.loads(line[6:]) - if(line["choices"][0]["delta"]["content"]!=None): - yield line["choices"][0]["delta"]["content"] \ No newline at end of file + chunk = line["choices"][0]["delta"].get("content") + if(chunk): + yield chunk \ No newline at end of file -- cgit v1.2.3