Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI endpoints to interact with API and LLM endpoints #21

Merged
merged 9 commits into from
Jul 25, 2023
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,25 @@ interacting with Neon systems remotely, via [MQ](https://github.com/NeonGeckoCom

Install the Iris Python package with: `pip install neon-iris`
The `iris` entrypoint is available to interact with a bus via CLI. Help is available via `iris --help`.


## Debugging a Diana installation
The `iris` CLI includes utilities for interacting with a `Diana` backend.

### Configuration
Configuration files can be specified via environment variables. By default,
`Iris` will set default values:
```
OVOS_CONFIG_BASE_FOLDER=neon
OVOS_CONFIG_FILENAME=diana.yaml
```

The example below would override defaults to read configuration from
`~/.config/mycroft/mycroft.conf`.
```
export OVOS_CONFIG_BASE_FOLDER=mycroft
export OVOS_CONFIG_FILENAME=mycroft.conf
```

More information about configuration handling can be found
[in the docs](https://neongeckocom.github.io/neon-docs/quick_reference/configuration/).
130 changes: 113 additions & 17 deletions neon_iris/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,30 @@
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import json
import logging
from pprint import pformat

import click
import yaml

from os import environ
from os.path import expanduser, isfile
from time import sleep
from click_default_group import DefaultGroup
from ovos_utils.log import LOG

from neon_utils.logger import LOG
from neon_iris.client import CLIClient
from neon_iris.util import load_config_file
from neon_iris.version import __version__

environ.setdefault("OVOS_CONFIG_BASE_FOLDER", "neon")
environ.setdefault("OVOS_CONFIG_FILENAME", "diana.yaml")


def _print_config():
from ovos_config.config import Configuration
config = Configuration().get('MQ')
mq_endpoint = f"{config.get('server')}:{config.get('port', 5672)}"
click.echo(f"Connecting to {mq_endpoint}")


@click.group("iris", cls=DefaultGroup,
no_args_is_help=True, invoke_without_command=True,
Expand All @@ -59,19 +70,11 @@ def neon_iris_cli(version: bool = False):
@click.option('--audio', '-a', is_flag=True, default=False,
help="Flag to enable audio playback")
def start_client(mq_config, user_config, lang, audio):
from neon_iris.client import CLIClient
if mq_config:
with open(mq_config) as f:
try:
mq_config = json.load(f)
except Exception as e:
f.seek(0)
mq_config = yaml.safe_load(f)
mq_config = load_config_file(expanduser(mq_config))
if user_config:
with open(user_config) as f:
try:
user_config = json.load(f)
except Exception as e:
user_config = None
user_config = load_config_file(expanduser(user_config))
client = CLIClient(mq_config, user_config)
LOG.init({"level": logging.WARNING})

Expand Down Expand Up @@ -111,5 +114,98 @@ def start_client(mq_config, user_config, lang, audio):
client.shutdown()


if __name__ == "__main__":
start_client(None, None, "en-us")
@neon_iris_cli.command(help="Query a weather endpoint")
@click.option('--unit', '-u', default='imperial',
help="units to use ('metric' or 'imperial')")
@click.option('--latitude', '--lat', default=47.6815,
help="location latitude")
@click.option('--longitude', '--lon', default=-122.2087,
help="location latitude")
@click.option('--api', '-a', default='onecall',
help="api to query ('onecall' or 'weather')")
def get_weather(unit, latitude, longitude, api):
from neon_iris.util import query_api
_print_config()
query = {"lat": latitude,
"lon": longitude,
"units": unit,
"api": api,
"service": "open_weather_map"}
resp = query_api(query)
click.echo(pformat(resp))


@neon_iris_cli.command(help="Query a stock price endpoint")
@click.argument('symbol')
def get_stock_quote(symbol):
from neon_iris.util import query_api
_print_config()
query = {"symbol": symbol,
"api": "quote",
"service": "alpha_vantage"}
resp = query_api(query)
click.echo(pformat(resp))


@neon_iris_cli.command(help="Query a stock symbol endpoint")
@click.argument('company')
def get_stock_symbol(company):
from neon_iris.util import query_api
_print_config()
query = {"company": company,
"api": "symbol",
"service": "alpha_vantage"}
resp = query_api(query)
click.echo(pformat(resp))


@neon_iris_cli.command(help="Query a WolframAlpha endpoint")
@click.option('--api', '-a', default='short',
help="Wolfram|Alpha API to query")
@click.option('--unit', '-u', default='imperial',
help="units to use ('metric' or 'imperial')")
@click.option('--latitude', '--lat', default=47.6815,
help="location latitude")
@click.option('--longitude', '--lon', default=-122.2087,
help="location latitude")
@click.argument('question')
def get_wolfram_response(api, unit, latitude, longitude, question):
from neon_iris.util import query_api
_print_config()
query = {"api": api,
"units": unit,
"latlong": f"{latitude},{longitude}",
"query": question,
"service": "wolfram_alpha"}
resp = query_api(query)
click.echo(pformat(resp))


@neon_iris_cli.command(help="Converse with an LLM")
@click.option('--llm', default="chat_gpt",
help="LLM Queue to interact with ('chat_gpt' or 'fastchat')")
def start_llm_chat(llm):
from neon_iris.llm import LLMConversation
_print_config()
conversation = LLMConversation(llm)
while True:
query = click.prompt(">")
resp = conversation.get_response(query)
click.echo(resp)


@neon_iris_cli.command(help="Converse with an LLM")
def get_coupons():
from neon_iris.util import get_brands_coupons
data = get_brands_coupons()
click.echo(pformat(data))


@neon_iris_cli.command(help="Parse a Neon CCL script")
@click.argument("script_file")
def parse_script(script_file):
from neon_iris.util import parse_ccl_script
data = parse_ccl_script(script_file)
click.echo(pformat(data))

# TODO: email, metrics
5 changes: 3 additions & 2 deletions neon_iris/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from time import time
from typing import Optional
from uuid import uuid4
from mycroft_bus_client import Message
from ovos_bus_client.message import Message
from pika.exceptions import StreamLostError
from neon_utils.configuration_utils import get_neon_user_config
from neon_utils.mq_utils import NeonMQHandler
Expand Down Expand Up @@ -290,7 +290,8 @@ def _send_serialized_message(self, serialized: dict):
self.shutdown()

def _init_mq_connection(self):
mq_connection = NeonMQHandler(self._config, "mq_handler", self._vhost)
mq_config = self._config.get("MQ") or self._config
mq_connection = NeonMQHandler(mq_config, "mq_handler", self._vhost)
mq_connection.register_consumer("neon_response_handler", self._vhost,
self.uid, self.handle_neon_response,
auto_ack=False)
Expand Down
45 changes: 45 additions & 0 deletions neon_iris/llm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2022 Neongecko.com Inc.
# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds,
# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo
# BSD-3 License
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from neon_mq_connector.utils.client_utils import send_mq_request


class LLMConversation:
def __init__(self, llm: str = "chat_gpt"):
self.history = list()
self.queue = f"{llm}_input"

def get_response(self, query: str):
resp = send_mq_request("/llm", {'query': query,
'history': self.history}, self.queue,
timeout=90)
reply = resp.get("response") or ""
if reply:
self.history.append(("user", query))
self.history.append(("llm", reply))
return reply
92 changes: 92 additions & 0 deletions neon_iris/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2021 Neongecko.com Inc.
# BSD-3
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS

import json
import yaml

from os.path import isfile
from ovos_utils.log import LOG


def load_config_file(file_path: str) -> dict:
"""
Load a config file (json or yaml) and return the dict contents
:param file_path: path to config file to load
"""
if not isfile(file_path):
raise FileNotFoundError(f"Requested config file not found: {file_path}")
with open(file_path) as f:
try:
config = json.load(f)
except Exception as e:
LOG.debug(e)
f.seek(0)
config = yaml.safe_load(f)
return config


def query_api(query_params: dict, timeout: int = 10) -> dict:
"""
Query an API service on the `/neon_api` vhost.
:param query_params: dict query to send
:param timeout: seconds to wait for a response
:returns: dict MQ response
"""
from neon_mq_connector.utils.client_utils import send_mq_request
response = send_mq_request("/neon_api", query_params, "neon_api_input",
"neon_api_output", timeout)
return response


def get_brands_coupons(timeout: int = 5) -> dict:
"""
Get brands/coupons data on the `/neon_coupons` vhost.
:param timeout: seconds to wait for a response
:returns: dict MQ response
"""
from neon_mq_connector.utils.client_utils import send_mq_request
response = send_mq_request("/neon_coupons", {}, "neon_coupons_input",
"neon_coupons_output", timeout)
return response


def parse_ccl_script(script_path: str, metadata: dict = None,
timeout: int = 30) -> dict:
"""
Parse a nct script file into an ncs formatted file
:param script_path: path to file to parse
:param metadata: Optional dict metadata to include in output
:param timeout: seconds to wait for a response
:returns: dict MQ response
"""
from neon_mq_connector.utils.client_utils import send_mq_request
with open(script_path, 'r') as f:
text = f.read()
metadata = metadata or {}
response = send_mq_request("/neon_script_parser", {"text": text,
"metadata": metadata},
"neon_script_parser_input",
"neon_script_parser_output", timeout)
return response
5 changes: 3 additions & 2 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
click~=8.0
click-default-group~=1.2
neon-utils~=1.0
pyyaml~=5.4
neon-mq-connector~=0.6
pyyaml>=5.4,<7.0.0
neon-mq-connector~=0.7
ovos-bus-client~=0.0.3
4 changes: 3 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from neon_utils.mq_utils import NeonMQHandler

sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from neon_iris.client import NeonAIClient, CLIClient
from neon_iris.client import NeonAIClient

_test_config = {
"MQ": {
Expand All @@ -53,6 +53,8 @@ class TestClient(unittest.TestCase):
def test_client_create(self):
client = NeonAIClient(_test_config)
self.assertIsInstance(client.uid, str)
self.assertEqual(client._config, _test_config)
self.assertEqual(client._connection.config, _test_config["MQ"])
self.assertTrue(os.path.isdir(client.audio_cache_dir))
self.assertIsInstance(client.client_name, str)
self.assertIsInstance(client.connection, NeonMQHandler)
Expand Down
Loading