At Fonction Labs, we are constantly exploring innovative ways to leverage AI for various applications.
In this article, we will walk you through an example of using OpenAI’s GPT-4 model to interact with a news API in order to fetch and summarize top news headlines. This process involves integrating function calling within the OpenAI framework, allowing for dynamic and context-aware API requests based on user queries.
Overview
The primary goal is to create a system where a user can input a query, and the AI will automatically extract the necessary parameters, call the relevant API, and provide a summarized response. We will use the News API to fetch top headlines and OpenAI’s GPT-4 for processing and summarizing the data.
What is function calling ?
In the context of OpenAI models, function calling allows the AI to dynamically interact with external functions or APIs based on natural language queries. This enables the AI to perform complex tasks, such as fetching data using an API or processing information by 1) interpreting the user’s intent and 2) translating it into actionable function calls. This approach enhances the AI’s utility, making it capable of performing real-time external operations.
In this article, we present a very simple example of a function calling application in python, where a GPT-4 agent triggers a News API from any given user prompt.
Prerequisites
Here are the tools we will need:
- Python 3.7+
- A valid OpenAI API key
- A valid News API Key
- The following Python packages:
tenacity
,openai
,newsapi-python
Setting up the API keys
First, we ensure that the API keys for both OpenAI and News API are set up. We need to store them securely in the following environment variables:
export OPENAI_API_KEY=my_openai_api_key
export API_KEY_NEWS=my_news_api_key
Code Breakdown
1. Defining the API Interaction
Based on the API’s documentation, we will start by defining the object newsapi and provide an example on how to use it:
from newsapi import NewsApiClient
newsapi = NewsApiClient(api_key=os.environ["API_KEY_NEWS"])
# usage example
params = {
"country": "us",
"category": "technology",
}
response = newsapi.get_top_headlines(**params)
print(response)
The function get_top_headlines
returns a list of articles based on the specified arguments like category or country.
2. Integrating OpenAI for Function Calling
First, let’s define a global variable TOOLS
, which will define the interface of the News API get_top_headlines
function so that it can be understood and called by the GPT-4 agent.
We need to specify the name of the function, with all the arguments described with their type.
TOOLS = [
{
"type": "function",
"function": {
"name": "get_top_headlines",
"description": "Fetch top headlines based on specified parameters",
"parameters": {
"type": "object",
"properties": {
"country": {
"type": "string",
"description": "The 2-letter ISO 3166-1 code of the country you want to get headlines for",
"enum": ["ae", "ar", "at", "au", "be", "bg", "br", "ca", "ch", "cn", "co", "cu", "cz", "de", "eg", "fr", "gb", "gr", "hk", "hu", "id", "ie", "il", "in", "it", "jp", "kr", "lt", "lv", "ma", "mx", "my", "ng", "nl", "no", "nz", "ph", "pl", "pt", "ro", "rs", "ru", "sa", "se", "sg", "si", "sk", "th", "tr", "tw", "ua", "us", "ve", "za"]
},
"category": {
"type": "string",
"description": "The category you want to get headlines for",
"enum": ["business", "entertainment", "general", "health", "science", "sports", "technology"]
},
"sources": {
"type": "string",
"description": "A comma-separated string of identifiers for the news sources or blogs you want headlines from"
},
"q": {
"type": "string",
"description": "Keywords or a phrase to search for"
},
"page": {
"type": "integer",
"description": "Use this to page through the results if the total results found is greater than the page size",
"default": 1,
"minimum": 1
}
}
}
}
}
]
For more complex function calling applications, you could specify many functions like the one above.
We will now integrate OpenAI’s GPT-4 model to handle the extraction of parameters from the user queries in order to call the API.
To do so, let’s write some functions that will convert a user query into proper function calling:
import json
from openai import OpenAI
from tenacity import retry, wait_random_exponential, stop_after_attempt
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))
def chat_completion_request(messages, tools=None, tool_choice=None, model="gpt-4"):
try:
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
tool_choice=tool_choice,
)
return response
except Exception as e:
print("Unable to generate ChatCompletion response")
print(f"Exception: {e}")
return None
def get_headlines_from_user_query(user_query: str) -> dict | None:
messages = [
{"role": "system", "content": "Extract the parameters for the get_top_headlines function based on the user query."},
{"role": "user", "content": user_query}
]
chat_response = chat_completion_request(messages, tools=TOOLS, tool_choice="required")
if chat_response:
assistant_message = chat_response.choices[0].message
if assistant_message.tool_calls:
tool_call = assistant_message.tool_calls[0]
if tool_call.function.name == 'get_top_headlines':
function_args = json.loads(tool_call.function.arguments)
return newsapi.get_top_headlines(**function_args)
else:
print("The assistant did not provide arguments for the function calling.")
return None
else:
return None
The chat_completion_request
function, will, based on a user_query
, trigger chat_completion_request
with the previously defined TOOLS
interface object.
Note that the tool_choice
argument for OpenAI is set to "required"
to indicate that the object returned by the client.chat.completions.create
method should be a function calling.
3. Summarizing the Headlines
Finally, we create a function that will summarize the headlines fetched from the News API:
def summarize_headlines(top_headlines: dict | None) -> str:
messages = [
{"role": "system", "content": "Summarize the top headlines based on the query."},
{"role": "user", "content": str(top_headlines)}
]
chat_response = chat_completion_request(messages)
return chat_response.choices[0].message.content
4. Example Usage
Here’s how you can use the functions to get and summarize news headlines based on a user query:
if __name__ == "__main__":
user_query = "Give me the latest technology news in the US."
headlines = summarize_headlines(get_headlines_from_user_query(user_query))
print(headlines)
Once evaluated, this will output the following answer:
1. iOS 18 is set to allow users more customization of their iPhone's home screen, with the ability to recolor app icons for personal home screen customization.
2. The Verge rounded up some of the best tiny online games to enjoy on a daily basis, including Wordle, Coffee Golf, and Connections.
3. There are major Windows privacy concerns with Noctua's "New" Fan, AMD Epyc in AM5 Desktops as discussed on Gamers Nexus.
4. Fox News highlighted a simple trick on iPhone's password autofill feature which has the potential to help keep out cyber creeps on the device.
5. Google Pixel 9 may be replaced by the Pixel 10, which has the potential to feature a "Samsung-free" Tensor G5 chip.
6. A rare $60000 historical computer was discovered by a salvage company in London as reported by Hackaday.
7. YouTube Music has added a hum-to-search feature that lets you hum, whistle, or sing to find music on Android Phones.
8. Bungie has won a court battle against cheat and mod site, AimJunkies, successfully gaining a significant payout.
9. Samsung is offering significant savings on the Galaxy S24 Ultra, boosting the discount to $970.
10. Forbes delved into the expected features for the upcoming Apple iPhone 16 and iPhone 16 Plus, based on various reports and rumours.
Conclusion
By combining the power of OpenAI’s GPT-4 with a news API, we can create an intelligent system that understands user queries, fetches relevant news, and provides concise summaries.
This integration showcases the potential of AI in automating information retrieval and summarization tasks, making it a valuable tool for various applications that extend well beyond the scope of simple news summarization.
Stay tuned to Fonction Labs blog for more insights and tutorials on leveraging AI in innovative ways!