mirror of https://github.com/xtekky/gpt4free
commit
2e4bce90b2
@ -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()
|
||||||
|
```
|
@ -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
|
||||||
|
}
|
||||||
|
])
|
@ -0,0 +1,18 @@
|
|||||||
|
from gpt4free import deepai
|
||||||
|
|
||||||
|
#single completion
|
||||||
|
for chunk in deepai.Completion.create("Write a list of possible vacation destinations:"):
|
||||||
|
print(chunk, end="", flush=True)
|
||||||
|
print()
|
||||||
|
|
||||||
|
#chat completion
|
||||||
|
print("==============")
|
||||||
|
messages = [ #taken from the openai docs
|
||||||
|
{"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()
|
Loading…
Reference in New Issue