In this tutorial, we will learn about the ChatGPT API and how you can painlessly make use of language models to add human-like text to your projects from your data inputs.
In the very unlikely case that you are new to ChatGPT, it is a cutting-edge language model developed by OpenAI that uses artificial intelligence to generate human-like responses to text inputs. By leveraging the power of ChatGPT and other models, developers can access a wealth of information and insights to enhance their sports analytics projects.
An API, or Application Programming Interface, allows different software applications to communicate with each other. In the context of sports analytics, APIs play a crucial role in retrieving data from various sources, such as sports databases or websites, and making it accessible for analysis and visualization.
We will guide you through making a basic API call with ChatGPT and demonstrate how you can use it to summarize player stats. So let’s dive in and discover the endless possibilities that ChatGPT and APIs offer in the realm of sports analytics!
Setting Up
Before starting, we need to sign up to OpenAI and get an API key to allow us to make calls to it. Once signed in, create a new secret key on the API key page and “Create new secret key”. It is super important to never share this with anyone!
Once we have an API key, we can get coding! Firstly, ensure that you have installed the latest version of the openai library:
pip install --upgrade openai
We then need to import the module, and add in our API key from the beginning. Again, do not share your keys with anyone! You’ll notice that mine isn’t shared – and you should also hide yours if you ever give your code to anyone.
We will also use the module’s ‘client’ function to set up our ability to call the API.
from openai import OpenAI
APIKey = "YOUR KEY HERE!!!"
client = OpenAI(
api_key= APIKey
)
This is all that we need to send a basic request. Let’s give it a try:
chat = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "This is going into a tutorial on the ChatGPT API - say hello world!"}
]
)
Within the ‘client’ function that we called before, there is a client.chat.completions.create
function that is called with arguments to specify the model version to use (in this case, “gpt-3.5-turbo”, others are available! At the time of writing, 4 is the most advanced, yet more expensive) and a message from the user.
The messages
argument is a list containing a dictionary with the user’s message and role. The API will generate a response based on the user’s input using the specified model.
We assigned the response of the API call to a variable called ‘chat’. Let’s see what ‘chat’ looks like:
chat
'''
ChatCompletion(id='chatcmpl-96I2mNF2uR4OgStmzTBczFBvdXQgD',
choices=[Choice(finish_reason='stop', index=0, logprobs=None,
message=ChatCompletionMessage(content="Hello world! Welcome to the ChatGPT API tutorial. Let's get started on exploring the features and capabilities of this powerful tool.",
role='assistant', function_call=None, tool_calls=None))], created=1711286356,
model='gpt-3.5-turbo-0125', object='chat.completion', system_fingerprint='fp_3bc1b5746c',
usage=CompletionUsage(completion_tokens=27, prompt_tokens=24, total_tokens=51))
'''
It returns a whole heap of information, all of it useful for different things. A few key points:
- choices – why did the text generator stop?
- message – our response! This is the important bit.
- usage – how many tokens were needed to create the response. Tokens decide how much an API calls is charged for.
To get the message out of that response, we use this code to extract just the message:
chat.choices[0].message.content
'''
Hello world! Welcome to the ChatGPT API tutorial.
Let's get started on exploring the features and capabilities of this powerful tool.
'''
Fantastic! You have received your first response from a language model!
One Level Deeper
This is of course very basic, but we can use some smarter and programmatic responses to do so many things. As our main example, let’s put ourselves in the position of a scouting coordinator. You receive scores on the players seen by your scouts, and you want to process them into nice messages for the rest of the team to read.
Your data process normally gives you a dictionary with the players’ key details called ‘players:
players = {
"teams": {
"Merseyside Blue": [
{"name":"Hughes", "age":19, "rating":10, "goals": 3},
{"model":"Vega", "age":21, "rating":8},
{"model":"Jones", "age":22, "rating":7, "assists": 2}
],
"Museumplein": [
{"name":"Johnson", "age":30, "rating":6},
{"model":"Petit", "age":23, "rating":6},
{"model":"Adam", "age":22, "rating":5, "red card": 1}
]}
}
The ‘players’ dictionary contains information about the players, such as their name, age, scout’s rating, and additional statistics like goals, assists, and red cards received.
The code below iterates through the ‘home’ and ‘away’ teams, sending a message to the ChatGPT API with the team’s player data. The API then generates a sentence summarizing the key players for each team. The ‘TakeawayMessage’ variable accumulates these generated sentences for both teams, providing a summary of the player stats for each team.
In the function call to create a chat completion, the ‘model’ argument specifies which GPT version to use, and the ‘messages’ argument contains the conversation messages between the system and the user. This time, we add a message as the ‘system’ to prime the overall conversation. The team’s player data is injected into the user’s message for the conversation to work with. The final output is the generated summary of the player stats for both teams.
TakeawayMessage = ''
teams = ['Merseyside Blue', 'Museumplein']
for team in teams:
chat = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are summarising the scouting report for a football team. You will receive a json file of the three key players. Create a sentence summarising the three players"},
{"role": "user", "content": f"{team} team: {str(players['teams'][team])}"}
]
)
TakeawayMessage+=chat.choices[0].message.content
TakeawayMessage += ' '
When we run the code and then call ‘TakeawayMessage’, we got this response:
“The key players for the Merseyside Blue team are Hughes, a 19-year-old with a rating of 10 and 3 goals, Vega, a 21-year-old with a rating of 8, and Jones, a 22-year-old with a rating of 7 and 2 assists. The Museumplein team’s key players include Johnson, aged 30 with a rating of 6, Petit, aged 23 with a rating of 6, and Adam, aged 22 with a rating of 5 and 1 red card.”
Conclusion
That is a much more readable output than our original dictionaries, and could be easily sent to your colleagues. The only ‘watch-out’ in the case of automating this process is the risk of any mistakes from ChatGPT – there is always the risk of hallucinations, confusion or a lack of understanding/ability to not flag obvious errors in your data.
Wrapping, we have explored the power of the ChatGPT API and how it can be leveraged to enhance sports analytics projects. By making basic API calls and using the generated responses to summarize player stats, we have seen how ChatGPT can simplify and streamline data processing and communications tasks.
As you continue to experiment with ChatGPT and APIs in your projects, remember to always handle your API keys securely and be cautious of potential errors that may arise from the language model. With the right approach and creativity, the possibilities offered by ChatGPT are endless in enriching the sports analytics experience. As ever, experiment, build cool things and let us know what you manage to spin up!