Working Chatbot code

mail@pastecode.io avatar
unknown
plain_text
9 days ago
1.9 kB
2
Indexable
Never
import openai

# Set OpenAI API key (replace with your actual key)
openai.api_key = "sk-proj-vD56KKlahuUXEicb8HovT3BlbkFJOHdkAAeOcAeppC9C5cjg"

# Function to get customization options from user
def get_customizations():
    personality = input("Enter your Chatbot's personality: ")
    lengthOfOutput = input("Enter the desired length of output: ")
    complexity = input("Enter the desired complexity of the Chatbot: ")
    return personality, lengthOfOutput, complexity

# Function to create system message based on customization
def create_system_message(personality, lengthOfOutput, complexity):
    return f"Personality: {personality}. Length of output: {lengthOfOutput}. Complexity: {complexity}"

# Main function to interact with the Chatbot
def main():
    personality, lengthOfOutput, 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! Thank you for using Hari AI.")
        else:
            system_message = create_system_message(personality, lengthOfOutput, complexity)
            system_data = [
                {"role": "system", "content": system_message},
                {"role": "user", "content": get_input}
            ]
            try:
                response = openai.ChatCompletion.create(
                    model="gpt-4o",
                    messages=system_data
                )
                assistant_response = response['choices'][0]['message']['content']
                print("Assistant: " + assistant_response)
            
            except openai.OpenAIError as e:
                print(f"An error occurred: {e}")

if __name__ == "__main__":
    main()
Leave a Comment