From c190d0eae4c1fcc023035778e357e247d3b05259 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 13 Jan 2024 16:18:31 +0100 Subject: Improve FreeChatgpt Provider --- g4f/Provider/FreeChatgpt.py | 76 ++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 32 deletions(-) (limited to 'g4f/Provider/FreeChatgpt.py') diff --git a/g4f/Provider/FreeChatgpt.py b/g4f/Provider/FreeChatgpt.py index 82fc3ef4..75514118 100644 --- a/g4f/Provider/FreeChatgpt.py +++ b/g4f/Provider/FreeChatgpt.py @@ -7,13 +7,9 @@ from ..typing import AsyncResult, Messages from .base_provider import AsyncGeneratorProvider - models = { - "claude-v1":"claude-2.1", - "claude-v2":"claude-2.0", - "gpt_35_turbo":"gpt-3.5-turbo-1106", - "gpt-4":"gpt-4", - "gemini-pro":"google-gemini-pro" + "claude-v2": "claude-2.0", + "gemini-pro": "google-gemini-pro" } class FreeChatgpt(AsyncGeneratorProvider): @@ -31,31 +27,47 @@ class FreeChatgpt(AsyncGeneratorProvider): proxy: str = None, **kwargs ) -> AsyncResult: - model = models[model] if model in models else "gpt-3.5-turbo-1106" + if model in models: + model = models[model] + elif not model: + model = "gpt-3.5-turbo" headers = { - "Accept": "application/json, text/event-stream", - "Content-Type":"application/json", - "Accept-Encoding": "gzip, deflate, br", - "Accept-Language": "en-US,en;q=0.5", - "Host":"free.chatgpt.org.uk", - "Referer":f"{cls.url}/", - "Origin":f"{cls.url}", - "Sec-Fetch-Dest": "empty", - "Sec-Fetch-Mode": "cors", - "Sec-Fetch-Site": "same-origin", - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", -} + "Accept": "application/json, text/event-stream", + "Content-Type":"application/json", + "Accept-Encoding": "gzip, deflate, br", + "Accept-Language": "en-US,en;q=0.5", + "Host":"free.chatgpt.org.uk", + "Referer":f"{cls.url}/", + "Origin":f"{cls.url}", + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "same-origin", + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", + } async with ClientSession(headers=headers) as session: - data = {"messages":messages,"stream":True,"model":model,"temperature":0.5,"presence_penalty":0,"frequency_penalty":0,"top_p":1} - async with session.post(f'{cls.url}/api/openai/v1/chat/completions',json=data) as result: - async for chunk in result.content: - - line = chunk.decode() - if line.startswith("data: [DONE]"): - break - elif line.startswith("data: "): - line = json.loads(line[6:]) - if(line["choices"]==[]): - continue - if(line["choices"][0]["delta"].get("content") and line["choices"][0]["delta"]["content"]!=None): - yield line["choices"][0]["delta"]["content"] \ No newline at end of file + data = { + "messages":messages, + "stream":True, + "model":model, + "temperature":0.5, + "presence_penalty":0, + "frequency_penalty":0, + "top_p":1, + **kwargs + } + async with session.post(f'{cls.url}/api/openai/v1/chat/completions', json=data, proxy=proxy) as response: + response.raise_for_status() + started = False + async for line in response.content: + if line.startswith(b"data: [DONE]"): + break + elif line.startswith(b"data: "): + line = json.loads(line[6:]) + if(line["choices"]==[]): + continue + chunk = line["choices"][0]["delta"].get("content") + if chunk: + started = True + yield chunk + if not started: + raise RuntimeError("Empty response") \ No newline at end of file -- cgit v1.2.3