Create your own ChatGPT with Python

Create your own ChatGPT with Python

ChatGPT is a product of openai, currently ChatGPT is the most famous product of openai but there are many other products. In this article, we will see how to use ChatGPT with Python means we create a program that can be used to simulate ChatGPT just like it does on https://chat.openai.com/chat/.

So, we are really going to design a program that can be used just like we use ChatGPT.

Installing openai library

We need openai library to use ChatGPT with Python, run the following command in terminal:

pip install openai

or

pip install –upgrade openai

Code to use ChatGPT with Python

# import openai library
import openai

# Set up the OpenAI API client
openai.api_key = "your secret api key"

# this loop will let us ask questions continuously and behave like ChatGPT
while True:
    # Set up the model and prompt
    model_engine = "text-davinci-003"
    
    prompt = input('Enter new prompt: ')

    if 'exit' in prompt or 'quit' in prompt:
        break

    # Generate a response
    completion = openai.Completion.create(
        engine=model_engine,
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    )

    # extracting useful part of response
    response = completion.choices[0].text
    
    # printing response
    print(response)

Output for ChatGPT with Python:

Video output:

Text output

Enter new prompt: top 5 cricket players in the world

1. Virat Kohli (India)
2. Steve Smith (Australia)
3. Kane Williamson (New Zealand)
4. Joe Root (England)
5. AB de Villiers (South Africa)

Enter new prompt: population to top 5 countries

1. China - 1,439,323,776
2. India - 1,380,004,385
3. United States - 331,002,651
4. Indonesia - 273,523,615
5. Brazil - 212,559,417

Enter new prompt: write a poem on dog

A furry friend so faithful and true
A loyal pup that loves me through and through
A four-legged friend who's always there      
To show me love and endless care

A wagging tail that always greets
A best friend that never competes
A pup who's always by my side
A bond that's never been denied

A furry friend that loves to play
A companion that's here to stay
A pup who loves to make me smile
A loyal friend that's been around for a while

A pup that's always been my friend
A bond that will never end
A loyal pup that loves me so
My furry friend, my pup, my doggo.

Enter new prompt: write a poem on cow

A cow so white and gentle,
A creature so divine,
A beauty so sublime.

Her eyes so gentle, her nose so soft,
Her udder so full and round,
Her mooing so melodic, her presence so profound.

Her coat so soft and warm,
A creature so serene,
She’s the source of life,
A blessing to be seen.

Her milk so sweet, her butter so creamy,
Her cheese so savory,
Her meat so tender,
A delicacy so hearty.

A cow so wise and kind,
A creature so divine,
She’s a blessing to us all,
A cow so white and gentle.

How to get Your Secret API?

Don’t think that getting API key is a tough task and is a barrier for you, you just need to signup. Follow the steps below:

1. Go to openai.com/api/

2. Click on signup

openai website

3. Complete SignUp with google or email or microsoft

create account

4. Now, login and go to platform.openai.com/

login

5. Click on Personal and then click on View API keys

get api key

6. Now, click on ‘Create new secret key‘ and copy the secret key

create new secret key

Conclusion

First you should install openai library, then copy code given above, and run the program that’s it. This way you can easily use ChatGPT with Python program. You can even create a GUI App just like ChatGPT. I recommend you to try more on ChatGPT as it’s another magical invention that we are seeing nowdays.

Also, you can react out me on admin@violet-cat-415996.hostingersite.com to discuss on anything you have in your mind like any idea or help with error in code.

Thank you for visiting our website.


Also Read:

Share:

Author: Yogesh Kumar