XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

add deepai wrapper

34e042bb
ading2210 <ading2019@gmail.com>
提交于

代码差异

4 个文件 +97 -1
Modified gpt4free/__init__.py +7 -1
@@ -15,6 +15,7 @@ class Provider(Enum):
15 15 ForeFront = 'fore_front'
16 16 Theb = 'theb'
17 17 UseLess = 'useless'
18 DeepAI = 'deepai'
18 19
19 20
20 21 class Completion:
@@ -40,6 +41,8 @@ class Completion:
40 41 return Completion.__theb_service(prompt, **kwargs)
41 42 elif provider == Provider.UseLess:
42 43 return Completion.__useless_service(prompt, **kwargs)
44 elif provider == Provider.DeepAI:
45 return Completion.__deepai_service(prompt, **kwargs)
43 46 else:
44 47 raise Exception('Provider not exist, Please try again')
45 48
@@ -62,4 +65,7 @@ class Completion:
62 65 @staticmethod
63 66 def __theb_service(prompt: str, **kwargs):
64 67 return ''.join(theb.Completion.create(prompt=prompt))
65
68
69 @staticmethod
70 def __deepai_service(prompt: str, **kwargs):
71 return ''.join(deepai.Completion.create(prompt=prompt))
Added gpt4free/deepai/README.md +26 -0
@@ -0,0 +1,26 @@
1 # DeepAI Wrapper
2 Written by [ading2210](https://github.com/ading2210/).
3
4 ## Examples:
5 These functions are generators which yield strings containing the newly generated text.
6
7 ### Completion:
8 ```python
9 for chunk in deepai.Completion.create("Who are you?"):
10 print(chunk, end="", flush=True)
11 print()
12 ```
13
14 ### Chat Completion:
15 Use the same format for the messages as you would for the [official OpenAI API](https://platform.openai.com/docs/guides/chat/introduction).
16 ```python
17 messages = [
18 {"role": "system", "content": "You are a helpful assistant."},
19 {"role": "user", "content": "Who won the world series in 2020?"},
20 {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
21 {"role": "user", "content": "Where was it played?"}
22 ]
23 for chunk in deepai.ChatCompletion.create(messages):
24 print(chunk, end="", flush=True)
25 print()
26 ```
Added gpt4free/deepai/__init__.py +46 -0
@@ -0,0 +1,46 @@
1 import requests
2 import json
3 import hashlib
4 import random
5 import string
6 from fake_useragent import UserAgent
7
8 class ChatCompletion:
9 @classmethod
10 def md5(self, text):
11 return hashlib.md5(text.encode()).hexdigest()[::-1]
12
13 @classmethod
14 def get_api_key(self, user_agent):
15 part1 = str(random.randint(0, 10**11))
16 part2 = self.md5(user_agent+self.md5(user_agent+self.md5(user_agent+part1+"x")))
17 return f"tryit-{part1}-{part2}"
18
19 @classmethod
20 def create(self, messages):
21 user_agent = UserAgent().random
22 api_key = self.get_api_key(user_agent)
23 headers = {
24 "api-key": api_key,
25 "user-agent": user_agent
26 }
27 files = {
28 "chat_style": (None, "chat"),
29 "chatHistory": (None, json.dumps(messages))
30 }
31
32 r = requests.post("https://api.deepai.org/chat_response", headers=headers, files=files, stream=True)
33
34 for chunk in r.iter_content(chunk_size=None):
35 r.raise_for_status()
36 yield chunk.decode()
37
38 class Completion:
39 @classmethod
40 def create(self, prompt):
41 return ChatCompletion.create([
42 {
43 "role": "user",
44 "content": prompt
45 }
46 ])
Added testing/deepai_test.py +18 -0
@@ -0,0 +1,18 @@
1 from gpt4free import deepai
2
3 #single completion
4 for chunk in deepai.Completion.create("Write a list of possible vacation destinations:"):
5 print(chunk, end="", flush=True)
6 print()
7
8 #chat completion
9 print("==============")
10 messages = [ #taken from the openai docs
11 {"role": "system", "content": "You are a helpful assistant."},
12 {"role": "user", "content": "Who won the world series in 2020?"},
13 {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
14 {"role": "user", "content": "Where was it played?"}
15 ]
16 for chunk in deepai.ChatCompletion.create(messages):
17 print(chunk, end="", flush=True)
18 print()