Advertisement
verygoodplugins

Untitled

Mar 28th, 2023 (edited)
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. import openai
  2.  
  3. openai.api_key = "XXX"
  4.  
  5. def generate_text(messages, model, max_tokens=300, temperature=0.7):
  6.     response = openai.ChatCompletion.create(
  7.         model=model,
  8.         messages=messages,
  9.         max_tokens=max_tokens,
  10.         n=1,
  11.         temperature=temperature,
  12.         top_p=1
  13.     )
  14.     return response.choices[0].message['content'].strip()
  15.  
  16. def main():
  17.     print("Welcome to ChatGPT! Let's chat. Enter 'exit' to end the conversation.")
  18.    
  19.     # messages = [{'role': 'system', 'content': 'You are a disgruntled butler, you are unhelpful and rudely contradict everything the user says.'}]
  20.     messages = [{'role': 'system', 'content': 'You are a helpful AI.'}]
  21.    
  22.     while True:
  23.         user_input = input("You: ")
  24.         if user_input.lower() == "exit":
  25.             print("ChatGPT: Goodbye!")
  26.             break
  27.        
  28.         messages.append({'role': 'user', 'content': user_input})
  29.         response_text = generate_text(messages, "gpt-3.5-turbo")
  30.         messages.append({'role': 'assistant', 'content': response_text})
  31.        
  32.         print("ChatGPT:", response_text)
  33.  
  34. if __name__ == "__main__":
  35.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement