2023-04-05 20:23:25 +00:00
|
|
|
"""Example FastAPI server for llama.cpp.
|
|
|
|
|
|
|
|
To run this example:
|
|
|
|
|
|
|
|
```bash
|
2023-07-08 01:38:46 +00:00
|
|
|
pip install fastapi uvicorn sse-starlette pydantic-settings
|
2023-04-05 20:23:25 +00:00
|
|
|
export MODEL=../models/7B/...
|
|
|
|
```
|
|
|
|
|
2023-04-29 05:43:37 +00:00
|
|
|
Then run:
|
|
|
|
```
|
|
|
|
uvicorn llama_cpp.server.app:app --reload
|
|
|
|
```
|
2023-04-05 20:23:25 +00:00
|
|
|
|
2023-04-29 05:43:37 +00:00
|
|
|
or
|
2023-04-05 20:23:25 +00:00
|
|
|
|
2023-04-29 05:43:37 +00:00
|
|
|
```
|
|
|
|
python3 -m llama_cpp.server
|
|
|
|
```
|
2023-04-05 20:23:25 +00:00
|
|
|
|
2023-04-29 05:43:37 +00:00
|
|
|
Then visit http://localhost:8000/docs to see the interactive API docs.
|
2023-04-05 20:23:25 +00:00
|
|
|
|
2023-04-29 05:43:37 +00:00
|
|
|
"""
|
|
|
|
import os
|
2023-05-05 16:08:28 +00:00
|
|
|
import argparse
|
|
|
|
|
2023-04-29 05:43:37 +00:00
|
|
|
import uvicorn
|
2023-04-05 20:23:25 +00:00
|
|
|
|
2023-05-05 16:08:28 +00:00
|
|
|
from llama_cpp.server.app import create_app, Settings
|
2023-04-05 20:23:25 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-05-08 18:46:25 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
2023-07-09 22:03:31 +00:00
|
|
|
for name, field in Settings.model_fields.items():
|
|
|
|
description = field.description
|
2023-05-08 18:46:25 +00:00
|
|
|
if field.default is not None and description is not None:
|
|
|
|
description += f" (default: {field.default})"
|
2023-05-05 16:08:28 +00:00
|
|
|
parser.add_argument(
|
|
|
|
f"--{name}",
|
|
|
|
dest=name,
|
2023-07-09 22:03:31 +00:00
|
|
|
type=field.annotation if field.annotation is not None else str,
|
2023-05-08 18:46:25 +00:00
|
|
|
help=description,
|
2023-05-05 16:08:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2023-05-08 18:20:53 +00:00
|
|
|
settings = Settings(**{k: v for k, v in vars(args).items() if v is not None})
|
2023-05-05 16:08:28 +00:00
|
|
|
app = create_app(settings=settings)
|
2023-04-05 20:23:25 +00:00
|
|
|
|
2023-04-07 01:07:35 +00:00
|
|
|
uvicorn.run(
|
2023-06-11 00:11:24 +00:00
|
|
|
app, host=os.getenv("HOST", settings.host), port=int(os.getenv("PORT", settings.port))
|
2023-04-07 01:07:35 +00:00
|
|
|
)
|