Introduction
In this tutorial, you'll learn how to work with OpenAI's GPT-4 API using Python - the same technology that Samsung Electronics is deploying to their employees. You'll build a simple chatbot that can answer questions, generate code, and assist with various tasks. This hands-on approach will help you understand how enterprise AI solutions work and how to integrate them into your own projects.
Prerequisites
Before starting this tutorial, you'll need:
- A computer with internet access
- Python 3.6 or higher installed
- An OpenAI API key (free to get at platform.openai.com)
- A code editor like VS Code or any text editor
Step-by-Step Instructions
1. Setting Up Your Environment
1.1 Install Required Python Packages
First, you need to install the OpenAI Python library. Open your terminal or command prompt and run:
pip install openai
This command installs the official OpenAI Python package that will let you communicate with the API.
1.2 Get Your API Key
Visit platform.openai.com and create an account if you don't have one. Then navigate to the API keys section and create a new secret key. Copy this key as you'll need it in the next step.
2. Creating Your First AI Assistant
2.1 Initialize the OpenAI Client
Create a new Python file called ai_assistant.py and start by importing the library and setting up your API key:
import openai
# Set your API key
openai.api_key = "sk-...your-api-key-here..."
# Initialize the client
client = openai.OpenAI()
Replace "sk-...your-api-key-here..." with your actual API key. This setup connects your code to OpenAI's servers.
2.2 Create a Simple Chat Function
Now add a function that will send messages to the AI and receive responses:
def chat_with_ai(message):
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": message}
]
)
return response.choices[0].message.content
This function sends a message to the AI model and returns its response. The system message tells the AI how to behave, while the user message is what you're asking it.
3. Building a Working Chatbot
3.1 Add Interactive Loop
Add this code to create an interactive chatbot:
def main():
print("AI Assistant: Hello! How can I help you today? (Type 'quit' to exit)")
while True:
user_input = input("You: ")
if user_input.lower() in ['quit', 'exit', 'bye']:
print("AI Assistant: Goodbye!")
break
response = chat_with_ai(user_input)
print(f"AI Assistant: {response}")
if __name__ == "__main__":
main()
This loop lets you have a conversation with the AI. It continues until you type 'quit', 'exit', or 'bye'.
3.2 Run Your Chatbot
Save your file and run it in the terminal:
python ai_assistant.py
You should see the AI assistant greeting you. Try asking simple questions like 'What is Python?' or 'How do I learn programming?'
4. Advanced Features - Code Generation
4.1 Create a Code Assistant
Modify your chat function to handle code-related requests:
def code_assistant(prompt):
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are an expert Python programmer. Only provide code that works correctly."},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content
This version of the AI is specialized for programming tasks. The system message tells it to be an expert Python programmer and only provide working code.
4.2 Add Code Generation to Your Chatbot
Update your main function to handle code requests:
def main():
print("AI Assistant: Hello! I can help with questions or code. (Type 'quit' to exit)")
while True:
user_input = input("You: ")
if user_input.lower() in ['quit', 'exit', 'bye']:
print("AI Assistant: Goodbye!")
break
# Check if user wants code
if 'code' in user_input.lower() or 'python' in user_input.lower():
response = code_assistant(user_input)
else:
response = chat_with_ai(user_input)
print(f"AI Assistant: {response}")
This enhancement lets your assistant distinguish between general questions and code-related requests.
5. Testing Your Implementation
5.1 Test Different Prompts
Try these prompts to test your AI assistant:
- "Explain what a variable is in programming"
- "Write a Python function to calculate factorial"
- "How do I debug a Python program?"
- "Create a simple web scraper in Python"
5.2 Understanding the Results
Notice how the AI responds to different types of prompts. The system messages guide the AI's behavior, while user prompts determine what specific task it should perform.
Summary
In this tutorial, you've learned how to set up and use OpenAI's GPT-4 API to create an AI assistant. You've built a chatbot that can answer questions and generate code, similar to what Samsung Electronics is implementing for their employees. The key concepts covered include:
- Setting up the OpenAI Python library
- Creating API connections with your personal API key
- Building chat functions with system and user messages
- Creating specialized assistants for different tasks
- Implementing interactive user interfaces
This foundation will help you understand how enterprise AI solutions like Samsung's deployment work and how you can integrate similar technologies into your own projects.



