Sure! Here is an example code snippet in Python that uses the OpenAI API to interact with ChatGPT:
This code assumes you have already installed OpenAI’s python client using pip:
```
pip install openai
```
Now the sample code:
```
import openai
import os
openai.api_key = os.getenv(“OPENAI_API_KEY”)
def prompt_chat(): prompt_message = { ‘role’: ‘system’, ‘content’: ‘You are a helpful assistant.’} messages = [prompt_message]
while True: message_content = input(“User: “) user_message = {‘role’:‘user’, ‘content’: message_content} messages.append(user_message) response = openai.ChatCompletion.create( model=“gpt-3.5-turbo”, messages=messages ) assistant_message = response[‘choices’]0[‘message’][‘content’] messages.append({‘role’:‘assistant’, ‘content’: assistant_message}) print(“Assistant: “, assistant_message)You will need to replace `os.getenv(“OPENAI_API_KEY”)` with your secret key that you get from OpenAI:
```
openai.api_key = ‘your-api-key‘
```
Remember to keep your API key secure. Do not expose it in public repositories or in shared code.