ในบล็อกโพสต์นี้ เราจะเจาะลึก AI applications ที่สามารถสร้างได้โดยใช้ Upstash Redis, Upstash Vector, และ Vercel AI SDK เราจะสำรวจคุณสมบัติและความสามารถเฉพาะของแต่ละเครื่องมือ เน้นว่าพวกมันสามารถผสานรวมกันเพื่อสร้าง AI solutions ที่ทรงพลังและมีประสิทธิภาพได้อย่างไร ขณะที่เราตรวจสอบฟังก์ชันการทำงานของเครื่องมือเหล่านี้ เราจะดูตัวอย่างแอปพลิเคชันอย่างสั้นๆ ด้วย
Upstash Vector
มาเข้าใจ vectors และ vector databases ก่อน
Vector databases เป็นระบบจัดเก็บข้อมูลเฉพาะทางที่ออกแบบมาเพื่อจัดเก็บและดึงข้อมูลในรูปแบบ numerical array ที่เรียกว่า vectors ฐานข้อมูลเหล่านี้จัดการ high-dimensional vectors ปริมาณมากได้อย่างมีประสิทธิภาพ ทำให้เหมาะสำหรับจัดการ complex data structures ข้อได้เปรียบหลักของการใช้ vector databases:
Efficiency: ฐานข้อมูลเหล่านี้ให้การจัดเก็บและการดึงข้อมูล high-dimensional vectors อย่างมีประสิทธิภาพ ลด computational load และเร่งการตอบสนอง query ใน AI applications
Accuracy: Optimized search algorithms รับรองว่า vector databases ค้นหาจุดข้อมูลที่เกี่ยวข้องที่สุดได้อย่างแม่นยำ เพิ่มประสิทธิภาพของระบบที่ต้องการ data classification เช่น recommendation engines และ fraud detection systems
Real-Time Processing: ด้วยความสามารถในการประมวลผลข้อมูลแบบ real-time vector databases เหมาะสำหรับแอปพลิเคชันที่ต้องการ immediate responses เช่น chatbots และ real-time analytics
Vectors ใน AI ปกติจะแทน embedding-based model ของ objects ที่จับคุณสมบัติหรือลักษณะสำคัญในบริบทของแอปพลิเคชัน ตัวอย่างเช่น ใน AI application ที่พัฒนาจาก natural language processing vector อาจแทน semantics ของข้อความ เนื้อหาพูด หรือเอกสารที่ให้มา ใน image processing-based application vectors แทน features ของภาพที่ให้มา
ตอนนี้ มาเจาะลึก Upstash Vector
Upstash Vector เป็น serverless vector database ที่ใช้ DiskANN เป็น vector similarity search algorithm พร้อม 3 ประเภทของ similarity functions: Cosine, Euclidian distance และ dot product เนื่องจาก Upstash Vector เป็น serverless มันมี pay-as-you-go pricing และ free tier ที่สนับสนุนผู้ที่อยากสำรวจ
คุณสามารถหารายละเอียดเพิ่มเติมเกี่ยวกับ algorithm และ similarity functions ที่ใช้ใน Upstash Vector ได้ใน Upstash docs
นอกจากความสามารถในการค้นหาที่ low-cost high-performance แล้ว มันยังให้ Rest API และ SDKs ใน Typescript และ Python เพื่อให้การผสานรวม Upstash Vector เข้ากับ codebase ของเราง่ายขึ้น
เพื่อผสานรวมกับ Upstash Vector เราต้อง login เข้า console ก่อน เมื่อ login แล้ว เราสามารถสร้าง vector Index ได้โดยการคลิกปุ่ม Create Index ใน modal ที่โผล่ขึ้นมา เราสามารถตั้งชื่อ index, เลือก region ที่ index อยู่ หลังจากนั้น เราสามารถเลือก embedding model ที่จะใช้ extract embeddings จาก object ที่ให้มาก่อนเขียนไป Upstash Vector index มี pre-defined open-source embedding models บางตัวใน Upstash ที่ใช้ได้ มิฉะนั้น เราสามารถเลือก Custom เพื่อที่เราจะตั้งค่า dimensions ของ vectors ตาม external embedding model ที่เราใช้
สุดท้าย เราสามารถเลือก similarity function ที่คำนวณระยะห่างระหว่าง vectors มี 3 similarity functions ที่กำหนดใน Upstash: Cosine, Euclidean distance และ dot product คุณสามารถเรียนรู้เพิ่มเติมเกี่ยวกับ similarity functions ใน Upstash docs

ในหน้าจอถัดไป เราสามารถเลือก plan ที่เหมาะกับเราได้
เมื่อเสร็จสิ้นการสร้าง index แล้ว เราสามารถผสานรวม Upstash Vector กับ codebase ของเราได้ ผมจะใช้ Typescript ในบล็อกโพสต์นี้ อย่างไรก็ตาม Python หรือ Go SDKs ก็สามารถใช้ได้เช่นกัน เราสามารถทำตาม integration guide ที่ให้ภายใต้แท็บ Details บน Upstash Vector console เราต้องติดตั้ง dependency @upstash/vector ก่อน
npm i @upstash/vector
ตอนนี้เราสามารถ connect, write และ query ด้วย methods ต่อไปนี้ได้อย่างง่ายดาย
import { Index } from "@upstash/vector" const index = new Index({ url: <UPSTASH-VECTOR-ENDPOINT>, token: <UPSTASH-VECTOR-TOKEN>, }) await index.upsert({ id: "id1", vector: […], metadata: { metadata_field: "metadata_value" }, }); await index.query({ vector: […], topK: 1, includeVectors: true, includeMetadata: true, });
Vercel AI SDK
Vercel AI SDK เป็น TypeScript toolkit ที่ออกแบบมาเพื่อลดความซับซ้อนของการผสานรวม AI capabilities เข้าไปใน web applications มันรองรับ frameworks ต่างๆ เช่น React, Next.js, Vue, Svelte, และ Node.js ช่วยให้นักพัฒนาสร้าง AI-powered applications ได้อย่างมีประสิทธิภาพ Vercel AI SDK ประกอบด้วย 3 main concepts:
AI SDK Core
Vercel AI SDK Core เป็นส่วนประกอบสำคัญของ Vercel AI SDK ออกแบบมาเพื่อลดความซับซ้อนของการผสานรวม artificial intelligence capabilities เข้าไปใน web applications โมดูล core นี้ให้ unified API ที่facilitate การใช้ large language models (LLMs) เพื่อ generate text, structured objects, และ tool calls
ตัวอย่างการใช้งานที่พบบ่อยที่สุดคือการใช้ API generateText หรือ streamText มาลองใช้งานดูเร็วๆ ใน Typescript
ก่อนอื่น import Vercel AI SDK โดยรันคำสั่งต่อไปนี้ในเทอร์มินัล
`npm i ai`
จากนั้น เราสามารถใช้ API generateText เป็นตัวอย่าง Vercel AI SDK ให้การผสานรวมกับ known model providers บางตัว เช่น OpenAI, Antropic, Google, Mistral และอื่นๆ เราจะใช้ OpenAI ในตัวอย่างนี้ แต่อื่นๆ ก็ง่ายเท่ากับการผสานรวม OpenAI
import { generateText } from "ai" import { openai } from "@ai-sdk/openai" const { text } = await generateText({ model: openai("gpt-4-turbo"), prompt: "What is love?" })
AI SDK UI
Vercel AI SDK UI เป็น framework-agnostic toolkit ที่ออกแบบมาเพื่อช่วยให้นักพัฒนาสร้าง interactive chat, completion, และ assistant applications พร้อมฟังก์ชันเพิ่มเติม เช่น การสร้าง chat interfaces, generative AI components และ state management
ตัวอย่างการใช้งานที่พบบ่อยที่สุดคือ hooks useChat และ useCompletion useChat ให้ streaming ของ chat messages โดย abstraction state management สำหรับประเภท inputs และ outputs ที่ต้องการ เช่น messages, loading states และ errors useCompletion อนุญาตให้ developers จัดการ text completions ภายใน AI applications จัดการ chat input state และอัปเดต UI อัตโนมัติเมื่อ completions ใหม่มาถึงจาก provider
นี่ มาดูวิธีใช้ useChat เร็วๆ เป็นตัวอย่างสำหรับ AI SDK UI
const { messages, input, handleInputChange, handleSubmit} = useChat({ api: "api/chat", initialMessages: […], onResponse(response) {}, streamMode: "text", });
comme on peut le voir, il prend l'endpoint API à appeler lorsque le message est envoyé, une fonction lorsque la réponse est reçue, et il gère l'état lui-même.
AI SDK RSC
ส่วนนี้ของ Vercel AI SDK เป็นสำหรับ server-side rendering เป้าหมายของ AI SDK RSC คือการให้การสนับสนุนสำหรับ large language model (LLM) เพื่อ generate และ stream UI โดยตรงจาก server ไปยัง client ผ่าน React Server Components (RSC)
AI SDK RSC มี multiple functions เพื่อรองรับ streaming outputs สำหรับ server-side rendered UIs สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับพวกมัน โปรดเยี่ยมชม Vercel AI SDK RSC docs ในบล็อกนี้ เราจะแค่ตรวจสอบฟังก์ชันหนึ่งเพื่อดูว่ามันทำงานอย่างไร เพื่อวัตถุประสงค์นี้ มาเข้าใจวิธีใช้ฟังก์ชัน streamUI
const result = await streamUI({ model: openai('gpt-4o'), prompt: 'Get the weather for San Francisco', text: ({ content }) => <div>{content}</div>, tools: {}, });
comme on peut le voir, streamUI fonctionne aussi comme les fonctions AI SDK Core. La principale différence est le streaming de l'UI rendue depuis le serveur.
สรุปสั้นๆ Vercel AI SDK ช่วยให้นักพัฒนาสร้าง AI applications ในทุกส่วนของ software development จาก backend ถึง frontend
Upstash Redis
ใน AI applications Upstash Redis สามารถใช้ได้หลักเพื่อ rate limiting เพื่อปกป้อง APIs และ LLM model usage, caching purposes หรือจัดเก็บ message history ใน AI chatbot application
เราควรสร้างฐานข้อมูล Redis บน Upstash console ก่อน

เมื่อเราสร้างฐานข้อมูล Redis แล้ว เราสามารถผสานรวม Redis กับ codebase ของเราเพื่อ write และ query data ซึ่งอาจเป็น caching data, message history, conversation state ฯลฯ เพื่อทำเช่นนั้น เราควรติดตั้ง Upstash Redis SDK ก่อน
npm i @upstash/redis
จากนั้น เราสามารถเชื่อมต่อ Redis database ของเราจากโค้ดได้
import { Redis } from '@upstash/redis' const redis = new Redis({ url: <UPSTASH-REDIS-ENDPOINT>, token: <UPSTASH-REDIS-TOKEN>, }) const data = await redis.set('foo', 'bar');
หากเราอยากใช้ Redis สำหรับ rate limiting เราสามารถใช้ rate limiting SDK ที่ Upstash จัดเตรียมไว้ได้เช่นกัน Dependency ที่จะติดตั้งแตกต่างสำหรับ rate limiting SDK
npm install @upstash/ratelimit
ตอนนี้ เพื่อ implement rate limiting;
import { Ratelimit } from "@upstash/ratelimit"; import { Redis } from "@upstash/redis"; // Create a new ratelimiter, that allows 10 requests per 10 seconds const ratelimit = new Ratelimit({ redis: Redis.fromEnv(), limiter: Ratelimit.slidingWindow(10, "10 s"), analytics: true, /** * Optional prefix for the keys used in redis. This is useful if you want to share a redis * instance with other applications and want to avoid key collisions.5 */ prefix: "prefix", }); // Use a constant string to limit all requests with a single ratelimit // Or use a userID, apiKey or ip address for individual limits. const identifier = "identifier"; const { success } = await ratelimit.limit(identifier); if (!success) { return "Unable to process at this time"; } // rest of the code
การใช้ Upstash Redis และ Vector กับ Vercel AI SDK
ตอนนี้ สิ่งที่ทำได้กับเครื่องมือที่อธิบายข้างต้นคืออะไร? เยอะมาก...
RAG chatbots เป็น generative AI applications ที่พบบ่อยที่สุดที่สามารถสร้างได้ด้วย Vercel AI SDK และ Upstash Vector database Retrieval-Augmented Generation (RAG) เป็น advanced framework ที่เพิ่มความสามารถของ chatbots และ conversational AI systems อื่นๆ อย่างมีนัยสำคัญ มันรวมสอง critical components —retrieval และ generation— เพื่อส่งมอบ responses ที่แม่นยำ contextually relevant และ informative มากขึ้น RAG chatbots ใช้ vector database เป็น retriever ที่จัดเก็บและให้ history ของ chat ใน embedding format ที่สามารถใช้สำหรับ generate response ถัดไปจาก LLM model ด้วยวิธีนี้ LLM model สามารถสร้าง responses ที่เกี่ยวข้องกับ context ของการสนทนาและเรียนรู้จากการพูดคุยในอดีต
สำหรับประเภท chatbot applications แบบนี้ Upstash Vector เป็น vector database ที่สมบูรณ์แบบซึ่งสามารถเป็น retriever ได้ เพิ่มเติมจากการผสานรวมกับ Upstash Vector เราสามารถใช้ APIs streamText หรือ streamObject ของ Vercel AI SDK Core component ในฝั่ง backend เพื่อ generate และ stream response ที่มาจาก LLM models เช่น OpenAI, Anthropic, Mistral ฯลฯ ในฝั่ง frontend เราสามารถใช้ประโยชน์จาก hook useChat ของ Vercel AI SDK UI component ซึ่งเป็น framework ที่ทรงพลังที่จัดการ chat UI และ state management คุณสามารถดูตัวอย่าง RAG application ใน DegreeGuru blog post
อีกตัวอย่าง AI application type ที่พบบ่อยซึ่งสามารถสร้างได้ง่ายด้วย Vercel AI SDK คือ recommendation systems Recommendation system เป็นหนึ่งในฟีเจอร์สำคัญที่สุดของ e-commerce platform มันใช้ personal preferences และ history ของผู้ใช้เพื่อระบุ interests ของผู้ใช้เพื่อให้ useful recommendations ในขณะที่สร้าง recommendation system Vercel AI SDK Core ช่วยเรา extract embeddings ของ user data ได้ง่ายๆ โดยใช้ฟังก์ชัน embed มาดูตัวอย่างโค้ดด้านล่าง
import { embed } from 'ai'; import { openai } from '@ai-sdk/openai'; // 'embedding' is a single embedding object (number[]) const { embedding } = await embed({ model: openai.embedding('text-embedding-3-small'), value: 'sunny day at the beach', });
หลังจาก extract embeddings ซึ่งเป็น semantic data ใน numeric array format เราสามารถจัดเก็บ output ใน Upstash Vector ได้ Embeddings เหล่านี้สามารถ query และ classify ตาม user habits และใช้ใน future recommendations
ฟังก์ชัน embed ที่ extract embeddings จาก input ใน Vercel AI SDK Core สามารถใช้ใน machine learning application ใดๆ ก็ได้ เช่น semantic search, image search, content summarization และอื่นๆ Developers สามารถ insert vector embeddings ที่ extract โดยฟังก์ชันนี้เข้าไปใน Upstash Vector database เพื่อให้ products สามารถวิเคราะห์ inputs ตาม embeddings ได้โดยไม่ต้องจัดเก็บ raw data ซึ่งไม่มีประโยชน์สำหรับ AI algorithms ตัวอย่างแอปพลิเคชันสุดท้ายคือ Customer review's AI summary ในตัวอย่างแอปพลิเคชันนี้ เราสามารถ extract embeddings จาก texts ที่ให้มา ซึ่งคือ customer reviews ของ product และจัดเก็บใน vector database ภายหลัง analysts สามารถใช้ vectors เพื่อได้ summary ของข้อมูลโดยการ prompt ให้ LLM model ด้วย Vercel AI SDK Core อีกครั้ง
ตัวอย่างสุดท้ายที่เราสามารถอภิปรายในบล็อกโพสต์นี้คือ fraud/anomaly detection systems ในรูปแบบพื้นฐานมากของ anomaly detection developers ต้อง classify transactions หรือ user behaviours เพื่อที่จะ detect ว่ามี unusual movement หรือไม่ สำหรับวัตถุประสงค์นั้น Upstash vector database สามารถใช้สร้าง vectors เพื่อ locate ใน n-dimensional space เมื่อ interactions ของผู้ใช้ถูก locate ใน space ผ่านการจัดเก็บ vectors ใน Upstash Vector analyzer tools สามารถได้ similarity ของ incoming interaction กับ interactions ใน vector space Similarity scores เหล่านี้ช่วยระบุว่า interaction เป็น normal หรือไม่
ในตัวอย่างแอปพลิเคชันทั้งหมดที่เราอภิปรายข้างต้น เราสามารถใช้ประโยชน์จาก Upstash Rate limiting feature สำหรับ heavy operations ใดๆ เช่น generating texts, calling AI models เพื่อ extract embeddings ฯลฯ ขอบคุณ rate limiting เราสามารถปกป้อง operations ของเราจาก burst of traffic เพิ่มเติมจากนั้น Upstash Redis สามารถเป็นที่จัดเก็บ raw user data สำหรับแอปพลิเคชันของเราได้อย่างยอดเยี่ยม เช่น message history, cache ของ prompts ที่เราใช้สำหรับ LLM models และอื่นๆ
สรุป
ในบล็อกโพสต์นี้ เราค้นพบว่า Upstash Redis, Upstash Vector, และ Vercel AI SDK รวมกันเป็น combination ที่มีค่าสำหรับการสร้าง advanced AI applications เครื่องมือเหล่านี้ทำงานร่วมกันเพื่อจัดการข้อมูล ประมวลผลข้อมูล และ deploy AI models อย่างมีประสิทธิภาพ ตัวอย่างที่ให้มาแสดงถึงความหลากหลายของ technology stack นี้ในการสร้าง AI solutions หลากหลาย
สำหรับตัวอย่างเพิ่มเติม คุณสามารถดู Upstash blogs และ Vercel templates ได้