Giter Club home page Giter Club logo

gpt-pydecorator's Introduction

GPT-4 Function Python Decorator

This Python package creates a function decorator @openaifunc which can be used to automatically generate the functions parameter for the ChatGPT API.

The original code was generated with GPT-4. I'm new to Python and have never created a Python package.

Inspired by @memespdf on @sentdex YouTube-video comments

How to use

First, import the package at the top of your Python code:

from openai_decorator.openai_decorator import openaifunc, get_openai_funcs

Then, add a @openaifunc decorator to the functions you want to use with ChatGPT:

@openaifunc
def add_numbers(a: int, b: int):
    """
    This function adds two numbers.
    """
    return a + b

Then, you can get a list of all the functions and their definitions for ChatGPT with get_openai_funcs() like so:

response = openai.ChatCompletion.create(
    model="gpt-4-0613",
    messages=messages,
    functions=get_openai_funcs(),
    function_call="auto",
)

Parameter descriptions

As far as I know, there is no "official" way to add docstrings for parameters in Python, but you can add the parameter definitions to the docstring in PHP DocBlock style, and GPT-4 seems to obey them.

@openaifunc
def get_current_weather(location: str, country: str) -> str:
    """
    Gets the current weather information
    @param location: The location for which to get the weather
    @param country: The country in which to look for the location
    """

    if location is None:
        return "A location must be provided. Please ask the user which location they want the weather for"
    else:
        return "The weather is nice and sunny"

Currently, this will not populate the description of the parameters in the API request, but GPT-4 still adheres to the rules.

Pydantic Models

You can also set descriptions for the function parameters with Pydantic models. This will actually populate the description of the parameters in the API request.

from pydantic import BaseModel, Field

class LocationModel(BaseModel):
    location: str = Field(
        description="The location for which to get the weather"
    )
    country: str = Field(
        description="The country in which to look for the location"
    )

@openaifunc
def get_current_weather(location: LocationModel) -> str:
    """
    Gets the current weather information
    """
    location = LocationModel.parse_obj(location)

    if location is None:
        return "A location must be provided. Please ask the user which location they want the weather for"
    else:
        return "The weather is nice and sunny"

Chatbot

There's a demo chatbot that uses the GPT-4 API with function calling. You can run it by exporting your OpenAI API key first:

$ export OPENAI_API_KEY=YOUR_API_KEY

And then running the script:

$ ./chatbot.py

You can test it by asking it about the weather, some YouTube channel recommendations or to calculate the length of a string.

You can also modify the functions in the chatbot code, to test your own functions.

gpt-pydecorator's People

Contributors

malikmalna avatar oulianov avatar unconv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gpt-pydecorator's Issues

Adding this feature to the LangChain module

Hey Unconv, as you noticed I'm a big fan of the concept behind gpt-pydecorator.

It's a great abstraction to document OpenAI function calls efficiently. Actually, it's so simple, I wonder if that's not something OpenAI developers had in mind from the start.

Right now, a popular module to interact with LLM and build agents is LangChain. I snooped around and didn't see anything similar in the library. Adding gpt-pydecorator feature into LangChain would be a nice contribution to opensource !

Do you think this is a good idea? If so, what should be our plan to achieve this ?

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.