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:
openaiandrequestsare imported to interact with the OpenAI API and make HTTP requests (though requests is not actually used).jsonis used for working with JSON data.configis imported to retrieve the API key (stored externally in theconfigfile).
-
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 theunitparameter 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
messageslist that tracks the conversation and atools_listthat defines two functions available to the model:get_current_weatherandget_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_orchestratoruntil 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.