# Python OpenPlayground API
[![PyPi Version](https://img.shields.io/pypi/v/openplayground-api.svg)](https://pypi.org/project/openplayground-api/)
This is an unoffical API wrapper for the website [OpenPlayground](https://nat.dev), which provides access to a wide array of AI models for free, including ChatGPT, GPT-4, and Claude.
## Notice:
OpenPlayground has recently announced that they are going to starting deleting accounts that access their API via automated means (probably as as a direct response to this library). You should be fine as long as you don't send to many requests, since this library is able to bypass their bot detection by spoofing the `X-Session` header. They've also recently been requiring SMS verification upon signup, but this can easily be bypassed by signing in using a Google account.
![screenshot from their discord server](https://media.discordapp.net/attachments/1072352756481929316/1088019955322196048/image.png)
## Features:
This library has the following abilities:
- Log in using OTP code
- List models
- Generate text
## Installation:
You can install this library by running the following command:
```
pip3 install openplayground-api
```
## Documentation:
An example of how to use this library can be found in `/examples/example.py`.
### The `Model` Class:
The `openplayground.Model` class describes a model that is available to the user. Valid attributes are:
- `provider` - The company that developed the model (e.g., openai, anthropic)
- `name` - The name of the model, such as `text-davinci-003`.
- `version` - The version of the model. This may return `None` on some models.
- `tag` - A string that combines the provider and name, such as `openai:text-davinci-003`.
- `params` - A dictionary containing possible parameters for the model.
### Authenticating With an OTP Code:
The `openplayground.Auth` class can be used to get your token using an OTP code emailed to you. Note that the following examples assume that `auth` is the name of your `openplayground.Auth` class.
```python
import openplayground
auth = openplayground.Auth()
```
#### Sending the OTP Code:
The `openplayground.Auth.send_otp_code` function sends an email containing the OTP code to the specificed email address.
```python
auth.send_otp_code("sample@example.com")
```
#### Verifiying the OTP Code:
Once you have the OTP code, you can use the `openplayground.Auth.verify_otp_code` function to get your token from that OTP code. You can then use this token to create an `openplayground.Client` instance.
```python
otp_code = input("Enter OTP code: ")
token = auth.verify_otp_code()
```
### Using the Client:
The `openplayground.Client` class accepts two arguments, which are your account's token and its email. Your token can be obtained from the `__session` field in your browser's cookies, or using the `openplayground.Auth` class as shown above. The email field is optional, but filling it out might reduce the chances of this library being detected.
```python
import openplayground
client = openplayground.Client(token, email=email)
```
Note that the following examples assume `client` is the name of your `openplayground.Client` instance.
#### Downloading the Available Models:
The `client.get_models` function fetches the available models from `https://nat.dev/api/all_models`, and returns a dictionary of `openplayground.Model` objects. The client downloads the available models upon initialization and stores it in `client.models`, so calling this function shouldn't be necessary.
Some popular model tags are:
- OpenAI: `openai:gpt-4`, `openai:gpt-3.5-turbo`, `openai:text-davinci-003`
- Anthropic: `anthropic:claude-instant-v1.0`, `anthropic:claude-v1.2`
- Facebook/Stanford: `textgeneration:llama-65b`, `textgeneration:alpaca-7b`
```python
print(client.models.keys())
#dict_keys(['forefront:EleutherAI/GPT-J', 'forefront:EleutherAI/GPT-NeoX', 'forefront:pythia-12b', 'forefront:pythia-20b', 'forefront:pythia-6.9b', 'anthropic:claude-instant-v1.0', 'anthropic:claude-v1.2', 'textgeneration:alpaca-7b', 'textgeneration:llama-65b', 'huggingface:bigscience/bloomz', 'huggingface:google/flan-t5-xxl', 'huggingface:google/flan-ul2', 'cohere:command-medium-nightly', 'cohere:command-xlarge-nightly', 'cohere:medium', 'cohere:xlarge', 'openai:gpt-4', 'openai:code-cushman-001', 'openai:code-davinci-002', 'openai:gpt-3.5-turbo', 'openai:text-ada-001', 'openai:text-babbage-001', 'openai:text-curie-001', 'openai:text-davinci-002', 'openai:text-davinci-003'])
```
#### Generating Text:
The `client.generate` function generates some text given a model and a prompt. Optionally, you can also specify arguments such as the maximum length in the kwargs. You can find a list of valid arguments and their defaults in `openplayground.Model.params`. A few common ones are:
- `maximum_length`
- `temperature`
- `top_k`
- `top_p`
The values returned from this function are streamed and expressed in a dictionary. Note that GPT-4 access currently has a daily limit of around 10 requests/day, and may become paid in the future.
Streamed example:
```python
for chunk in client.generate("openai:gpt-3.5-turbo", prompt):
if chunk["event"] == "infer":
print(chunk["message"], end="", flush=True)
```
Non-streamed example:
```python
message = ""
for chunk in client.generate("openai:gpt-3.5-turbo", prompt):
if chunk["event"] == "infer":
message += chunk["message"]
print(message)
```
### Misc:
#### Changing the Spoofed User-Agent:
You can change the global User-Agent by setting `openplayground.user_agent` right after importing the library.
```python
import openplayground
openplayground.user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
```
## Copyright:
This project is licensed under the [GNU GPL v3](https://www.gnu.org/licenses/gpl-3.0.txt). Most of the code has been written by me, [ading2210](https://github.com/ading2210). A list of all the contributors can be found [here](https://github.com/ading2210/openplayground-api/graphs/contributors).
Raw data
{
"_id": null,
"home_page": "https://github.com/ading2210/openplayground-api",
"name": "openplayground-api",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "",
"author": "ading2210",
"author_email": "",
"download_url": "",
"platform": null,
"description": "# Python OpenPlayground API\n[![PyPi Version](https://img.shields.io/pypi/v/openplayground-api.svg)](https://pypi.org/project/openplayground-api/)\n\nThis is an unoffical API wrapper for the website [OpenPlayground](https://nat.dev), which provides access to a wide array of AI models for free, including ChatGPT, GPT-4, and Claude.\n\n## Notice:\nOpenPlayground has recently announced that they are going to starting deleting accounts that access their API via automated means (probably as as a direct response to this library). You should be fine as long as you don't send to many requests, since this library is able to bypass their bot detection by spoofing the `X-Session` header. They've also recently been requiring SMS verification upon signup, but this can easily be bypassed by signing in using a Google account. \n\n![screenshot from their discord server](https://media.discordapp.net/attachments/1072352756481929316/1088019955322196048/image.png)\n\n## Features:\nThis library has the following abilities:\n - Log in using OTP code\n - List models\n - Generate text\n\n## Installation:\nYou can install this library by running the following command:\n```\npip3 install openplayground-api\n```\n\n## Documentation:\nAn example of how to use this library can be found in `/examples/example.py`.\n\n### The `Model` Class:\nThe `openplayground.Model` class describes a model that is available to the user. Valid attributes are:\n - `provider` - The company that developed the model (e.g., openai, anthropic)\n - `name` - The name of the model, such as `text-davinci-003`.\n - `version` - The version of the model. This may return `None` on some models.\n - `tag` - A string that combines the provider and name, such as `openai:text-davinci-003`.\n - `params` - A dictionary containing possible parameters for the model.\n\n### Authenticating With an OTP Code:\nThe `openplayground.Auth` class can be used to get your token using an OTP code emailed to you. Note that the following examples assume that `auth` is the name of your `openplayground.Auth` class.\n\n```python\nimport openplayground\nauth = openplayground.Auth()\n```\n\n#### Sending the OTP Code:\nThe `openplayground.Auth.send_otp_code` function sends an email containing the OTP code to the specificed email address. \n\n```python\nauth.send_otp_code(\"sample@example.com\")\n```\n\n#### Verifiying the OTP Code:\nOnce you have the OTP code, you can use the `openplayground.Auth.verify_otp_code` function to get your token from that OTP code. You can then use this token to create an `openplayground.Client` instance.\n\n```python\notp_code = input(\"Enter OTP code: \")\ntoken = auth.verify_otp_code()\n```\n\n### Using the Client:\nThe `openplayground.Client` class accepts two arguments, which are your account's token and its email. Your token can be obtained from the `__session` field in your browser's cookies, or using the `openplayground.Auth` class as shown above. The email field is optional, but filling it out might reduce the chances of this library being detected.\n\n```python\nimport openplayground\nclient = openplayground.Client(token, email=email)\n```\n\nNote that the following examples assume `client` is the name of your `openplayground.Client` instance.\n\n#### Downloading the Available Models:\nThe `client.get_models` function fetches the available models from `https://nat.dev/api/all_models`, and returns a dictionary of `openplayground.Model` objects. The client downloads the available models upon initialization and stores it in `client.models`, so calling this function shouldn't be necessary. \n\nSome popular model tags are:\n - OpenAI: `openai:gpt-4`, `openai:gpt-3.5-turbo`, `openai:text-davinci-003`\n - Anthropic: `anthropic:claude-instant-v1.0`, `anthropic:claude-v1.2`\n - Facebook/Stanford: `textgeneration:llama-65b`, `textgeneration:alpaca-7b`\n\n```python\nprint(client.models.keys())\n#dict_keys(['forefront:EleutherAI/GPT-J', 'forefront:EleutherAI/GPT-NeoX', 'forefront:pythia-12b', 'forefront:pythia-20b', 'forefront:pythia-6.9b', 'anthropic:claude-instant-v1.0', 'anthropic:claude-v1.2', 'textgeneration:alpaca-7b', 'textgeneration:llama-65b', 'huggingface:bigscience/bloomz', 'huggingface:google/flan-t5-xxl', 'huggingface:google/flan-ul2', 'cohere:command-medium-nightly', 'cohere:command-xlarge-nightly', 'cohere:medium', 'cohere:xlarge', 'openai:gpt-4', 'openai:code-cushman-001', 'openai:code-davinci-002', 'openai:gpt-3.5-turbo', 'openai:text-ada-001', 'openai:text-babbage-001', 'openai:text-curie-001', 'openai:text-davinci-002', 'openai:text-davinci-003'])\n```\n\n#### Generating Text:\nThe `client.generate` function generates some text given a model and a prompt. Optionally, you can also specify arguments such as the maximum length in the kwargs. You can find a list of valid arguments and their defaults in `openplayground.Model.params`. A few common ones are:\n - `maximum_length`\n - `temperature`\n - `top_k`\n - `top_p`\n\nThe values returned from this function are streamed and expressed in a dictionary. Note that GPT-4 access currently has a daily limit of around 10 requests/day, and may become paid in the future. \n\nStreamed example:\n```python\nfor chunk in client.generate(\"openai:gpt-3.5-turbo\", prompt):\n if chunk[\"event\"] == \"infer\":\n print(chunk[\"message\"], end=\"\", flush=True)\n```\n\nNon-streamed example:\n```python\nmessage = \"\"\nfor chunk in client.generate(\"openai:gpt-3.5-turbo\", prompt):\n if chunk[\"event\"] == \"infer\":\n message += chunk[\"message\"]\nprint(message)\n```\n\n### Misc:\n#### Changing the Spoofed User-Agent:\nYou can change the global User-Agent by setting `openplayground.user_agent` right after importing the library.\n\n```python\nimport openplayground\nopenplayground.user_agent = \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36\"\n```\n\n## Copyright:\nThis project is licensed under the [GNU GPL v3](https://www.gnu.org/licenses/gpl-3.0.txt). Most of the code has been written by me, [ading2210](https://github.com/ading2210). A list of all the contributors can be found [here](https://github.com/ading2210/openplayground-api/graphs/contributors).\n",
"bugtrack_url": null,
"license": "GPLv3",
"summary": "A reverse engineered API wrapper for OpenPlayground (nat.dev)",
"version": "0.1.1",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "725094ce710750a66a01e92e301effd975d678dc9714397a81dfdf0a0ad9f0eb",
"md5": "f6c0dde9fa0f48006fc648d1029ac581",
"sha256": "8f0eb04bda9ec5e9e4f521fade97c534feac5f4f4a51bd99ad23552e2f297ecd"
},
"downloads": -1,
"filename": "openplayground_api-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f6c0dde9fa0f48006fc648d1029ac581",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 18003,
"upload_time": "2023-03-28T08:34:31",
"upload_time_iso_8601": "2023-03-28T08:34:31.301273Z",
"url": "https://files.pythonhosted.org/packages/72/50/94ce710750a66a01e92e301effd975d678dc9714397a81dfdf0a0ad9f0eb/openplayground_api-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-28 08:34:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "ading2210",
"github_project": "openplayground-api",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "openplayground-api"
}