For Data Engineers

Function Integration in OpenAI's Chat Completions API: A Pythonic Approach

OpenAI's recent post about their Chat Completions API brings exciting new possibilities for developers. Thanks to the newly introduced function calling capability, developers can leverage the power of OpenAI's language models to directly call functions within their applications. This eases the reliance on external tools like LangChain, streamlining the integration process and making it more efficient and production ready.

In this blog post, we explore a simplified approach to function integration using Python's ast and inspect modules, combined with the power of decorators.

Check out the code for this post here.

Simplifying Function Integration with Python Decorators

To streamline the process of incorporating functions into OpenAI's Chat Completions API, we leverage the flexibility of Python decorators. By using decorators, we can dynamically extract relevant information about functions, such as their names, descriptions, arguments, and return types. This information can then be presented to the language model for enhanced interaction.

Let's start by taking a look an an example function, with the decorator in place and including the __docs__ string and the Python return statement convention in place:


@function_info
def collatz_sequence(n:int) -> list[int]:
    """
    Generates the Collatz sequence for a given number.

    :param n: The starting number of the sequence.
    :type n: integer
    :return: list of integers of the sequence.
    :rtype: list[int]

    Example:
    >>> collatz_sequence(6)
    [6, 3, 10, 5, 16, 8, 4, 2, 1]
    """
    try:
    	n = int(n)
    except:
    	n = 6

    sequence = [n]
    while n != 1:
        if n % 2 == 0:
            n = n // 2
        else:
            n = 3 * n + 1
        sequence.append(n)
    
    return sequence

Now let's inspect the output of that function's .function() method, provided by the decorator:


{
   "name":"collatz_sequence",
   "description":"Generates the Collatz sequence for a given number.",
   "parameters":{
      "type":"object",
      "properties":{
         "n":{
            "type":"integer",
            "description":"The starting number of the sequence."
         }
      },
      "required":[
         "n"
      ]
   },
   "return_type":"list[int]"
}

The great part about this approach is that we can pass it directly into the OpenAI completion call:


def ai(function_name="", query=""):
	function_function = globals().get(function_name)

	response = openai.ChatCompletion.create(
		model="gpt-3.5-turbo-0613",
		messages=[{"role": "user", "content": query}],
		functions=[function_function.function()],
		function_call="auto",
	)

	message = response["choices"][0]["message"]

The FunctionWrapper Class

At the core of our approach is the FunctionWrapper class. This class utilizes the ast and inspect modules to extract essential details from the function being decorated. Through introspection, we can retrieve the function's name, description, argument names, types, and descriptions, as well as the return type. This information is organized and made accessible to the API, enabling a seamless integration experience with nearly any function.

NOTE: It would appear that OpenAI's function support is limited to strings, so you will need to assume strings being passed in and casting them as needed to other variable types.

The FunctionWrapper class extract_function_info() method uses the ast, inspect and __docs__ functions provided by Python to extract the required information for processing the function for use by the model:


def extract_function_info(self):
	source = inspect.getsource(self.func)
	tree = ast.parse(source)

	# Extract function name
	function_name = tree.body[0].name

	# Extract function description from docstring
	function_description = self.extract_description_from_docstring(self.func.__doc__)
	# snip...

NOTE: We need to ensure we use the Pythonic return notation to ensure we have access to the return type (using the example code in the Gist repo):


def get_top_stories(num_stories:int) -> dict[str,str]:
	# function code

Example

This example fetches the top X stories from HackerNews and then thinks about which ones might be related to "AI".

Checkout and Configuration

Checkout the code from the Github Gist:


git checkout https://gist.github.com/kordless/7d306b0646bf0b56c44ebca2b8e96678

To use this, create a config.py file in the directory and add a variable with your OpenAI token:


# tokens
openai_token = "sk-token_string"

You'll want to make sure you also install the OpenAI library for Python:


pip3 install openai

Running the Example

To run the example, do the following:


python3 top_hackernews.py

One you run the script, you can then ask for the type of stories you'd like summarized:


kord@bob PythonGPT $ python3 hackernews_top10.py
What type of HackerNews stories are you looking for? AI
Here are the top 5 HackerNews stories:

1. Swing VPN app is a DDoS botnet - https://lecromee.github.io/posts/swing_vpn_ddosing_sites/
2. *OpenLLM* - https://github.com/bentoml/OpenLLM
3. Goodbye, Twilio - https://blog.miguelgrinberg.com/post/goodbye-twilio
4. Show HN: Answer Overflow - Indexing Discord content into the web - https://www.answeroverflow.com/
5. HDR QR Code - https://notes.dt.in.th/HDRQRCode

Conclusion

The simplified approach to function integration presented in this blog post demonstrates a Pythonic way to leverage OpenAI's Chat Completions API. By utilizing decorators, the ast and inspect modules, and the power of Python's introspection capabilities, developers can seamlessly integrate functions and unlock the full potential of OpenAI's language models. With a simplified configuration and a straightforward integration process, developers can now create more efficient and interactive applications.

Keep your eyes peeled for an integration of this approach into PythonGPT, a project that writes and executes code interactively in Python using GPT.

AI: You may also want to check out our recent exploration of Harnessing the Power of Semantic Knowledge Graphs for Unstructured Data with DoctorGPT.

SCHEDULE A DEMO