1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
| class EcommerceSkills2System { constructor(apiKey) { this.caller = new AdvancedSkills2Caller('gpt-4-turbo', apiKey); this.setupEcommerceTools(); }
setupEcommerceTools() { this.caller.registerTool('search_products', { description: '根据关键词搜索产品', parameters: { type: 'object', properties: { query: { type: 'string', description: '搜索关键词' }, category: { type: 'string', description: '产品分类' }, price_range: { type: 'object', properties: { min: { type: 'number' }, max: { type: 'number' } } }, limit: { type: 'number', default: 10, description: '返回结果数量' } }, required: ['query'] } }, async (args) => { const products = [ { id: 1, name: 'iPhone 15 Pro', price: 999, category: 'electronics' }, { id: 2, name: 'Samsung Galaxy S24', price: 899, category: 'electronics' }, { id: 3, name: 'MacBook Air M2', price: 1099, category: 'computers' } ];
return { products: products.slice(0, args.limit || 10), total_count: products.length, search_query: args.query }; });
this.caller.registerTool('check_inventory', { description: '检查特定产品的库存情况', parameters: { type: 'object', properties: { product_ids: { type: 'array', items: { type: 'string' }, description: '产品ID列表' } }, required: ['product_ids'] } }, async (args) => { const inventory = {}; for (const productId of args.product_ids) { inventory[productId] = { stock: Math.floor(Math.random() * 100), available: Math.random() > 0.2 }; }
return { inventory }; });
this.caller.registerTool('create_order', { description: '创建新订单', parameters: { type: 'object', properties: { customer_id: { type: 'string', description: '客户ID' }, items: { type: 'array', items: { type: 'object', properties: { product_id: { type: 'string' }, quantity: { type: 'number' }, price: { type: 'number' } }, required: ['product_id', 'quantity', 'price'] } }, shipping_address: { type: 'object', properties: { street: { type: 'string' }, city: { type: 'string' }, zip_code: { type: 'string' }, country: { type: 'string' } }, required: ['street', 'city', 'zip_code', 'country'] }, payment_method: { type: 'string' } }, required: ['customer_id', 'items', 'shipping_address'] } }, async (args) => { const orderId = `ORD-${Date.now()}-${Math.random().toString(36).substr(2, 6).toUpperCase()}`;
return { order_id: orderId, status: 'created', total_amount: args.items.reduce((sum, item) => sum + (item.price * item.quantity), 0), estimated_delivery: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString() }; });
this.caller.registerTool('process_payment', { description: '处理支付请求', parameters: { type: 'object', properties: { order_id: { type: 'string', description: '订单ID' }, amount: { type: 'number', description: '支付金额' }, payment_method: { type: 'object', properties: { type: { type: 'string', enum: ['credit_card', 'paypal', 'bank_transfer'] }, details: { type: 'object' } }, required: ['type', 'details'] }, currency: { type: 'string', default: 'USD' } }, required: ['order_id', 'amount', 'payment_method'] } }, async (args) => { return { transaction_id: `TXN-${Date.now()}`, status: 'approved', timestamp: new Date().toISOString(), payment_method: args.payment_method.type }; }); }
async handleEcommerceQuery(userQuery, customerId = null) { const context = { userProfile: { customerId }, time: new Date().toISOString() };
try { const result = await this.caller.callTools(userQuery, context); return result; } catch (error) { console.error('Ecommerce query error:', error); return { error: error.message, suggestion: '请稍后重试或联系客服' }; } }
async processPurchaseFlow(customerId, searchQuery, shippingAddress, paymentMethod) { const steps = [];
try { steps.push({ step: 'search', result: await this.caller.callToolWithErrorHandling('search_products', { query: searchQuery, limit: 5 }) });
if (!steps[0].result.products || steps[0].result.products.length === 0) { throw new Error('未找到匹配的产品'); }
const selectedProduct = steps[0].result.products[0];
steps.push({ step: 'inventory_check', result: await this.caller.callToolWithErrorHandling('check_inventory', { product_ids: [selectedProduct.id.toString()] }) });
if (!steps[1].result.inventory[selectedProduct.id]?.available) { throw new Error('所选产品暂无库存'); }
steps.push({ step: 'create_order', result: await this.caller.callToolWithErrorHandling('create_order', { customer_id: customerId, items: [{ product_id: selectedProduct.id, quantity: 1, price: selectedProduct.price }], shipping_address: shippingAddress, payment_method: paymentMethod }) });
const orderId = steps[2].result.order_id;
steps.push({ step: 'process_payment', result: await this.caller.callToolWithErrorHandling('process_payment', { order_id: orderId, amount: selectedProduct.price, payment_method: { type: paymentMethod, details: {} } }) });
return { success: true, order_id: orderId, steps: steps, total_amount: selectedProduct.price };
} catch (error) { return { success: false, error: error.message, completed_steps: steps }; } } }
|