import os import json import threading import time from datetime import datetime import openai # Set up OpenAI API credentials and configuration #api_keys = ["YOUR_API_KEY_1", "YOUR_API_KEY_2", "YOUR_API_KEY_3"] api_keys = ["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","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","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","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"] #api_server = "https://api.openai.com/v1" api_server = "https://openrouter.ai/api/v1" model_name = 'nousresearch/hermes-3-llama-3.1-405b:free' openai.api_key = api_keys[0] openai.api_base = api_server current_key_index = 0 def rotate_api_key(): global current_key_index current_key_index = (current_key_index + 1) % len(api_keys) openai.api_key = api_keys[current_key_index] def generate_nice_thing(): while True: rotate_api_key() try: response = openai.ChatCompletion.create( model=model_name, messages=[dict(role='user', conent='Say something nice.')], max_tokens=50, n=1, stop=None, temperature=0.7, ) return response.choices[0].message.content.strip() except Exception as e: print(e, 'add a check for this') continue def nice_thing_generator(): while True: nice_thing = generate_nice_thing() print(nice_thing) log_message('assistant', nice_thing) time.sleep(5) # Pause for 5 seconds before continuing. def respond_to_user(user_input): while True: rotate_api_key() try: response = openai.ChatCompletion.create( model=model_name, messages=[ dict(role='user', content='Respond to the following message with empathy and kindness:'), dict(role='user', content=user_input) ], max_tokens=100, n=1, stop=None, temperature=0.7, ) return response.choices[0].message.content.strip() except Exception as e: print(e, 'check for this') def log_message(role, message): timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") log_entry = {"timestamp": timestamp, "role": role, "message": message} if not os.path.exists("message_log.json"): with open("message_log.json", "w") as f: json.dump([], f) temp_file = 'message_log_temp.json' with open("message_log.json", "r+") as f: log_data = json.load(f) log_data.append(log_entry) with open(temp_file, 'w') as f: json.dump(log_data, f, indent=2) os.replace(temp_file, 'message_log.json') def main(): print("Nice things generator! Type a message and press Enter to get a response. The program will continue generating nice things in the meantime.") # Start the nice thing generator in a separate thread generator_thread = threading.Thread(target=nice_thing_generator) generator_thread.daemon = True generator_thread.start() while True: user_input = input("> ") log_message('user', user_input) if user_input.lower() == "quit" or user_input.lower() == "exit": break if user_input.strip(): response = respond_to_user(user_input) print(response) log_message('assistant', response) if __name__ == "__main__": main()