From b0276f6c9e2925c65a00b7189bad92feb25cefbd Mon Sep 17 00:00:00 2001 From: madonchik123 <68397448+madonchik123@users.noreply.github.com> Date: Sat, 2 Dec 2023 01:11:52 +0300 Subject: Added new provider PI (Hacky way to use) (#1291) * Added new provider PI (Hacky way to use) * Updated models endpoint made it show real info about the model.py * Added cloudscraper to the requirements * Fixed some bugs aka made streaming also return role --- g4f/api/__init__.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'g4f/api/__init__.py') diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index d2244ff5..410fcc5f 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -40,12 +40,15 @@ class Api: @self.app.get("/v1/models") async def models(): - model_list = [{ + model_list = [] + for model in g4f.Model.__all__(): + model_info = (g4f.ModelUtils.convert[model]) + model_list.append({ 'id': model, 'object': 'model', 'created': 0, - 'owned_by': 'g4f'} for model in g4f.Model.__all__()] - + 'owned_by': model_info.base_provider} + ) return Response(content=json.dumps({ 'object': 'list', 'data': model_list}, indent=4), media_type="application/json") @@ -80,17 +83,25 @@ class Api: model = item_data.get('model') stream = True if item_data.get("stream") == "True" else False messages = item_data.get('messages') + conversation = item_data.get('conversation') if item_data.get('conversation') != None else None try: - response = g4f.ChatCompletion.create( - model=model, - stream=stream, - messages=messages, - ignored=self.list_ignored_providers) + if model == 'pi': + response = g4f.ChatCompletion.create( + model=model, + stream=stream, + messages=messages, + conversation=conversation, + ignored=self.list_ignored_providers) + else: + response = g4f.ChatCompletion.create( + model=model, + stream=stream, + messages=messages, + ignored=self.list_ignored_providers) except Exception as e: logging.exception(e) return Response(content=json.dumps({"error": "An error occurred while generating the response."}, indent=4), media_type="application/json") - completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28)) completion_timestamp = int(time.time()) @@ -134,6 +145,7 @@ class Api: { 'index': 0, 'delta': { + 'role': 'assistant', 'content': chunk, }, 'finish_reason': None, -- cgit v1.2.3