-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
1,282 additions
and
569 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/quora/__pycache__ | ||
/gpt4free/__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# gpt4free package | ||
|
||
### What is it? | ||
|
||
gpt4free is a python package that provides some language model api's | ||
|
||
### Main Features | ||
|
||
- It's free to use | ||
- Easy access | ||
|
||
### Installation: | ||
|
||
```bash | ||
pip install gpt4free | ||
``` | ||
|
||
#### Usage: | ||
|
||
```python | ||
import gpt4free | ||
import gpt4free | ||
from gpt4free import Provider, quora, forefront | ||
|
||
# usage You | ||
response = gpt4free.Completion.create(Provider.You, prompt='Write a poem on Lionel Messi') | ||
print(response) | ||
|
||
# usage Poe | ||
token = quora.Account.create(logging=False) | ||
response = gpt4free.Completion.create(Provider.Poe, prompt='Write a poem on Lionel Messi', token=token, model='ChatGPT') | ||
print(response) | ||
|
||
# usage forefront | ||
token = forefront.Account.create(logging=False) | ||
response = gpt4free.Completion.create( | ||
Provider.ForeFront, prompt='Write a poem on Lionel Messi', model='gpt-4', token=token | ||
) | ||
print(response) | ||
print(f'END') | ||
|
||
# usage theb | ||
response = gpt4free.Completion.create(Provider.Theb, prompt='Write a poem on Lionel Messi') | ||
print(response) | ||
|
||
# usage cocalc | ||
response = gpt4free.Completion.create(Provider.CoCalc, prompt='Write a poem on Lionel Messi', cookie_input='') | ||
print(response) | ||
|
||
``` | ||
|
||
### Invocation Arguments | ||
|
||
`gpt4free.Completion.create()` method has two required arguments | ||
|
||
1. Provider: This is an enum representing different provider | ||
2. prompt: This is the user input | ||
|
||
#### Keyword Arguments | ||
|
||
Some of the keyword arguments are optional, while others are required. | ||
|
||
- You: | ||
- `safe_search`: boolean - default value is `False` | ||
- `include_links`: boolean - default value is `False` | ||
- `detailed`: boolean - default value is `False` | ||
- Quora: | ||
- `token`: str - this needs to be provided by the user | ||
- `model`: str - default value is `gpt-4`. | ||
|
||
(Available models: `['Sage', 'GPT-4', 'Claude+', 'Claude-instant', 'ChatGPT', 'Dragonfly', 'NeevaAI']`) | ||
- ForeFront: | ||
- `token`: str - this need to be provided by the user | ||
|
||
- Theb: | ||
(no keyword arguments required) | ||
- CoCalc: | ||
- `cookie_input`: str - this needs to be provided by user | ||
|
||
#### Token generation of quora | ||
```python | ||
from gpt4free import quora | ||
|
||
token = quora.Account.create(logging=False) | ||
``` | ||
|
||
### Token generation of ForeFront | ||
```python | ||
from gpt4free import forefront | ||
|
||
token = forefront.Account.create(logging=False) | ||
``` | ||
|
||
## Copyright: | ||
|
||
This program is licensed under the [GNU GPL v3](https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
### Copyright Notice: <a name="copyright"></a> | ||
|
||
``` | ||
xtekky/gpt4free: multiple reverse engineered language-model api's to decentralise the ai industry. | ||
Copyright (C) 2023 xtekky | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from enum import Enum | ||
|
||
from gpt4free import cocalc | ||
from gpt4free import forefront | ||
from gpt4free import quora | ||
from gpt4free import theb | ||
from gpt4free import you | ||
|
||
|
||
class Provider(Enum): | ||
"""An enum representing different providers.""" | ||
|
||
You = 'you' | ||
Poe = 'poe' | ||
ForeFront = 'fore_front' | ||
Theb = 'theb' | ||
CoCalc = 'cocalc' | ||
|
||
|
||
class Completion: | ||
"""This class will be used for invoking the given provider""" | ||
|
||
@staticmethod | ||
def create(provider: Provider, prompt: str, **kwargs) -> str: | ||
""" | ||
Invokes the given provider with given prompt and addition arguments and returns the string response | ||
:param provider: an enum representing the provider to use while invoking | ||
:param prompt: input provided by the user | ||
:param kwargs: Additional keyword arguments to pass to the provider while invoking | ||
:return: A string representing the response from the provider | ||
""" | ||
if provider == Provider.Poe: | ||
return Completion.__poe_service(prompt, **kwargs) | ||
elif provider == Provider.You: | ||
return Completion.__you_service(prompt, **kwargs) | ||
elif provider == Provider.ForeFront: | ||
return Completion.__fore_front_service(prompt, **kwargs) | ||
elif provider == Provider.Theb: | ||
return Completion.__theb_service(prompt, **kwargs) | ||
elif provider == Provider.CoCalc: | ||
return Completion.__cocalc_service(prompt, **kwargs) | ||
else: | ||
raise Exception('Provider not exist, Please try again') | ||
|
||
@staticmethod | ||
def __you_service(prompt: str, **kwargs) -> str: | ||
return you.Completion.create(prompt, **kwargs).text | ||
|
||
@staticmethod | ||
def __poe_service(prompt: str, **kwargs) -> str: | ||
return quora.Completion.create(prompt=prompt, **kwargs).text | ||
|
||
@staticmethod | ||
def __fore_front_service(prompt: str, **kwargs) -> str: | ||
return forefront.Completion.create(prompt=prompt, **kwargs).text | ||
|
||
@staticmethod | ||
def __theb_service(prompt: str, **kwargs): | ||
return ''.join(theb.Completion.create(prompt=prompt)) | ||
|
||
@staticmethod | ||
def __cocalc_service(prompt: str, **kwargs): | ||
return cocalc.Completion.create(prompt, cookie_input=kwargs.get('cookie_input', '')).text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import requests | ||
from fake_useragent import UserAgent | ||
from pydantic import BaseModel | ||
|
||
|
||
class CoCalcResponse(BaseModel): | ||
text: str | ||
status: bool | ||
|
||
|
||
class Completion: | ||
@staticmethod | ||
def create(prompt: str, cookie_input: str) -> CoCalcResponse: | ||
# Initialize a session with custom headers | ||
session = Completion._initialize_session(cookie_input) | ||
|
||
# Set the data that will be submitted | ||
payload = Completion._create_payload(prompt, 'ASSUME I HAVE FULL ACCESS TO COCALC. ') | ||
|
||
# Submit the request and return the results | ||
return Completion._submit_request(session, payload) | ||
|
||
@classmethod | ||
def _initialize_session(cls, conversation_cookie) -> requests.Session: | ||
"""Initialize a session with custom headers for the request.""" | ||
|
||
session = requests.Session() | ||
headers = { | ||
'Accept': '*/*', | ||
'Accept-Language': 'en-US,en;q=0.5', | ||
'Origin': 'https://cocalc.com', | ||
'Referer': 'https://cocalc.com/api/v2/openai/chatgpt', | ||
'Cookie': conversation_cookie, | ||
'User-Agent': UserAgent().random, | ||
} | ||
session.headers.update(headers) | ||
|
||
return session | ||
|
||
@staticmethod | ||
def _create_payload(prompt: str, system_prompt: str) -> dict: | ||
return {'input': prompt, 'system': system_prompt, 'tag': 'next:index'} | ||
|
||
@staticmethod | ||
def _submit_request(session: requests.Session, payload: dict) -> CoCalcResponse: | ||
response = session.post('https://cocalc.com/api/v2/openai/chatgpt', json=payload).json() | ||
return CoCalcResponse(text=response['output'], status=response['success']) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
### Example: `cocalc` <a name="example-cocalc"></a> | ||
|
||
```python | ||
# import library | ||
from gpt4free import cocalc | ||
|
||
cocalc.Completion.create(prompt="How are you!", cookie_input="cookieinput") ## Tutorial | ||
``` | ||
|
||
### How to grab cookie input | ||
```js | ||
// input this into ur developer tools console and the exact response u get from this u put into ur cookieInput! | ||
var cookies = document.cookie.split("; "); | ||
var cookieString = ""; | ||
for (var i = 0; i < cookies.length; i++) { | ||
cookieString += cookies[i] + "; "; | ||
} | ||
console.log(cookieString); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
### Example: `forefront` (use like openai pypi package) <a name="example-forefront"></a> | ||
|
||
```python | ||
|
||
from gpt4free import forefront | ||
|
||
# create an account | ||
token = forefront.Account.create(logging=False) | ||
print(token) | ||
|
||
# get a response | ||
for response in forefront.StreamingCompletion.create(token=token, | ||
prompt='hello world', model='gpt-4'): | ||
print(response.completion.choices[0].text, end='') | ||
print("") | ||
``` |
Oops, something went wrong.