Aan de slagGa gratis aan de slag

Defining a function with external APIs

You are developing a flight simulation application and have been asked to develop a system that provides specific information about airports mentioned in users' requests. You decide to use the OpenAI API to convert the user request into airport codes, and then call the AviationAPI to return the information requested. As the first step in your coding project, you configure the function to pass to the tools parameter in the Chat Completions endpoint.

In this exercise, the get_airport_info() and get_response() functions have been preloaded. The get_airport_info() function uses the AviationAPI and takes as input one airport code, returning the response with the requested airport information.

Deze oefening maakt deel uit van de cursus

Developing AI Systems with the OpenAI API

Cursus bekijken

Oefeninstructies

  • Define the function to pass to tools: that should include the function 'name' for the function, a 'description' specifying that a matching airport code should be returned, and 'parameters' and 'result' details.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

client = OpenAI(api_key="")

# Define the function to pass to tools
function_definition = [{"type": ____,
                        ____ : {"name": ____,
                                ____: ____,
                                ____: {"type": ____, ____: {"airport_code": {____}, ____} }, 
                                "result": ____ }}]

response = get_response(function_definition)
print(response)
Code bewerken en uitvoeren