|
| 1 | +import OpenAI from "openai"; |
| 2 | +import { ObjectQL } from '@objectql/core'; |
| 3 | +import { KnexDriver } from '@objectql/driver-sql'; |
| 4 | +import dotenv from 'dotenv'; |
| 5 | + |
| 6 | +dotenv.config(); |
| 7 | + |
| 8 | +// Tool Definitions |
| 9 | +const productTools: OpenAI.Chat.Completions.ChatCompletionTool[] = [ |
| 10 | + { |
| 11 | + type: "function", |
| 12 | + function: { |
| 13 | + name: "find_products", |
| 14 | + description: "Search for products and check stock levels. Returns list of products matching the filter.", |
| 15 | + parameters: { |
| 16 | + type: "object", |
| 17 | + properties: { |
| 18 | + filters: { |
| 19 | + type: "array", |
| 20 | + description: "ObjectQL filters, e.g. [['name', 'contains', 'ipad']]", |
| 21 | + items: { |
| 22 | + type: "array" |
| 23 | + } |
| 24 | + } |
| 25 | + }, |
| 26 | + required: ["filters"] |
| 27 | + } |
| 28 | + } |
| 29 | + }, |
| 30 | + { |
| 31 | + type: "function", |
| 32 | + function: { |
| 33 | + name: "update_stock", |
| 34 | + description: "Update the stock quantity for a product", |
| 35 | + parameters: { |
| 36 | + type: "object", |
| 37 | + properties: { |
| 38 | + sku: { type: "string" }, |
| 39 | + quantity_change: { type: "number", description: "Negative to reduce stock, positive to add" } |
| 40 | + }, |
| 41 | + required: ["sku", "quantity_change"] |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | +]; |
| 46 | + |
| 47 | +async function runAgent() { |
| 48 | + // 1. Setup ObjectQL |
| 49 | + const app = new ObjectQL({ |
| 50 | + driver: new KnexDriver({ |
| 51 | + client: 'sqlite3', |
| 52 | + connection: { filename: './inventory.db' }, |
| 53 | + useNullAsDefault: true |
| 54 | + }) |
| 55 | + }); |
| 56 | + await app.init(); // Load metadata |
| 57 | + |
| 58 | + // Seed some data if empty |
| 59 | + try { |
| 60 | + const existing = await app.find('product', {}); |
| 61 | + if (existing.length === 0) { |
| 62 | + console.log("Seeding initial data..."); |
| 63 | + await app.create('product', { name: "MacBook Pro 16", sku: "MBP16", stock_quantity: 10, price: 2499 }); |
| 64 | + await app.create('product', { name: "iPad Air", sku: "IPAD-AIR", stock_quantity: 50, price: 599 }); |
| 65 | + } |
| 66 | + } catch (e) { |
| 67 | + console.warn("Database init error:", e); |
| 68 | + } |
| 69 | + |
| 70 | + if (!process.env.OPENAI_API_KEY) { |
| 71 | + console.error("Please set OPENAI_API_KEY in .env"); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + // 2. Setup OpenAI |
| 76 | + const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); |
| 77 | + |
| 78 | + const userQuery = process.argv[2] || "How many iPad Airs do we have?"; |
| 79 | + console.log(`\nUser: "${userQuery}"`); |
| 80 | + |
| 81 | + // Step 1: LLM decides what to do |
| 82 | + const completion = await openai.chat.completions.create({ |
| 83 | + model: "gpt-4-turbo", |
| 84 | + messages: [{ role: "user", content: userQuery }], |
| 85 | + tools: productTools, |
| 86 | + }); |
| 87 | + |
| 88 | + const choice = completion.choices[0]; |
| 89 | + const toolCall = choice.message.tool_calls?.[0]; |
| 90 | + |
| 91 | + if (toolCall) { |
| 92 | + console.log(`\nAgent decided to call tool: ${toolCall.function.name}`); |
| 93 | + const args = JSON.parse(toolCall.function.arguments); |
| 94 | + console.log(`Arguments:`, args); |
| 95 | + |
| 96 | + let result; |
| 97 | + if (toolCall.function.name === 'find_products') { |
| 98 | + result = await app.find('product', args.filters || []); |
| 99 | + } else if (toolCall.function.name === 'update_stock') { |
| 100 | + const products = await app.find('product', [['sku', '=', args.sku]]); |
| 101 | + if (products.length > 0) { |
| 102 | + const product = products[0]; |
| 103 | + const newQty = (product.stock_quantity || 0) + args.quantity_change; |
| 104 | + result = await app.update('product', product.id, { stock_quantity: newQty }); |
| 105 | + } else { |
| 106 | + result = { error: "Product not found" }; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + console.log(`\nTool Output:`, JSON.stringify(result, null, 2)); |
| 111 | + |
| 112 | + // Step 2: Feed back to LLM (Optional simplification for tutorial) |
| 113 | + const finalResponse = await openai.chat.completions.create({ |
| 114 | + model: "gpt-4-turbo", |
| 115 | + messages: [ |
| 116 | + { role: "user", content: userQuery }, |
| 117 | + choice.message, |
| 118 | + { |
| 119 | + role: "tool", |
| 120 | + tool_call_id: toolCall.id, |
| 121 | + content: JSON.stringify(result) |
| 122 | + } |
| 123 | + ] |
| 124 | + }); |
| 125 | + |
| 126 | + console.log(`\nAgent Final Answer: ${finalResponse.choices[0].message.content}`); |
| 127 | + } else { |
| 128 | + console.log(`\nAgent Answer: ${choice.message.content}`); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +runAgent(); |
0 commit comments