Skip to content

Commit

Permalink
Add hana configuration to setup (#75)
Browse files Browse the repository at this point in the history
* Add `hana` configuration to backend CLI
Closes #74

* Add CLI output before HANA config

---------

Co-authored-by: Daniel McKnight <daniel@neon.ai>
  • Loading branch information
NeonDaniel and NeonDaniel authored May 21, 2024
1 parent deb66ec commit 82a073e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
38 changes: 35 additions & 3 deletions neon_diana_utils/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def make_keys_config(write_config: bool,
openai_key = click.prompt("OpenAI API Key", type=str,
default=chatgpt_config.get('key'))
model = click.prompt("Anthropic Model", type=str,
default="claude-2")
default="claude-2")
role = click.prompt("Role", type=str,
default="You are trying to give a short "
"answer in less than 40 words.")
Expand Down Expand Up @@ -405,6 +405,34 @@ def generate_mq_auth_config(rmq_config: dict) -> dict:
return mq_config


def generate_hana_config() -> dict:
"""
Generate HANA config based on user inputs.
:returns: Configuration for HANA frontend
"""
click.echo("Configuring HANA (HTTP API for Neon AI)")
email = click.confirm("Enable endpoint to send email?")
node_user, node_pass = None, None
if click.confirm("Enable node websocket connections?"):
node_user = click.prompt("Node username", type=str, default="neon")
node_pass = click.prompt("Node password", type=str, default="neon")
rpm = click.prompt("Client maximum requests per limit", type=int,
default=60)
auth_rpm = click.prompt("Client maximum auth requests per limit",
type=int, default=6)

hana_config = {"enable_email": email,
"node_username": node_user,
"node_password": node_pass,
"access_token_secret": secrets.token_hex(32),
"refresh_token_secret": secrets.token_hex(32),
"requests_per_minute": rpm,
"auth_requests_per_minute": auth_rpm
}
LOG.debug(pformat(hana_config))
return {"hana": hana_config}


def update_env_file(env_file: str):
"""
Update a .env file with absolute paths for `docker compose` compat.
Expand Down Expand Up @@ -675,6 +703,9 @@ def configure_backend(username: str = None,
mq_auth_config = generate_mq_auth_config(rmq_config)
click.echo(f"Generated auth for services: {set(mq_auth_config.keys())}")

# Generate HANA frontend config
hana_config = generate_hana_config()

# Generate `diana.yaml` output
if keys_config.get("LLM_CHAT_GPT"):
llm_config = make_llm_bot_config()
Expand All @@ -696,7 +727,8 @@ def configure_backend(username: str = None,
"server": "neon-rabbitmq",
"port": 5672}},
**keys_config,
**llm_config}
**llm_config,
**hana_config}
click.echo(f"Writing configuration to {diana_config}")
with open(diana_config, 'w+') as f:
yaml.dump(config, f)
Expand Down Expand Up @@ -936,7 +968,7 @@ def configure_klat_chat(external_url: str = None,
confirmed = False
while not confirmed:
libretranslate_url = click.prompt("Libretranslate API URL", type=str,
default=libretranslate_url)
default=libretranslate_url)
confirmed = click.confirm(f"Is '{libretranslate_url}' correct?")

# Validate https URL
Expand Down
33 changes: 33 additions & 0 deletions tests/test_diana_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,39 @@ def test_generate_mq_auth_config(self):
self.assertEqual(user['name'], service_auth['user'])
self.assertEqual(user['password'], service_auth['password'])

@patch("neon_diana_utils.configuration.click.prompt")
@patch("neon_diana_utils.configuration.click.confirm")
def test_generate_hana_config(self, confirm, prompt):
from neon_diana_utils.configuration import generate_hana_config

# Test all confirmed
confirm.return_value = True
prompt.side_effect = ['neon', 'neon', 60, 6]
hana_config = generate_hana_config()
self.assertIsInstance(hana_config['hana'], dict)
hana_config = hana_config['hana']
self.assertTrue(hana_config['enable_email'])
self.assertEqual(hana_config['node_username'], 'neon')
self.assertEqual(hana_config['node_password'], 'neon')
self.assertIsInstance(hana_config['access_token_secret'], str)
self.assertIsInstance(hana_config['refresh_token_secret'], str)
self.assertEqual(hana_config['requests_per_minute'], 60)
self.assertEqual(hana_config['auth_requests_per_minute'], 6)

# Test none confirmed
confirm.return_value = False
prompt.side_effect = [30, 3]
hana_config = generate_hana_config()
self.assertIsInstance(hana_config['hana'], dict)
hana_config = hana_config['hana']
self.assertFalse(hana_config['enable_email'])
self.assertIsNone(hana_config['node_username'])
self.assertIsNone(hana_config['node_password'])
self.assertIsInstance(hana_config['access_token_secret'], str)
self.assertIsInstance(hana_config['refresh_token_secret'], str)
self.assertEqual(hana_config['requests_per_minute'], 30)
self.assertEqual(hana_config['auth_requests_per_minute'], 3)

def test_update_env_file(self):
from neon_diana_utils.configuration import update_env_file
test_file = join(dirname(__file__), "test.env")
Expand Down

0 comments on commit 82a073e

Please sign in to comment.