├── JarvisChatGPT ├── Install.bat ├── run.bat ├── apikey.py └── main.py ├── .github ├── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md ├── LICENSE ├── CONTRIBUTING.md ├── SECURITY.md ├── README.md └── CODE_OF_CONDUCT.md └── LICENSE /JarvisChatGPT/Install.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | python install pip 3 | pip install openai==0.28.1 SpeechRecognition pyttsx3 pyaudio spotipy -------------------------------------------------------------------------------- /JarvisChatGPT/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | Echo Say "Always Listen" to turn the wake word off. 3 | Echo Say "Slient Mode" to turn the wake word on. 4 | Echo To close Jarvis just say "bye", "thats all", 5 | Echo "shut down", "exit", or "stop listening". 6 | Echo: 7 | Echo Jarvis will not hear you if you make a loud noise 8 | Echo and talk without waiting a couple seconds afterwards. 9 | Echo: 10 | Echo: Try saying: "Jarvis, are you there?" 11 | Echo: 12 | python main.py -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /JarvisChatGPT/apikey.py: -------------------------------------------------------------------------------- 1 | api_key="replace this text with your own openai API key, keep the quotes" 2 | weather_api_key="c8b138cd625d476fbdb31921231507" #it costs me nothing for you guys to use my weather API key, but if someone uses it 1 million times a month it will not work for anyone else so don't do that 3 | DEFAULT_LOCATION = 'replace this text with your default location, if left blank Jarvis will ask you for the location every time.' 4 | UNIT = "imperial" #change 'imperial' to 'metric' if you want jarvis to give you metric measurments 5 | spotify_client_id ="replace this text with your spotify developer client id" 6 | spotify_client_secret ="replace this text with your spotify developer client secret" #instructions for this are in the github description 7 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 small-cactus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 antmannacho 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to JarvisChatGPT 2 | 3 | Welcome to JarvisChatGPT! We appreciate your interest in contributing. We believe in an open and inclusive community where everyone can participate. 4 | 5 | ## Submitting Issues 6 | 7 | You can contribute by submitting issues of any kind. Whether you've found a bug, have a question, or want to suggest an improvement, we welcome all kinds of issues. 8 | 9 | To submit an issue: 10 | 1. Go to the [Issues tab](https://github.com/antmannacho/J.A.R.V.I.S.-ChatGPT-VoiceAssistant/issues) on the GitHub repository. 11 | 2. Click on the "New Issue" button. 12 | 3. Choose the type of issue (bug report, question, enhancement, etc.). 13 | 4. Provide a clear and descriptive title and description. 14 | 5. Add relevant labels and assignees, if applicable. 15 | 16 | We'll review your issue and respond as soon as possible. Your feedback helps us improve JarvisChatGPT for everyone. 17 | 18 | ## Be Respectful 19 | 20 | Maintaining a respectful and friendly environment is important to us. Treat fellow contributors with kindness and consideration. Harassment or offensive behavior will not be tolerated. 21 | 22 | Thank you for your contribution and for being a part of JarvisChatGPT's community! 23 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | I take security seriously in JarvisChatGPT to ensure the safety and privacy of our users and their data. 4 | 5 | ## Data Security 6 | 7 | - **Local Data**: All data within the JarvisChatGPT application is kept local on the user's device and is not transmitted or shared with external parties. 8 | 9 | - **OpenAI API**: Interaction with the OpenAI API for generating responses is managed securely through OpenAI's infrastructure. Data sent to the API is subject to OpenAI's own security measures and practices. 10 | 11 | - **Outgoing data**: The only data being sent outside your computer is the response to OpenAI's servers, and what song you wanted to play on Spotify through Spotify's servers. Both of these are sent securely through each of their API's and they both have user identification keys that you provided to use it. Weather information is requested through an API but no data is sent, it just asks for all weather data and Jarvis formats it later, this feature also uses an API key of my own and it is secure, everything is secure and private. 12 | 13 | - **Other features**: All other features not mentioned here are 100% local on your computer, all memory files are stored and created on device, all User and Jarvis messages are deleted after closing the app, all voice files are deleted after being played, all speech is generated on device, all recorded speech is deleted after the listening message disappears, and all calculations are not saved anywhere. 14 | 15 | - **NOTE**: If you share your folder with Jarvis, even if you deleted the api key out of the api key file, it will still show up in cache and anyone else could use it and read it, to fix this, deleted pycache from the folder and you're all set. 16 | 17 | ## Reporting Security Vulnerabilities 18 | 19 | If you discover a security vulnerability in JarvisChatGPT, I encourage you to responsibly disclose it by submitting an issue through GitHub's issue tracker. 20 | 21 | To report a security vulnerability: 22 | - Go to the [Issues tab](https://github.com/antmannacho/J.A.R.V.I.S.-ChatGPT-VoiceAssistant/issues) on the GitHub repository. 23 | - Click on the "New Issue" button. 24 | - Select the "Security Vulnerability" issue template. 25 | - Provide a clear and detailed description of the vulnerability. 26 | 27 | I will thoroughly investigate and address all reported security vulnerabilities in a timely manner. Your cooperation is invaluable in upholding the security of JarvisChatGPT. 28 | 29 | ## Responsible Disclosure 30 | 31 | I kindly request that you: 32 | - Allow me a reasonable amount of time to respond to your report before making it public. 33 | - Avoid unauthorized access, modification, or damage to other users' data. 34 | - Adhere to relevant laws and regulations when reporting security issues. 35 | 36 | Thank you for collaborating with me to maintain the security and privacy of JarvisChatGPT. Your assistance is vital to our commitment to data security. 37 | -------------------------------------------------------------------------------- /.github/README.md: -------------------------------------------------------------------------------- 1 | # 🎙️ JarvisChatGPT 2 | 3 | **JarvisChatGPT**, or simply **Jarvis**, is your cutting-edge AI voice assistant, leveraging the sophisticated technology behind ChatGPT and OpenAI's API for a user experience that far surpasses conventional assistants like Siri or Alexa. It's designed to be both simple for casual interaction and powerful for more complex tasks, reminiscent of the Jarvis AI from popular films. 4 | 5 | **Last Updated**: December 12th, 2023 (12-12-23) 6 | 7 | **Important Update**: We've moved on from the old repository and shifted our focus to a significantly improved version, known as **M.I.L.E.S**. Although the original **JarvisChatGPT** repo is not actively updated, we're still committed to addressing any issues that arise, provided there isn't an available workaround. 8 | 9 | For a better, more refined AI voice assistant experience, especially tailored for Mac users, check out our new and enhanced repository: 10 | 11 | 🌟 **[Visit the updated M.I.L.E.S repository](https://github.com/small-cactus/M.I.L.E.S)** 🌟 12 | 13 | This latest version promises an enhanced performance with an installation process that is notably straightforward on MacOS. Windows users, please be informed that while support exists, the installation process can be significantly more challenging and might not succeed in every case. We primarily recommend this for Mac users, though Windows users willing to navigate the complexities are welcome to try. 14 | 15 | **OS Support**: Optimized for MacOS. Windows compatibility exists but with potential installation challenges. 16 | 17 | By default, Jarvis utilizes the **GPT-3.5-Turbo** API model. However, if you fancy an upgrade, you can easily transition to **GPT-4** via the `main.py` file. 18 | 19 | ⚠️ **Important**: To harness the power of Jarvis, you'll need an **OpenAI API key**. Insert this key in the `apikey.py` file. 20 | 21 | ## 🚀 Quick Start 22 | 1. **Setup Python**: Ensure you have Python installed. If not, [download Python](https://www.python.org/downloads/). 23 | 2. **Install Dependencies**: Simply run `install.bat`. 24 | 3. **Launch Jarvis**: Run `Run.bat`. Should you encounter any hiccups, dive into the troubleshooting section. 25 | 4. **Have Fun!** 26 | 27 | ## 💬 Share with your friends 28 | The people that clone, download, and view jarvis are what makes this possible for me to not lose motivation. *Please share Jarvis* with a link to this page! **Everyone has permission to share this anywhere they want**, post my projects anywhere you want!! 29 | # 30 | 31 | ## 🔍 Troubleshooting 32 | - **Initial Issues**: If `Run.bat` closes abruptly, manually open a terminal in the Jarvis directory and input: 33 | ``` shell 34 | python main.py 35 | ``` 36 | - **Installation Failures**: Facing issues with `install.bat`? Manually install the required packages: 37 | ``` shell 38 | python install pip 39 | ``` 40 | Then run: 41 | ``` shell 42 | pip install openai==0.28.1 SpeechRecognition pyttsx3 pyaudio spotipy 43 | ``` 44 | - **Spotify Playback Errors**: Ensure you have an active Spotify session and check if you've entered correct Spotify credentials. Also, a premium account is a must-have or it won't work. 45 | - **Mic Troubles**: Navigate to the control panel, access sound settings, select the recording tab, and set your mic as the default device. 46 | 47 | ## 📌 Key Features & Tips 48 | - 🚫 Jarvis has **no token limit**. This means lengthier requests might have you twiddling your thumbs for a bit. 49 | - 🛠️ Dive into `main.py` for customization options. Use `CTRL + F` and search for `system_prompt =` or `startswith("jarvis")` to swiftly locate and modify system prompts or wake word configurations. 50 | - 🎤 For optimal performance, ensure the wake word, "Jarvis", starts your sentence. 51 | - 🎵 Fancy some tunes? Remember, you need Spotify Premium for Jarvis's music features. 52 | 53 | ## 🎵 Spotify Integration 54 | Follow the steps below to set up Spotify integration: 55 | 56 | ### 1️⃣ Create a Spotify Account 57 | Start by [creating or accessing your Spotify account](https://www.spotify.com/). 58 | 59 | ### 2️⃣ Access the Spotify Developer Dashboard 60 | - Navigate to the [Spotify Developer Dashboard](https://developer.spotify.com/dashboard/) and sign in. 61 | 62 | ### 3️⃣ Create a New App 63 | - After logging in, click on the "Create an App" button. 64 | - Fill in: 65 | - **App Name**: `Jarvis` 66 | - **App Description**: `Helpful voice assistant` 67 | - **Redirect URL**: `http://localhost:8080/callback` 68 | 69 | ### 4️⃣ Get Your Client ID and Client Secret 70 | - On your app's dashboard, locate your **Client ID** and enter it in the `apikey.py` file. 71 | - Click "Show Client Secret" to retrieve your **Client Secret** and add it to the `apikey.py` file as well. 72 | 73 | ### 5️⃣ Jam With Jarvis 74 | Initiate Jarvis and request your favorite songs! 75 | 76 | ## 📜 Changelog 77 | - **Weather Updates**: Jarvis now boasts a built-in weather API. 78 | - **Spotify Integration**: Dance away with Jarvis's Spotify playback feature. 79 | - **Calculator**: Jarvis will now crunch numbers for you. 80 | - **Permanent Memory**: Jarvis will remember tidbits for you. If ever needed, he can also be asked to forget. 81 | - **Timekeeper**: Ask Jarvis for the current date and time. 82 | - **Expanded Weather Data**: More comprehensive data about the skies above. 83 | 84 | ## 🤝 Contribute 85 | Your feedback is gold! Please suggest features or report issues. Let's refine JarvisChatGPT together. 86 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | . 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | -------------------------------------------------------------------------------- /JarvisChatGPT/main.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import openai 3 | import json 4 | import math 5 | import os 6 | from apikey import weather_api_key, DEFAULT_LOCATION, UNIT, spotify_client_id, spotify_client_secret 7 | from datetime import datetime 8 | 9 | def get_current_weather(location=None, unit=UNIT): 10 | """Get the current weather in a given location and detailed forecast""" 11 | if location is None: 12 | location = DEFAULT_LOCATION 13 | API_KEY = weather_api_key 14 | base_url = "http://api.weatherapi.com/v1/forecast.json" 15 | params = { 16 | "key": API_KEY, 17 | "q": location, 18 | "days": 1 19 | } 20 | 21 | response = requests.get(base_url, params=params) 22 | data = response.json() 23 | 24 | if response.status_code == 200 and 'current' in data and 'forecast' in data and data['forecast']['forecastday']: 25 | weather_info = { 26 | "location": location, 27 | "temperature": data["current"]["temp_f"], 28 | "feels_like": data["current"]["feelslike_f"], 29 | "max_temp": data['forecast']['forecastday'][0]['day']['maxtemp_f'], 30 | "min_temp": data['forecast']['forecastday'][0]['day']['mintemp_f'], 31 | "unit": "fahrenheit", 32 | "forecast": data["current"]["condition"]["text"], 33 | "wind_speed": data["current"]["wind_mph"], 34 | "wind_direction": data["current"]["wind_dir"], 35 | "humidity": data["current"]["humidity"], 36 | "pressure": data["current"]["pressure_in"], 37 | "rain_inches": data["current"]["precip_in"], 38 | "sunrise": data['forecast']['forecastday'][0]['astro']['sunrise'], 39 | "sunset": data['forecast']['forecastday'][0]['astro']['sunset'], 40 | "moonrise": data['forecast']['forecastday'][0]['astro']['moonrise'], 41 | "moonset": data['forecast']['forecastday'][0]['astro']['moonset'], 42 | "moon_phase": data['forecast']['forecastday'][0]['astro']['moon_phase'], 43 | "visibility": data["current"]["vis_miles"], 44 | "will_it_rain": data['forecast']['forecastday'][0]['day']['daily_will_it_rain'], 45 | "chance_of_rain": data['forecast']['forecastday'][0]['day']['daily_chance_of_rain'], 46 | "uv": data["current"]["uv"] 47 | } 48 | else: 49 | weather_info = { 50 | "error": "Unable to retrieve the current weather. Try again in a few seconds." 51 | } 52 | 53 | return weather_info 54 | 55 | def perform_math(operation, operands): 56 | """Perform a math operation based on the given operation and operands""" 57 | if not operands: 58 | return {"error": "No operands provided"} 59 | 60 | result = None 61 | if operation == "add": 62 | result = sum(operands) 63 | elif operation == "subtract": 64 | result = operands[0] - sum(operands[1:]) 65 | elif operation == "multiply": 66 | result = 1 67 | for op in operands: 68 | result *= op 69 | elif operation == "divide": 70 | result = operands[0] / operands[1] if operands[1] != 0 else "Undefined (division by zero)" 71 | elif operation == "power": 72 | result = pow(operands[0], operands[1]) 73 | elif operation == "square_root": 74 | result = math.sqrt(operands[0]) 75 | 76 | return {"result": result} 77 | 78 | memory_file_path = None 79 | 80 | def get_memory_file_path(): 81 | """Return the full path to the memory.txt file. Create the file if it doesn't exist.""" 82 | global memory_file_path 83 | 84 | if memory_file_path: 85 | return memory_file_path 86 | 87 | current_dir = os.path.dirname(os.path.abspath(__file__)) 88 | 89 | memory_file_path = os.path.join(current_dir, "memory.txt") 90 | 91 | if not os.path.exists(memory_file_path): 92 | with open(memory_file_path, 'w') as file: 93 | json.dump([], file) 94 | 95 | return memory_file_path 96 | 97 | def memory_manager(operation, data=None): 98 | """Store, retrieve, or clear data in a file""" 99 | file_path = get_memory_file_path() 100 | current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") 101 | 102 | if operation == "store": 103 | with open(file_path, 'r') as file: 104 | memory = json.load(file) 105 | 106 | memory.append({ 107 | "data": data, 108 | "store_time": current_time, 109 | "retrieve_time": None 110 | }) 111 | 112 | with open(file_path, 'w') as file: 113 | json.dump(memory, file) 114 | 115 | return {"message": f"Data stored successfully on {current_time}"} 116 | 117 | elif operation == "retrieve": 118 | with open(file_path, 'r') as file: 119 | memory = json.load(file) 120 | 121 | if not memory: 122 | return {"message": "No data stored yet"} 123 | 124 | for item in memory: 125 | item["retrieve_time"] = current_time 126 | 127 | with open(file_path, 'w') as file: 128 | json.dump(memory, file) 129 | 130 | retrieved_data = [{"data": item["data"], "store_time": item["store_time"], "retrieve_time": current_time} for item in memory] 131 | return {"message": f"Data retrieved on {current_time}", "data": retrieved_data} 132 | 133 | elif operation == "clear": 134 | with open(file_path, 'w') as file: 135 | json.dump([], file) 136 | return {"message": "Memory cleared successfully"} 137 | 138 | def get_current_datetime(mode="date & time"): 139 | """Get the current date and/or time""" 140 | now = datetime.now() 141 | date_str = now.strftime("%Y-%m-%d") 142 | time_str = now.strftime("%I:%M:%S %p") 143 | 144 | if mode == "date": 145 | return {"datetime": date_str} 146 | elif mode == "time": 147 | return {"datetime": time_str} 148 | else: 149 | return {"datetime": f"{date_str} {time_str}"} 150 | 151 | import openai 152 | from apikey import api_key 153 | 154 | import spotipy 155 | from spotipy.oauth2 import SpotifyOAuth 156 | 157 | sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=spotify_client_id, 158 | client_secret=spotify_client_secret, 159 | redirect_uri="http://localhost:8080/callback", 160 | scope="user-library-read user-modify-playback-state")) 161 | 162 | def search_and_play_song(song_name: str): 163 | results = sp.search(q=song_name, limit=1) 164 | if results and results['tracks'] and results['tracks']['items']: 165 | song_uri = results['tracks']['items'][0]['uri'] 166 | song_name = results['tracks']['items'][0]['name'] 167 | try: 168 | sp.start_playback(uris=[song_uri]) 169 | return {"message": f"The song '{song_name}' is now playing"} 170 | except spotipy.exceptions.SpotifyException: 171 | return {"message": "Tell the user they have to open Spotify first before you can play the song."} 172 | else: 173 | return {"message": "Sorry, I couldn't find the song you requested."} 174 | 175 | import speech_recognition as sr 176 | import pyttsx3 177 | 178 | openai.api_key = api_key 179 | 180 | engine = pyttsx3.init() 181 | 182 | voices = engine.getProperty('voices') 183 | for voice in voices: 184 | if "george" in voice.name.lower(): 185 | engine.setProperty('voice', voice.id) 186 | break 187 | 188 | system_prompt = "You are Jarvis, you are a helpful assistant, RESPOND AS CONCISE AS POSSIBLE. You can get the weather using the get_current_weather function ALWAYS format and delete uncessory weather info. You can play songs using the search_and_play_song function, if you get an error you will tell the user what the error was. You don't have to play the exact words the user gives you for a song, you can paraphrase or choose what you think fits better. ALWAYS SUMMARIZE WEATHER RESPONSE. If the user asks you a personal question, check the memory manager for stored data." 189 | conversation = [{"role": "system", "content": system_prompt}] 190 | 191 | def speak(text): 192 | engine.say(text) 193 | engine.runAndWait() 194 | 195 | def listen(): 196 | r = sr.Recognizer() 197 | with sr.Microphone() as source: 198 | print("Listening...") 199 | audio = r.listen(source) 200 | 201 | try: 202 | return r.recognize_google(audio) 203 | except: 204 | print("Didn't get that. Try again") 205 | return "" 206 | 207 | 208 | available_functions = { 209 | "search_and_play_song": search_and_play_song, 210 | "get_current_weather": get_current_weather, 211 | "get_current_datetime": get_current_datetime, 212 | "perform_math": perform_math, 213 | "memory_manager": memory_manager 214 | } 215 | 216 | conversation_history = [] 217 | 218 | def ask(question): 219 | global conversation_history 220 | 221 | if not question: 222 | return "Sorry, I didn't receive a valid query." 223 | 224 | messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": question}] 225 | if conversation_history: 226 | messages = conversation_history + [{"role": "user", "content": question}] 227 | else: 228 | conversation_history.append({"role": "system", "content": system_prompt}) 229 | 230 | functions = [ 231 | { 232 | "name": "search_and_play_song", 233 | "description": "Search for a song by name on Spotify and play it", 234 | "parameters": { 235 | "type": "object", 236 | "properties": { 237 | "song_name": { 238 | "type": "string", 239 | "description": "The name of the song to search for (can be anything, doesn't have to be exactly what the user typed)" 240 | } 241 | }, 242 | "required": ["song_name"] 243 | } 244 | }, 245 | { 246 | "name": "get_current_datetime", 247 | "description": "Get the current date and/or time", 248 | "parameters": { 249 | "type": "object", 250 | "properties": { 251 | "mode": { 252 | "type": "string", 253 | "enum": ["date", "time", "date & time"], 254 | "description": "Choose whether to get date, time, or both", 255 | } 256 | }, 257 | "required": ["mode"], 258 | }, 259 | }, 260 | { 261 | "name": "perform_math", 262 | "description": "Perform a math operation", 263 | "parameters": { 264 | "type": "object", 265 | "properties": { 266 | "operation": { 267 | "type": "string", 268 | "enum": ["add", "subtract", "multiply", "divide", "power", "square_root"], 269 | "description": "The math operation to perform" 270 | }, 271 | "operands": { 272 | "type": "array", 273 | "items": { 274 | "type": "number" 275 | }, 276 | "description": "The numbers to perform the operation on" 277 | } 278 | }, 279 | "required": ["operation", "operands"] 280 | } 281 | }, 282 | { 283 | "name": "memory_manager", 284 | "description": "Store, retrieve, or clear data in a file", 285 | "parameters": { 286 | "type": "object", 287 | "properties": { 288 | "operation": { 289 | "type": "string", 290 | "enum": ["store", "retrieve", "clear"], 291 | "description": "Operation to perform" 292 | }, 293 | "data": { 294 | "type": "string", 295 | "description": "The data to store, in this format: User asked me to remember... (required for 'store' operation)" 296 | } 297 | }, 298 | "required": ["operation"] 299 | } 300 | }, 301 | { 302 | "name": "get_current_weather", 303 | "description": "Get the current weather and condition data for any given location, eg moon phases, rain %, rainfall measurement, wind and pressure data, and more, defaults to clearwater FL.", 304 | "parameters": { 305 | "type": "object", 306 | "properties": { 307 | "location": { 308 | "type": "string", 309 | "description": "The city and state, e.g. Clearwater, FL" 310 | }, 311 | "unit": { 312 | "type": "string", 313 | "enum": ["celsius", "fahrenheit"] 314 | } 315 | }, 316 | "required": [] 317 | } 318 | } 319 | ] 320 | 321 | response = openai.ChatCompletion.create( 322 | model="gpt-3.5-turbo", 323 | messages=messages, 324 | functions=functions, 325 | function_call="auto", 326 | ) 327 | response_message = response["choices"][0]["message"] 328 | 329 | conversation_history.append(response_message) 330 | 331 | if response_message.get("function_call"): 332 | function_name = response_message["function_call"]["name"] 333 | function_args = json.loads(response_message["function_call"]["arguments"]) 334 | function_to_call = available_functions[function_name] 335 | function_response = function_to_call(**function_args) 336 | 337 | if not function_response: 338 | function_response = "Sorry, I couldn't fetch the data." 339 | elif isinstance(function_response, dict) and 'error' in function_response: 340 | function_response = function_response['error'] 341 | 342 | messages.append( 343 | { 344 | "role": "function", 345 | "name": function_name, 346 | "content": json.dumps(function_response), 347 | } 348 | ) 349 | response = openai.ChatCompletion.create( 350 | model="gpt-3.5-turbo", 351 | messages=messages, 352 | ) 353 | 354 | conversation_history.append(response["choices"][0]["message"]) 355 | 356 | return response["choices"][0]["message"]["content"] 357 | def reply(question): 358 | response = ask(question) 359 | 360 | print("User:", question) 361 | print("Jarvis:", response) 362 | speak(response) 363 | 364 | return response 365 | 366 | def handle_special_commands(query): 367 | if "always listen" in query.lower(): 368 | print("Jarvis is now always listening") 369 | return False 370 | elif "silent mode" in query.lower(): 371 | print("Jarvis is now in silent mode") 372 | return True 373 | return None 374 | 375 | def is_break_command(query): 376 | return any(keyword in query.lower() for keyword in ["bye", "thats all", "shutdown", "shut down", "exit", "stop listening"]) 377 | 378 | if __name__ == '__main__': 379 | use_wake_word = True 380 | 381 | while True: 382 | query = listen() 383 | if query: 384 | if is_break_command(query): 385 | break 386 | 387 | special_command_result = handle_special_commands(query) 388 | if special_command_result is not None: 389 | use_wake_word = special_command_result 390 | elif use_wake_word and query.lower().startswith("jarvis"): 391 | ans = reply(query) 392 | elif not use_wake_word: 393 | ans = reply(query) --------------------------------------------------------------------------------