Historical Python learning example: this article uses older library and API interfaces and has not been validated as a current installation guide. Check the current documentation before running it. Speech recognition may send audio to an external service, conversation history is stored locally by this sample, and model responses still need verification.
Ever dreamed of having your own personal AI assistant like Tony Stark‘s J.A.R.V.I.S from the Iron Man movies? Python is one way to explore that idea. This historical example outlines a voice-based AI assistant inspired by J.A.R.V.I.S.
The example connects microphone input, speech recognition, generated replies and a local conversation log using Python code.
AI assistant: Meet J.A.R.V.I.S
This example is inspired by J.A.R.V.I.S (Just A Rather Very Intelligent System), the fictional assistant. The sample illustrates a voice-conversation loop; it does not implement a general-purpose task assistant.

How J.A.R.V.I.S Works:
- Listening to You: The sample captures audio from the default microphone using SpeechRecognition and calls its Google recognition service to convert speech to text. This recognition step sends audio to an external service.
- Generating a Response: The sample sends recognized text and stored conversation content to OpenAI through an older Python API interface. The model generates a reply from that input; it can misunderstand a request or produce incorrect information.
- Engaging in Dialogue: The sample reads the generated reply aloud with pyttsx3. Its SAPI5 driver is Windows-specific. Spoken output does not make a reply more reliable.
- Remembering Conversations: The sample loads and saves a local JSON history. Its request-building code labels every saved message as a user message, including earlier assistant replies, so it does not preserve the original conversation roles when sending history to the model.
Saving Conversations
After a successful model response, the sample appends the user input and reply to conversation_history.json. The file contains readable conversation text, and saved content is included in later API requests. Avoid recording sensitive information and manage the file accordingly.
The sample uses the process’s current working directory for conversation_history.json. Launch location matters; placing the Python file in a separate folder does not by itself determine where the history is written.
Keep your API key outside the source code. Load it from your local environment and never publish it.
Historical Python code:
import os
import json
import speech_recognition as sr
import openai
import pyttsx3
# Set up your OpenAI API key
openai.api_key = os.environ['OPENAI_API_KEY']
# Initialize the recognizer
recognizer = sr.Recognizer()
# Initialize the text-to-speech engine with SAPI5 driver (Windows specific)
engine = pyttsx3.init(driverName='sapi5')
# Set properties for the text-to-speech engine (optional)
engine.setProperty('rate', 150) # Speed of speech
engine.setProperty('volume', 1) # Volume level (0.0 to 1.0)
# Get the current working directory
script_dir = os.getcwd()
# File to store conversation history
CONVERSATION_FILE = os.path.join(script_dir, 'conversation_history.json')
# Load conversation history from file
def load_conversation_history():
if os.path.exists(CONVERSATION_FILE):
with open(CONVERSATION_FILE, 'r') as f:
return json.load(f)
return []
# Save conversation history to file
def save_conversation_history(conversation_memory):
with open(CONVERSATION_FILE, 'w') as f:
json.dump(conversation_memory, f)
# Load conversation history
conversation_memory = load_conversation_history()
# Function to listen to the microphone and convert speech to text
def listen_microphone():
with sr.Microphone() as source:
print("Listening...")
recognizer.adjust_for_ambient_noise(source) # Adjust for ambient noise
audio = recognizer.listen(source) # Listen to microphone input
try:
text = recognizer.recognize_google(audio) # Recognize speech using Google Speech Recognition
print("You said:", text)
return text.lower()
except sr.UnknownValueError:
print("Sorry, I couldn't understand what you said.")
return ""
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return ""
# Function to generate response using ChatGPT API
def generate_response(user_input):
global conversation_memory
messages = [{"role": "user", "content": m["content"]} for m in conversation_memory]
messages.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=100,
temperature=0.7,
n=1,
stop=None,
)
result = response.choices[0].message.content
conversation_memory.append({"role": "user", "content": user_input})
conversation_memory.append({"role": "assistant", "content": result})
# Save conversation history to file
save_conversation_history(conversation_memory)
return result
# Main loop
while True:
print("How can I assist you?")
user_input = listen_microphone()
if user_input:
# Generate response
try:
jarvis_reply = generate_response(user_input)
print("J.A.R.V.I.S:", jarvis_reply)
# Convert text response to speech and play it
engine.say(jarvis_reply)
engine.runAndWait()
except Exception as e:
print("Error generating response:", e)
continue # Skip saving conversation history if there's an error
Taking It Further
Possible next steps include preserving message roles, limiting stored history and adding a clear way to stop listening. Review any generated code suggestions before using them and test changes in an isolated environment.
This example can help explain how speech recognition, a language-model API and text-to-speech fit together. It needs compatibility and behavior checks before it can serve as a working assistant.
Treat it as a starting point for learning, rather than a ready-to-run assistant.



