Elffuss BraidHub

kikocis/llama-stack public

main · 17.5 KiBRenderedRawPermalink
1# llama-stack
2
3[![PyPI - Downloads](https://img.shields.io/pypi/dm/llama-stack)](https://pypi.org/project/llama-stack/)
4[![Discord](https://img.shields.io/discord/1257833999603335178)](https://discord.gg/TZAAYNVtrU)
5
6This repository contains the specifications and implementations of the APIs which are part of the Llama Stack.
7
8The Llama Stack defines and standardizes the building blocks needed to bring generative AI applications to market. These blocks span the entire development lifecycle: from model training and fine-tuning, through product evaluation, to invoking AI agents in production. Beyond definition, we're developing open-source versions and partnering with cloud providers, ensuring developers can assemble AI solutions using consistent, interlocking pieces across platforms. The ultimate goal is to accelerate innovation in the AI space.
9
10The Stack APIs are rapidly improving, but still very much work in progress and we invite feedback as well as direct contributions.
11
12
13## APIs
14
15The Llama Stack consists of the following set of APIs:
16
17- Inference
18- Safety
19- Memory
20- Agentic System
21- Evaluation
22- Post Training
23- Synthetic Data Generation
24- Reward Scoring
25
26Each of the APIs themselves is a collection of REST endpoints.
27
28## API Providers
29
30A Provider is what makes the API real -- they provide the actual implementation backing the API.
31
32As an example, for Inference, we could have the implementation be backed by open source libraries like `[ torch | vLLM | TensorRT ]` as possible options.
33
34A provider can also be just a pointer to a remote REST service -- for example, cloud providers or dedicated inference providers could serve these APIs.
35
36
37## Llama Stack Distribution
38
39A Distribution is where APIs and Providers are assembled together to provide a consistent whole to the end application developer. You can mix-and-match providers -- some could be backed by local code and some could be remote. As a hobbyist, you can serve a small model locally, but can choose a cloud provider for a large model. Regardless, the higher level APIs your app needs to work with don't need to change at all. You can even imagine moving across the server / mobile-device boundary as well always using the same uniform set of APIs for developing Generative AI applications.
40
41
42## Installation
43
44You can install this repository as a [package](https://pypi.org/project/llama-stack/) with `pip install llama-stack`
45
46If you want to install from source:
47
48```bash
49mkdir -p ~/local
50cd ~/local
51git clone git@github.com:meta-llama/llama-stack.git
52
53conda create -n stack python=3.10
54conda activate stack
55
56cd llama-stack
57$CONDA_PREFIX/bin/pip install -e .
58```
59
60# Getting Started
61
62The `llama` CLI tool helps you setup and use the Llama toolchain & agentic systems. It should be available on your path after installing the `llama-stack` package.
63
64This guides allows you to quickly get started with building and running a Llama Stack server in < 5 minutes!
65
66You may also checkout this [notebook](https://github.com/meta-llama/llama-stack/blob/main/docs/getting_started.ipynb) for trying out out demo scripts.
67
68## Quick Cheatsheet
69- Quick 3 line command to build and start a LlamaStack server using our Meta Reference implementation for all API endpoints with `conda` as build type.
70
71**`llama stack build`**
72- You'll be prompted to enter build information interactively.
73```
74llama stack build
75
76> Enter an unique name for identifying your Llama Stack build distribution (e.g. my-local-stack): my-local-stack
77> Enter the image type you want your distribution to be built with (docker or conda): conda
78
79 Llama Stack is composed of several APIs working together. Let's configure the providers (implementations) you want to use for these APIs.
80> Enter the API provider for the inference API: (default=meta-reference): meta-reference
81> Enter the API provider for the safety API: (default=meta-reference): meta-reference
82> Enter the API provider for the agents API: (default=meta-reference): meta-reference
83> Enter the API provider for the memory API: (default=meta-reference): meta-reference
84> Enter the API provider for the telemetry API: (default=meta-reference): meta-reference
85
86 > (Optional) Enter a short description for your Llama Stack distribution:
87
88Build spec configuration saved at ~/.conda/envs/llamastack-my-local-stack/my-local-stack-build.yaml
89You can now run `llama stack configure my-local-stack`
90```
91
92**`llama stack configure`**
93- Run `llama stack configure <name>` with the name you have previously defined in `build` step.
94```
95llama stack configure <name>
96```
97- You will be prompted to enter configurations for your Llama Stack
98
99```
100$ llama stack configure my-local-stack
101
102Could not find my-local-stack. Trying conda build name instead...
103Configuring API `inference`...
104=== Configuring provider `meta-reference` for API inference...
105Enter value for model (default: Llama3.1-8B-Instruct) (required):
106Do you want to configure quantization? (y/n): n
107Enter value for torch_seed (optional):
108Enter value for max_seq_len (default: 4096) (required):
109Enter value for max_batch_size (default: 1) (required):
110
111Configuring API `safety`...
112=== Configuring provider `meta-reference` for API safety...
113Do you want to configure llama_guard_shield? (y/n): n
114Do you want to configure prompt_guard_shield? (y/n): n
115
116Configuring API `agents`...
117=== Configuring provider `meta-reference` for API agents...
118Enter `type` for persistence_store (options: redis, sqlite, postgres) (default: sqlite):
119
120Configuring SqliteKVStoreConfig:
121Enter value for namespace (optional):
122Enter value for db_path (default: /home/xiyan/.llama/runtime/kvstore.db) (required):
123
124Configuring API `memory`...
125=== Configuring provider `meta-reference` for API memory...
126> Please enter the supported memory bank type your provider has for memory: vector
127
128Configuring API `telemetry`...
129=== Configuring provider `meta-reference` for API telemetry...
130
131> YAML configuration has been written to ~/.llama/builds/conda/my-local-stack-run.yaml.
132You can now run `llama stack run my-local-stack --port PORT`
133```
134
135**`llama stack run`**
136- Run `llama stack run <name>` with the name you have previously defined.
137```
138llama stack run my-local-stack
139
140...
141> initializing model parallel with size 1
142> initializing ddp with size 1
143> initializing pipeline with size 1
144...
145Finished model load YES READY
146Serving POST /inference/chat_completion
147Serving POST /inference/completion
148Serving POST /inference/embeddings
149Serving POST /memory_banks/create
150Serving DELETE /memory_bank/documents/delete
151Serving DELETE /memory_banks/drop
152Serving GET /memory_bank/documents/get
153Serving GET /memory_banks/get
154Serving POST /memory_bank/insert
155Serving GET /memory_banks/list
156Serving POST /memory_bank/query
157Serving POST /memory_bank/update
158Serving POST /safety/run_shield
159Serving POST /agentic_system/create
160Serving POST /agentic_system/session/create
161Serving POST /agentic_system/turn/create
162Serving POST /agentic_system/delete
163Serving POST /agentic_system/session/delete
164Serving POST /agentic_system/session/get
165Serving POST /agentic_system/step/get
166Serving POST /agentic_system/turn/get
167Serving GET /telemetry/get_trace
168Serving POST /telemetry/log_event
169Listening on :::5000
170INFO: Started server process [587053]
171INFO: Waiting for application startup.
172INFO: Application startup complete.
173INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit)
174```
175
176
177## Step 1. Build
178In the following steps, imagine we'll be working with a `Meta-Llama3.1-8B-Instruct` model. We will name our build `8b-instruct` to help us remember the config. We will start build our distribution (in the form of a Conda environment, or Docker image). In this step, we will specify:
179- `name`: the name for our distribution (e.g. `8b-instruct`)
180- `image_type`: our build image type (`conda | docker`)
181- `distribution_spec`: our distribution specs for specifying API providers
182 - `description`: a short description of the configurations for the distribution
183 - `providers`: specifies the underlying implementation for serving each API endpoint
184 - `image_type`: `conda` | `docker` to specify whether to build the distribution in the form of Docker image or Conda environment.
185
186
187At the end of build command, we will generate `<name>-build.yaml` file storing the build configurations.
188
189After this step is complete, a file named `<name>-build.yaml` will be generated and saved at the output file path specified at the end of the command.
190
191#### Building from scratch
192- For a new user, we could start off with running `llama stack build` which will allow you to a interactively enter wizard where you will be prompted to enter build configurations.
193```
194llama stack build
195```
196
197Running the command above will allow you to fill in the configuration to build your Llama Stack distribution, you will see the following outputs.
198
199```
200> Enter an unique name for identifying your Llama Stack build distribution (e.g. my-local-stack): 8b-instruct
201> Enter the image type you want your distribution to be built with (docker or conda): conda
202
203 Llama Stack is composed of several APIs working together. Let's configure the providers (implementations) you want to use for these APIs.
204> Enter the API provider for the inference API: (default=meta-reference): meta-reference
205> Enter the API provider for the safety API: (default=meta-reference): meta-reference
206> Enter the API provider for the agents API: (default=meta-reference): meta-reference
207> Enter the API provider for the memory API: (default=meta-reference): meta-reference
208> Enter the API provider for the telemetry API: (default=meta-reference): meta-reference
209
210 > (Optional) Enter a short description for your Llama Stack distribution:
211
212Build spec configuration saved at ~/.conda/envs/llamastack-my-local-llama-stack/8b-instruct-build.yaml
213```
214
215**Ollama (optional)**
216
217If you plan to use Ollama for inference, you'll need to install the server [via these instructions](https://ollama.com/download).
218
219
220#### Building from templates
221- To build from alternative API providers, we provide distribution templates for users to get started building a distribution backed by different providers.
222
223The following command will allow you to see the available templates and their corresponding providers.
224```
225llama stack build --list-templates
226```
227
228![alt text](resources/list-templates.png)
229
230You may then pick a template to build your distribution with providers fitted to your liking.
231
232```
233llama stack build --template local-tgi --name my-tgi-stack
234```
235
236```
237$ llama stack build --template local-tgi --name my-tgi-stack
238...
239...
240Build spec configuration saved at ~/.conda/envs/llamastack-my-tgi-stack/my-tgi-stack-build.yaml
241You may now run `llama stack configure my-tgi-stack` or `llama stack configure ~/.conda/envs/llamastack-my-tgi-stack/my-tgi-stack-build.yaml`
242```
243
244#### Building from config file
245- In addition to templates, you may customize the build to your liking through editing config files and build from config files with the following command.
246
247- The config file will be of contents like the ones in `llama_stack/distributions/templates/`.
248
249```
250$ cat llama_stack/distribution/templates/local-ollama-build.yaml
251
252name: local-ollama
253distribution_spec:
254 description: Like local, but use ollama for running LLM inference
255 providers:
256 inference: remote::ollama
257 memory: meta-reference
258 safety: meta-reference
259 agents: meta-reference
260 telemetry: meta-reference
261image_type: conda
262```
263
264```
265llama stack build --config llama_stack/distribution/templates/local-ollama-build.yaml
266```
267
268#### How to build distribution with Docker image
269
270To build a docker image, you may start off from a template and use the `--image-type docker` flag to specify `docker` as the build image type.
271
272```
273llama stack build --template local --image-type docker --name docker-0
274```
275
276Alternatively, you may use a config file and set `image_type` to `docker` in our `<name>-build.yaml` file, and run `llama stack build <name>-build.yaml`. The `<name>-build.yaml` will be of contents like:
277
278```
279name: local-docker-example
280distribution_spec:
281 description: Use code from `llama_stack` itself to serve all llama stack APIs
282 docker_image: null
283 providers:
284 inference: meta-reference
285 memory: meta-reference-faiss
286 safety: meta-reference
287 agentic_system: meta-reference
288 telemetry: console
289image_type: docker
290```
291
292The following command allows you to build a Docker image with the name `<name>`
293```
294llama stack build --config <name>-build.yaml
295
296Dockerfile created successfully in /tmp/tmp.I0ifS2c46A/DockerfileFROM python:3.10-slim
297WORKDIR /app
298...
299...
300You can run it with: podman run -p 8000:8000 llamastack-docker-local
301Build spec configuration saved at ~/.llama/distributions/docker/docker-local-build.yaml
302```
303
304
305## Step 2. Configure
306After our distribution is built (either in form of docker or conda environment), we will run the following command to
307```
308llama stack configure [ <name> | <docker-image-name> | <path/to/name.build.yaml>]
309```
310- For `conda` environments: <path/to/name.build.yaml> would be the generated build spec saved from Step 1.
311- For `docker` images downloaded from Dockerhub, you could also use <docker-image-name> as the argument.
312 - Run `docker images` to check list of available images on your machine.
313
314```
315$ llama stack configure 8b-instruct
316
317Configuring API: inference (meta-reference)
318Enter value for model (existing: Meta-Llama3.1-8B-Instruct) (required):
319Enter value for quantization (optional):
320Enter value for torch_seed (optional):
321Enter value for max_seq_len (existing: 4096) (required):
322Enter value for max_batch_size (existing: 1) (required):
323
324Configuring API: memory (meta-reference-faiss)
325
326Configuring API: safety (meta-reference)
327Do you want to configure llama_guard_shield? (y/n): y
328Entering sub-configuration for llama_guard_shield:
329Enter value for model (default: Llama-Guard-3-8B) (required):
330Enter value for excluded_categories (default: []) (required):
331Enter value for disable_input_check (default: False) (required):
332Enter value for disable_output_check (default: False) (required):
333Do you want to configure prompt_guard_shield? (y/n): y
334Entering sub-configuration for prompt_guard_shield:
335Enter value for model (default: Prompt-Guard-86M) (required):
336
337Configuring API: agentic_system (meta-reference)
338Enter value for brave_search_api_key (optional):
339Enter value for bing_search_api_key (optional):
340Enter value for wolfram_api_key (optional):
341
342Configuring API: telemetry (console)
343
344YAML configuration has been written to ~/.llama/builds/conda/8b-instruct-run.yaml
345```
346
347After this step is successful, you should be able to find a run configuration spec in `~/.llama/builds/conda/8b-instruct-run.yaml` with the following contents. You may edit this file to change the settings.
348
349As you can see, we did basic configuration above and configured:
350- inference to run on model `Meta-Llama3.1-8B-Instruct` (obtained from `llama model list`)
351- Llama Guard safety shield with model `Llama-Guard-3-8B`
352- Prompt Guard safety shield with model `Prompt-Guard-86M`
353
354For how these configurations are stored as yaml, checkout the file printed at the end of the configuration.
355
356Note that all configurations as well as models are stored in `~/.llama`
357
358
359## Step 3. Run
360Now, let's start the Llama Stack Distribution Server. You will need the YAML configuration file which was written out at the end by the `llama stack configure` step.
361
362```
363llama stack run 8b-instruct
364```
365
366You should see the Llama Stack server start and print the APIs that it is supporting
367
368```
369$ llama stack run 8b-instruct
370
371> initializing model parallel with size 1
372> initializing ddp with size 1
373> initializing pipeline with size 1
374Loaded in 19.28 seconds
375NCCL version 2.20.5+cuda12.4
376Finished model load YES READY
377Serving POST /inference/batch_chat_completion
378Serving POST /inference/batch_completion
379Serving POST /inference/chat_completion
380Serving POST /inference/completion
381Serving POST /safety/run_shield
382Serving POST /agentic_system/memory_bank/attach
383Serving POST /agentic_system/create
384Serving POST /agentic_system/session/create
385Serving POST /agentic_system/turn/create
386Serving POST /agentic_system/delete
387Serving POST /agentic_system/session/delete
388Serving POST /agentic_system/memory_bank/detach
389Serving POST /agentic_system/session/get
390Serving POST /agentic_system/step/get
391Serving POST /agentic_system/turn/get
392Listening on :::5000
393INFO: Started server process [453333]
394INFO: Waiting for application startup.
395INFO: Application startup complete.
396INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit)
397```
398
399> [!NOTE]
400> Configuration is in `~/.llama/builds/local/conda/8b-instruct-run.yaml`. Feel free to increase `max_seq_len`.
401
402> [!IMPORTANT]
403> The "local" distribution inference server currently only supports CUDA. It will not work on Apple Silicon machines.
404
405> [!TIP]
406> You might need to use the flag `--disable-ipv6` to Disable IPv6 support
407
408This server is running a Llama model locally.
409
410## Step 4. Test with Client
411Once the server is setup, we can test it with a client to see the example outputs.
412```
413cd /path/to/llama-stack
414conda activate <env> # any environment containing the llama-stack pip package will work
415
416python -m llama_stack.apis.inference.client localhost 5000
417```
418
419This will run the chat completion client and query the distribution’s /inference/chat_completion API.
420
421Here is an example output:
422```
423User>hello world, write me a 2 sentence poem about the moon
424Assistant> Here's a 2-sentence poem about the moon:
425
426The moon glows softly in the midnight sky,
427A beacon of wonder, as it passes by.
428```
429
430Similarly you can test safety (if you configured llama-guard and/or prompt-guard shields) by:
431
432```
433python -m llama_stack.apis.safety.client localhost 5000
434```
435
436You can find more example scripts with client SDKs to talk with the Llama Stack server in our [llama-stack-apps](https://github.com/meta-llama/llama-stack-apps/tree/main/examples) repo.