Git Product home page Git Product logo

openplayground-api's Introduction

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("[email protected]")

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.

openplayground-api's People

Contributors

ading2210 avatar liej6799 avatar jph00 avatar

Stargazers

Nikolaus Schlemm avatar Virgil avatar Murali Manohar Kondragunta avatar arr avatar

Watchers

 avatar arr avatar

Forkers

mildmillard

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.