{"id":97529,"date":"2026-11-12T22:35:49","date_gmt":"2026-11-12T15:35:49","guid":{"rendered":"https:\/\/itsystems.vn\/?p=97529"},"modified":"2026-11-19T15:46:19","modified_gmt":"2026-11-19T08:46:19","slug":"ai-chatbot-python-openai","status":"publish","type":"post","link":"https:\/\/itsystems.vn\/en\/ai-chatbot-python-openai\/","title":{"rendered":"Build a Simple AI Chatbot With Python and OpenAI"},"content":{"rendered":"<p><!-- its-entranslate-20261119 --><\/p>\n<div>\n<p><strong>Quick answer \u2014 build a simple AI chatbot with Python and OpenAI:<\/strong> (1) install the library: <code>pip install openai<\/code>; (2) get an <strong>API key<\/strong> from your OpenAI account and store it in an environment variable (never hard-code it); (3) call the Chat Completions API: create a client, send a list of <code>messages<\/code> (system\/user roles) and read the response; (4) wrap it in a loop to keep chatting, storing the conversation history so the bot &#8220;remembers&#8221; context; (5) add a <strong>system prompt<\/strong> to set its role and tone; (6) handle errors and limit costs (cap tokens and requests). Next steps: add a web interface (Flask\/Streamlit) or RAG to answer from your own documents. Keep your API key secret and monitor API costs. It&#8217;s a great starting point for automating customer support.<\/p>\n<\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_91 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">The content of the article<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itsystems.vn\/en\/ai-chatbot-python-openai\/#Build_a_simple_AI_chatbot_with_Python_and_OpenAI\" >Build a simple AI chatbot with Python and OpenAI<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itsystems.vn\/en\/ai-chatbot-python-openai\/#What_is_an_AI_chatbot_and_why_build_one\" >What is an AI chatbot and why build one?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itsystems.vn\/en\/ai-chatbot-python-openai\/#Set_up_your_Python_development_environment\" >Set up your Python development environment<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itsystems.vn\/en\/ai-chatbot-python-openai\/#OpenAI_and_its_API\" >OpenAI and its API<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itsystems.vn\/en\/ai-chatbot-python-openai\/#Build_the_chatbot_step_by_step\" >Build the chatbot step by step<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itsystems.vn\/en\/ai-chatbot-python-openai\/#Extending_your_chatbot\" >Extending your chatbot<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/itsystems.vn\/en\/ai-chatbot-python-openai\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itsystems.vn\/en\/ai-chatbot-python-openai\/#Contact_IT_Systems_Vietnam\" >Contact IT Systems Vietnam<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/itsystems.vn\/en\/ai-chatbot-python-openai\/#Does_this_keep_happening_in_your_office\" >Does this keep happening in your office?<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Build_a_simple_AI_chatbot_with_Python_and_OpenAI\"><\/span>Build a simple AI chatbot with Python and OpenAI<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>AI chatbots are increasingly popular for improving communication and customer interaction, and building one is no longer difficult. This guide walks you through creating a simple chatbot with Python and OpenAI step by step.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_an_AI_chatbot_and_why_build_one\"><\/span>What is an AI chatbot and why build one?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An <strong>AI chatbot<\/strong> is an application that talks with people through messages using artificial intelligence. Chatbots can automate many tasks, saving businesses time and resources.<\/p>\n<ul>\n<li><strong>Uses:<\/strong> customer service, e-commerce, answering questions, providing product information and more.<\/li>\n<li><strong>Benefits:<\/strong> businesses use chatbots to streamline work and improve customer experience; individuals can build one for fun or to help with daily tasks.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Set_up_your_Python_development_environment\"><\/span>Set up your Python development environment<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><strong>Install Python and pip:<\/strong> install Python from the official website; pip, Python&#8217;s package manager, is installed with it.<\/li>\n<li><strong>Create a virtual environment:<\/strong> run <code>python -m venv env<\/code>, then activate it with <code>source env\/bin\/activate<\/code> on macOS\/Linux or <code>env\\Scripts\\activate<\/code> on Windows.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"OpenAI_and_its_API\"><\/span>OpenAI and its API<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>OpenAI is an AI research company whose API lets developers integrate advanced language models into their applications.<\/p>\n<ul>\n<li><strong>The models:<\/strong> OpenAI&#8217;s large language models (the GPT family) let a chatbot understand questions and reply in natural language. The model line-up changes often, so check OpenAI&#8217;s documentation to choose one that balances quality and cost.<\/li>\n<li><strong>Get an API key:<\/strong> sign in to OpenAI&#8217;s API dashboard, create a key and store it in the <code>OPENAI_API_KEY<\/code> environment variable. Never put the key directly in your code or push it to GitHub.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Build_the_chatbot_step_by_step\"><\/span>Build the chatbot step by step<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><strong>Step 1:<\/strong> Install the library: <code>pip install openai<\/code>.<\/p>\n<p><strong>Step 2:<\/strong> Create <code>chatbot.py<\/code> with the following code:<\/p>\n<pre><code>import os\nfrom openai import OpenAI\n\nclient = OpenAI(api_key=os.environ[\"OPENAI_API_KEY\"])\nMODEL = os.getenv(\"OPENAI_MODEL\", \"gpt-4o-mini\")\n\nmessages = [{\"role\": \"system\", \"content\": \"You are a helpful IT support assistant.\"}]\n\nwhile True:\n    user_input = input(\"You: \")\n    if user_input.lower() in (\"exit\", \"quit\"):\n        break\n    messages.append({\"role\": \"user\", \"content\": user_input})\n    response = client.chat.completions.create(model=MODEL, messages=messages, max_tokens=500)\n    reply = response.choices[0].message.content\n    messages.append({\"role\": \"assistant\", \"content\": reply})\n    print(\"Bot:\", reply)<\/code><\/pre>\n<p><strong>Step 3:<\/strong> Run <code>python chatbot.py<\/code> and start chatting. The <code>messages<\/code> list stores the conversation so the bot remembers context; the system prompt sets its role and tone.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Extending_your_chatbot\"><\/span>Extending your chatbot<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><strong>Web interface:<\/strong> use Flask or Streamlit to build a browser chat UI.<\/li>\n<li><strong>Answers from your own documents (RAG):<\/strong> combine search over internal documents so the bot answers accurately from your business data.<\/li>\n<li><strong>Cost control:<\/strong> limit tokens, the amount of history kept and the number of requests; monitor usage in OpenAI&#8217;s dashboard.<\/li>\n<li><strong>Error handling:<\/strong> catch connection and rate-limit errors and retry sensibly.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>With Python and the OpenAI API, you can build a simple AI chatbot in a few dozen lines of code \u2014 a great starting point for automating customer support or building an internal assistant. Keep your API key secret and keep an eye on costs.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Contact_IT_Systems_Vietnam\"><\/span>Contact IT Systems Vietnam<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Contact <a href=\"https:\/\/itsystems.vn\/en\/\">IT Systems<\/a> at hotline <strong>028.3995.03.59<\/strong> or <a href=\"mailto:contact@itsystems.com.vn\">contact@itsystems.com.vn<\/a>. Follow us on <a href=\"https:\/\/www.facebook.com\/ITSystems.VIETNAM\" target=\"_blank\" rel=\"noopener\">Facebook<\/a> and <a href=\"https:\/\/www.youtube.com\/@ITSYSTEMS\" target=\"_blank\" rel=\"noopener\">YouTube<\/a>.<\/p>\n<p><!-- its-topic-cta-20261112 --><\/p>\n<section class=\"its-post-cta its-topic-cta\">\n<h2><span class=\"ez-toc-section\" id=\"Does_this_keep_happening_in_your_office\"><\/span>Does this keep happening in your office?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The IT Systems Helpdesk logs requests as tickets, responds remotely within 30 minutes and comes on site in Ho Chi Minh City and Hanoi when needed.<\/p>\n<p><a href=\"https:\/\/itsystems.vn\/en\/it-services-for-businesses\/it-helpdesk-services\/\">IT Helpdesk services<\/a> \u00b7 <a href=\"tel:+842839950359\">Call +84 28 3995 0359<\/a><\/p>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Quick answer \u2014 build a simple AI chatbot with Python and OpenAI: (1) install the library: pip install openai; (2) get an API key from your OpenAI account and store it in an environment variable (never hard-code it); (3) call the Chat Completions API: create a client, send a list of messages (system\/user roles) and [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":58879,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_focus_keyword":"ai chatbot python openai","rank_math_title":"Build a Simple AI Chatbot With Python and OpenAI | IT Systems","rank_math_description":"Build a simple AI chatbot with Python and OpenAI: install the library, use an API key safely, call Chat Completions, keep conversation history and add a system prompt.","rank_math_robots":"","rank_math_canonical_url":"","rank_math_schema":"","footnotes":""},"categories":[2146],"tags":[],"class_list":["post-97529","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-it-knowledge"],"wpml_current_locale":"en_US","wpml_translations":{"vi_VN":{"locale":"vi_VN","id":58877,"slug":"chatbot-ai-don-gian-voi-python-va-openai-huong-dan-chi-tiet","post_title":"Chatbot AI \u0110\u01a1n Gi\u1ea3n v\u1edbi Python v\u00e0 OpenAI: H\u01b0\u1edbng D\u1eabn Chi Ti\u1ebft","href":"https:\/\/itsystems.vn\/chatbot-ai-don-gian-voi-python-va-openai-huong-dan-chi-tiet\/"}},"_links":{"self":[{"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/posts\/97529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/comments?post=97529"}],"version-history":[{"count":2,"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/posts\/97529\/revisions"}],"predecessor-version":[{"id":102425,"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/posts\/97529\/revisions\/102425"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/media\/58879"}],"wp:attachment":[{"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/media?parent=97529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/categories?post=97529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsystems.vn\/en\/wp-json\/wp\/v2\/tags?post=97529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}