Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/smooth-bees-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"create-llama": patch
"@llamaindex/server": patch
---

chore: add llamaindex server package
7 changes: 7 additions & 0 deletions packages/create-llama/README.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,11 @@ Pro mode is ideal for developers who want fine-grained control over their projec
- [TS/JS docs](https://ts.llamaindex.ai/)
- [Python docs](https://docs.llamaindex.ai/en/stable/)

## LlamaIndex Server

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:

- [LlamaIndex Server For TypeScript](./packages/server/README.md)
- [LlamaIndex Server For Python](./python/llama-index-server/README.md)

Inspired by and adapted from [create-next-app](https://github.com/vercel/next.js/tree/canary/packages/create-next-app)
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
"new-version": "pnpm -r build && changeset version",
"release": "pnpm -r build && changeset publish",
"release-snapshot": "pnpm -r build && changeset publish --tag snapshot",
"build": "pnpm -r --filter create-llama build",
"e2e": "pnpm -r --filter create-llama e2e",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: add e2e tests for server package

"dev": "pnpm -r --filter create-llama dev",
"format": "pnpm -r --filter create-llama format",
"format:write": "pnpm -r --filter create-llama format:write",
"lint": "pnpm -r --filter create-llama lint"
"build": "pnpm -r build",
"e2e": "pnpm -r e2e",
"dev": "pnpm -r dev",
"format": "pnpm -r format",
"format:write": "pnpm -r format:write",
"lint": "pnpm -r lint"
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/create-llama/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ __pycache__

# build artifacts
create-llama-*.tgz

# copied from root
README.md
LICENSE.md
File renamed without changes.
6 changes: 5 additions & 1 deletion packages/create-llama/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
"create-llama": "./dist/index.js"
},
"files": [
"dist"
"dist",
"README.md",
"LICENSE.md"
],
"scripts": {
"copy": "cp -r ../../README.md ../../LICENSE.md .",
"build": "bash ./scripts/build.sh",
"build:ncc": "pnpm run clean && ncc build ./index.ts -o ./dist/ --minify --no-cache --no-source-map-register",
"postbuild": "pnpm run copy",
"clean": "rimraf --glob ./dist ./templates/**/__pycache__ ./templates/**/node_modules ./templates/**/poetry.lock",
"dev": "ncc build ./index.ts -w -o dist/",
"e2e": "playwright test",
Expand Down
1 change: 1 addition & 0 deletions packages/server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server/
7 changes: 7 additions & 0 deletions packages/server/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
lib/
dist/
server/
next/.next/
next/out/
node_modules/
build/
115 changes: 115 additions & 0 deletions packages/server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# @llamaindex/server

## 0.1.6

### Patch Changes

- 82d4b46: feat: re-add supports for artifacts

## 0.1.5

### Patch Changes

- 7ca9ddf: Add generate ui workflow to @llamaindex/server
- 3310eaa: chore: bump chat-ui
- [email protected]

## 0.1.4

### Patch Changes

- [email protected]

## 0.1.3

### Patch Changes

- edb8b87: fix: shadcn components cannot be used in next server
- Updated dependencies [6cf928f]
- [email protected]

## 0.1.2

### Patch Changes

- bb34ade: feat: support cn utils for server UI
- [email protected]

## 0.1.1

### Patch Changes

- 400b3b5: feat: use full-source code with import statements for custom comps
- [email protected]

## 0.1.0

### Minor Changes

- 3ffee26: feat: enhance config params for LlamaIndexServer

## 0.0.9

### Patch Changes

- 0b75bd6: feat: component dir in llamaindex server

## 0.0.8

### Patch Changes

- Updated dependencies [3534c37]
- [email protected]

## 0.0.7

### Patch Changes

- 4999df1: bump nextjs
- Updated dependencies [f5e4d09]
- [email protected]

## 0.0.6

### Patch Changes

- 8c02684: fix: handle stream error
- c515a32: feat: return raw output for agent toolcall result
- [email protected]

## 0.0.5

### Patch Changes

- 9d951b2: feat: support llamacloud in @llamaindex/server
- Updated dependencies [9d951b2]
- [email protected]

## 0.0.4

### Patch Changes

- 164cf7a: fix: custom next server start fail

## 0.0.3

### Patch Changes

- 299008b: feat: copy create-llama to @llamaindex/servers
- 75d6e29: feat: response source nodes in query tool output
- Updated dependencies [75d6e29]
- [email protected]

## 0.0.2

### Patch Changes

- f8a86e4: feat: @llamaindex/server
- Updated dependencies [21bebfc]
- Updated dependencies [93bc0ff]
- Updated dependencies [91a18e7]
- Updated dependencies [f8a86e4]
- Updated dependencies [5189b44]
- Updated dependencies [58a9446]
- @llamaindex/[email protected]
- @llamaindex/[email protected]
161 changes: 161 additions & 0 deletions packages/server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# LlamaIndex Server

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.

## Features

- Serving a workflow as a chatbot
- Built on Next.js for high performance and easy API development
- Optional built-in chat UI with extendable UI components
- Prebuilt development code

## Installation

```bash
npm i @llamaindex/server
```

## Quick Start

Create an `index.ts` file and add the following code:

```ts
import { LlamaIndexServer } from "@llamaindex/server";
import { wiki } from "@llamaindex/tools"; // or any other tool

const createWorkflow = () => agent({ tools: [wiki()] });

new LlamaIndexServer({
workflow: createWorkflow,
uiConfig: {
appTitle: "LlamaIndex App",
starterQuestions: ["Who is the first president of the United States?"],
},
}).start();
```

## Running the Server

In the same directory as `index.ts`, run the following command to start the server:

```bash
tsx index.ts
```

The server will start at `http://localhost:3000`

You can also make a request to the server:

```bash
curl -X POST "http://localhost:3000/api/chat" -H "Content-Type: application/json" -d '{"message": "Who is the first president of the United States?"}'
```

## Configuration Options

The `LlamaIndexServer` accepts the following configuration options:

- `workflow`: A callable function that creates a workflow instance for each request
- `uiConfig`: An object to configure the chat UI containing the following properties:
- `appTitle`: The title of the application (default: `"LlamaIndex App"`)
- `starterQuestions`: List of starter questions for the chat UI (default: `[]`)
- `componentsDir`: The directory for custom UI components rendering events emitted by the workflow. The default is undefined, which does not render custom UI components.
- `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`)

LlamaIndexServer accepts all the configuration options from Nextjs Custom Server such as `port`, `hostname`, `dev`, etc.
See all Nextjs Custom Server options [here](https://nextjs.org/docs/app/building-your-application/configuring/custom-server).

## AI-generated UI Components

The LlamaIndex server provides support for rendering workflow events using custom UI components, allowing you to extend and customize the chat interface.
These components can be auto-generated using an LLM by providing a JSON schema of the workflow event.

### UI Event Schema

To display custom UI components, your workflow needs to emit UI events that have an event type for identification and a data object:

```typescript
class UIEvent extends WorkflowEvent<{
type: "ui_event";
data: UIEventData;
}> {}
```

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):

```typescript
const MyEventDataSchema = z
.object({
stage: z
.enum(["retrieve", "analyze", "answer"])
.describe("The current stage the workflow process is in."),
progress: z
.number()
.min(0)
.max(1)
.describe("The progress in percent of the current stage"),
})
.describe("WorkflowStageProgress");

type UIEventData = z.infer<typeof MyEventDataSchema>;
```

### Generate UI Components

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:

```typescript
import { OpenAI } from "llamaindex";
import { generateEventComponent } from "@llamaindex/server";
import { MyEventDataSchema } from "./your-workflow";

// Also works well with Claude 3.5 Sonnet and Google Gemini 2.5 Pro
const llm = new OpenAI({ model: "gpt-4.1" });
const code = generateEventComponent(MyEventDataSchema, llm);
```

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):

```ts
fs.writeFileSync("components/ui_event.jsx", code);
```

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.

> Note that `generateEventComponent` is generating JSX code, but you can also provide a TSX file.

### Server Setup

To use the generated UI components, you need to initialize the LlamaIndex server with the `componentsDir` that contains your custom UI components:

```ts
new LlamaIndexServer({
workflow: createWorkflow,
uiConfig: {
appTitle: "LlamaIndex App",
componentsDir: "components",
},
}).start();
```

## Default Endpoints and Features

### Chat Endpoint

The server includes a default chat endpoint at `/api/chat` for handling chat interactions.

### Chat UI

The server always provides a chat interface at the root path (`/`) with:

- Configurable starter questions
- Real-time chat interface
- API endpoint integration

### Static File Serving

- 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.
- 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.

## API Reference

- [LlamaIndexServer](https://ts.llamaindex.ai/docs/api/classes/LlamaIndexServer)
42 changes: 42 additions & 0 deletions packages/server/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import eslint from "@eslint/js";
import eslintConfigPrettier from "eslint-config-prettier";
import globals from "globals";
import tseslint from "typescript-eslint";

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
eslintConfigPrettier,
{
languageOptions: {
ecmaVersion: 2022,
sourceType: "module",
globals: {
...globals.browser,
...globals.node,
},
},
},
{
rules: {
"no-irregular-whitespace": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-explicit-any": [
"error",
{
ignoreRestArgs: true,
},
],
},
},
{
ignores: [
"**/dist/**",
"**/lib/*",
"**/.next/**",
"**/out/**",
"**/node_modules/**",
"**/build/**",
],
},
);
Loading