Skip to content

Commit

Permalink
Add Passio Nutrition AI Food Search Tool to LLAMA_index (run-llama#11621
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ivyas21 authored Mar 9, 2024
1 parent 4df8748 commit c674a44
Show file tree
Hide file tree
Showing 14 changed files with 5,148 additions and 0 deletions.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
poetry_requirements(
name="poetry",
)
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)
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/
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.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_sources()
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"]
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)
Loading

0 comments on commit c674a44

Please sign in to comment.