jph00 / openplayground-api

A reverse engineered Python API wrapper for OpenPlayground (nat.dev)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python OpenPlayground API

PyPi Version

This is an unoffical API wrapper for the website OpenPlayground, which provides access to a wide array of AI models for free, including ChatGPT, GPT-4, and Claude.

(Since nat.dev has started charging for access, they've removed the rule that API access isn't allowed.)

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.

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.

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.

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.

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
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:

for chunk in client.generate("openai:gpt-3.5-turbo", prompt):
  if chunk["event"] == "infer":
    print(chunk["message"], end="", flush=True)

Non-streamed example:

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.

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. Most of the code has been written by me, ading2210. A list of all the contributors can be found here.

About

A reverse engineered Python API wrapper for OpenPlayground (nat.dev)

License:GNU General Public License v3.0


Languages

Language:Python 100.0%