Skip to content

Commit

Permalink
Interactive shell
Browse files Browse the repository at this point in the history
  • Loading branch information
blythed committed Oct 18, 2024
1 parent f8e7387 commit ee56746
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 876 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Permissions of artifacts increased
- Make JSON-able a configuration depending on the databackend
- Restore some training test cases
- Simple querying shell

#### New Features & Functionality

Expand Down
7 changes: 2 additions & 5 deletions superduper/backends/local/vector_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ def initialize(self):
except TypeError as e:
import traceback

logging.error(
f'Could not load vector index: {identifier} '
f'{e}'
)
logging.error(traceback.format_exc())
logging.error(f'Could not load vector index: {identifier} ' f'{e}')
logging.error(traceback.format_exc())
continue

# TODO needed?
Expand Down
45 changes: 28 additions & 17 deletions superduper/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import os
import subprocess
import typing as t

from superduper import Component, logging, superduper
from superduper.components.template import Template
Expand Down Expand Up @@ -53,29 +52,41 @@ def start(


@command(help='Initialize a template in the system')
def bootstrap(templates: t.List[str], pip_install: bool = False):
def bootstrap(template: str, destination: str | None, pip_install: bool = False):
"""Initialize a template in the system.

:param templates: List of templates to initialize.
:param template: Template to initialize.

Add template to the system.

>>> superduper bootstrap rag

Copy template to a directory to update it.

>>> superduper bootstrap rag ./my_template
"""
from superduper import templates as inbuilt

if templates == ['*']:
templates = inbuilt.ls()
templates = ['rag', 'text_vector_search']
db = superduper()
existing = db.show('template')
for tem in templates:
if tem in existing:
logging.info(f'Template {tem} already exists')
continue
logging.info(f'Applying template: {tem} from inbuilt')
tem = getattr(inbuilt, tem)
if tem.requirements and pip_install:
with open('/tmp/requirements.txt', 'w') as f:
f.write('\n'.join(tem.requirements))
subprocess.run(['pip', 'install', '-r', '/tmp/requirements.txt'])
db.apply(tem, force=True)
if destination is not None:
root = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
template_directory = os.path.join(root, f'templates/{template}')
print(template_directory)
import shutil

shutil.copytree(template_directory, destination)
return

if template in existing:
logging.warn(f'Template {template} already exists')
logging.info(f'Applying template: {template} from inbuilt')
tem = getattr(inbuilt, template)
if tem.requirements and pip_install:
with open('/tmp/requirements.txt', 'w') as f:
f.write('\n'.join(tem.requirements))
subprocess.run(['pip', 'install', '-r', '/tmp/requirements.txt'])
db.apply(tem)


@command(help='Apply a template or application to a `superduper` deployment')
Expand Down
3 changes: 1 addition & 2 deletions superduper/components/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ def __post_init__(self, db, artifacts, substitutions):
def __call__(self, **kwargs):
"""Method to create component from the given template and `kwargs`."""
kwargs.update({k: v for k, v in self.default_values.items() if k not in kwargs})
msg = 'Types of variables don\'t match with the template variables.'
assert set(self.types.keys()) == set(self.template_variables), msg
assert set(kwargs.keys()) == set(self.template_variables)
component = _replace_variables(self.template, **kwargs)
return Document.decode(component, db=self.db).unpack()
Expand Down Expand Up @@ -92,6 +90,7 @@ class Template(_BaseTemplate):
"""Application template component.

:param data: Sample data to test the template.
:param requirements: pip requirements for the template.
"""

_artifacts: t.ClassVar[t.Tuple[str]] = (('data', pickle_serializer),)
Expand Down
23 changes: 20 additions & 3 deletions superduper/misc/interactive_prompt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pprint
import sys

from superduper import logging
from superduper.base.cursor import SuperDuperCursor


Expand Down Expand Up @@ -34,22 +35,38 @@ def _prompt():
import re

if re.match('[A-Za-z0-9_]+ = ', input_):
exec(input_, values)
try:
exec(input_, values)
except Exception as e:
logging.error(str(e))
continue
except KeyboardInterrupt:
print('Aborted')
sys.exit(0)
continue

if '.predict' in input_:
parts = input_.strip().split('.')
model = parts[0]
rest = '.'.join(parts[1:])
values['model'] = db.load('model', model)
exec(f'result = model.{rest}', values)
try:
exec(f'result = model.{rest}', values)
except Exception as e:
logging.error(e)
continue

pprint.pprint(values['result'])
continue

parts = input_.strip().split('.')
table = parts[0]
rest = '.'.join(parts[1:])
exec(f'result = db["{table}"].{rest}.execute()', values)
try:
exec(f'result = db["{table}"].{rest}.execute()', values)
except Exception as e:
logging.error(e)
continue
if isinstance(values['result'], SuperDuperCursor):
for r in values['result']:
pprint.pprint(r.unpack())
Expand Down
Loading

0 comments on commit ee56746

Please sign in to comment.