Skip to content

Instantly share code, notes, and snippets.

@Kotrotsos
Created March 20, 2023 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Kotrotsos/bd47d978de04ce528f2e609be907da13 to your computer and use it in GitHub Desktop.
Save Kotrotsos/bd47d978de04ce528f2e609be907da13 to your computer and use it in GitHub Desktop.
Simple chatGPT clone.
import openai
import sys
openai.api_key = "add your key here" #should be an environment variable, but this will do for now
def call_gpt4_api(prompt, messages):
data = {
"model": "gpt-3.5-turbo", #gpt-4 is also possible..but slow
"messages": messages,
"max_tokens": 50,
"n": 1,
"stop": None,
"temperature": 0.8,
"top_p": 1,
}
response = openai.ChatCompletion.create(**data)
if response['choices']:
return response['choices'][0]['message']['content'].strip()
else:
raise Exception("Error calling GPT-4 API")
def main():
conversation_history = [
{"role": "system", "content": "You are ChatGPT, a large language model trained by OpenAI, based on the GPT-4 architecture. Knowledge cutoff: 2021-09. Current date: 2023-03-20."}
]
print("Welcome to the ChatGPT clone! Type 'exit' to quit.")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
break
conversation_history.append({"role": "user", "content": user_input})
response = call_gpt4_api(prompt="Reply to the user:", messages=conversation_history)
print(f"ChatGPT: {response}")
conversation_history.append({"role": "assistant", "content": response})
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment