help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
openai-gpt-chatbot
RSK World
openai-gpt-chatbot
OpenAI GPT Chatbot - GPT-3 + GPT-4 + ChatGPT + Streaming + Token Tracking + Flask + Python
openai-gpt-chatbot
  • __pycache__
  • static
  • templates
  • .env.example1.9 KB
  • .gitignore566 B
  • DOCUMENTATION.md17.6 KB
  • LICENSE1.2 KB
  • README.md8.9 KB
  • RELEASE_NOTES_v1.0.0.md5.5 KB
  • app.py10.9 KB
  • chatbot.py16.9 KB
  • config.py2.8 KB
  • env_example.txt1.9 KB
  • example_usage.py6.1 KB
  • personas.py5.1 KB
  • requirements.txt202 B
  • setup.py1.1 KB
example_usage.py
example_usage.py
Raw Download
Find: Go to:
"""
Example Usage of OpenAI GPT Chatbot

Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Year: 2026

This file demonstrates various ways to use the OpenAI GPT Chatbot.
"""

from chatbot import GPTChatbot
from config import Config
import os

# Author: RSK World (https://rskworld.in) - Year: 2026
def example_basic_usage():
    """
    Basic usage example
    
    Author: RSK World (https://rskworld.in) - Year: 2026
    """
    print("=" * 60)
    print("Example 1: Basic Usage")
    print("=" * 60)
    
    # Initialize chatbot
    # Author: RSK World (https://rskworld.in) - Year: 2026
    chatbot = GPTChatbot(model=Config.GPT3_MODEL)
    
    # Simple conversation
    response = chatbot.get_response("Hello! What can you do?")
    print(f"Assistant: {response}\n")


def example_gpt4_usage():
    """
    GPT-4 usage example
    
    Author: RSK World (https://rskworld.in) - Year: 2026
    """
    print("=" * 60)
    print("Example 2: Using GPT-4")
    print("=" * 60)
    
    # Initialize with GPT-4
    # Author: RSK World (https://rskworld.in) - Year: 2026
    chatbot = GPTChatbot(model=Config.GPT4_MODEL)
    chatbot.set_system_prompt("You are an expert Python programmer.")
    
    response = chatbot.get_response(
        "Explain list comprehensions in Python",
        temperature=0.7,
        max_tokens=300
    )
    print(f"Assistant: {response}\n")


def example_conversation_context():
    """
    Conversation with context example
    
    Author: RSK World (https://rskworld.in) - Year: 2026
    """
    print("=" * 60)
    print("Example 3: Conversation with Context")
    print("=" * 60)
    
    # Author: RSK World (https://rskworld.in) - Year: 2026
    chatbot = GPTChatbot(model=Config.DEFAULT_MODEL)
    
    # Multi-turn conversation
    messages = [
        "My name is John and I love programming.",
        "What programming languages do you recommend?",
        "Can you give me a simple example in Python?"
    ]
    
    for msg in messages:
        print(f"You: {msg}")
        response = chatbot.get_response(msg)
        print(f"Assistant: {response}\n")
    
    # View conversation history
    print("Conversation History:")
    for msg in chatbot.get_conversation_history():
        print(f"  {msg['role']}: {msg['content'][:50]}...")


def example_custom_system_prompt():
    """
    Custom system prompt example
    
    Author: RSK World (https://rskworld.in) - Year: 2026
    """
    print("=" * 60)
    print("Example 4: Custom System Prompt")
    print("=" * 60)
    
    # Author: RSK World (https://rskworld.in) - Year: 2026
    chatbot = GPTChatbot(model=Config.DEFAULT_MODEL)
    chatbot.set_system_prompt(
        "You are a helpful coding assistant created by RSK World. "
        "Always provide clear, concise answers with code examples when relevant."
    )
    
    response = chatbot.get_response("How do I create a Python function?")
    print(f"Assistant: {response}\n")


def example_save_load_conversation():
    """
    Save and load conversation example
    
    Author: RSK World (https://rskworld.in) - Year: 2026
    """
    print("=" * 60)
    print("Example 5: Save and Load Conversation")
    print("=" * 60)
    
    # Author: RSK World (https://rskworld.in) - Year: 2026
    chatbot = GPTChatbot(model=Config.DEFAULT_MODEL)
    
    # Have a conversation
    chatbot.get_response("Hello!")
    chatbot.get_response("Tell me a fun fact about Python.")
    
    # Save conversation
    filename = "example_conversation.json"
    chatbot.save_conversation(filename)
    print(f"Conversation saved to {filename}")
    
    # Create new chatbot and load conversation
    new_chatbot = GPTChatbot(model=Config.DEFAULT_MODEL)
    new_chatbot.load_conversation(filename)
    
    print(f"Loaded conversation with {len(new_chatbot.get_conversation_history())} messages")
    
    # Continue conversation
    response = new_chatbot.get_response("Tell me another fact!")
    print(f"Assistant: {response}\n")


def example_different_temperatures():
    """
    Different temperature settings example
    
    Author: RSK World (https://rskworld.in) - Year: 2026
    """
    print("=" * 60)
    print("Example 6: Different Temperature Settings")
    print("=" * 60)
    
    # Author: RSK World (https://rskworld.in) - Year: 2026
    question = "Write a short creative story about AI."
    
    temperatures = [0.3, 0.7, 1.2]
    
    for temp in temperatures:
        chatbot = GPTChatbot(model=Config.DEFAULT_MODEL)
        chatbot.clear_history()
        
        response = chatbot.get_response(question, temperature=temp, max_tokens=200)
        print(f"\nTemperature {temp}:")
        print(f"{response[:150]}...\n")


def main():
    """
    Main function to run examples
    
    Author: RSK World (https://rskworld.in) - Year: 2026
    """
    print("\n" + "=" * 60)
    print("OpenAI GPT Chatbot - Usage Examples")
    print("Created by RSK World (https://rskworld.in)")
    print("Year: 2026")
    print("=" * 60 + "\n")
    
    # Check if API key is set
    # Author: RSK World (https://rskworld.in) - Year: 2026
    if not Config.validate():
        print("Error: OPENAI_API_KEY not set!")
        print("Please set your API key in .env file or as environment variable.")
        print("For more information, visit: https://rskworld.in")
        return
    
    try:
        # Run examples
        # Author: RSK World (https://rskworld.in) - Year: 2026
        example_basic_usage()
        example_gpt4_usage()
        example_conversation_context()
        example_custom_system_prompt()
        example_save_load_conversation()
        example_different_temperatures()
        
        print("\n" + "=" * 60)
        print("All examples completed!")
        print("Visit https://rskworld.in for more projects.")
        print("=" * 60)
        
    except Exception as e:
        print(f"\nError running examples: {e}")
        print("For support, contact: help@rskworld.in")


if __name__ == "__main__":
    main()

212 lines•6.1 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer