Slide 1

Slide 1 text

Using Hono in B2B SaaS Application Hono Conference 2024 - Our first step

Slide 2

Slide 2 text

Agenda 1. Self Introduction 2. Basics of Hono 3. UseCases in AI Worker 4. Conclusion

Slide 3

Slide 3 text

Self Introduction sugar cat Cyber Agent/AIShift Backend Engineer Interests: Web Performance, Security @sugar235711

Slide 4

Slide 4 text

Product Introduction ● AI Worker ○ A platform for business improvement utilizing generative AI

Slide 5

Slide 5 text

AI Worker Tech Stack

Slide 6

Slide 6 text

AI Worker Tech Stack

Slide 7

Slide 7 text

Basics of Hono

Slide 8

Slide 8 text

What is Hono ● Fast, lightweight, built on Web Standards ○ Runtime Compatibility: Node, Deno, Bun, Workers Key Features: ● Built-in Middleware: A wide range of middleware is included by default, ensuring smooth development. ● Simple and Intuitive API: Designed with a clear and easy-to-understand API, allowing for rapid development.

Slide 9

Slide 9 text

Example: Simple API

Slide 10

Slide 10 text

Example: Simple API Generate a Hono instance and define API endpoints based on it.

Slide 11

Slide 11 text

Example: Simple API You can insert any middleware before executing endpoints.

Slide 12

Slide 12 text

Example: Simple API The Context object can manipulate headers, request bodies, and response bodies.

Slide 13

Slide 13 text

Example: Handling Context

Slide 14

Slide 14 text

Example: Handling Context With generics, you can bind any variables or functions to the Context object.

Slide 15

Slide 15 text

Example: Handling Context Use context getters and setters to handle and propagate bindings between methods.

Slide 16

Slide 16 text

Example: Integration with Zod OpenAPI

Slide 17

Slide 17 text

Example: Integration with Zod OpenAPI Create request and response schemas with Zod and register them to a route.

Slide 18

Slide 18 text

Example: Integration with Zod OpenAPI By combining this with @hono/zod-openapi, requests are automatically validated.

Slide 19

Slide 19 text

Example: Integration with Zod OpenAPI Combine with Swagger middleware to auto-generate a UI.

Slide 20

Slide 20 text

Use Cases in AI Worker

Slide 21

Slide 21 text

Keyword ● AI ● B2B (Multi-tenant)

Slide 22

Slide 22 text

AI

Slide 23

Slide 23 text

Recent Trends in AI ● Utilization of LLM ○ GPT, Claude…

Slide 24

Slide 24 text

Using Streaming SSE for Real-time Response ● Expressing that responses are gradually returned in an interactive AI UI.

Slide 25

Slide 25 text

Example: SSE with Hono Helper + GPT

Slide 26

Slide 26 text

Example: SSE with Hono Helper + GPT streamSSE: A helper to handle responses as SSE (Server-Sent Events)

Slide 27

Slide 27 text

Example: SSE with Hono Helper + GPT Stream returned by GPT

Slide 28

Slide 28 text

Example: SSE with Hono Helper + GPT Push to the client using data: { "hoge": "fuga!" } format.

Slide 29

Slide 29 text

Example: SSE with Hono Helper + GPT Close the stream at the end of processing or on interruption.

Slide 30

Slide 30 text

Stream is easy to handle. How is it integrated into actual products?

Slide 31

Slide 31 text

AI Worker Tech Stack

Slide 32

Slide 32 text

RAG

Slide 33

Slide 33 text

RAG Quick Start

Slide 34

Slide 34 text

RAG Quick Start Accept user input, like text in a chat box.

Slide 35

Slide 35 text

RAG Quick Start The accepted text is embedded and represented as a vector.

Slide 36

Slide 36 text

RAG Quick Start Retrieve information related to the user's input.

Slide 37

Slide 37 text

RAG Quick Start Embed context in the prompt and generate a response.

Slide 38

Slide 38 text

RAG Quick Start Retrieval-Augmented Generation

Slide 39

Slide 39 text

Uploading Documents for Search Now, one important point in performing RAG (Retrieval-Augmented Generation) is that documents need to be registered in the Vector Store in advance.

Slide 40

Slide 40 text

Uploading Documents for Search The user uploads a file in some way, which is then embedded and stored in the vector store.

Slide 41

Slide 41 text

Uploading Documents for Search

Slide 42

Slide 42 text

AI Worker Tech Stack

Slide 43

Slide 43 text

Example: File Upload with @hono/zod-openapi

Slide 44

Slide 44 text

Example: File Upload with @hono/zod-openapi Enable validation of multiple files using multipart/form-data.

Slide 45

Slide 45 text

Example: File Upload with @hono/zod-openapi HonoRequest has a parseBody implementation, making it easy to parse multiple files.

Slide 46

Slide 46 text

B2B(Multi-tenant)

Slide 47

Slide 47 text

AI Worker as a Bridge Model Multi-tenant ● Single Instance with Multiple Schemas ● Switching Based on Tenant ID

Slide 48

Slide 48 text

AI Worker as a Bridge Model Multi-tenant ● Single Instance with Multiple Schemas ● Switching Based on Tenant ID TenantID

Slide 49

Slide 49 text

Extracting Tenant ID from Access Token

Slide 50

Slide 50 text

Extracting Tenant ID from Access Token Set tenantID in the context and propagate it to subsequent processes.

Slide 51

Slide 51 text

Extracting Tenant ID from Access Token Clearly identify the requester and propagate the Logger in the Context.

Slide 52

Slide 52 text

Security:Validation at DB and Response Levels Our company adopts Clean Architecture and DDD, resulting in separated layers. Request Response Handler Usecase Repository

Slide 53

Slide 53 text

Security:Validation at DB and Response Levels It is necessary to properly validate each layer to prevent unnecessary fields from leaking. Request Response Handler Usecase Repository

Slide 54

Slide 54 text

Security:Validation at DB and Response Levels Request Response Handler Usecase Repository

Slide 55

Slide 55 text

Security:Validation at DB and Response Levels Request Response Handler Usecase Repository Pass the response through Zod's parse as a DTO to avoid unnecessary fields and potential incidents.

Slide 56

Slide 56 text

Conclusion ● In AI Worker, we use Hono's features for various aspects such as RAG, B2B tenant management, and security measures. For those who want to learn more about Hono: