Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Seamless Inventory Management with ChatGPT and ...

Seamless Inventory Management with ChatGPT and LINE SHOPPING API

Seamless Inventory Management with ChatGPT and LINE SHOPPING API
by Phisit Jorphochaudom, Team lead, Solution Engineer at LINE Thailand

LINE Developers Thailand

November 03, 2024
Tweet

More Decks by LINE Developers Thailand

Other Decks in Technology

Transcript

  1. Summary What have we done • Use AI to convert

    speech to text • Use AI to extract data from JSON • Use AI to think what to call (which API to use) • Integrate with LINE Messaging API and LINE Shopping API • We create a Rich workflow using AI
  2. • OpenAI API • Speech to text API • Chat

    completion API • Function calling • Structured data extraction • LINE API • LINE Messaging API ส ำ หรับรับ webhook และ reply message • LINE SHOPPING API ส ำ หรับ get product และ update inventory How?
  3. • What is Function Calling? • Function calling allows us

    to connect model to external tools • We can enhance our AI to use external functions (that we code) Open AI Function Calling
  4. Architecture LINE Webhook server OpenAI API 1. Webhook LINE Shopping

    API 2. Get Content 3. Audio 4. Speech to text 7. Get function call User 8. Call API 9. Send Reply Message 5. text 6. Chat completion
  5. Architecture LINE Webhook server OpenAI API 1. Webhook LINE Shopping

    API 2. Get Content 3. Audio 4. Speech to text 7. Get function call User 8. Call API 9. Send Reply Message 5. text 6. Chat completion
  6. { "destination": "xxxxxxxxxx", "events": [ { "replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", "type": "message",

    "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." }, "webhookEventId": "01FZ74A0TDDPYRVKNK77XKC3ZR", "deliveryContext": { "isRedelivery": false }, "message": { "id": "325708", "type": "audio", "duration": 60000, "contentProvider": { "type": "line" } } } ] }
  7. Architecture LINE Webhook server OpenAI API 1. Webhook LINE Shopping

    API 2. Get Content 3. Audio 4. Speech to text 7. Get function call User 8. Call API 9. Send Reply Message 5. text 6. Chat completion
  8. LINE Get Content API to Get Message Content by Message

    ID We will get audio file and save to our local disk first
  9. Architecture LINE Webhook server OpenAI API 1. Webhook LINE Shopping

    API 2. Get Content 3. Audio 4. Speech to text 7. Get function call User 8. Call API 9. Send Reply Message 5. text 6. Chat completion
  10. LINE Get Content API to Get Message Content by Message

    ID curl --request POST \ --url https://api.openai.com/v1/audio/transcriptions \ --header "Authorization: Bearer $OPENAI_API_KEY" \ --header 'Content-Type: multipart/form-data' \ --form file=@/path/to/file/speech.mp3 \ --form model=whisper-1 \ --form response_format=text ได้ response เ ป็ น text = `ดูสินค้าในร้าน` And we use OpenAI speech to text API
  11. Architecture LINE Webhook server OpenAI API 1. Webhook LINE Shopping

    API 2. Get Content 3. Audio 4. Speech to text 7. Get function call User 8. Call API 9. Send Reply Message 5. text 6. Chat completion
  12. Use OpenAI function calling chat completion API curl https://api.openai.com/v1/chat/completions \

    --header "Authorization: Bearer $OPENAI_API_KEY" \ --header 'Content-Type: application/json‘ \ -d '{ "model": "gpt-4o", "messages": [ { "role": "user", "content": " ดู สิ น ค้ าใน ร้ าน" } ], "tools": [ { "type": "function", "function": { "name": "get_products", "description": "Get products in MyShop", "parameters": { "type": "object", "properties": { "ids": { "type": "string", "description": “id of products" } }, "required": [] } } } ], "tool_choice": "auto" }'
  13. Tools De fi nition "tools": [ { "type": "function", "function":

    { "name": "get_products", "description": "Get products in MyShop", "parameters": { "type": "object", "properties": { "ids": { "type": "string", "description": “id of products" } }, "required": [] } } } ], "tool_choice": "auto" }'
  14. OpenAI function calling chat completion API Response { "id": "chatcmpl-abc123",

    "object": "chat.completion", "created": 1699896916, "model": "gpt-4o-mini", "choices": [ { "index": 0, "message": { "role": "assistant", "content": null, "tool_calls": [ { "id": "call_abc123", "type": "function", "function": { "name": “get_products", "arguments": "{\n\"ids\": \"\"\n}" } } ] }, "logprobs": null, "finish_reason": "tool_calls" } ], "usage": { "prompt_tokens": 82, "completion_tokens": 17, "total_tokens": 99, "completion_tokens_details": { "reasoning_tokens": 0 } } }
  15. Choices Response "choices": [ { "index": 0, "message": { "role":

    "assistant", "content": null, "tool_calls": [ { "id": "call_abc123", "type": "function", "function": { "name": "get_products", "arguments": "{\n\"ids\": \"\"\n}" } } ] }, "logprobs": null, "finish_reason": "tool_calls" } ],
  16. Architecture LINE Webhook server OpenAI API 1. Webhook LINE Shopping

    API 2. Get Content 3. Audio 4. Speech to text 7. Get function call User 8. Call API 9. Send Reply Message 5. text 6. Chat completion
  17. Now we know we have to get products from LINE

    Shopping API curl https://developers-oaplus.line.biz/myshop/v1/products \ --header “X-API-KEY: Bearer $LINE_SHOPPING_API_KEY” \ --header ‘Content-Type: application/json‘ We then use normal LINE Messaging Reply API to reply to user
  18. Architecture LINE Webhook server OpenAI API 1. Webhook LINE Shopping

    API 2. Get Content 3. Audio 4. Speech to text 7. Get function call User 8. Call API 9. Send Reply Message 5. text 6. Chat completion
  19. Update product’s inventory is the same: LINE Get Content API

    to Get Message Content by Message ID to get audio Use OpenAI Speech to text to convert audio to text We get “อัพเดท inventory เ สื้ อไลน์เดฟ เ ป็ น 25 ตัว”
  20. Now is function calling’s job curl https://api.openai.com/v1/chat/completions \ --header "Authorization:

    Bearer $OPENAI_API_KEY" \ --header 'Content-Type: application/json‘ \ -d '{ "model": "gpt-4o", "messages": [ { "role": "user", "content": "อัพเดท inventory เสื้อไล น์ เดฟ เ ป็ น 25 ตัว" } ], "tools": [ { "type": "function", "function": { "name": "update_inventory_by_name", "description": "Update product inventory in MyShop by product name", "parameters": { "type": "object", "properties": { "name": { "type": "string", "description": “name of product" }, "amount": { "type": "integer", "description": “quantity of product" } }, "required": [] } } } ], "tool_choice": "auto" }'
  21. But LINE Shopping API doesn’t have update inventory by product

    name How do we solve this? LINE Shopping API provides only update by `inventory id`
  22. Let AI solve this! We can ask AI to get

    all products, and fi nd inventory id for us. 1. Call LINE shopping get products 2. Ask AI to fi nd inventory id by product name 3. Call LINE Shopping API update inventory by id
  23. curl https://api.openai.com/v1/chat/completions \ --header "Authorization: Bearer $OPENAI_API_KEY" \ --header 'Content-Type:

    application/json‘ \ -d '{ "model": "gpt-4o", "messages": [ { "role": "system", "content": "You are an expert at structured data extraction. You will be given json response api and should parse inventory id" }, { "role": "system", "content": "extract inventory id for $productName with following: $json" } ], "tools": [ { "type": "function", "function": { "name": "update_inventory_by_name", "description": "Update product inventory in MyShop by product name", "parameters": { "type": "object", "properties": { "name": { "type": "string", "description": “name of product" }, "amount": { "type": "integer", "description": “quantity of product" } }, "required": [] } } } ], "tool_choice": "auto" }'
  24. "messages": [ { "role": "system", "content": "You are an expert

    at structured data extraction. You will be given json response from api and should parse inventory id" }, { "role": "system", "content": "extract inventory id for เสื้อไล น์ เดฟ with following: { … // JSON … }” } ],
  25. This is a combination of different things - We can

    ask AI to parse data from JSON - Then we ask them to do function calling - We then call our function in our code
  26. Architecture LINE Webhook server OpenAI API 1. Webhook LINE Shopping

    API 2. Get Content 3. Audio 4. Speech to text 7. Get function call User 8. Call API 9. Send Reply Message 5. text 6. Chat completion
  27. Pricing At what cost? { "id": "chatcmpl-abc123", "object": "chat.completion", "created":

    1699896916, "model": "gpt-4o-mini", "choices": [ { "index": 0, "message": { "role": "assistant", "content": null, "tool_calls": [ { "id": "call_abc123", "type": "function", "function": { "name": “get_products", "arguments": "{\n\"ids\": \"\"\n}" } } ] }, "logprobs": null, "finish_reason": "tool_calls" } ], "usage": { "prompt_tokens": 82, "completion_tokens": 17, "total_tokens": 99, "completion_tokens_details": { "reasoning_tokens": 0 } } } แทบจะฟรี!
  28. What can be improved • What if we have hundreds

    of products? • Maybe using vector embedding to do text search • Using assistant API to do `thread` conversation • OpenAI now have speech to speech real-time API • And more!
  29. Summary What have we done • Use AI to convert

    speech to text • Use AI to think what to call (which API to use) • Use AI to extract data from JSON • Integrate with LINE Messaging API and LINE Shopping API • We create a Rich workflow using AI