From 0a59e65c6a8860635d55ba72252c96a4712e5fab Mon Sep 17 00:00:00 2001 From: ading2210 Date: Thu, 18 May 2023 00:21:18 -0700 Subject: add gpt4free.ChatCompletion class --- README.md | 2 +- gpt4free/__init__.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c6d79bad..0adc9e69 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ docker-compose up --build -d This program is licensed under the [GNU GPL v3](https://www.gnu.org/licenses/gpl-3.0.txt) -Most code, with the exception of `quora/api.py` (by [ading2210](https://github.com/ading2210)), has been written by me, [xtekky](https://github.com/xtekky). +Most code, with the exception of `quora/api.py` and `deepai/__init__.py` (by [ading2210](https://github.com/ading2210)), has been written by me, [xtekky](https://github.com/xtekky). ### Copyright Notice: diff --git a/gpt4free/__init__.py b/gpt4free/__init__.py index 739cf763..2dc6f5f3 100644 --- a/gpt4free/__init__.py +++ b/gpt4free/__init__.py @@ -5,6 +5,7 @@ from gpt4free import quora from gpt4free import theb from gpt4free import usesless from gpt4free import you +from gpt4free import deepai class Provider(Enum): @@ -69,3 +70,25 @@ class Completion: @staticmethod def __deepai_service(prompt: str, **kwargs): return ''.join(deepai.Completion.create(prompt=prompt)) + +class ChatCompletion: + """This class is used to execute a chat completion for a specified provider""" + + @staticmethod + def create(provider: Provider, messages: list, **kwargs) -> str: + """ + Invokes the given provider with given chat messages and addition arguments and returns the string response + + :param provider: an enum representing the provider to use while invoking + :param messages: a list of chat messages, see the OpenAI docs for how to format this (https://platform.openai.com/docs/guides/chat/introduction) + :param kwargs: Additional keyword arguments to pass to the provider while invoking + :return: A string representing the response from the provider + """ + if provider == Provider.DeepAI: + return ChatCompletion.__deepai_service(messages, **kwargs) + else: + raise Exception('Provider not exist, Please try again') + + @staticmethod + def __deepai_service(messages: list, **kwargs): + return ''.join(deepai.ChatCompletion.create(messages=messages)) \ No newline at end of file -- cgit v1.2.3