diff options
Diffstat (limited to 'g4f/Provider/nexra/NexraBing.py')
-rw-r--r-- | g4f/Provider/nexra/NexraBing.py | 106 |
1 files changed, 60 insertions, 46 deletions
diff --git a/g4f/Provider/nexra/NexraBing.py b/g4f/Provider/nexra/NexraBing.py index 59e06a3d..02f3724d 100644 --- a/g4f/Provider/nexra/NexraBing.py +++ b/g4f/Provider/nexra/NexraBing.py @@ -1,21 +1,42 @@ from __future__ import annotations + from aiohttp import ClientSession +from aiohttp.client_exceptions import ContentTypeError + from ...typing import AsyncResult, Messages from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin from ..helper import format_prompt import json + class NexraBing(AsyncGeneratorProvider, ProviderModelMixin): label = "Nexra Bing" + url = "https://nexra.aryahcr.cc/documentation/bing/en" api_endpoint = "https://nexra.aryahcr.cc/api/chat/complements" - - bing_models = { - 'Bing (Balanced)': 'Balanced', - 'Bing (Creative)': 'Creative', - 'Bing (Precise)': 'Precise' - } + working = True + supports_gpt_4 = False + supports_stream = False - models = [*bing_models.keys()] + default_model = 'Bing (Balanced)' + models = ['Bing (Balanced)', 'Bing (Creative)', 'Bing (Precise)'] + + model_aliases = { + "gpt-4": "Bing (Balanced)", + "gpt-4": "Bing (Creative)", + "gpt-4": "Bing (Precise)", + } + + @classmethod + def get_model_and_style(cls, model: str) -> tuple[str, str]: + # Default to the default model if not found + model = cls.model_aliases.get(model, model) + if model not in cls.models: + model = cls.default_model + + # Extract the base model and conversation style + base_model, conversation_style = model.split(' (') + conversation_style = conversation_style.rstrip(')') + return base_model, conversation_style @classmethod async def create_async_generator( @@ -23,20 +44,19 @@ class NexraBing(AsyncGeneratorProvider, ProviderModelMixin): model: str, messages: Messages, proxy: str = None, + stream: bool = False, + markdown: bool = False, **kwargs ) -> AsyncResult: + base_model, conversation_style = cls.get_model_and_style(model) + headers = { "Content-Type": "application/json", - "Accept": "application/json", - "Origin": cls.url or "https://default-url.com", - "Referer": f"{cls.url}/chat" if cls.url else "https://default-url.com/chat", + "origin": cls.url, + "referer": f"{cls.url}/chat", } - async with ClientSession(headers=headers) as session: prompt = format_prompt(messages) - if prompt is None: - raise ValueError("Prompt cannot be None") - data = { "messages": [ { @@ -44,39 +64,33 @@ class NexraBing(AsyncGeneratorProvider, ProviderModelMixin): "content": prompt } ], - "conversation_style": cls.bing_models.get(model, 'Balanced'), - "markdown": False, - "stream": True, - "model": "Bing" + "conversation_style": conversation_style, + "markdown": markdown, + "stream": stream, + "model": base_model } - - full_response = "" - last_message = "" - async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response: response.raise_for_status() - - async for line in response.content: - if line: - raw_data = line.decode('utf-8').strip() - - parts = raw_data.split('') - for part in parts: - if part: - try: - json_data = json.loads(part) - except json.JSONDecodeError: - continue - - if json_data.get("error"): - raise Exception("Error in API response") - - if json_data.get("finish"): - break - - if message := json_data.get("message"): - if message != last_message: - full_response = message - last_message = message + try: + # Read the entire response text + text_response = await response.text() + # Split the response on the separator character + segments = text_response.split('\x1e') + + complete_message = "" + for segment in segments: + if not segment.strip(): + continue + try: + response_data = json.loads(segment) + if response_data.get('message'): + complete_message = response_data['message'] + if response_data.get('finish'): + break + except json.JSONDecodeError: + raise Exception(f"Failed to parse segment: {segment}") - yield full_response.strip() + # Yield the complete message + yield complete_message + except ContentTypeError: + raise Exception("Failed to parse response content type.") |