How to Build an AI Assistant in Termux With Python
You can build a simple AI assistant directly from your Android phone using Termux and Python. You do not need a computer or a complicated development setup. With a Python script, an AI API, and a few packages, you can create an assistant that accepts your questions and returns AI-generated responses from the terminal. In this guide, we will build a simple version from scratch and run it inside…
Creating an artificial intelligence assistant on your Android device using Termux and Python is a straightforward process that does not require any additional hardware or complex setup. By utilizing a Python script, an AI API, and a few necessary packages, you can develop an assistant that accepts user inquiries and provides AI-generated answers through the terminal interface. In this article, we will walk you through the steps to construct a basic version of this assistant and execute it within the Termux environment.
To begin, ensure that you have installed the Termux application and possess basic proficiency in using the terminal. Additionally, you should have Python installed and have obtained an API key from a reputable AI service provider. We will leverage Python to manage the conversation flow and make requests to the AI API.
Before proceeding, it is essential to update Termux packages by executing `pkg update && pkg upgrade`. Subsequently, install Python using the command `pkg install python`. Verify the installation by running `python --version`, which should display the version of Python installed on your device.
Next, create a dedicated directory for your project by running `mkdir ai-assistant` and navigate into it using `cd ai-assistant`. To handle sensitive information securely, such as your API key, install the `python-dotenv` package with `pip install python-dotenv`. This package allows you to store API keys in a `.env` file, keeping them separate from your main script to enhance security.
Create a `.env` file by executing `nano .env` and add your API key under the line `OPENAI_API_KEY = your_api_key_here`. Replace `your_api_key_here` with your actual API key provided by the AI service. Upon saving the file and exiting, refrain from sharing this file publicly or uploading it to any public repository, as exposing an API key can lead to unauthorized access and potential misuse. For further security measures, consider exploring our guide on creating a cyber security plan for small projects or businesses.
Now, create your Python script file named `assistant.py` by executing `nano assistant.py`. Inside this file, add the following code:
```python
import os
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
print("AI Assistant")
print("Type exit to quit.\n")
while True:
question = input("You: ")
if question.lower() == "exit":
print("Goodbye!")
break
try:
response = client.responses.create(
model="gpt-5-mini",
input=question,
text={"format": {"type": "text"}}
)
print(f"AI: {response.output_text}\n")
except Exception as e:
print(f"Error: {e}\n")
```
This script performs several key functions. It first loads the API key from the `.env` file, then initializes an API client. It awaits your input, sends the question to the AI model for processing, and subsequently displays the AI's response. The `text={ format : { type : text }}` argument ensures that you consistently receive a plain text response from the AI, avoiding instances where only internal reasoning is returned.
The while loop ensures that the assistant remains active, waiting for your next question until you decide to exit by typing "exit".
Once you have entered the `assistant.py` file and pasted the provided code, save the file and exit the nano editor. To initiate the assistant, enter `python assistant.py` in your terminal. You should observe a welcome message followed by an instruction prompt instructing you to type "exit" to terminate the program.
Upon entering your first question, the assistant will send the query to the AI model and display the generated response in the terminal. This process allows you to continuously engage with the assistant by inputting new queries without needing to restart the program. If you wish to enhance the assistant's functionality, consider adding features such as maintaining conversation history, implementing specific commands like `/help`, `/clear`, or `/exit`, integrating voice input, enabling text-to-speech functionality, or connecting the assistant to other Termux features for file manipulation, command execution, or automated tasks.
Ultimately, Termux provides a versatile platform for experimenting with AI applications directly from your Android device. While building a simple AI assistant is achievable with just a small Python script and access to an AI API, you can progressively expand its capabilities by incorporating additional features and functionalities tailored to your needs.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
This story
This is one outlet's version. Read the fullest account.
