{"slug": "how-to-build-an-ai-assistant-in-termux-with-python", "title": "How to Build an AI Assistant in Termux With Python", "summary": "A developer published a step-by-step guide for building a simple AI assistant on an Android phone using Termux and Python. The tutorial walks through installing Python and the OpenAI package, storing an API key in a .env file with python-dotenv, and running a terminal loop that sends user questions to the gpt-5-mini model via the Responses API. The guide notes that forcing a plain text output format prevents the model from returning only internal reasoning and leaving output_text empty.", "body_md": "You can build a simple AI assistant directly from your Android phone using [Termux](https://terminaltools.blogspot.com/p/termux-tutorial-comprehensive-guide-to.html) and Python.\n\nYou 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.\n\nIn this guide, we will build a simple version from scratch and run it inside Termux.\n\nBefore starting, make sure you have:\n\nWe will use Python to handle the conversation and send requests to the AI API.\n\nFirst, update Termux packages:\n\n```\npkg update && pkg upgrade\n```\n\nThen install Python. If you want a deeper walkthrough of this step, see our guide on [how to install and use Python in Termux for beginners](https://terminaltools.blogspot.com/2025/09/how-to-install-and-use-python-in-termux.html):\n\n```\npkg install python\n```\n\nCheck that it was installed correctly:\n\n```\npython --version\n```\n\nYou should see the installed Python version.\n\nNext, create a directory for the project:\n\n```\nmkdir ai-assistant\ncd ai-assistant\n```\n\nFor this example, we can use the OpenAI Python package to communicate with the API.\n\nInstall it with:\n\n```\npip install openai\n```\n\nWe also need a way to keep the API key outside the Python code. Install `python-dotenv`:\n\n```\npip install python-dotenv\n```\n\nThis keeps your credentials separate from the main script.\n\nCreate a `.env` file:\n\n```\nnano .env\n```\n\nAdd your API key:\n\n```\nOPENAI_API_KEY=your_api_key_here\n```\n\nReplace `your_api_key_here` with your actual API key.\n\nSave the file and exit nano.\n\nDo not share this file publicly or upload it to GitHub. An exposed API key can be used by someone else and may result in unexpected API usage. If you want to go further and build good security habits around credentials in general, our guide on [building a cyber security plan for a small project or business](https://terminaltools.blogspot.com/2025/08/cyber-security-plan-for-small-business.html) is a useful next read.\n\nNow create the Python file:\n\n```\nnano assistant.py\n```\n\nAdd this code:\n\n``` python\nimport os\nfrom dotenv import load_dotenv\nfrom openai import OpenAI\n\nload_dotenv()\n\nclient = OpenAI(api_key=os.getenv(\"OPENAI_API_KEY\"))\n\nprint(\"AI Assistant\")\nprint(\"Type 'exit' to quit.\\n\")\n\nwhile True:\n    question = input(\"You: \")\n\n    if question.lower() == \"exit\":\n        print(\"Goodbye!\")\n        break\n\n    try:\n        response = client.responses.create(\n            model=\"gpt-5-mini\",\n            input=question,\n            text={\"format\": {\"type\": \"text\"}}\n        )\n\n        print(f\"AI: {response.output_text}\\n\")\n\n    except Exception as e:\n        print(f\"Error: {e}\\n\")\n```\n\nThe script does a few simple things.\n\nIt loads the API key from the `.env` file, creates an API client, waits for your input, sends the question to the AI model, and prints the response.\n\nThe `text={\"format\": {\"type\": \"text\"}}` argument is worth calling out: with the Responses API, `gpt-5-mini` will sometimes return only internal reasoning and leave `output_text` empty. Forcing a plain text output format avoids that and ensures you consistently get a visible reply.\n\nThe `while` loop keeps the assistant running until you type:\n\n```\nexit\n```\n\nStart the program with:\n\n```\npython assistant.py\n```\n\nYou should see something similar to:\n\n```\nAI Assistant\nType 'exit' to quit.\n\nYou:\n```\n\nYou can now type a question:\n\n```\nYou: Explain Python functions in simple terms\n```\n\nThe assistant sends the question to the AI model and displays the response in Termux.\n\nYou can continue asking questions without restarting the program.\n\nThe basic assistant is intentionally simple, but you can build on it.\n\nFor example, you could add conversation history so the assistant remembers previous messages during the current session.\n\nYou could also add commands for specific tasks, such as:\n\n```\n/help\n/clear\n/exit\n```\n\nAnother useful addition would be voice input and text-to-speech, turning the terminal program into something closer to a voice assistant.\n\nYou could even connect it to other Termux features so the assistant can work with files, run approved commands, or automate tasks — similar in spirit to what we covered in [how to run AI coding agents on Termux in 2026](https://terminaltools.blogspot.com/2026/09/run-ai-coding-agents-on-termux-2026.html), or the general project ideas in [3 easy Termux projects you can build on Android](https://terminaltools.blogspot.com/2025/07/quick-termux-projects-you-can-do.html).\n\nThe important part is that Termux is not limited to running simple Python scripts. It can provide a useful environment for experimenting with AI applications directly from Android.\n\nBuilding an AI assistant in Termux does not require a large project. A small Python script and an AI API are enough to get started.\n\nOnce the basic version works, you can gradually add memory, commands, voice features, file handling, and other capabilities.\n\nWhat would you add to your own Termux AI assistant first?", "url": "https://wpnews.pro/news/how-to-build-an-ai-assistant-in-termux-with-python", "canonical_source": "https://dev.to/terminaltools/how-to-build-an-ai-assistant-in-termux-with-python-4278", "published_at": "2026-09-17 18:46:54+00:00", "updated_at": "2026-09-17 18:52:53.961630+00:00", "lang": "en", "topics": ["ai-tools", "ai-products", "developer-tools", "large-language-models"], "entities": ["Termux", "Python", "OpenAI", "gpt-5-mini", "python-dotenv"], "alternates": {"html": "https://wpnews.pro/news/how-to-build-an-ai-assistant-in-termux-with-python", "markdown": "https://wpnews.pro/news/how-to-build-an-ai-assistant-in-termux-with-python.md", "text": "https://wpnews.pro/news/how-to-build-an-ai-assistant-in-termux-with-python.txt", "jsonld": "https://wpnews.pro/news/how-to-build-an-ai-assistant-in-termux-with-python.jsonld"}}