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.
Add Passio Nutrition AI Food Search Tool to LLAMA_index (run-llama#11621
- Loading branch information
Showing
14 changed files
with
5,148 additions
and
0 deletions.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
llama-index-integrations/tools/llama-index-tools-passio-nutrition-ai/.gitignore
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,153 @@ | ||
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 | ||
|
||
# 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 |
3 changes: 3 additions & 0 deletions
3
llama-index-integrations/tools/llama-index-tools-passio-nutrition-ai/BUILD
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", | ||
) |
5 changes: 5 additions & 0 deletions
5
llama-index-integrations/tools/llama-index-tools-passio-nutrition-ai/CHANGELOG.md
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.2] - 2024-02-27 | ||
|
||
- Add maintainers and keywords from library.json (llamahub) |
17 changes: 17 additions & 0 deletions
17
llama-index-integrations/tools/llama-index-tools-passio-nutrition-ai/Makefile
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/ |
25 changes: 25 additions & 0 deletions
25
llama-index-integrations/tools/llama-index-tools-passio-nutrition-ai/README.md
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,25 @@ | ||
# Passio Nutrition AI Tool | ||
|
||
This tool connects to a Passio Nutrition AI account and allows an Agent to perform searches against a database of over 2.2M foods. | ||
|
||
You will need to set up a search key using Passio Nutrition API,learn more here: https://www.passio.ai/nutrition-ai#nutrition-api-pricing | ||
|
||
## Usage | ||
|
||
Here's an example usage of the NutritionAIToolSpec. | ||
|
||
```python | ||
from llama_index.tools.passio_nutrition_ai import NutritionAIToolSpec | ||
from llama_index.agent import OpenAIAgent | ||
|
||
tool_spec = NutritionAIToolSpec(api_key="your-key") | ||
|
||
agent = OpenAIAgent.from_tools(tool_spec.to_tool_list()) | ||
|
||
agent.chat("What is the nutritional value of an apple?") | ||
agent.chat("I had a cobb salad for lunch, how many calories did I eat?") | ||
``` | ||
|
||
`passio_nutrition_ai`: Search for foods and their micro nutrition results related to a query | ||
|
||
This loader is designed to be used as a way to load data as a Tool in a Agent. See [here](https://github.com/emptycrown/llama-hub/tree/main) for examples. |
170 changes: 170 additions & 0 deletions
170
...tegrations/tools/llama-index-tools-passio-nutrition-ai/examples/passio_nutrition_ai.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...s/tools/llama-index-tools-passio-nutrition-ai/llama_index/tools/passio_nutrition_ai/BUILD
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 @@ | ||
python_sources() |
7 changes: 7 additions & 0 deletions
7
...s/llama-index-tools-passio-nutrition-ai/llama_index/tools/passio_nutrition_ai/__init__.py
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,7 @@ | ||
## init | ||
from llama_index.tools.passio_nutrition_ai.base import ( | ||
ENDPOINT_BASE_URL, | ||
NutritionAIToolSpec, | ||
) | ||
|
||
__all__ = ["NutritionAIToolSpec", "ENDPOINT_BASE_URL"] |
144 changes: 144 additions & 0 deletions
144
...tools/llama-index-tools-passio-nutrition-ai/llama_index/tools/passio_nutrition_ai/base.py
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,144 @@ | ||
"""Passio Nutrition Search tool spec.""" | ||
|
||
from typing import final, NoReturn | ||
from datetime import datetime, timedelta | ||
|
||
import requests | ||
from llama_index.core.tools.tool_spec.base import BaseToolSpec | ||
|
||
ENDPOINT_BASE_URL = "https://api.passiolife.com/v2/products/napi/food/search/advanced" | ||
|
||
|
||
class NoDiskStorage: | ||
@final | ||
def __getstate__(self) -> NoReturn: | ||
raise AttributeError("Do not store on disk.") | ||
|
||
@final | ||
def __setstate__(self, state) -> NoReturn: | ||
raise AttributeError("Do not store on disk.") | ||
|
||
|
||
try: | ||
from tenacity import ( | ||
retry, | ||
stop_after_attempt, | ||
wait_random, | ||
wait_exponential, | ||
retry_if_result, | ||
) | ||
except ImportError: | ||
# No retries if tenacity is not installed. | ||
def retry(f, *args, **kwargs): | ||
return f | ||
|
||
def stop_after_attempt(n): | ||
return None | ||
|
||
def wait_random(a, b): | ||
return None | ||
|
||
def wait_exponential(multiplier, min, max): | ||
return None | ||
|
||
|
||
def is_http_retryable(rsp): | ||
# -return rsp and rsp.status_code >= 500 | ||
return ( | ||
rsp | ||
and not isinstance(rsp, dict) | ||
and rsp.status_code in [408, 425, 429, 500, 502, 503, 504] | ||
) | ||
|
||
|
||
class ManagedPassioLifeAuth(NoDiskStorage): | ||
"""Manages the token for the NutritionAI API.""" | ||
|
||
def __init__(self, subscription_key: str): | ||
self.subscription_key = subscription_key | ||
self._last_token = None | ||
self._access_token_expiry = None | ||
self._access_token = None | ||
self._customer_id = None | ||
|
||
@property | ||
def headers(self) -> dict: | ||
if not self.is_valid_now(): | ||
self.refresh_access_token() | ||
return { | ||
"Authorization": f"Bearer {self._access_token}", | ||
"Passio-ID": self._customer_id, | ||
} | ||
|
||
def is_valid_now(self): | ||
return ( | ||
self._access_token is not None | ||
and self._customer_id is not None | ||
and self._access_token_expiry is not None | ||
and self._access_token_expiry > datetime.now() | ||
) | ||
|
||
@retry( | ||
retry=retry_if_result(is_http_retryable), | ||
stop=stop_after_attempt(4), | ||
wait=wait_random(0, 0.3) + wait_exponential(multiplier=1, min=0.1, max=2), | ||
) | ||
def _http_get(self, subscription_key): | ||
return requests.get( | ||
f"https://api.passiolife.com/v2/token-cache/napi/oauth/token/{subscription_key}" | ||
) | ||
|
||
def refresh_access_token(self): | ||
"""Refresh the access token for the NutritionAI API.""" | ||
rsp = self._http_get(self.subscription_key) | ||
if not rsp: | ||
raise ValueError("Could not get access token") | ||
self._last_token = token = rsp.json() | ||
self._customer_id = token["customer_id"] | ||
self._access_token = token["access_token"] | ||
self._access_token_expiry = ( | ||
datetime.now() | ||
+ timedelta(seconds=token["expires_in"]) | ||
- timedelta(seconds=5) | ||
) # 5 seconds: approximate time for a token refresh to be processed. | ||
|
||
|
||
class NutritionAIToolSpec(BaseToolSpec): | ||
"""Tool that queries the Passio Nutrition AI API.""" | ||
|
||
spec_functions = ["nutrition_ai_search"] | ||
auth_: ManagedPassioLifeAuth | ||
|
||
def __init__(self, api_key: str) -> None: | ||
"""Initialize with parameters.""" | ||
self.auth_ = ManagedPassioLifeAuth(api_key) | ||
|
||
@retry( | ||
retry=retry_if_result(is_http_retryable), | ||
stop=stop_after_attempt(4), | ||
wait=wait_random(0, 0.3) + wait_exponential(multiplier=1, min=0.1, max=2), | ||
) | ||
def _http_get(self, query: str): | ||
return requests.get( | ||
ENDPOINT_BASE_URL, | ||
headers=self.auth_.headers, | ||
params={"term": query}, # type: ignore | ||
) | ||
|
||
def _nutrition_request(self, query: str): | ||
response = self._http_get(query) | ||
if not response: | ||
raise ValueError("No response from NutritionAI API.") | ||
return response.json() | ||
|
||
def nutrition_ai_search(self, query: str): | ||
""" | ||
Retrieve nutrition facts for a given food item. | ||
Input should be a search query string for the food item. | ||
Args: | ||
query (str): The food item to look for. | ||
Returns a JSON result with the nutrition facts for the food item and, if available, alternative food items which sometimes are a better match. | ||
""" | ||
return self._nutrition_request(query) |
Oops, something went wrong.