Skip to content

rsarosh/ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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:

  1. Imports:

    • openai and requests 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 the config file).
  2. 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".
  3. 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 the unit parameter and returns a simple JSON string with the location and temperature.
  4. 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.
  5. 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 a tools_list that defines two functions available to the model: get_current_weather and get_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.
  6. 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 using agent_orchestrator until the user types "exit" to quit.

Summary:

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages