<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Souleymane Sagna on Medium]]></title>
        <description><![CDATA[Stories by Souleymane Sagna on Medium]]></description>
        <link>https://medium.com/@souleylosmane?source=rss-78f6940c3283------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*OGleB5vHvP9ucUwzJ2SvVQ@2x.jpeg</url>
            <title>Stories by Souleymane Sagna on Medium</title>
            <link>https://medium.com/@souleylosmane?source=rss-78f6940c3283------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:09:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@souleylosmane/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Step-by-step guide: How to use the ChatGPT API with Python — your ultimate tool for enhanced…]]></title>
            <link>https://medium.com/@souleylosmane/step-by-step-guide-how-to-use-the-chatgpt-api-with-python-your-ultimate-tool-for-enhanced-e4392a6c7bda?source=rss-78f6940c3283------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4392a6c7bda</guid>
            <dc:creator><![CDATA[Souleymane Sagna]]></dc:creator>
            <pubDate>Fri, 03 Mar 2023 08:32:02 GMT</pubDate>
            <atom:updated>2023-03-03T08:32:02.697Z</atom:updated>
            <content:encoded><![CDATA[<h2>Step-by-step guide: How to use the ChatGPT API with Python — your ultimate tool for enhanced chatbot capabilities!</h2><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*RevlBqqSDlcr2hFSdJWsww@2x.jpeg" /></figure><p>In a recent development, OpenAI has launched an API version of ChatGPT, following the initial release of a free version and a premium version at $20 per month. With the integration of ChatGPT in any application, the already powerful GPT-3 has opened up new avenues for innovation and efficiency.</p><p>In this article, we delve into how you can easily integrate the ChatGPT API with your applications using Python, unlocking the full potential of this groundbreaking technology.</p><p><strong>How to use the ChatGPT API with Python? A step-by-step guide for developers looking to integrate cutting-edge conversational AI into their projects.</strong></p><p>The Python code to do this is very simple. All you have to do is create an OpenAI account, access your API key, and run the Python code using your key.</p><p><strong>Create an OpenAI account to access the ChatGPT API.</strong></p><p>If you haven’t done so already, you must first create an account on OpenAI.</p><p>Once logged in, go to “Personal” in the top right corner, then “View API keys”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*CP2krJtmWN1n90HoYYKbpQ@2x.jpeg" /></figure><p>Click on ‘New secret token’ to generate your API key. Keep it secret and be sure to update it regularly, as it provides full access to your account.</p><p><strong>Calling the ChatGPT API from Python.</strong></p><p>Start by installing the OpenAI library.</p><p><strong><em>pip install openai</em></strong></p><p>If you’re on a notebook</p><p><strong><em>!pip install openai</em></strong></p><p><strong><em>import openai</em></strong></p><p><strong><em>openai.api_key = VOTRE_API_KEY</em></strong></p><p><strong><em>prompt = f”Your question”</em></strong></p><p><strong><em>completion = openai.ChatCompletion.create(</em></strong></p><p><strong><em>. model=”gpt-3.5-turbo”,</em></strong></p><p><strong><em>. messages=[{“role”: “user”, “content”: prompt}])</em></strong></p><p><strong><em>print(completion[‘choices’][0][‘message’][‘content’])</em></strong></p><p>Now, you can customize the prompt to generate the expected output as you desire. This function can take other parameters that may be interesting, such as max_tokens, which gives the maximum number of tokens in the response, or temperature, which determines the degree of unpredictability of the agent. If the temperature is set to 0, the model will always give the same response without taking any risks. However, if it is set to 1, the responses will be more original each time.</p><p><strong><em>prompt = “Your question ”</em></strong></p><p><strong><em>response = openai.Completion.create(</em></strong></p><p><strong><em>engine=”gpt-3.5-turbo”,</em></strong></p><p><strong><em>prompt=prompt,</em></strong></p><p><strong><em>max_tokens=60,</em></strong></p><p><strong><em>n=1,</em></strong></p><p><strong><em>stop=None,</em></strong></p><p><strong><em>temperature=0.7,</em></strong>)</p><p>One of the most fascinating features of ChatGPT is its ability to remember ongoing conversations and reuse what you have said before. This behavior can be replicated as follows:</p><p><strong><em>openai.ChatCompletion.create(</em></strong></p><p><strong><em>model=”gpt-3.5-turbo”,</em></strong></p><p><strong><em>messages=[</em></strong></p><p><strong><em>{“role”: “system”, “content”: “You are a helpful assistant.”},</em></strong></p><p><strong><em>{“role”: “user”, “content”: “Who won the world series in 2020?”},</em></strong></p><p><strong><em>{“role”: “assistant”, “content”: “The Los Angeles Dodgers won the World Series in 2020.”},</em></strong></p><p><strong><em>{“role”: “user”, “content”: “Where was it played?”}])</em></strong></p><p>As the conversation is specified in full, the model will take into account everything that has been said to generate the output. One can imagine an exchange taking place in this form:</p><p><strong><em>for k in range(3):</em></strong></p><p><strong><em>messages=[{“role”: “system”, “content”: “Your question ”}]</em></strong></p><p><strong><em>msg = input()</em></strong></p><p><strong><em>messages.append({“role”: “user”, “content”: msg})</em></strong></p><p><strong><em>reply = openai.ChatCompletion.create(</em></strong></p><p><strong><em>model=”gpt-3.5-turbo”,</em></strong></p><p><strong><em>messages=messages)</em></strong></p><p><strong><em>messages.append({“role”: “user”, “content”:completion[‘choices’][0][‘message’][‘content’]})</em></strong></p><p><strong><em>print(reply[‘choices’][0][‘message’][‘content’])</em></strong></p><p>Initiating my exchange with ChatGPT, I informed it that I was looking for a Python professor and made my first request. After manually inputting my initial message and adding it to the conversation thread, I awaited ChatGPT’s response. Once generated, I added the AI’s reply to our chat log and prompted the user to continue the discussion with another message.</p><p><strong>Moving on to an important subject — money — I will now address pricing and billing for the ChatGPT API.</strong></p><p>Here are two things you need to know: the ChatGPT API is cost-effective and you can monitor your monthly budget to avoid any potential catastrophes.</p><p><strong>How much does the API cost, you ask? </strong>Well, the ChatGPT API comes in at a cheaper rate than its previous model APIs. At a mere $0.002 per 1000 tokens, it’s an absolute steal.</p><p>Breaking news, folks! It turns out that 1000 tokens roughly represent 750 words. So, if you need to generate a 750-word article in English by writing a 250-word prompt, it will only cost you about $0.0026. Incredible, isn’t it?</p><p><strong><em>How to limit your budget?</em></strong></p><p>If you don’t want any unpleasant surprises on your bank account at the end of the month, I suggest setting up a budget limit from your account.</p><p>It’s an easy process.</p><p>Just go to “billing” on the left-hand side, then select “Usage Limits.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*dMcs0cPRRBtq52BwfSA1gg@2x.jpeg" /></figure><p>Define a monthly ‘soft limit’ and ‘hard limit.’ Once you hit the soft limit, you’ll receive an email notification. Once you hit the hard limit, API calls will be blocked.</p><p><strong>Conclusion</strong></p><p>As I asked ChatGPT to generate this conclusion using Python, and it cost me money, I’ll include it even though I’m not too fond of it.</p><p>In conclusion, the ChatGPT API represents a remarkable advancement in the field of chatbots and artificial intelligence.</p><p>With its advanced natural language processing capabilities, it allows for the creation of highly personalized and effective chatbots to meet the needs of each business.</p><p>Furthermore, it also opens up new horizons for automating customer communication processes, making the task easier for entrepreneurs.</p><p>In short, the use of the ChatGPT API offers interesting prospects for businesses looking to improve their customer relations while reducing costs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4392a6c7bda" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>