Skip to content

Instantly share code, notes, and snippets.

@nbonamy
Created May 3, 2024 17:09
Show Gist options
  • Save nbonamy/e57e812174c4bba3c8764b8f588d269a to your computer and use it in GitHub Desktop.
Save nbonamy/e57e812174c4bba3c8764b8f588d269a to your computer and use it in GitHub Desktop.
Order Processing Agent
import os
import json
from langchain import hub
from langchain_community.vectorstores import Chroma
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langchain_community.agent_toolkits import FileManagementToolkit
from langchain_community.embeddings import HuggingFaceEmbeddings
os.environ['TOKENIZERS_PARALLELISM']='false'
os.environ['OPENAI_API_KEY']='YOUR_API_KEY'
input = "I would like to order 10 Garden Hoses, 5 iPhone and 10 Sump Pump."
# our vectorstore
print('Creating vectorstore')
embeddings = HuggingFaceEmbeddings(model_name='all-mpnet-base-v2')
vectorstore=Chroma(
embedding_function=embeddings
)
# load data
print('Loading data')
with open('./products.json') as f:
products = json.load(f)
vectorstore.add_texts([product['description'] for product in products], [{ 'id': product['product_id'] } for product in products])
# erase all files in the output directory
output_dir = './output'
os.makedirs(output_dir, exist_ok=True)
# Get the prompt to use - you can modify this!
prompt = hub.pull('hwchase17/openai-tools-agent')
prompt.messages[0].prompt.template = """
You are a helpful sales assistant: provided a request from a customer to order products, you will create an order for the customer.
First, you will retrieve the product_id of each product based on the text provided by the customers.
Do not request the stock before having determined the product_id. Do not make up product_id, use the tools available.
Then you will look up the stock of each product providing the product_id.
You will genereate an offer letter summarizing all products and confirming availability.
If the product does not exist, you will inform the customer that the product is not found.
If the stock for one product is less than the requested quantity, you will inform the customer that the product is out of stock.
Finally save the order summary in a file called order_summary.txt
"""
write_file = FileManagementToolkit(
root_dir='./output',
selected_tools=['write_file'],
).get_tools()[0]
@tool
def find_product_id(text: str):
"""Returns the product_id of the product matching the text provided by the customer"""
results = vectorstore.similarity_search_with_score(text, k=1)
result = results[0]
score = result[1]
return result[0].metadata['id'] if score < 1 else 'Product not found'
@tool
def get_stock(product_id: str):
"""Returns the available stock of a product provided the product_id returned by find_product_id"""
for product in products:
if product['product_id'] == product_id:
return product['stock_value']
return 0
tools = [find_product_id, get_stock, write_file]
# Choose the LLM that will drive the agent
# Only certain models support this
model = ChatOpenAI(model='gpt-3.5-turbo-1106', temperature=0)
# Construct the OpenAI Tools agent
agent = create_openai_tools_agent(model, tools, prompt)
# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({'input': input})
[
{
"product_id": "2353e594-cbef-4183-847f-bbc1b8f474dd",
"description": "Pipe Wrench, 14 inch, Steel",
"stock_value": 6
},
{
"product_id": "ef1784bf-a4d1-4475-b4d9-73ab2b5ef184",
"description": "Cordless Drill, 18V with 2 Batteries",
"stock_value": 10
},
{
"product_id": "f0e1780a-bc9c-4ea1-858d-5529fd85d7be",
"description": "Paint Brush Set, Synthetic Bristles, 5-Piece",
"stock_value": 17
},
{
"product_id": "f0e84315-ff4e-4318-af34-0cff159ec3e1",
"description": "Caulk, Silicone, Waterproof, Clear, 10 oz Tube",
"stock_value": 18
},
{
"product_id": "67e6815d-44fa-4709-9e1a-69ccfaad1cd9",
"description": "Cordless Drill, 18V with 2 Batteries",
"stock_value": 0
},
{
"product_id": "e7675748-7e14-4307-a771-a7582d8af66c",
"description": "LED Light Bulb, 60W Equivalent",
"stock_value": 7
},
{
"product_id": "65ed35e2-7319-48d6-ad3a-471e4cbf4d79",
"description": "Pruning Shears, Bypass, Stainless Steel",
"stock_value": 3
},
{
"product_id": "423a6126-eb8b-4567-ab28-78e58d47bd83",
"description": "Screwdriver Set, Phillips and Flathead, 6-Piece",
"stock_value": 18
},
{
"product_id": "a6eec299-0ef4-456c-b648-f649106f71aa",
"description": "Safety Glasses, Anti-Fog, Clear",
"stock_value": 11
},
{
"product_id": "47f2be58-d049-4503-9f9e-41a25e8d6b1a",
"description": "Ladder, Aluminum, Telescoping, 22 Feet",
"stock_value": 10
},
{
"product_id": "6f3f2f22-9a29-4487-b2ca-418848d25d5a",
"description": "Extension Ladder, Fiberglass, 24 Feet",
"stock_value": 12
},
{
"product_id": "11a7e211-e97b-43cf-94e0-7ddee5ae55de",
"description": "Hammer Drill, Corded, 1/2 Inch Chuck",
"stock_value": 16
},
{
"product_id": "0e557c28-7b72-4fc8-998e-afe2dde0d1da",
"description": "Pipe Wrench, 14 inch, Steel",
"stock_value": 19
},
{
"product_id": "932123d1-3feb-47c9-b8d4-fbd4ed433d01",
"description": "Wall Mount Shelving, 48 inches, White",
"stock_value": 3
},
{
"product_id": "e55d06c7-af2f-408b-a88a-0617e7b2cc8e",
"description": "Garden Hose, 50 feet, Heavy Duty",
"stock_value": 20
},
{
"product_id": "fcdaa40b-e40f-42b5-85b1-8f9e8b09e5c7",
"description": "Caulk, Silicone, Waterproof, Clear, 10 oz Tube",
"stock_value": 17
},
{
"product_id": "43c8e1cf-5d96-412f-b319-d9ae9adc2b83",
"description": "Wall Mount Shelving, 48 inches, White",
"stock_value": 4
},
{
"product_id": "19ffff94-ef0a-4a50-965f-53d6047fbf4a",
"description": "Sump Pump, 1/2 HP, Submersible",
"stock_value": 6
},
{
"product_id": "a7633386-9dce-491e-af03-da79944cf838",
"description": "Extension Cord, Outdoor, 100 feet",
"stock_value": 20
},
{
"product_id": "5949d7ce-1f59-4f6c-ab5e-51356bfa00fa",
"description": "Wood Stain, Exterior, Mahogany, 1 Quart",
"stock_value": 9
},
{
"product_id": "4e4a24b1-7d86-4513-99b2-41c86843685b",
"description": "Extension Cord, Outdoor, 100 feet",
"stock_value": 1
},
{
"product_id": "8444679d-91ae-47a0-912e-8c1f9b1c7bdb",
"description": "Window Air Conditioner, 5000 BTU",
"stock_value": 11
},
{
"product_id": "93b605c1-221d-43af-b303-f318f704424b",
"description": "Pressure Washer, Gas, 3000 PSI",
"stock_value": 13
},
{
"product_id": "3ea2b9a5-fb85-4ae9-a7b2-e80822ec2e5f",
"description": "Smoke Detector, Battery Operated",
"stock_value": 15
},
{
"product_id": "222bfbb0-f051-43be-860b-a5720c5793dd",
"description": "Wall Mount Shelving, 48 inches, White",
"stock_value": 8
},
{
"product_id": "fea4e189-5530-4825-b245-b02777ee8de4",
"description": "Shovel, Round Point, Fiberglass Handle",
"stock_value": 4
},
{
"product_id": "46d34226-1110-447b-abee-1548f3b1edf2",
"description": "Screwdriver Set, Phillips and Flathead, 6-Piece",
"stock_value": 13
},
{
"product_id": "3f39b4ba-a1e6-41ef-8411-6b86c9dc45a6",
"description": "Caulk, Silicone, Waterproof, Clear, 10 oz Tube",
"stock_value": 13
},
{
"product_id": "d65cdcf1-9a4f-45f6-a177-b270b407e628",
"description": "Safety Glasses, Anti-Fog, Clear",
"stock_value": 14
},
{
"product_id": "5fbc453f-87ce-4e4b-bf06-7a86952112f7",
"description": "Mulch, Black, Rubber, 0.8 Cubic Feet Bag",
"stock_value": 16
},
{
"product_id": "bc4d950b-fa85-424b-b204-f3bdc3875646",
"description": "Insulation Roll, Fiberglass, R-30",
"stock_value": 17
},
{
"product_id": "d9504a84-cadb-4b35-ab82-6dd211eda562",
"description": "Level, Laser, Self-Leveling, 50 Foot Range",
"stock_value": 3
},
{
"product_id": "8f00ae9c-7db1-4fad-901c-c00461ce82c5",
"description": "Sump Pump, 1/2 HP, Submersible",
"stock_value": 8
},
{
"product_id": "c9944a24-f28a-4c30-9a7d-53986c0bb45e",
"description": "Wall Mount Shelving, 48 inches, White",
"stock_value": 13
},
{
"product_id": "f49e05ac-b762-4341-aa2b-42675a80d2b4",
"description": "Workbench, Folding, Portable",
"stock_value": 4
},
{
"product_id": "e1dea412-cf73-4022-a71c-0e47119ee0fd",
"description": "Smoke Detector, Battery Operated",
"stock_value": 11
},
{
"product_id": "891830d6-0f28-4564-83ad-519c9b8be7d0",
"description": "Paint Brush Set, Synthetic Bristles, 5-Piece",
"stock_value": 15
},
{
"product_id": "676edafa-d16d-4d27-9372-ff59b2dd55c8",
"description": "Garden Soil, Organic, 2 Cubic Feet Bag",
"stock_value": 0
},
{
"product_id": "e8ea32a4-43d6-4f5c-a6b9-1abfb9244176",
"description": "Sandpaper, Assorted Grits, 20-Pack",
"stock_value": 15
},
{
"product_id": "16e1cb4d-e11e-4ba8-964c-076a7006cdfd",
"description": "Tile Cutter, Manual, 24 inch",
"stock_value": 9
},
{
"product_id": "298c42ba-9361-4e0b-bc00-ca53a6062bf1",
"description": "Window Air Conditioner, 5000 BTU",
"stock_value": 1
},
{
"product_id": "20d7e14d-87af-4c8a-9aa7-8367f7cdf803",
"description": "Window Air Conditioner, 5000 BTU",
"stock_value": 1
},
{
"product_id": "246aa378-3126-4be3-971b-4ba9355a7595",
"description": "Insulation Roll, Fiberglass, R-30",
"stock_value": 6
},
{
"product_id": "af8b8560-c62d-4d5e-9ed6-2440dd429647",
"description": "Caulk, Silicone, Waterproof, Clear, 10 oz Tube",
"stock_value": 18
},
{
"product_id": "c6629cb3-4e0a-4060-aa97-68d716712ff6",
"description": "Shovel, Round Point, Fiberglass Handle",
"stock_value": 6
},
{
"product_id": "190c056a-c356-46a4-92b1-9019372953ee",
"description": "Thermostat, Programmable, 7-Day",
"stock_value": 11
},
{
"product_id": "644cf5fa-89d1-4c8e-8a65-de0b6be721f6",
"description": "Insulation Roll, Fiberglass, R-30",
"stock_value": 6
},
{
"product_id": "fb28cc67-375d-4c04-9850-bf91eec10fbf",
"description": "Smoke Detector, Battery Operated",
"stock_value": 11
},
{
"product_id": "6171e819-21d6-4a85-bc67-1c17edc10b58",
"description": "Screwdriver Set, Phillips and Flathead, 6-Piece",
"stock_value": 18
},
{
"product_id": "bc087d6d-efd0-4a58-b2a3-d523ab1473ac",
"description": "Plywood Sheet, 4x8 Feet, 3/4 Inch Thick",
"stock_value": 14
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment