"I want to create an AI like Chat GPT myself," but it seems too difficult to get started.
Is there anyone out there who feels that way?
With Python, Streamlit, and the OpenAI API,An AI chatbot that anyone can easily converse withYou can create
In this article,Easy to understand even for programming beginners, and explains the steps in an easy-to-understand manner.
We'll show you everything step by step, from obtaining an API key to displaying the screen and getting the AI to respond.
This content is perfect for those who want to create their own AI or want to take on the challenge as a learning experience.
What is Chatbot AI? [Chatbot AI Python]
conclusion
A chatbot AI is an AI that can be spoken to as if in a conversation with a person.
Using Python, you can create this mechanism yourself.
reason
In recent years, chatbots have been used in many places, such as LINE's automatic responses and website support.
These are AIs that understand people's questions and give appropriate responses.
In Python,AI that responds like a human using OpenAI APIcan be easily built.
With a tool called Streamlit,Apps with screensIt can also be run as
Examples
• AI chat to handle reception duties
• A tutor BOT that answers study questions
• Support chat to help you choose the product
summary
Chatbots areConversational AI appsis.
Combining Python and APIsYou can easily create your own AI.
Libraries to be used and preparation [Python chatbot preparation]
conclusion
To create a chatbot using Python, you will need the following libraries:
List of libraries used
• streamlit: Create web screens
• openai: Uses AI brain (GPT)
• python-dotenv: Manage your API keys securely
Installation Instructions
1 | pip install streamlit openai python-dotenv |
Next, create an OpenAI account and get your API key.
Acquisition page: https://platform.openai.com/account/api-keys
The obtained API key is written in the .env file.
1 | OPENAI_API_KEY=Your key |
summary
All the tools you need are available for free.
If you can write a little bit of Python, you can start developing a chatbot right away.
Let's write some code [Chatbot AI code]
conclusion
We will use Streamlit and OpenAI to create a simple chat app.
Code example
1 | import streamlit as st import openai import os from dotenv import load_dotenv load_dotenv() openai.api_key = os.getenv( "OPENAI_API_KEY" ) st.title( "Chatbot AI" ) user_input = st.text_input( "Ask your question:" ) if user_input: response = openai.ChatCompletion.create( model = "gpt-3.5-turbo" , messages = [{ "role" : "user" , "content" : user_input}] ) st.write( "AI's answer:" , response[ "choices" ][ 0 ][ "message" ][ "content" ]) |
supplement
• Don't forget to prepare the .env file.
• The app will start with streamlit run filename.py.
summary
With just a few dozen lines of code, you can create a working AI chat app.
Try it out and you'll find interacting with the AI to be very interesting.
How to improve your app [Python Chatbot Extension]
conclusion
Chatbot AI can be further improved to make it more useful.
method
The following ideas will make it easier to use.
• Save conversation history: View the previous conversation
• Adding voice input: Talk without typing
• Style Changes: Colorfully adjust the design
Code example (history added)
1 | if "messages" not in st.session_state: st.session_state.messages = [] for msg in st.session_state.messages: st.write(f "{msg['role']}: {msg['content']}" ) if user_input: st.session_state.messages.append({ "role" : "user" , "content" : user_input}) response = openai.ChatCompletion.create( model = "gpt-3.5-turbo" , messages = st.session_state.messages ) answer = response[ "choices" ][ 0 ][ "message" ][ "content" ] st.session_state.messages.append({ "role" : "assistant" , "content" : answer}) st.write( "AI answer:" , answer) |
summary
By saving history and adding functions, you can make your chatbot more practical.
Use your own ingenuity to create an app that is easy to use.
The potential of chatbot AI [Python chatbot application]
conclusion
Chatbot AI can be used for a wide range of purposes, from personal use to business.
Examples of use
• Linking with diary apps: Record your mood for the day
• Support for children's studies: Ask a question about something you don't understand
• In-house help desk: Immediate response to inquiries from employees
Examples of use by public institutions
The Ministry of Internal Affairs and Communications' materials (https://www.soumu.go.jp/johotsusintokei/whitepaper/ja/r05/pdf/n4200000.pdf) also state that
This article introduces examples of local governments using chatbots to more efficiently respond to citizens.
summary
The possibilities for using chatbot AI are endless.
Find your own uses for it and make the most of it.
Summary [Python Chatbot Summary]
• Anyone can create a chatbot using Python, Streamlit, and the OpenAI API.
• You can get started by simply installing the necessary libraries and setting an API key.
• The code is short and easy to run.
• With some refinement, it can be applied to work or hobbies.
We encourage you to try creating your own one-of-a-kind AI chatbot.