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

Seamless inventory management with AI

Seamless inventory management with AI

関数呼び出しやチャット補完など、LLM AIの機能を応用し、音声やテキストによる会話を通じて店舗の在庫を管理できるAIチャットボットの作成方法を示します。

More Decks by LINEヤフーTech (LY Corporation Tech)

Other Decks in Technology

Transcript

  1. • 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 • LINE Shopping lets Thailand sellers create a shop on LINE Official Account • We create a Rich workflow using AI In the demo summary
  2. • OpenAI API • Speech to text API • Chat

    completion API • Function calling • Structured data extraction • LINE API • LINE Messaging API for receiving webhook and send a reply message • LINE SHOPPING API for get product listing and 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) OpenAI 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 = `Can I get a product list` 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": "Can I get product list" } ], "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 Definition "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 “Update inventory of LINE dev t-shirt to 20”
  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": "Update inventory of LINE dev t-shirt to 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 find inventory id for us. 1. Call LINE shopping get products 2. Ask AI to find 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 techniques - 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 } } } Almost Free!
  28. What can be improved • What if we have hundreds

    of products? • Using vector embedding to do text search • Using assistant API to do `thread` conversation • OpenAI now have speech to speech real-time API • We can build MCP server for product management!
  29. • 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 Summary What have we done