├── .env ├── README.md ├── requirements.txt └── tbyp2.py /.env: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=your_openai_api_key 2 | ANTHROPIC_API_KEY=your_anthropic_api_key 3 | ELEVENLABS_API_KEY=your_elevenlabs_api_key 4 | OUTPUT_FILE_PATH=your_desired_output_file_path 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # think-before-you-speak 2 | Think Before You Speak 3 | 4 | 1. git clone https://github.com/AllAboutAI-YT/think-before-you-speak.git 5 | 2. cd think-before-you-speak 6 | 3. pip install -r requirements.txt 7 | 4. SET Your API Keys and File PATH in .env 8 | 5. SET Your Voice ID in def text_to_speech from Eleven Labs 9 | 6. Adjust Prompts 10 | 7. Run tbyp2.py 11 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | anthropic 2 | python-dotenv 3 | requests 4 | pyaudio 5 | pydub 6 | openai 7 | -------------------------------------------------------------------------------- /tbyp2.py: -------------------------------------------------------------------------------- 1 | import anthropic 2 | import os 3 | from dotenv import load_dotenv 4 | import requests 5 | import pyaudio 6 | from pydub import AudioSegment 7 | from pydub.playback import play 8 | import openai 9 | from openai import OpenAI 10 | 11 | load_dotenv() 12 | 13 | api_keys = { 14 | "openai": os.getenv("OPENAI_API_KEY"), 15 | "anthropic": os.getenv("ANTHROPIC_API_KEY"), 16 | "elevenlabs": os.getenv("ELEVENLABS_API_KEY") 17 | } 18 | client = { 19 | "openai": openai.OpenAI(api_key=api_keys["openai"]), 20 | "anthropic": anthropic.Anthropic(api_key=api_keys["anthropic"]) 21 | } 22 | 23 | NEON_GREEN = '\033[92m' 24 | CYAN = '\033[96m' 25 | YELLOW = '\033[93m' 26 | RESET_COLOR = '\033[0m' 27 | 28 | def text_to_speech(text, voice_id="YOUR VOICE ID", filename="output.mp3"): 29 | # Get the output file path from the environment variable 30 | output_file_path = os.getenv("OUTPUT_FILE_PATH") 31 | 32 | # Ensure the directory exists 33 | if not os.path.exists(output_file_path): 34 | os.makedirs(output_file_path) 35 | 36 | # Complete file path 37 | full_file_path = os.path.join(output_file_path, filename) 38 | 39 | # URL and headers for the ElevenLabs API request 40 | url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}" 41 | headers = { 42 | "Accept": "audio/mpeg", 43 | "Content-Type": "application/json", 44 | "xi-api-key": api_keys["elevenlabs"] 45 | } 46 | 47 | # Data payload for the API request 48 | data = { 49 | "text": text, 50 | "model_id": "eleven_monolingual_v1", 51 | "voice_settings": { 52 | "stability": 0.5, 53 | "similarity_boost": 0.5 54 | } 55 | } 56 | 57 | # Making a POST request to the API 58 | response = requests.post(url, json=data, headers=headers) 59 | 60 | # Check if the request was successful 61 | if response.status_code == 200: 62 | # Writing the received audio content to the specified file 63 | with open(full_file_path, 'wb') as f: 64 | f.write(response.content) 65 | return {"success": True, "message": "Audio generated successfully.", "file_path": full_file_path} 66 | else: 67 | return {"success": False, "message": "Failed to generate audio."} 68 | 69 | def play_audio(file_path): 70 | # Load the audio file 71 | audio = AudioSegment.from_mp3(file_path) 72 | 73 | # Play the audio 74 | play(audio) 75 | 76 | def openai_chat(user_input, system_message): 77 | """ 78 | Function to send a query to OpenAI's model, get the response, and print it in yellow color. 79 | Logs the conversation to a file. 80 | """ 81 | messages = [ 82 | {"role": "system", "content": system_message}, 83 | {"role": "user", "content": user_input} 84 | ] 85 | # Perform the chat completion request 86 | chat_completion = client["openai"].chat.completions.create( 87 | model="gpt-4-0125-preview", # Adjust the model as necessary 88 | messages=messages, 89 | temperature=0.7, 90 | ) 91 | # Extract the response content 92 | response_content = chat_completion.choices[0].message.content 93 | return response_content 94 | 95 | def anthropic_chat(user_input, system_message): 96 | """ 97 | Function to send a query to Anthropic's model, get the streamed response, and log it. 98 | """ 99 | # Preparing the messages payload 100 | messages = [ 101 | { 102 | "role": "user", 103 | "content": user_input 104 | } 105 | ] 106 | # Perform the chat completion request with streaming 107 | response_text = "" 108 | with client["anthropic"].messages.stream( 109 | model="claude-3-opus-20240229", # Adjust the model as necessary 110 | max_tokens=3500, 111 | temperature=0.7, 112 | messages=messages, 113 | system=system_message, 114 | ) as stream: 115 | for text in stream.text_stream: 116 | response_text += text 117 | print(NEON_GREEN + text + RESET_COLOR, end="", flush=True) 118 | # Replace '\n' with actual newline characters 119 | response_text = response_text.replace('\\n', '\n') 120 | return response_text 121 | 122 | qa = "Here is my issue: I am a 24 year old Male. In 3 months I have an opportunity to quit my job, and travel the world with some money i have saved up. The only problem is that the job market is very harsh for people in tech atm, so i dont know if i will find a job again. But I have saved for this trip for 7 years. What do you suggest I do?" 123 | 124 | system_message1 = "You are a helpful assistant" 125 | 126 | p1 = anthropic_chat(qa, system_message1) 127 | 128 | #think before you speak 129 | system_message = "You are a wise and empathetic life coach. Your role is to carefully consider the user's situation, have a inner monolouge and reflect on it as if it were your own. ALWAYS give the answers the reflects your thoughs about the problem" 130 | 131 | t1 = anthropic_chat(f"USERs Question:{qa}\n\nPicture your self in the same situation as the USER. Have an inner monolouge and reflect about your thoughts about the question and write your down thoughs, DO NOT answer the question directly:", system_message) 132 | 133 | t2 = anthropic_chat(f"USER`s Question:{qa}\n\nYour thoughts and reflections about the USERs questions:{t1}\n\nNow ONLY answer the USER`s question, give a SHORT and CLEAR answer (DONT USE *), in a personal tone based on your thoughts and reflections:", system_message) 134 | 135 | text_to_speech(t2) 136 | play_audio(full_file_path) 137 | 138 | system_message2 = "You are an expert in analysing and giving ratings to the best answers to a human from a given question" 139 | 140 | e1 = openai_chat(f"USER Question: {qa}\n\nANSWER1: {p1}\n\n ANSWER2: {t2}\n\nFrom the answers to the USERs question above, give your thoughs and rating to both ANSWER1 and ANSWER2:", system_message2) 141 | print(CYAN + e1 + RESET_COLOR) 142 | --------------------------------------------------------------------------------