Chapter 4: Making J.A.R.V.I.S. Smart – Chatbot Brain & AI Mode
A great assistant doesn’t just respond—it understands. In this chapter, you’ll upgrade J.A.R.V.I.S. to become conversational using basic logic and optional AI integration.
---
4.1 Smart Responses with Logic
Let’s begin by adding a simple chatbot-style response system.
elif "how are you" in command:
print("J.A.R.V.I.S.: Functioning at full capacity, sir. How about you?")
elif "i'm fine" in command or "i am fine" in command:
print("J.A.R.V.I.S.: Good to hear that, sir.")
elif "thank you" in command:
print("J.A.R.V.I.S.: Always at your service.")
This gives your assistant a sense of flow—it feels like a conversation.
---
4.2 Adding Randomized Replies (Personality Upgrade)
To make it more natural, let’s give J.A.R.V.I.S. multiple possible answers.
import random
elif "hello" in command:
responses = ["Hello, sir.", "Greetings.", "At your service, Stark."]
print("J.A.R.V.I.S.:", random.choice(responses))
Now he won’t sound like a broken robot repeating the same line.
---
4.3 Optional: Real AI Using OpenAI API
If you want to connect J.A.R.V.I.S. to ChatGPT (requires internet and API key):
Step 1: Install required library
pip install openai
Step 2: Use this function:
import openai
openai.api_key = "your-api-key"
def chat_with_ai(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
return response['choices'][0]['message']['content']
Step 3: Integrate into your loop:
elif "ai" in command:
ask = input("Ask J.A.R.V.I.S. (AI): ")
reply = chat_with_ai(ask)
print("J.A.R.V.I.S.:", reply)
> Note: Don’t share your API key, and always keep it safe.
---
4.4 Custom Chat Mode (Without AI)
Want to simulate intelligence without APIs? Try keyword trees:
elif "what can you do" in command:
print("J.A.R.V.I.S.: I can tell you the time, open websites, give system info, and talk a little too.")
elif "who created you" in command:
print("J.A.R.V.I.S.: I was created by Tony Stark. Just kidding… You did, sir. Respect.")
---
Challenge: Add Memory
You can store user inputs or favorite websites using a simple dictionary or file.
---
What’s Next?
In Chapter 5, J.A.R.V.I.S. gets practical.
You’ll build:
A to-do list system
File search
Notepad features
you are not just coding you are engineering.