-
Notifications
You must be signed in to change notification settings - Fork 189
chore: add llamaindex server package #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
95f646e
copy llamaindex server package code
thucpn c294250
fix: changelog.md of create-llama should be in packages/create-llama
thucpn 37215e5
prevent format
thucpn 04ef180
llamaindex workspace:* -> fixed version
thucpn 6294ad5
update lock
thucpn 146fed1
missing bunchee
thucpn 0efb823
update build and dev comamnds
thucpn 3d27874
add lint check
thucpn b4ecd13
update lint config
thucpn aa96989
setup prettier
thucpn d146403
update ignore prettier
thucpn 632487e
run format
thucpn 81af2df
update scripts
thucpn d1ac62f
fix: format
thucpn bab4a48
Create smooth-bees-brush.md
thucpn 1026e0f
update readme
thucpn cbdf19c
postbuild copy README file in root
thucpn 348f828
include readme and license in published npm package
thucpn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,7 @@ __pycache__ | |
|
||
# build artifacts | ||
create-llama-*.tgz | ||
|
||
# copied from root | ||
README.md | ||
LICENSE.md |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
server/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
lib/ | ||
dist/ | ||
server/ | ||
next/.next/ | ||
next/out/ | ||
node_modules/ | ||
build/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**", | ||
], | ||
}, | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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