Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import openai
- openai.api_key = "XXX"
- def generate_text(messages, model, max_tokens=300, temperature=0.7):
- response = openai.ChatCompletion.create(
- model=model,
- messages=messages,
- max_tokens=max_tokens,
- n=1,
- temperature=temperature,
- top_p=1
- )
- return response.choices[0].message['content'].strip()
- def main():
- print("Welcome to ChatGPT! Let's chat. Enter 'exit' to end the conversation.")
- # messages = [{'role': 'system', 'content': 'You are a disgruntled butler, you are unhelpful and rudely contradict everything the user says.'}]
- messages = [{'role': 'system', 'content': 'You are a helpful AI.'}]
- while True:
- user_input = input("You: ")
- if user_input.lower() == "exit":
- print("ChatGPT: Goodbye!")
- break
- messages.append({'role': 'user', 'content': user_input})
- response_text = generate_text(messages, "gpt-3.5-turbo")
- messages.append({'role': 'assistant', 'content': response_text})
- print("ChatGPT:", response_text)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement