Knowledge Base · Learning paths
Getting Started With the AI Stack for JavaScript
a16z has released a JavaScript AI starter template that brings together Next.js, LangChain.js, Pinecone, Replicate, OpenAI, and Clerk. It helps developers build generative AI apps faster by handling authentication, hosting, and integration work out of the box.
Translated from Chinese by AI. Spotted an issue? Use the feedback button.
Original link: https://a16z.com/2023/06/21/the-getting-started-with-ai-stack-for-javascript/
Translation: WaytoAGI (Way to AGI). If there are any imperfections, please correct them in the paragraph comments. Many thanks.
Published: June 21, 2023
It's hard to understand new software infrastructure technologies without using them. At least, that's what the a16z infrastructure team found — because many of us started our careers as programmers, we usually learn by doing. This is especially true in the generative AI wave, which arrived so fast and so spectacularly that good documentation often lags the code by months. So, to better understand this space, we built a few projects around large language models (LLMs), large image models, vector databases, and more.
Along the way, we noticed that because all of this is so new and changing so quickly, there's effectively no good framework for getting started quickly. Every project requires a lot of boilerplate code and integration. Frankly, it's a hassle. So we set out to create a very simple "Getting Started with AI" template for people who want to experiment with the core technologies without having to think too much about ancillary concerns like licensing, hosting, and tooling choices.
You can fork and deploy the template here. We'd love to hear your thoughts and feedback to make the template better.
Components
Many of us are JavaScript/TypeScript enthusiasts, so we chose the JavaScript stack as a starting point. However, the framework can easily be modified to support other programming languages, and we plan to do so soon.
Here's a brief overview of the getting-started stack we built together with our longtime partner and open-source enthusiast Tim Qian. Our goal was to highlight the simplest path from pulling code on GitHub to running a generative AI application (including images and text). It's designed to be easily extended to more complex architectures and projects:
-
Authentication: Clerk
-
Application logic: Next.js
-
Vector database: Pinecone / Supabase pgvector
-
LLM orchestration: Langchain.js
-
Image models: Replicate
-
Text models: OpenAI
-
Deployment: Fly.io deployment
For a more detailed overview of the emerging LLM stack, check out our article titled "Emerging Architectures for LLM Applications."
Models and inference
Model hosting is a hassle and is largely a separate concern from building AI applications. So we use OpenAI for text models and Replicate for image inference. Replicate also offers text-based models (see how easy it is to run Vicuna), so you can use it as a substitute for OpenAI if needed.
Example code for running Stable Diffusion inference on Replicate
Authentication
For a starter framework, we usually don't bother including authentication. But in this case, the model is so powerful and general-purpose that it becomes a target for large-scale organized efforts to gain free access. Developers often only find out the hard way when they receive an unexpected $10,000 bill from the model provider. That's why we chose to include Clerk, which can play an important role in bot detection and, of course, also provides full authentication support if you eventually build a more complex application.
Example code for a drop-in Clerk component for user sign-up; this component handles bot detection, enforces 2FA, and detects user impersonation.
Vector database
LLMs need stable long-term memory to preserve state and work around context window limitations, and this is handled by vector databases. Currently, Pinecone is the most mature and popular vector store among the generative AI community. That said, we want to support all use cases and preferences, so we've also included support for Supabase's pg-vector in the repository.
Example code for using Pinecone with Langchain.js. You can also find examples of using Supabase pg-vector here.
Deployment
For deployment, we use Fly.io because it's multi-region, easy to manage, and provides a very general-purpose compute environment (anything that can run in a container). Over time, many AI projects end up using multiple programming languages and/or have non-trivial functionality in the backend, so Fly.io is a good compromise between JavaScript-native hosting environments (like Vercel or Netlify) and traditional cloud. That said, the code is also easy to support if you prefer a different hosting environment.
Fly.io will also soon offer GPUs for cases where you need to host your own models.
Roadmap
Although we think this first version is a great starting point, we're gradually refining the stack and adding more options. Here's a glimpse of our roadmap:
-
An interactive CLI for create-ai-stack, so developers can choose their own project scaffolding and dependencies.
-
A transactional database for advanced use cases (e.g., persisting questions, user preferences, etc. in Q&A).
-
More vector database and deployment platform options.
-
A lightweight fine-tuning step for open-source models.
We'd love PRs with bug fixes, feature requests, and feedback. We're excited to contribute to the open-source community and believe the ecosystem will always benefit.
We're grateful to the following open-source projects that played an important role in creating our AI starter stack. These indispensable tools simplified complex tasks and made them easier to handle. It's through their tireless efforts and valuable contributions that the JavaScript ecosystem continues to thrive and flourish!