Skip to content

Package to make working with the OpenAI GPT API in Python super easy.

License

Notifications You must be signed in to change notification settings

hpharmsen/gpteasy

Repository files navigation

GPT Easy

Package to make working with the OpenAI GPT API in Python super easy.

Author: Hans-Peter Harmsen ([email protected])
Current version: 1.5.2

Installation

  1. Install the package:
python -m pip install gpteasy
  1. Create an OpenAI acccount here
  2. Create OpenAI api keys here
  3. Create a .env file with the following content:
OPENAI_API_KEY=your-openai-api-key
OPENAI_ORGANIZATION=your-openai-organization-id

Usage

from gpteasy import GPT


if __name__ == "__main__":
    gpt = GPT()
    gpt.model = 'gpt-3.5-turbo' # Optional, the default model is gpt-4
    gpt.system = lambda: "You are a movie critic. I feed you with movie titles and you give me a review in 50 words."

    message = gpt.chat("Forrest Gump")
    print(message.text)

output

Forrest Gump is an American classic that tells the story of
a man with a kind heart and simple mind who experiences major
events in history. Tom Hanks gives an unforgettable performance, 
making us both laugh and cry. A heartwarming and nostalgic 
movie that still resonates with audiences today.

Using the examples

Install dependencies:

python -m pip install -r requirements.txt

Basic

python examples/basic.py

Starts an interactive session. In the session you dan chat with GPT-4 or another model.

Function calling

python examples/function_calling.py

Example of the function calling feature. This feature allows you to call functions from within the GPT-4 model. In this case, we query a wheather API to get the current weather.

Specifying a return type

python examples/return_types.py

You can specify a specific return type (like a list of dicts) for the completion. This is useful when you want to extract structured data from the completion.

To define a return type, specify it as a @dataclass derived from dataclasses-jsonschema.

See the example code for more details.

Interactive

python examples/interactive.py

Starts an interactive session. In the session you dan chat with GPT-4 or another model.

Special commands

In the interactive mode you can use these special commands which each start with a colon:

Syntax Description
:reset resets the conversation
:load name loads the saved conversation with the specified name
:save name saves the conversation under the specified name
:input filename loads an input from the specified file
:model gpt-4 Sets the AI model
:max_tokens 800 The maximum number of tokens to generate in the completion
:temperature 0.9 What sampling temperature to use, between 0 and 2
:n 1 Specifies the number answers given
:stop ["\n", " Human:", " AI:"] Up to 4 sequences where the API will stop generating further tokens
:bye quits but saves the conversation first
:exit or :quit quits the program

About

Package to make working with the OpenAI GPT API in Python super easy.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published