This Python code defines a simple conversational agent that uses OpenAI's API to interact with a user. Here's a breakdown of what the code does:
-
Imports:
openai
andrequests
are imported to interact with the OpenAI API and make HTTP requests (though requests is not actually used).json
is used for working with JSON data.config
is imported to retrieve the API key (stored externally in theconfig
file).
-
API Client Initialization:
- The OpenAI client is created using the API key stored in
config.YOUR_API_KEY
. The model used is"gpt-4o-2024-05-13"
.
- The OpenAI client is created using the API key stored in
-
Weather Function:
get_current_weather
: This function takes a location (e.g., city) and returns a mock weather response for a few hardcoded cities (Tokyo, Rome, Paris). It ignores theunit
parameter and returns a simple JSON string with the location and temperature.
-
Vacation Function:
get_vacation
: This function checks for hardcoded user names (john
,mary
,bob
) and returns mock vacation day information as a JSON string. If the user's name isn't recognized, it asks for their name.
-
Agent Orchestrator:
agent_orchestrator
: This function orchestrates the conversation by sending user prompts to the OpenAI API.- It creates a
messages
list that tracks the conversation and atools_list
that defines two functions available to the model:get_current_weather
andget_vacation
. - The agent sends the user prompt to the OpenAI API, and if the API suggests calling one of the functions, the relevant function is executed based on the model's tool call.
- After the function is called, the agent continues the conversation with the updated response from the model.
-
Main Function:
main
: This is the main entry point for the program. It starts a loop where the user can input prompts. The agent responds usingagent_orchestrator
until the user types "exit" to quit.
The code creates a chatbot using OpenAI's GPT model that can:
- Return mock weather information for a few cities.
- Provide mock vacation day information for a few users. The chatbot can invoke these functions during a conversation if relevant, enhancing the chat with dynamic information.