Skip to content

Commit df10caa

Browse files
committed
update readme
1 parent 0faa1a6 commit df10caa

File tree

7 files changed

+144
-12
lines changed

7 files changed

+144
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.DS_Store
12
node_modules
23
tinybird/mock/*.ndjson

README.md

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
# AI Analytics Template
22

3-
This is a template for a Logs Explorer web application. It is built with Next.js and Tinybird.
3+
This is a template for an AI analytics dashboard and cost calculator. It is built with Next.js and [Tinybird](https://tinybird.co).
44

55
Use this template to bootstrap a multi-tenant, user-facing LLM analytics dashboard and cost calculator. Fork it and make it your own!
66

7+
Features:
8+
9+
- Multi-tenant user-facing dashboard
10+
- AI cost calculator
11+
- Vector search
12+
- Ask AI integration
13+
14+
Stack:
15+
16+
- [Next.js](https://nextjs.org/)
17+
- [Tinybird](https://tinybird.co)
18+
- [OpenAI](https://openai.com/)
19+
- [Vercel AI SDK](https://sdk.vercel.ai/docs/introduction)
20+
- [Clerk](https://clerk.com/)
21+
722
## Live Demo
823

9-
https://ai-analytics.tinybird.app
24+
- https://ai-analytics.tinybird.app
1025

11-
## Local Development
26+
## Quick Start
1227

1328
Get started by forking the GitHub repository and then customizing it to your needs.
1429

1530
Start Tinybird locally:
1631

1732
```
18-
curl -L https://tbrd.co/fwd | sh
33+
curl https://tinybird.co | sh
1934
cd tinybird
2035
tb local start
2136
tb login
@@ -32,8 +47,9 @@ Edit the .env file with your Tinybird API key and other configuration.
3247
```
3348

3449
```
35-
NEXT_PUBLIC_TINYBIRD_API_KEY=<YOUR_TINYBIRD_READ_PIPES_TOKEN>
3650
NEXT_PUBLIC_TINYBIRD_API_URL=http://localhost:7181
51+
# read_pipes token
52+
NEXT_PUBLIC_TINYBIRD_API_KEY=
3753
```
3854

3955
Start the Next.js application:
@@ -50,4 +66,86 @@ Open the application in your browser:
5066
http://localhost:3000
5167
```
5268

53-
To use the AI features, click on Settings in the dashboard and in put an Open AI API key
69+
## Multi-tenancy
70+
71+
Create a Clerk project and set up these environment variables in your Next.js application:
72+
73+
```
74+
# workspace ID for multi-tenant JWT tokens
75+
TINYBIRD_WORKSPACE_ID=
76+
# workspace admin token for multi-tenant JWT tokens
77+
TINYBIRD_JWT_SECRET=
78+
79+
# Clerk publishable key
80+
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=
81+
# Clerk secret key
82+
CLERK_SECRET_KEY=
83+
# Clerk sign in URL
84+
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
85+
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
86+
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/
87+
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/
88+
```
89+
90+
The [middleware](https://github.com/tinybirdco/ai-analytics-template/blob/main/dashboard/ai-analytics/src/middleware.ts) will get the `org:name` permission from the Clerk user and use it to create a Tinybird JWT token with the `organization` dimension fixed to that value. Read more about Tinybird JWT tokens [here](https://www.tinybird.co/docs/forward/get-started/authentication#json-web-tokens-jwts).
91+
92+
[Watch a video of the Clerk + Tinybird JWT token flow](./assets/clerk-tinybird-jwt.mp4)
93+
94+
## AI features
95+
96+
To use the AI features, click on Settings in the dashboard and input an Open AI API key.
97+
98+
See the `search` and `extract-cost-parameters` [API routes](https://github.com/tinybirdco/ai-analytics-template/tree/main/dashboard/ai-analytics/src/app/api) for more details on how the AI features work.
99+
100+
[Watch a video of the AI cost calculator](./assets/ai-cost-calculator.mp4)
101+
102+
## Vector search
103+
104+
The vector search is powered by Tinybird, but embeddings need to be calculated in a separate process. See the [generate-embedding](https://github.com/tinybirdco/ai-analytics-template/blob/main/dashboard/ai-analytics/src/app/api/generate-embedding/route.ts) route for more details.
105+
106+
The process is:
107+
108+
- The user inputs a query and clicks the search button.
109+
- The query is sent to the `generate-embedding` route to get the embedding.
110+
- The embedding is sent to the Tinybird `llm_messages` as a query parameter.
111+
- `llm_messages` use `cosineDistance` to find the most similar vectors.
112+
- The frontend shows the table rows with the most similar vectors.
113+
114+
## Deployment
115+
116+
- Fork and connect this repository to Vercel.
117+
- Set the environment variables in Vercel.
118+
- Configure the [CI/CD GitHub actions](https://github.com/tinybirdco/ai-analytics-template/tree/main/.github/workflows) to deploy to Tinybird.
119+
120+
## Instrumentation
121+
122+
Check the examples folder for instrumentation examples for:
123+
124+
- LiteLLM (python)
125+
- Vercel AI SDK (typescript)
126+
127+
## Mock data
128+
129+
For local testing, generate mock data with the following commands:
130+
131+
```sh
132+
cd tinybird/mock
133+
npm install
134+
npm run generate -- --start-date 2025-02-01 --end-date 2025-03-31 --events-per-day 100 --output ../fixtures/llm_events.ndjson
135+
```
136+
137+
The [generate-llm-events.js](https://github.com/tinybirdco/ai-analytics-template/blob/main/tinybird/mock/generate-llm-events.js) script generates the embeddings.
138+
139+
## Contributing
140+
141+
Please open an issue or submit a pull request.
142+
143+
## Support
144+
145+
Join the Tinybird Slack community to get help with your project.
146+
147+
## License
148+
149+
MIT License
150+
151+
Copyright (c) 2025 Tinybird.co

assets/ai-cost-calculator.mp4

5.98 MB
Binary file not shown.

assets/clerk-tinybird-jwt.mp4

20.8 MB
Binary file not shown.

dashboard/ai-analytics/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ bun dev
1616

1717
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
1818

19-
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
20-
21-
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
22-
2319
## Learn More
2420

2521
To learn more about Next.js, take a look at the following resources:

dashboard/ai-analytics/src/middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default clerkMiddleware(async (auth) => {
1616
console.log('No user or session found')
1717

1818
const response = NextResponse.next()
19-
response.headers.set('x-tinybird-token', process.env.TINYBIRD_JWT_SECRET || '')
19+
response.headers.set('x-tinybird-token', process.env.NEXT_PUBLIC_TINYBIRD_API_KEY || '')
2020
return response
2121
}
2222

@@ -67,7 +67,7 @@ export default clerkMiddleware(async (auth) => {
6767
} catch (error) {
6868
console.error('Middleware error:', error)
6969
const response = NextResponse.next()
70-
response.headers.set('x-tinybird-token', process.env.TINYBIRD_JWT_SECRET || '')
70+
response.headers.set('x-tinybird-token', process.env.NEXT_PUBLIC_TINYBIRD_API_KEY || '')
7171
return response
7272
}
7373
})

tinybird/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
This is a [Tinybird project](https://www.tinybird.co/docs/forward) for the AI analytics template.
2+
3+
It has a single table `llm_events` that stores LLM events with usage metrics, costs, and metadata.
4+
5+
Endpoints:
6+
7+
- `generic_counter` shows cost by several dimensions.
8+
- `llm_usage` is used to build time series charts.
9+
- `llm_messages` is used to build the vector search.
10+
11+
## Local development
12+
13+
Start Tinybird locally:
14+
15+
```
16+
curl https://tinybird.co | sh
17+
cd tinybird
18+
tb local start
19+
tb login
20+
tb dev
21+
```
22+
23+
Generate mock data:
24+
25+
```
26+
cd tinybird/mock
27+
npm install
28+
npm run generate -- --start-date 2025-02-01 --end-date 2025-03-31 --events-per-day 100 --output ../fixtures/llm_events.ndjson
29+
```
30+
31+
## Deploy to cloud
32+
33+
Use the [CI/CD GitHub actions](https://github.com/tinybirdco/ai-analytics-template/tree/main/.github/workflows) in this repository to deploy to Tinybird or the Tinybird CLI:
34+
35+
```
36+
tb --cloud deploy
37+
```

0 commit comments

Comments
 (0)