Build CHATGPT
  Requirements:
  1. OpenAI API key (You can obtain it by signing up on the OpenAI platform)
  2. Python installed on your machine
    python
    python
  • 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}")
Put your real OpenAI API key in place of "YOUR_API_KEY." The code creates a basic loop in which the model reacts to input entered by the user. The user can keep having conversations until they write "exit."

Leave a Reply

Your email address will not be published. Required fields are marked *