llama-index fastapi server
Project description
LlamaIndex Server
LlamaIndexServer is a FastAPI-based application that allows you to quickly launch your LlamaIndex Workflows and Agent Workflows 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 FastAPI for high performance and easy API development
- Optional built-in chat UI
- Prebuilt development code
Installation
pip install llama-index-server
Quick Start
# main.py
from llama_index.core.agent.workflow import AgentWorkflow
from llama_index.core.workflow import Workflow
from llama_index.core.tools import FunctionTool
from llama_index.server import LlamaIndexServer
# Define a factory function that returns a Workflow or AgentWorkflow
def create_workflow() -> Workflow:
def fetch_weather(city: str) -> str:
return f"The weather in {city} is sunny"
return AgentWorkflow.from_tools(
tools=[
FunctionTool.from_defaults(
fn=fetch_weather,
)
]
)
# Create an API server for the workflow
app = LlamaIndexServer(
workflow_factory=create_workflow, # Supports Workflow or AgentWorkflow
env="dev", # Enable development mode
include_ui=True, # Include chat UI
starter_questions=["What can you do?", "How do I use this?"],
verbose=True
)
Running the Server
-
In the same directory as
main.py, run the following command to start the server:fastapi dev -
Making a request to the server:
curl -X POST "http://localhost:8000/api/chat" -H "Content-Type: application/json" -d '{"message": "What is the weather in Tokyo?"}'
-
See the API documentation at
http://localhost:8000/docs -
Access the chat UI at
http://localhost:8000/(Make sure you set theenv="dev"orinclude_ui=Truein the server configuration)
Configuration Options
The LlamaIndexServer accepts the following configuration parameters:
workflow_factory: A callable that creates a workflow instance for each requestlogger: Optional logger instance (defaults to uvicorn logger)use_default_routers: Whether to include default routers (chat, static file serving)env: Environment setting ('dev' enables CORS and UI by default)include_ui: Whether to include the chat UIstarter_questions: List of starter questions for the chat UIverbose: Enable verbose loggingapi_prefix: API route prefix (default: "/api")server_url: The deployment URL of the server (default is None)ui_path: Path for downloaded UI static files (default: ".ui")
Default Routers and Features
Chat Router
The server includes a default chat router at /api/chat for handling chat interactions.
Static File Serving
- The server automatically mounts the
dataandoutputfolders 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. As a convention, the
datafolder is used for documents that are ingested and theoutputfolder is used for documents that are generated by the workflow. - The example workflows from
create-llama(see below) are following this pattern.
Chat UI
When enabled, the server provides a chat interface at the root path (/) with:
- Configurable starter questions
- Real-time chat interface
- API endpoint integration
Development Mode
In development mode (env="dev"), the server:
- Enables CORS for all origins
- Automatically includes the chat UI
- Provides more verbose logging
API Endpoints
The server provides the following default endpoints:
/api/chat: Chat interaction endpoint/api/files/data/*: Access to data directory files/api/files/output/*: Access to output directory files
Best Practices
- Always provide a workflow factory that creates fresh workflow instances
- Use environment variables for sensitive configuration
- Enable verbose logging during development
- Configure CORS appropriately for your deployment environment
- Use starter questions to guide users in the chat UI
Getting Started with a New Project
Want to start a new project with LlamaIndexServer? Check out our create-llama tool to quickly generate a new project with LlamaIndexServer.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llama_index_server-0.1.8.tar.gz.
File metadata
- Download URL: llama_index_server-0.1.8.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.11.11 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c633da6fb35cd764f2eeb753a54e2e6b1025d0010a0f34ab528251096f0583d7
|
|
| MD5 |
5b12f8a668a5a2d8185b60b1b735b34e
|
|
| BLAKE2b-256 |
a68158afcc9856c5e4858251874bdd1297618909b1f2e5af1516c09b563015d3
|
File details
Details for the file llama_index_server-0.1.8-py3-none-any.whl.
File metadata
- Download URL: llama_index_server-0.1.8-py3-none-any.whl
- Upload date:
- Size: 32.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.11.11 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e80c7c221d07e56a3a9e2349d541d4a5e029f5a7403b67dca6b720acd2b36e08
|
|
| MD5 |
b6fbe1c0b38a6e830d19312e8cddd369
|
|
| BLAKE2b-256 |
cc00c6cc571fb50109e4ec8eab52d83739957d4d9d073c9071f2f30d305030ac
|