Skip to content

Instantly share code, notes, and snippets.

@rhenter
Last active April 6, 2025 14:32
Show Gist options
  • Save rhenter/d0f776198102c04f3c61c05100508323 to your computer and use it in GitHub Desktop.
Save rhenter/d0f776198102c04f3c61c05100508323 to your computer and use it in GitHub Desktop.
Creating a Simple Chatbot using OpenAI API using Python
# Rename this file to .env and replace the value below with your actual API key
OPENAI_API_KEY="your-api-key-here"

Simple Chatbot using OpenAI API with Python

This is a basic example of how to build a command-line chatbot using the OpenAI API and Python.

Requirements

This project requires:

Setup

1. Clone or Create Your Project Folder

Create a new folder where you want to run this chatbot example.

2. Add the Required Files

Create or copy the following files into your project folder:

  • chatbot.py – Main Python script
  • requirements.txt – Lists required Python packages
  • .env – Stores your API key (you can start with the .env.example provided)

3. Install Dependencies

Install the required packages using pip:

$ pip install -r requirements.txt

4. Add Your OpenAI API Key

Create a .env file (or rename .env.example), and add your API key:

OPENAI_API_KEY="your-api-key-here"

How to Run

To start the chatbot, run the following command:

$ python chatbot.py

Then just start chatting! Type exit to quit.


Example

Welcome to Chatbot!
You: Hello!
Assistant: Hi there! How can I assist you today?

License

This project is open source and available under the MIT License.


Let me know if you want to add sections like screenshots, architecture diagram, or deployment instructions later!
from typing import Dict, List
import openai
from .setttings import settings
openai_client = openai.Client(api_key=settings.OPENAI_API_KEY)
def get_gpt_response(
current_messages: List[Dict],
model: str = settings.OPENAI_CHATGPT_MODEL,
max_tokens: int = settings.OPENAI_CHATGPT_MAX_TOKENS,
temperature: int = settings.OPENAI_CHATGPT_TEMPERATURE,
stream: bool = settings.OPENAI_CHATGPT_STREAM_RESPONSE,
) -> List:
response = openai_client.chat.completions.create(
messages=current_messages,
model=model,
max_tokens=max_tokens,
temperature=temperature,
stream=stream
)
if stream:
full_response = ''
print('Assistant: ', end='')
for chunk in response:
text = chunk.choices[0].delta.content
if text:
full_response += text
print(text, end='')
print()
else:
full_response = response.choices[0].message.content
print(full_response)
current_messages.append({'role': 'assistant', 'content': full_response})
return current_messages
if __name__ == '__main__':
print(
'Welcome to Chatbot! \n'
'Assistant: Hi there! How can I assist you today?')
messages = []
while True:
question = input('User: ')
if not question or question in ['quit', 'exit']:
print('Assistant: See you later!')
break
messages.append({'role': 'user', 'content': question})
messages = get_gpt_response(messages)
openai
prettyconf
from prettyconf import config
class Settings:
OPENAI_API_KEY = config('OPENAI_API_KEY', default='')
OPENAI_CHATGPT_MODEL = config('OPENAI_CHATGPT_MODEL', default='gpt-3.5-turbo-0125')
OPENAI_CHATGPT_MAX_TOKENS = config('OPENAI_CHATGPT_MAX_TOKENS', default=1000, cast=int)
OPENAI_CHATGPT_TEMPERATURE = config('OPENAI_CHATGPT_TEMPERATURE', default=0, cast=int)
OPENAI_CHATGPT_STREAM_RESPONSE = config('OPENAI_CHATGPT_STREAM_RESPONSE', default=True, cast=config.boolean)
settings = Settings()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment