Requirements:
- OpenAI API key (You can obtain it by signing up on the OpenAI platform)
- Python installed on your machine
- import openai
# Set your OpenAI API key openai.api_key = 'YOUR_API_KEY'
- def chat_with_gpt(prompt): # Define the model and parameters model = "text-davinci-003" # or use the latest version like "text-davinci-004" temperature = 0.7 max_tokens = 150
- # Generate a response from the model response = openai.Completion.create( engine=model, prompt=prompt, temperature=temperature, max_tokens=max_tokens )
- return response.choices[0].text.strip()
- # Example conversation loop print("ChatGPT-4: Hello! How can I assist you today?") while True: user_input = input("You: ") if user_input.lower() == 'exit': print("ChatGPT-4: Goodbye!") break else: # Include the user's input as a prompt for the model prompt = f"You: {user_input}\nChatGPT-4:" response = chat_with_gpt(prompt) print(f"ChatGPT-4: {response}")