
OpenAI Has a Free Tier API — Build a Chatbot in 10 Lines of Python
OpenAI gives you free API credits when you sign up. Here is how to build a working chatbot in 10 lines. The Complete Code from openai import OpenAI client = OpenAI () # uses OPENAI_API_KEY env var print ( ' Chat with AI (type " quit " to exit) ' ) history = [{ ' role ' : ' system ' , ' content ' : ' You are a helpful assistant. ' }] while True : user_input = input ( ' You: ' ) if user_input . lower () == ' quit ' : break history . append ({ ' role ' : ' user ' , ' content ' : user_input }) response = client . chat . completions . create ( model = ' gpt-3.5-turbo ' , messages = history ) reply = response . choices [ 0 ]. message . content history . append ({ ' role ' : ' assistant ' , ' content ' : reply }) print ( f ' AI: { reply } ' ) Setup (3 minutes) pip install openai export OPENAI_API_KEY = 'your_key_here' python chatbot.py Get your key at platform.openai.com/api-keys . Free tier gives you enough credits for thousands of messages. Make It Useful: Specialized Bots Change the system
Continue reading on Dev.to Python
Opens in a new tab




