Skip to content

Commit 8fe5fc2

Browse files
authored
chore: add llamaindex server package (#585)
1 parent 3960618 commit 8fe5fc2

File tree

108 files changed

+19131
-1110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+19131
-1110
lines changed

.changeset/smooth-bees-brush.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"create-llama": patch
3+
"@llamaindex/server": patch
4+
---
5+
6+
chore: add llamaindex server package

packages/create-llama/README.md renamed to README.md

+7
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,11 @@ Pro mode is ideal for developers who want fine-grained control over their projec
130130
- [TS/JS docs](https://ts.llamaindex.ai/)
131131
- [Python docs](https://docs.llamaindex.ai/en/stable/)
132132

133+
## LlamaIndex Server
134+
135+
We also provide a server implementation for LlamaIndex that you can use to serve your LlamaIndex workflows and agent workflows as an API server. See the following guides for more information:
136+
137+
- [LlamaIndex Server For TypeScript](./packages/server/README.md)
138+
- [LlamaIndex Server For Python](./python/llama-index-server/README.md)
139+
133140
Inspired by and adapted from [create-next-app](https://github.com/vercel/next.js/tree/canary/packages/create-next-app)

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
"new-version": "pnpm -r build && changeset version",
2222
"release": "pnpm -r build && changeset publish",
2323
"release-snapshot": "pnpm -r build && changeset publish --tag snapshot",
24-
"build": "pnpm -r --filter create-llama build",
25-
"e2e": "pnpm -r --filter create-llama e2e",
26-
"dev": "pnpm -r --filter create-llama dev",
27-
"format": "pnpm -r --filter create-llama format",
28-
"format:write": "pnpm -r --filter create-llama format:write",
29-
"lint": "pnpm -r --filter create-llama lint"
24+
"build": "pnpm -r build",
25+
"e2e": "pnpm -r e2e",
26+
"dev": "pnpm -r dev",
27+
"format": "pnpm -r format",
28+
"format:write": "pnpm -r format:write",
29+
"lint": "pnpm -r lint"
3030
},
3131
"devDependencies": {
3232
"@changesets/cli": "^2.27.1",

packages/create-llama/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ __pycache__
5959

6060
# build artifacts
6161
create-llama-*.tgz
62+
63+
# copied from root
64+
README.md
65+
LICENSE.md
File renamed without changes.

packages/create-llama/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
"create-llama": "./dist/index.js"
1818
},
1919
"files": [
20-
"dist"
20+
"dist",
21+
"README.md",
22+
"LICENSE.md"
2123
],
2224
"scripts": {
25+
"copy": "cp -r ../../README.md ../../LICENSE.md .",
2326
"build": "bash ./scripts/build.sh",
2427
"build:ncc": "pnpm run clean && ncc build ./index.ts -o ./dist/ --minify --no-cache --no-source-map-register",
28+
"postbuild": "pnpm run copy",
2529
"clean": "rimraf --glob ./dist ./templates/**/__pycache__ ./templates/**/node_modules ./templates/**/poetry.lock",
2630
"dev": "ncc build ./index.ts -w -o dist/",
2731
"e2e": "playwright test",

packages/server/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server/

packages/server/.prettierignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
lib/
2+
dist/
3+
server/
4+
next/.next/
5+
next/out/
6+
node_modules/
7+
build/

packages/server/CHANGELOG.md

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# @llamaindex/server
2+
3+
## 0.1.6
4+
5+
### Patch Changes
6+
7+
- 82d4b46: feat: re-add supports for artifacts
8+
9+
## 0.1.5
10+
11+
### Patch Changes
12+
13+
- 7ca9ddf: Add generate ui workflow to @llamaindex/server
14+
- 3310eaa: chore: bump chat-ui
15+
16+
17+
## 0.1.4
18+
19+
### Patch Changes
20+
21+
22+
23+
## 0.1.3
24+
25+
### Patch Changes
26+
27+
- edb8b87: fix: shadcn components cannot be used in next server
28+
- Updated dependencies [6cf928f]
29+
30+
31+
## 0.1.2
32+
33+
### Patch Changes
34+
35+
- bb34ade: feat: support cn utils for server UI
36+
37+
38+
## 0.1.1
39+
40+
### Patch Changes
41+
42+
- 400b3b5: feat: use full-source code with import statements for custom comps
43+
44+
45+
## 0.1.0
46+
47+
### Minor Changes
48+
49+
- 3ffee26: feat: enhance config params for LlamaIndexServer
50+
51+
## 0.0.9
52+
53+
### Patch Changes
54+
55+
- 0b75bd6: feat: component dir in llamaindex server
56+
57+
## 0.0.8
58+
59+
### Patch Changes
60+
61+
- Updated dependencies [3534c37]
62+
63+
64+
## 0.0.7
65+
66+
### Patch Changes
67+
68+
- 4999df1: bump nextjs
69+
- Updated dependencies [f5e4d09]
70+
71+
72+
## 0.0.6
73+
74+
### Patch Changes
75+
76+
- 8c02684: fix: handle stream error
77+
- c515a32: feat: return raw output for agent toolcall result
78+
79+
80+
## 0.0.5
81+
82+
### Patch Changes
83+
84+
- 9d951b2: feat: support llamacloud in @llamaindex/server
85+
- Updated dependencies [9d951b2]
86+
87+
88+
## 0.0.4
89+
90+
### Patch Changes
91+
92+
- 164cf7a: fix: custom next server start fail
93+
94+
## 0.0.3
95+
96+
### Patch Changes
97+
98+
- 299008b: feat: copy create-llama to @llamaindex/servers
99+
- 75d6e29: feat: response source nodes in query tool output
100+
- Updated dependencies [75d6e29]
101+
102+
103+
## 0.0.2
104+
105+
### Patch Changes
106+
107+
- f8a86e4: feat: @llamaindex/server
108+
- Updated dependencies [21bebfc]
109+
- Updated dependencies [93bc0ff]
110+
- Updated dependencies [91a18e7]
111+
- Updated dependencies [f8a86e4]
112+
- Updated dependencies [5189b44]
113+
- Updated dependencies [58a9446]
114+
- @llamaindex/core@0.6.0
115+
- @llamaindex/workflow@1.0.0

packages/server/README.md

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# LlamaIndex Server
2+
3+
LlamaIndexServer is a Next.js-based application that allows you to quickly launch your [LlamaIndex Workflows](https://ts.llamaindex.ai/docs/llamaindex/modules/agents/workflows) and [Agent Workflows](https://ts.llamaindex.ai/docs/llamaindex/modules/agents/agent_workflow) as an API server with an optional chat UI. It provides a complete environment for running LlamaIndex workflows with both API endpoints and a user interface for interaction.
4+
5+
## Features
6+
7+
- Serving a workflow as a chatbot
8+
- Built on Next.js for high performance and easy API development
9+
- Optional built-in chat UI with extendable UI components
10+
- Prebuilt development code
11+
12+
## Installation
13+
14+
```bash
15+
npm i @llamaindex/server
16+
```
17+
18+
## Quick Start
19+
20+
Create an `index.ts` file and add the following code:
21+
22+
```ts
23+
import { LlamaIndexServer } from "@llamaindex/server";
24+
import { wiki } from "@llamaindex/tools"; // or any other tool
25+
26+
const createWorkflow = () => agent({ tools: [wiki()] });
27+
28+
new LlamaIndexServer({
29+
workflow: createWorkflow,
30+
uiConfig: {
31+
appTitle: "LlamaIndex App",
32+
starterQuestions: ["Who is the first president of the United States?"],
33+
},
34+
}).start();
35+
```
36+
37+
## Running the Server
38+
39+
In the same directory as `index.ts`, run the following command to start the server:
40+
41+
```bash
42+
tsx index.ts
43+
```
44+
45+
The server will start at `http://localhost:3000`
46+
47+
You can also make a request to the server:
48+
49+
```bash
50+
curl -X POST "http://localhost:3000/api/chat" -H "Content-Type: application/json" -d '{"message": "Who is the first president of the United States?"}'
51+
```
52+
53+
## Configuration Options
54+
55+
The `LlamaIndexServer` accepts the following configuration options:
56+
57+
- `workflow`: A callable function that creates a workflow instance for each request
58+
- `uiConfig`: An object to configure the chat UI containing the following properties:
59+
- `appTitle`: The title of the application (default: `"LlamaIndex App"`)
60+
- `starterQuestions`: List of starter questions for the chat UI (default: `[]`)
61+
- `componentsDir`: The directory for custom UI components rendering events emitted by the workflow. The default is undefined, which does not render custom UI components.
62+
- `llamaCloudIndexSelector`: Whether to show the LlamaCloud index selector in the chat UI (requires `LLAMA_CLOUD_API_KEY` to be set in the environment variables) (default: `false`)
63+
64+
LlamaIndexServer accepts all the configuration options from Nextjs Custom Server such as `port`, `hostname`, `dev`, etc.
65+
See all Nextjs Custom Server options [here](https://nextjs.org/docs/app/building-your-application/configuring/custom-server).
66+
67+
## AI-generated UI Components
68+
69+
The LlamaIndex server provides support for rendering workflow events using custom UI components, allowing you to extend and customize the chat interface.
70+
These components can be auto-generated using an LLM by providing a JSON schema of the workflow event.
71+
72+
### UI Event Schema
73+
74+
To display custom UI components, your workflow needs to emit UI events that have an event type for identification and a data object:
75+
76+
```typescript
77+
class UIEvent extends WorkflowEvent<{
78+
type: "ui_event";
79+
data: UIEventData;
80+
}> {}
81+
```
82+
83+
The `data` object can be any JSON object. To enable AI generation of the UI component, you need to provide a schema for that data (here we're using Zod):
84+
85+
```typescript
86+
const MyEventDataSchema = z
87+
.object({
88+
stage: z
89+
.enum(["retrieve", "analyze", "answer"])
90+
.describe("The current stage the workflow process is in."),
91+
progress: z
92+
.number()
93+
.min(0)
94+
.max(1)
95+
.describe("The progress in percent of the current stage"),
96+
})
97+
.describe("WorkflowStageProgress");
98+
99+
type UIEventData = z.infer<typeof MyEventDataSchema>;
100+
```
101+
102+
### Generate UI Components
103+
104+
The `generateEventComponent` function uses an LLM to generate a custom UI component based on the JSON schema of a workflow event. The schema should contain accurate descriptions of each field so that the LLM can generate matching components for your use case. We've done this for you in the example above using the `describe` function from Zod:
105+
106+
```typescript
107+
import { OpenAI } from "llamaindex";
108+
import { generateEventComponent } from "@llamaindex/server";
109+
import { MyEventDataSchema } from "./your-workflow";
110+
111+
// Also works well with Claude 3.5 Sonnet and Google Gemini 2.5 Pro
112+
const llm = new OpenAI({ model: "gpt-4.1" });
113+
const code = generateEventComponent(MyEventDataSchema, llm);
114+
```
115+
116+
After generating the code, we need to save it to a file. The file name must match the event type from your workflow (e.g., `ui_event.jsx` for handling events with `ui_event` type):
117+
118+
```ts
119+
fs.writeFileSync("components/ui_event.jsx", code);
120+
```
121+
122+
Feel free to modify the generated code to match your needs. If you're not satisfied with the generated code, we suggest improving the provided JSON schema first or trying another LLM.
123+
124+
> Note that `generateEventComponent` is generating JSX code, but you can also provide a TSX file.
125+
126+
### Server Setup
127+
128+
To use the generated UI components, you need to initialize the LlamaIndex server with the `componentsDir` that contains your custom UI components:
129+
130+
```ts
131+
new LlamaIndexServer({
132+
workflow: createWorkflow,
133+
uiConfig: {
134+
appTitle: "LlamaIndex App",
135+
componentsDir: "components",
136+
},
137+
}).start();
138+
```
139+
140+
## Default Endpoints and Features
141+
142+
### Chat Endpoint
143+
144+
The server includes a default chat endpoint at `/api/chat` for handling chat interactions.
145+
146+
### Chat UI
147+
148+
The server always provides a chat interface at the root path (`/`) with:
149+
150+
- Configurable starter questions
151+
- Real-time chat interface
152+
- API endpoint integration
153+
154+
### Static File Serving
155+
156+
- The server automatically mounts the `data` and `output` folders at `{server_url}{api_prefix}/files/data` (default: `/api/files/data`) and `{server_url}{api_prefix}/files/output` (default: `/api/files/output`) respectively.
157+
- Your workflows can use both folders to store and access files. By convention, the `data` folder is used for documents that are ingested, and the `output` folder is used for documents generated by the workflow.
158+
159+
## API Reference
160+
161+
- [LlamaIndexServer](https://ts.llamaindex.ai/docs/api/classes/LlamaIndexServer)

packages/server/eslint.config.mjs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import eslint from "@eslint/js";
2+
import eslintConfigPrettier from "eslint-config-prettier";
3+
import globals from "globals";
4+
import tseslint from "typescript-eslint";
5+
6+
export default tseslint.config(
7+
eslint.configs.recommended,
8+
...tseslint.configs.recommended,
9+
eslintConfigPrettier,
10+
{
11+
languageOptions: {
12+
ecmaVersion: 2022,
13+
sourceType: "module",
14+
globals: {
15+
...globals.browser,
16+
...globals.node,
17+
},
18+
},
19+
},
20+
{
21+
rules: {
22+
"no-irregular-whitespace": "off",
23+
"@typescript-eslint/no-unused-vars": "off",
24+
"@typescript-eslint/no-explicit-any": [
25+
"error",
26+
{
27+
ignoreRestArgs: true,
28+
},
29+
],
30+
},
31+
},
32+
{
33+
ignores: [
34+
"**/dist/**",
35+
"**/lib/*",
36+
"**/.next/**",
37+
"**/out/**",
38+
"**/node_modules/**",
39+
"**/build/**",
40+
],
41+
},
42+
);

0 commit comments

Comments
 (0)