Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Demo Video (text version)

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

● 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?

Slide 5

Slide 5 text

● 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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

{ "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" } } } ] }

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

LINE Get Content API to Get Message Content by Message ID We will get audio file and save to our local disk first

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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" }'

Slide 15

Slide 15 text

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" }'

Slide 16

Slide 16 text

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 } } }

Slide 17

Slide 17 text

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" } ],

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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 ตัว”

Slide 22

Slide 22 text

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" }'

Slide 23

Slide 23 text

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`

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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" }'

Slide 26

Slide 26 text

"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 … }” } ],

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Pricing At what cost?

Slide 30

Slide 30 text

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 } } } แทบจะฟรี!

Slide 31

Slide 31 text

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!

Slide 32

Slide 32 text

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