forked from run-llama/llama_index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
llama-index-networks package (run-llama#11413)
* lint * pants tailor * remove unnecessary files/folders * make api_key an optional field * make secret field optional as well * top level imports * prerelease version bump * remove misleading env variables * override gitignore to add data to demo * add note in demo README that you can use instead of * update demo contributor poetry.locks pyproject.toml * published v0.1.0; updated demo deps
- Loading branch information
1 parent
473a3ee
commit 827ae2f
Showing
66 changed files
with
20,379 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,157 @@ | ||
llama_index/_static | ||
.DS_Store | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
bin/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
etc/ | ||
include/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
share/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
.ruff_cache | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
notebooks/ | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
pyvenv.cfg | ||
.env* | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# Jetbrains | ||
.idea | ||
modules/ | ||
*.swp | ||
|
||
# VsCode | ||
.vscode | ||
|
||
# pipenv | ||
Pipfile | ||
Pipfile.lock | ||
|
||
# pyright | ||
pyrightconfig.json | ||
|
||
# data | ||
data |
This file contains 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,3 @@ | ||
poetry_requirements( | ||
name="poetry", | ||
) |
This file contains 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,5 @@ | ||
# CHANGELOG | ||
|
||
## [0.1.0] - 2024-02-27 | ||
|
||
- Initial release |
This file contains 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,17 @@ | ||
GIT_ROOT ?= $(shell git rev-parse --show-toplevel) | ||
|
||
help: ## Show all Makefile targets. | ||
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}' | ||
|
||
format: ## Run code autoformatters (black). | ||
pre-commit install | ||
git ls-files | xargs pre-commit run black --files | ||
|
||
lint: ## Run linters: pre-commit (black, ruff, codespell) and mypy | ||
pre-commit install && git ls-files | xargs pre-commit run --show-diff-on-failure --files | ||
|
||
test: ## Run tests via pytest. | ||
pytest tests | ||
|
||
watch-docs: ## Build and watch documentation. | ||
sphinx-autobuild docs/ docs/_build/html --open-browser --watch $(GIT_ROOT)/llama_index/ |
This file contains 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,98 @@ | ||
# LlamaIndex Networks | ||
|
||
The `llama-index-networks` library extension allows for the creation of | ||
networks knowledge that can be queried and used for LLM context augmentation. | ||
In such networks, we have "data suppliers" and "data consumers" that participate | ||
and benefit from the network, respectively. | ||
|
||
## Data suppliers | ||
|
||
With this extension, you can easily wrap your llama-index `QueryEngine` with a | ||
`ContributorService` that exposes the query engine behind a REST (FastAPI) | ||
service, which renders it ready to contribute to LlamaIndex Network! | ||
|
||
```python | ||
from llama_index.networks.contributor import ( | ||
ContributorServiceSettings, | ||
ContributorService, | ||
) | ||
|
||
query_engine = ... # build a query engine as typically done with llama-index | ||
|
||
settings = ContributorServiceSettings() | ||
service = ContributorService(config=settings, query_engine=query_engine) | ||
app = service.app | ||
|
||
|
||
if __name__ == "__main__": | ||
import uvicorn | ||
|
||
uvicorn.run(app, port=8000) | ||
``` | ||
|
||
## Data consumers | ||
|
||
With `llama-index-networks`, you can build a `NetworkQueryEngine` that is able | ||
to connect to set of (network) `ContributorService`'s. | ||
|
||
```python | ||
from llama_index.networks.contributor import ContributorClient | ||
from llama_index.networks.query_engine import NetworkQueryEngine | ||
from llama_index.llms.groq import Groq | ||
|
||
# Use ContributorClient to connect to a ContributorService | ||
client = ContributorClient.from_config_file(env_file=".env.contributor.client") | ||
contributors = [client] | ||
|
||
# NetworkQueryEngine | ||
llm = Groq() | ||
network_query_engine = NetworkQueryEngine.from_args( | ||
contributors=contributors, llm=llm | ||
) | ||
|
||
# Can query it like any other query engine | ||
network_query_engine.query("Why is the sky blue?") | ||
``` | ||
|
||
The `.env.contributor.client` file contains the parameters to connect to the | ||
`ContributorService` (i.e., `api_url` and `api_token`.) | ||
|
||
## Examples | ||
|
||
For a full demo, checkout the `examples/demo` sub-folder. | ||
|
||
### FAQ | ||
|
||
#### 1. Can I add my own custom endpoints to a `ContributorService`? | ||
|
||
Yes, the (Fastapi) app is accessible via `ContributorService.app` and can be | ||
modified to your needs. | ||
|
||
```python | ||
settings = ContributorServiceSettings() | ||
service = ContributorService(config=settings, query_engine=query_engine) | ||
app = service.app | ||
|
||
|
||
@app.get("<custom-path>") | ||
def custom_endpoint_logic(): | ||
... | ||
``` | ||
|
||
#### 2. How can I add authentication to a `ContributorService`? | ||
|
||
Currently, the client supports authentication through request headers as well as | ||
as via an `api_token` str. On the server side, you should be able to modify the | ||
app to your needs: | ||
|
||
```python | ||
settings = ContributorServiceSettings() | ||
service = ContributorService(config=settings, query_engine=query_engine) | ||
app = service.app | ||
|
||
# modify app here to include your own security mechanisms | ||
``` | ||
|
||
As we continue to build out this library extension, we welcome any feedback or | ||
suggestions on what can be incorporated on this regard and others in order to | ||
make our tools more helpful to our users. |
This file contains 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,97 @@ | ||
# LlamaIndex Networks Demo | ||
|
||
In this demo, we have 3 network contributor services, whose Python source | ||
codes can be found in the folders listed below: | ||
|
||
- contributor-1/ | ||
- contributor-2/ | ||
- contributor-3/ | ||
|
||
Once all of these services are up and running (see usage instructions | ||
attached below), then we can connect to them using a `NetworkQueryEngine`. | ||
The demo for doing that is found in the notebook: `network_query_engine.ipynb`. | ||
|
||
## Building and Running the 3 Network Contributor Services | ||
|
||
### Setup Environment Variables | ||
|
||
Each of the three Contributor Services wrap `QueryEngine`'s that utilize | ||
OpenAI as the LLM provider. As such, you'll need to supply an `OPENAI_API_KEY`. | ||
|
||
To do so, we make use of .env files. Each contributor folder requires a filled | ||
in `.env.contributor.service` file. You can use the `template.env.contributor.service`, | ||
fill in your openai-api-key and then save it as `.env.contributor.service` (you can also save it simply as `.env` as the `ContributorServiceSettings` | ||
class will look for `.env` file if it can't find `.env.contributor.service`). | ||
|
||
### Installation | ||
|
||
_Requires Poetry v. 1.4.1 to be installed._ | ||
|
||
```sh | ||
cd contributor-1 && poetry install && cd - | ||
cd contributor-2 && poetry install && cd - | ||
cd contributor-3 && poetry install && cd - | ||
``` | ||
|
||
### Running the contributor servers locally | ||
|
||
_Requires Docker to be installed._ | ||
|
||
We've simplified running all three services with the help of | ||
`docker-compose`. It should be noted that in a real-world scenario, these | ||
contributor services are likely independently stood up. | ||
|
||
```sh | ||
docker-compose up --build | ||
``` | ||
|
||
Any code changes will be reflected in the running server automatically without having to rebuild/restart the server. | ||
|
||
### Viewing the SWAGGER docs (local) | ||
|
||
Once the server is running locally, the standard API docs of any of | ||
the three contributor services can be viewed in a browser. | ||
|
||
Use any port number `{8001,8002,8003}`: | ||
|
||
```sh | ||
# visit in any browser | ||
http://localhost:<PORT>/docs#/ | ||
``` | ||
|
||
## Building and Running the NetworkQueryEngine | ||
|
||
We begin by creating a fresh environment: | ||
|
||
```sh | ||
pyenv virtualenv networks-demo | ||
pyenv activate networks-demo | ||
pip install jupyterlab ipykernel | ||
pip install llama-index llama-index-networks --pre | ||
``` | ||
|
||
Let's create the kernel for our notebook so that we can use our newly | ||
created `networks-demo` virtual environment. | ||
|
||
```sh | ||
ipython kernel install --user --name=networks-demo | ||
``` | ||
|
||
### Setting up the environment files | ||
|
||
As in setting up the `ContributorService`'s we need to pass in the settings | ||
for the `ContributorClient`'s (that communicate with their respective services). | ||
Simply rename the template files in `client-env-files` directory by dropping | ||
the term "template" in all of the .env files (e.g., | ||
`template.env.contributor_1.client` becomes `.env.contributor_1.client`). | ||
|
||
### Running the Notebook | ||
|
||
Note this notebook uses OpenAI LLMs. We export the `OPENAI_API_KEY` | ||
at the same time that we spin up the notebook: | ||
|
||
```sh | ||
export OPENAI_API_KEY=<openai-api-key> && jupyter lab | ||
``` | ||
|
||
From there you can open up the `network_query_engine.ipynb`. |
1 change: 1 addition & 0 deletions
1
llama-index-networks/examples/demo/client-env-files/template.env.contributor_1.client
This file contains 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 @@ | ||
API_URL=http://0.0.0.0:8001 |
1 change: 1 addition & 0 deletions
1
llama-index-networks/examples/demo/client-env-files/template.env.contributor_2.client
This file contains 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 @@ | ||
API_URL=http://0.0.0.0:8002 |
1 change: 1 addition & 0 deletions
1
llama-index-networks/examples/demo/client-env-files/template.env.contributor_3.client
This file contains 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 @@ | ||
API_URL=http://0.0.0.0:8003 |
This file contains 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,3 @@ | ||
poetry_requirements( | ||
name="poetry", | ||
) |
Oops, something went wrong.