My working code

 avatar
unknown
plain_text
a year ago
1.8 kB
11
Indexable
import openai
from openai import OpenAIError  # Import the OpenAIError class directly

# Set your OpenAI API key here
openai.api_key = "sk-proj-vD56KKlahuUXEicb8HovT3BlbkFJOHdkAAeOcAeppC9C5cjg"

def get_customizations():
    personality = input("Enter the chatbot's personality (e.g., friendly, formal, humorous): ")
    length_of_output = input("Enter the desired length of output (e.g., short, medium, long): ")
    complexity = input("Enter the desired complexity of responses (e.g., simple, detailed): ")
    return personality, length_of_output, complexity

def create_system_message(personality, length_of_output, complexity):
    return f"Personality: {personality}. Length of output: {length_of_output}. Complexity: {complexity}."

def main():
    personality, length_of_output, complexity = get_customizations()
    end_program = False

    while not end_program:
        get_input = input("Enter a prompt: ")
        if get_input.lower() == "goodbye" or get_input.lower() == "exit":
            end_program = True
            print("Have a great day!")
        else:
            system_message = create_system_message(personality, length_of_output, complexity)
            system_data = [
                {"role": "system", "content": system_message},
                {"role": "user", "content": get_input}
            ]
            try:
                response = openai.ChatCompletion.create(
                    model="gpt-4",
                    messages=system_data
                )
                assistant_response = response['choices'][0]['message']['content']
                print("Assistant: " + assistant_response)
            except OpenAIError as e:
                print(f"An error occurred: {e}")

if __name__ == "__main__":
    main()
Editor is loading...
Leave a Comment