Connecting to ChatGPT via the API in Python
I connected to ChatGPT using the API in Python. I ran a couple of tests, which cost $0.01 to process.
Next step - take data from a database, process it, return results to the database.
How it’s billed
- The API billed separately.
- You get $5 credit free, and can set limits (but only during first three months since registration, mine ran out!).
- “With 128k context, fresher knowledge and the broadest set of capabilities, GPT-4 Turbo is more powerful than GPT-4 and offered at a lower price.”
- gpt-4-1106-preview is $0.01 / 1K tokens input, $0.03 / 1K tokens output
- gpt-4-1106-vision-preview is $0.01 / 1K tokens input, $0.03 / 1K tokens output
- gpt-4 is $0.03 / 1K tokens input, $0.06 / 1K tokens output
- gpt-4-32k $0.06 / 1K tokens input, $0.12 / 1K tokens output
🤔 Compare output of GPT-4 Turbo vs. GPT-4?
🤔 If you used 128k input, then that would cost $1.28 for a single prompt with gpt-4-1106-preview.
Process
- Get an API key link.
- Note, to install the latest version of python, I installed pyenv
- Install the openAI library
pip install openai
- Set up the API key for all projects
export OPENAI_API_KEY='your-api-key-here'
- Do a test with the code below.
from openai import OpenAI
= OpenAI()
client
= client.chat.completions.create(
completion ="gpt-3.5-turbo",
model=[
messages"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
{"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
{
]
)
print(completion.choices[0].message)
Output
Very slow to respond to both requests! 20 seconds or so.
Test with gpt-3.5-turbo
ChatCompletionMessage(content=“In the realm where code does weave and flow,lies a concept, enchantingly bestowed,, it is, a tale to be told,a function in itself does unfold.a mirror reflecting what’s within,embraces iterations without end,dance of patterns that do transcend,space and time, a twist to comprehend.! Picture a landscape lush and green,tasks to be done, a sight serene,a challenge arises, a puzzle unseen,by one, let us explore this machination keen.a function at hand, let us delve,call to itself, a miracle to behold,a Russian doll, it opens the shell,into an infinite nested spell.each cycle, it sheds a layer,deeper, a passage to wander,a problem into fragments rare,solutions beyond compare., heed this warning, dear programmer divine,the treacherous, infinite line,without a base, it will never decline,memory, a dire consequence malign.fret not, for a base case shall guide,threshold that leads our code to glide,the journey, preventing the tide,the recursion abides., recursion, a concept so profound,its endless loop, mysteries abound,problems, with elegance unbound,the poet’s realm, where dreams are found.”, role=‘assistant’, function_call=None, tool_calls=None)
The poem
In the realm where code does weave and flow,
There lies a concept, enchantingly bestowed,
Recursion, it is, a tale to be told,
Where a function in itself does unfold.Like a mirror reflecting what’s within,
Recursion embraces iterations without end,
A dance of patterns that do transcend,
Beyond space and time, a twist to comprehend.Ah! Picture a landscape lush and green,
With tasks to be done, a sight serene,
But a challenge arises, a puzzle unseen,
One by one, let us explore this machination keen.With a function at hand, let us delve,
A call to itself, a miracle to behold,
Like a Russian doll, it opens the shell,
Peering into an infinite nested spell.Through each cycle, it sheds a layer,
Diving deeper, a passage to wander,
Breaking a problem into fragments rare,
Revealing solutions beyond compare.Yet, heed this warning, dear programmer divine,
Beware the treacherous, infinite line,
For without a base, it will never decline,
Filling memory, a dire consequence malign.But fret not, for a base case shall guide,
A threshold that leads our code to glide,
Ending the journey, preventing the tide,
Ensuring the recursion abides.Oh, recursion, a concept so profound,
In its endless loop, mysteries abound,
Solving problems, with elegance unbound,
In the poet’s realm, where dreams are found.
Test with gpt-4-1106-preview
ChatCompletionMessage(content=‘In the realm of loops and flowing code,a concept that often is bestowed,, they say, is its name,mystical loop, not quite the same.calls upon itself, a mirror’s gaze,function that runs through a labyrinth’s maze,each step it takes, down it dives,its own essence, where it thrives.is how this magic unfolds,a tale that gently scolds,endless spiral of inquiring mind,the base case it must find.upon a syntax clear and bright,function whispered in the digital night,“If you wish to solve a problem so vast,upon me, and call me fast.beware the lure of my siren song,without a stop, you’ll loop too long,base case you’ll need, as a guiding light,end the recursion, and do what’s right.”each call a problem sliced anew,tasks, from the big one grew,last it stops, no more to spawn,with that, the recursion’s gone.picture a painter with a quirky vision,himself with accurate precision,the frame, he begins once more,paint himself, just like before.this strange loop, the task replicates,pattern within patterns, it integrates,the canvas showcases a start so small,painter’s no longer visible at all.is recursion, a loop deftly spun,journey within that’s never quite done,you declare when it should cease,grant the program its much-needed peace.’, role=‘assistant’, function_call=None, tool_calls=None)
The poem
In the realm of loops and flowing code,
Lies a concept that often is bestowed,
Recursion, they say, is its name,
A mystical loop, not quite the same.It calls upon itself, a mirror’s gaze,
A function that runs through a labyrinth’s maze,
With each step it takes, down it dives,
Into its own essence, where it thrives.Here is how this magic unfolds,
With a tale that gently scolds,
The endless spiral of inquiring mind,
Seeking the base case it must find.Once upon a syntax clear and bright,
A function whispered in the digital night,
“If you wish to solve a problem so vast,
Call upon me, and call me fast.But beware the lure of my siren song,
For without a stop, you’ll loop too long,
A base case you’ll need, as a guiding light,
To end the recursion, and do what’s right.”With each call a problem sliced anew,
Smaller tasks, from the big one grew,
At last it stops, no more to spawn,
And with that, the recursion’s gone.So picture a painter with a quirky vision,
Painting himself with accurate precision,
Inside the frame, he begins once more,
To paint himself, just like before.Through this strange loop, the task replicates,
A pattern within patterns, it integrates,
Until the canvas showcases a start so small,
The painter’s no longer visible at all.This is recursion, a loop deftly spun,
A journey within that’s never quite done,
Unless you declare when it should cease,
And grant the program its much-needed peace.
References
https://help.openai.com/en/articles/7039783-how-can-i-access-the-chatgpt-api