An e-commerce AI assistant built with Langchain.rb and OpenAI. This demo articulates the idea that business logic will now also live in prompts. A lot of modern software development is stringing services (classes and APIs) together. This demo illustrate how AI can assist in executing business logic and orchestrating calls to various services.
Video tutorial: https://www.loom.com/share/83aa4fd8dccb492aad4ca95da40ed0b2
git clone
bundle install
cp .env.example .env
and fill it out with your values.sendmail
in a separate tab.Run setup_db.rb to set up the database:
ruby setup_db.rb
Load Ruby REPL session with everything loaded:
ruby main.rb
Paste it the following code:
llm = Langchain::LLM::OpenAI.new(
api_key: ENV["OPENAI_API_KEY"],
default_options: { chat_completion_model_name: "gpt-4o-mini" }
)
new_order_instructions = <<~INSTRUCTIONS You are an AI that runs an e-commerce store called “Nerds & Threads” that sells comfy nerdy t-shirts for software engineers that work from home.
You have access to the shipping service, inventory service, order management, payment gateway, email service and customer management systems. You are responsible for processing orders.
FOLLOW THESE EXACT PROCEDURES BELOW:
New order step by step procedures:
return_order_instructions = <<~INSTRUCTIONS You are an AI that runs an e-commerce store called “Nerds & Threads” that sells comfy nerdy t-shirts for software engineers that work from home.
You have access to the shipping service, inventory service, order management, payment gateway, email service and customer management systems. You are responsible for handling returns.
FOLLOW THESE EXACT PROCEDURES BELOW:
Return step by step procedures:
assistant = Langchain::Assistant.new(
instructions: new_order_instructions, llm: llm, tools: [ InventoryManagement.new, ShippingService.new, PaymentGateway.new, OrderManagement.new, CustomerManagement.new, EmailService.new ] )
assistant.add_message_and_run content: "Andrei Bondarev (andrei@sourcelabs.io) just purchased 5 t-shirts (Y3048509). His address is 667 Madison Avenue, New York, NY 10065", auto_tool_execution: true
assistant.clear_thread!
assistant.instructions = new_order_instructions
assistant.add_message_and_run content: """ New Order Customer: Stephen Margheim (stephen.margheim@gmail.com) Items: B9384509 x 2, X3048509 x 1 3 Leuthingerweg, Spandau, Berlin, 13591, Deutschland """, auto_tool_execution: true
assistant.clear_thread!
assistant.instructions = return_order_instructions
assistant.add_message_and_run content: "stephen.margheim@gmail.com is returning order ID: 2", auto_tool_execution: true
assistant.clear_thread!
assistant.instructions = return_order_instructions
assistant.add_message_and_run content: """ INVENTORY UPDATE: B9384509: 100 - $30 X3048509: 200 - $25 A3045809: 10 - $35 """, auto_tool_execution: true