Integeration of chatgpt with python.

Integeration of chatgpt with python.

This blog provides a guide on integrating ChatGPT, a conversational AI model developed by OpenAI, with Python using the OpenAI Python library. By following these instructions, developers can easily set up interactions with ChatGPT within their Python applications.

Prerequisites:

  • Python installed on your system

  • An active OpenAI account

  • Access to the OpenAI API key

Here is the code for integeration

!pip install openai

import openai openai.api_key = 'api key'

messages = [ {"role": "system", "content": "You are a intelligent assistant."} ]

while True: message = input("User : ")

if message: messages.append( {"role": "user", "content": message}, )

chat = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages )

reply = chat.choices[0].message.content

print(f"ChatGPT: {reply}") messages.append({"role": "assistant", "content": reply})