Skip to content

Commit

Permalink
feat(g4f/api/__init__.py): support both ':' and '-' in model prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kqlio67 committed Oct 30, 2024
1 parent e6627d8 commit a008726
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions g4f/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ async def model_info(model_name: str):
@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")
Expand Down Expand Up @@ -229,6 +242,7 @@ async def generate_image(config: ImageGenerationConfig):
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({
Expand Down

1 comment on commit a008726

@hqnicolas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gpt4free-1  | INFO:     10.1.1.15:51312 - "POST /v1/chat/completions HTTP/1.1" 500 Internal Server Error
gpt4free-1  | ERROR:root:Model not found: gpt
gpt4free-1  | Traceback (most recent call last):
gpt4free-1  |   File "/app/g4f/api/__init__.py", line 190, in chat_completions
gpt4free-1  |     response = self.client.chat.completions.create(
gpt4free-1  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
gpt4free-1  |   File "/app/g4f/client/client.py", line 187, in create
gpt4free-1  |     model, provider = get_model_and_provider(
gpt4free-1  |                       ^^^^^^^^^^^^^^^^^^^^^^^
gpt4free-1  |   File "/app/g4f/client/service.py", line 66, in get_model_and_provider
gpt4free-1  |     raise ModelNotFoundError(f'Model not found: {model}')
gpt4free-1  | g4f.errors.ModelNotFoundError: Model not found: gpt

Please sign in to comment.