From a0087269b3f3fa21f6862904e5a5b64a33fb40ea Mon Sep 17 00:00:00 2001 From: kqlio67 Date: Wed, 30 Oct 2024 18:25:40 +0200 Subject: feat(g4f/api/__init__.py): support both ':' and '-' in model prefixes --- g4f/api/__init__.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'g4f/api') diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index 754a48f1..25694942 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -165,6 +165,19 @@ class Api: @self.app.post("/v1/chat/completions") async def chat_completions(config: ChatCompletionsConfig, request: Request = None, provider: str = None): try: + # Find the last delimiter with ':' or '-' + if ':' in config.model: + model_parts = config.model.rsplit(":", 1) + elif '-' in config.model: + model_parts = config.model.rsplit("-", 1) + else: + model_parts = [config.model] # There is no prefix. + + base_model = model_parts[0] # We use the base model name + model_prefix = model_parts[1] if len(model_parts) > 1 else None + + config.model = base_model # Update the configuration to the basic model + config.provider = provider if config.provider is None else config.provider if config.api_key is None and request is not None: auth_header = request.headers.get("Authorization") @@ -229,6 +242,7 @@ class Api: async def completions(): return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json") + def format_exception(e: Exception, config: Union[ChatCompletionsConfig, ImageGenerationConfig]) -> str: last_provider = g4f.get_last_provider(True) return json.dumps({ -- cgit v1.2.3 From 0d05825a7195234cc680797e3b5ea005a27071ea Mon Sep 17 00:00:00 2001 From: kqlio67 Date: Thu, 31 Oct 2024 00:34:49 +0200 Subject: feat(api): support async streaming in chat completions --- g4f/api/__init__.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) (limited to 'g4f/api') diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index 25694942..fadeb0d8 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -165,19 +165,6 @@ class Api: @self.app.post("/v1/chat/completions") async def chat_completions(config: ChatCompletionsConfig, request: Request = None, provider: str = None): try: - # Find the last delimiter with ':' or '-' - if ':' in config.model: - model_parts = config.model.rsplit(":", 1) - elif '-' in config.model: - model_parts = config.model.rsplit("-", 1) - else: - model_parts = [config.model] # There is no prefix. - - base_model = model_parts[0] # We use the base model name - model_prefix = model_parts[1] if len(model_parts) > 1 else None - - config.model = base_model # Update the configuration to the basic model - config.provider = provider if config.provider is None else config.provider if config.api_key is None and request is not None: auth_header = request.headers.get("Authorization") @@ -206,9 +193,13 @@ class Api: return JSONResponse(response_list[0].to_json()) # Streaming response + async def async_generator(sync_gen): + for item in sync_gen: + yield item + async def streaming(): try: - async for chunk in response: + async for chunk in async_generator(response): yield f"data: {json.dumps(chunk.to_json())}\n\n" except GeneratorExit: pass @@ -242,7 +233,6 @@ class Api: async def completions(): return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json") - def format_exception(e: Exception, config: Union[ChatCompletionsConfig, ImageGenerationConfig]) -> str: last_provider = g4f.get_last_provider(True) return json.dumps({ -- cgit v1.2.3 From b2b978c9de7f2bd7d2dfae374fe3d61290107ae5 Mon Sep 17 00:00:00 2001 From: kqlio67 Date: Sat, 9 Nov 2024 19:40:49 +0200 Subject: feat(g4f/api/__init__.py): Update image generation response format --- g4f/api/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'g4f/api') diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index fadeb0d8..b9591b20 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -223,7 +223,7 @@ class Api: response_format=config.response_format ) # Convert Image objects to dictionaries - response_data = [image.to_dict() for image in response.data] + response_data = [{"url": image.url, "b64_json": image.b64_json} for image in response.data] return JSONResponse({"data": response_data}) except Exception as e: logging.exception(e) -- cgit v1.2.3