AI agents are programs designed to interact with their environment, make decisions, and perform tasks autonomously. They often use APIs to retrieve or send data.
- Chatbots for customer support
- Personal assistants like Siri or Alexa
- Agents for automating tasks like email sorting or scheduling meetings
- Autonomy: Agents can perform tasks without constant user input
- Interactivity: They interact with humans or other systems (like APIs)
- Decision-Making: They can make decisions based on data or user inputs
OpenAI API provides powerful models that can understand and generate human language. It allows developers to build apps that leverage natural language processing (NLP), text generation, and more.
- Text completion: Generate text based on a given prompt
- Conversational agents: Create bots that can engage in human-like conversations
- Code generation: Generate code snippets based on instructions
- Writing assistants (e.g., Grammarly-like apps)
- Personalized recommendations
- Automated customer service agents
Make an API call using Python (or another language), send a prompt, and receive a response.
- Basic knowledge of Python (variables, functions)
- An OpenAI account and API key (signup at OpenAI API)
Ensure you have Python installed on your machine, along with pip to install libraries.
pip install openai
Create a .env
file in the root of your project and add your OpenAI API key:
OPENAI_API_KEY=sk-proj-1111
pip install python-dotenv
from dotenv import load_dotenv
load_dotenv()
- GPT-4o Our high-intelligence flagship model for complex, multi-step tasks
- GPT-4o mini Our affordable and intelligent small model for fast, lightweight tasks
- o1-preview and o1-mini Language models trained with reinforcement learning to perform complex reasoning.
- GPT-4 Turbo and GPT-4 The previous set of high-intelligence models
- GPT-3.5 Turbo A fast, inexpensive model for simple tasks
- DALL·E A model that can generate and edit images given a natural language prompt
- TTS A set of models that can convert text into natural sounding spoken audio
- Whisper A model that can convert audio into text
- Embeddings A set of models that can convert text into a numerical form
- Moderation A fine-tuned model that can detect whether text may be sensitive or unsafe
- GPT base A set of models without instruction following that can understand as well as generate natural language or code
- This is a simple chatbot that uses the OpenAI API to generate responses to user prompts.
- It uses the
gpt-4o-mini
model. - It uses the
conversation_history
to keep track of the chat history. - It uses the
client.chat.completions.create
method to generate responses.
At the most basic level, we are sending a prompt to the model and receiving a response in a while loop until the user types 'exit'.
- This is a simple agent that uses the OpenAI API to generate responses to user prompts.
- We create a class called
SimpleAgent
that has agenerate_response
method that takes a prompt and returns a response. - We give it a personality and a system prompt.
- It uses the
gpt-4o-mini
model. - It uses the
conversation_history
to keep track of the chat history. - It uses the
client.chat.completions.create
method to generate responses.
- This is a simple TTS app that uses the OpenAI API to convert text to speech.
- It uses the
tts-1
model. - It uses the
client.audio.speech.create
method to generate speech. - It uses the
speech_file_path
to save the speech to a file. - It uses the
response.stream_to_file
method to save the speech to a file.