📺 The Power of OpenAI Function Calls: A Simple Script & How-to

I’ve been playing with the new OpenAi API Function Calls. I’d to share a simple command line python script I created that helps show how to use the new feature. The default is a weather app, you could easily use it with langchain or MS Guidance for more complex intelligent agents.

With the OpenAI Function Call system, developers can create powerful AI applications that engage in interactive multi-turn dialogs, perform reasoning and inference tasks, extract structured data from unstructured text, and seamlessly integrate with external tools and APIs. It offers customization, flexibility, and advanced reasoning capabilities, opening up new possibilities for building intelligent and customizable applications.

The system seamlessly integrates with external tools, APIs, and databases, bridging the gap between natural language understanding and practical applications. It’s actually pretty cool.

OpenAI API Function Calls offer a range of use cases to enhance conversational AI. Some notable examples include:

Weather Information Retrieval
This is the default example provided by OpenAi, so I thought I’d use it. By utilizing OpenAI API Function Calls, you can create weather information retrieval systems. The model can call external weather APIs based on user inputs, such as location and temperature unit, and provide up-to-date weather details including temperature, description, and more.

Multi-Turn Dialogs
With OpenAI API Function Calls, you can create interactive multi-turn dialogs. The model can maintain context and carry out meaningful conversations with users. You can set up conversational flows where the model responds appropriately based on user inputs and past interactions.

Chain of Thought
OpenAI API Function Calls enable a chain of thought, allowing the model to reason and process complex sequences of instructions. Users can provide step-by-step guidance, and the model can intelligently follow and execute those instructions, providing accurate responses based on the context.

Reasoning and Inference
OpenAI API Function Calls empower the model to perform reasoning and inference tasks. Users can pose questions or present scenarios, and the model can utilize external tools or APIs to gather relevant information, analyze data, and provide insightful responses.

These are just a few examples of the versatility and power of OpenAI API Function Calls.

Please note that the code in this repository is provided as an example and may require customization and adaptation to suit your specific use case.

11 Likes

Very Cool! Thanks for sharing! :slight_smile:

this is awesome! We can also chain multiple requests.

suppose I have 2 functions, first gets a Spotify playlist ID based on a name, second gets a dataframe of tracks based on a playlist ID.

I prompt with “get a dataframe of tracks in the Spotify playlist named ‘Reddit Prettiest Songs’” , which needs 2 function calls

I run the first response and properly get the ID.
I add the 1st response to my message list and add a prompt with “the previous function returned {result}. What next?” This correctly gives the 2nd function call to make.

(original was asking how to do this but the below works as expected if the 2 functions are defined and passed correctly)

chain_message1 = "get a dataframe of tracks in the Spotify playlist named 'Reddit Prettiest Songs'"
messages = []
messages.append({"role": "system", "content": system_prompt,})
messages.append({"role": "user", "content": chain_message1,})
response=get_response(messages)

last_message = response['choices'][0]['message']

messages.append(last_message)

fn_obj = response['choices'][0]['message']['function_call']
args = eval(fn_obj['arguments'])
result = eval(f"{fn_obj['name']}(**args)")

chain_message2 = f"the previous function returned {result}. What next?"
messages.append({"role": "user", 'content': chain_message2})

response=get_response(messages)
fn_obj = response['choices'][0]['message']['function_call']
args = eval(fn_obj['arguments'])
eval(f"{fn_obj['name']}(**args)")

1 Like

Hi @drucev, very cool! The example from OpenAI is for just one question. I am having the same use case as yours which I need to call multiple APIs to come up with an answer.

Do you mind sharing the full source code? Looks like this is similar to what I need. :slight_smile:

Hey, Is it possible to use functions with the completions endpoint? Or is it supported only for chatCompletions?

it didn’t let me post a link but , my github is druce , the repo is reddit_prettiest_songs , the notebook is openai_function_calling.ipynb

1 Like

Welcome to the site.


The default for a Discourse site for new user is to be able to use 2 links in a post. (ref) However that value can be changed. While I am a moderator on this site I am not an admin and can not see the site settings. :slightly_frowning_face:

However if you spend more than 10 minutes just viewing some topics you should get to trust level 1 and then the restriction should be removed. :slightly_smiling_face:

1 Like

hey @drucev , I’ll check out your github. thanks!