summaryrefslogtreecommitdiffstats
path: root/gpt4free
diff options
context:
space:
mode:
authorxtekky <98614666+xtekky@users.noreply.github.com>2023-05-18 11:33:31 +0200
committerGitHub <noreply@github.com>2023-05-18 11:33:31 +0200
commit2e4bce90b2dab34cc9a37780b47bce43e339b4f7 (patch)
tree4b1af55c7e085b10c4ab955c109bfe6c988936d2 /gpt4free
parentcountdown (diff)
parentadd references to deepai to the main readme (diff)
downloadgpt4free-2e4bce90b2dab34cc9a37780b47bce43e339b4f7.tar
gpt4free-2e4bce90b2dab34cc9a37780b47bce43e339b4f7.tar.gz
gpt4free-2e4bce90b2dab34cc9a37780b47bce43e339b4f7.tar.bz2
gpt4free-2e4bce90b2dab34cc9a37780b47bce43e339b4f7.tar.lz
gpt4free-2e4bce90b2dab34cc9a37780b47bce43e339b4f7.tar.xz
gpt4free-2e4bce90b2dab34cc9a37780b47bce43e339b4f7.tar.zst
gpt4free-2e4bce90b2dab34cc9a37780b47bce43e339b4f7.zip
Diffstat (limited to 'gpt4free')
-rw-r--r--gpt4free/__init__.py29
-rw-r--r--gpt4free/deepai/README.md26
-rw-r--r--gpt4free/deepai/__init__.py46
3 files changed, 101 insertions, 0 deletions
diff --git a/gpt4free/__init__.py b/gpt4free/__init__.py
index 1e652897..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):
@@ -15,6 +16,7 @@ class Provider(Enum):
ForeFront = 'fore_front'
Theb = 'theb'
UseLess = 'useless'
+ DeepAI = 'deepai'
class Completion:
@@ -40,6 +42,8 @@ class Completion:
return Completion.__theb_service(prompt, **kwargs)
elif provider == Provider.UseLess:
return Completion.__useless_service(prompt, **kwargs)
+ elif provider == Provider.DeepAI:
+ return Completion.__deepai_service(prompt, **kwargs)
else:
raise Exception('Provider not exist, Please try again')
@@ -62,4 +66,29 @@ class Completion:
@staticmethod
def __theb_service(prompt: str, **kwargs):
return ''.join(theb.Completion.create(prompt=prompt))
+
+ @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
diff --git a/gpt4free/deepai/README.md b/gpt4free/deepai/README.md
new file mode 100644
index 00000000..a287cdb7
--- /dev/null
+++ b/gpt4free/deepai/README.md
@@ -0,0 +1,26 @@
+# DeepAI Wrapper
+Written by [ading2210](https://github.com/ading2210/).
+
+## Examples:
+These functions are generators which yield strings containing the newly generated text.
+
+### Completion:
+```python
+for chunk in deepai.Completion.create("Who are you?"):
+ print(chunk, end="", flush=True)
+print()
+```
+
+### Chat Completion:
+Use the same format for the messages as you would for the [official OpenAI API](https://platform.openai.com/docs/guides/chat/introduction).
+```python
+messages = [
+ {"role": "system", "content": "You are a helpful assistant."},
+ {"role": "user", "content": "Who won the world series in 2020?"},
+ {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
+ {"role": "user", "content": "Where was it played?"}
+]
+for chunk in deepai.ChatCompletion.create(messages):
+ print(chunk, end="", flush=True)
+print()
+``` \ No newline at end of file
diff --git a/gpt4free/deepai/__init__.py b/gpt4free/deepai/__init__.py
new file mode 100644
index 00000000..a2fc6f5a
--- /dev/null
+++ b/gpt4free/deepai/__init__.py
@@ -0,0 +1,46 @@
+import requests
+import json
+import hashlib
+import random
+import string
+from fake_useragent import UserAgent
+
+class ChatCompletion:
+ @classmethod
+ def md5(self, text):
+ return hashlib.md5(text.encode()).hexdigest()[::-1]
+
+ @classmethod
+ def get_api_key(self, user_agent):
+ part1 = str(random.randint(0, 10**11))
+ part2 = self.md5(user_agent+self.md5(user_agent+self.md5(user_agent+part1+"x")))
+ return f"tryit-{part1}-{part2}"
+
+ @classmethod
+ def create(self, messages):
+ user_agent = UserAgent().random
+ api_key = self.get_api_key(user_agent)
+ headers = {
+ "api-key": api_key,
+ "user-agent": user_agent
+ }
+ files = {
+ "chat_style": (None, "chat"),
+ "chatHistory": (None, json.dumps(messages))
+ }
+
+ r = requests.post("https://api.deepai.org/chat_response", headers=headers, files=files, stream=True)
+
+ for chunk in r.iter_content(chunk_size=None):
+ r.raise_for_status()
+ yield chunk.decode()
+
+class Completion:
+ @classmethod
+ def create(self, prompt):
+ return ChatCompletion.create([
+ {
+ "role": "user",
+ "content": prompt
+ }
+ ]) \ No newline at end of file