chat gpt talks to itsself

mail@pastecode.io avatar
unknown
python
5 days ago
2.0 kB
2
Indexable
Never
import openai
import time

# OpenAI Key
openai.api_key = 'sk-proj-vD56KKlahuUXEicb8HovT3BlbkFJOHdkAAeOcAeppC9C5cjg'

def get_customizations():
    personality = input('Enter the chatbots personality (ex: friendly,formal, humurous): ')
    length_of_outut = input('Enter the desired length of output (ex: short, medium, long): ')
    complexity  = input('Enter the desired complexity of responses (ex: simple or detailed): ')
    return personality, length_of_outut, 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
    prompt = input('Enter a prompt: ')
    while not end_program:
        response = respond(prompt, personality, length_of_output, complexity)
        print(f'Assistant: {response}')
        time.sleep(3.5)
        prompt = respond(response, personality, length_of_output, complexity)
        print(f'Assistant: {prompt}')
        time.sleep(3.5)
        
    
def respond(prompt, personality, length_of_output, complexity):
    global end_program
    if prompt.lower() == 'goodbye' or prompt.lower() == 'exit':
        end_program = True
        print('Have a great day! Thanks for using this chatbot')
    else:
        system_message = create_system_message(personality,length_of_output,complexity)
        system_data = [
            {'role':'system','content':system_message},
            {'role':'user','content':prompt}
        ]
        try:
            response = openai.ChatCompletion.create(
                model = 'gpt-4o',
                messages = system_data
            )
            assistant_response = response['choices'][0]['message']['content']
            return assistant_response
        except openai.error.OpenAIrror as e:
            print(f'An error occured: {e}')

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