Skip to content

Commit

Permalink
Merge pull request #143 from rgbkrk/tools
Browse files Browse the repository at this point in the history
Tools
  • Loading branch information
rgbkrk authored Mar 4, 2024
2 parents 35dc6d1 + fc5c759 commit fc31e06
Show file tree
Hide file tree
Showing 19 changed files with 663 additions and 586 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,5 @@ Untitled*.ipynb
lcov.info

ops.ipynb

.jupyter_ystore.db
12 changes: 6 additions & 6 deletions chatlab/builtins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Builtins for ChatLab."""
"""Simple tools for any LLM to use."""

from deprecation import deprecated

from .files import chat_functions as file_functions
from .python import get_python_docs, run_python
from .shell import chat_functions as shell_functions
from ..tools import run_python, get_python_docs, file_functions, shell_functions

# TODO: Deprecate extra namespace
# compose all the file, shell, and python functions into one list for ease of use
os_functions = file_functions + shell_functions + [run_python, get_python_docs]

# To prevent naming confusion, the builtin that isn't really running a cell
# is deprecated.
Expand All @@ -14,8 +16,6 @@
details="run_cell is deprecated. Use run_python instead for same-session execution.",
)(run_python)

# compose all the file, shell, and python functions into one list for ease of use
os_functions = file_functions + shell_functions + [run_python, get_python_docs]

__all__ = [
"run_python",
Expand Down
58 changes: 0 additions & 58 deletions chatlab/builtins/colors.py

This file was deleted.

8 changes: 5 additions & 3 deletions chatlab/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from .registry import FunctionRegistry, PythonHallucinationFunction
from .views import ToolArguments, AssistantMessageView

from .models import GPT_3_5_TURBO

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -65,7 +67,7 @@ def __init__(
*initial_context: Union[ChatCompletionMessageParam, str],
base_url=None,
api_key=None,
model="gpt-3.5-turbo-0613",
model=GPT_3_5_TURBO,
function_registry: Optional[FunctionRegistry] = None,
chat_functions: Optional[List[Callable]] = None,
allow_hallucinated_python: bool = False,
Expand Down Expand Up @@ -109,9 +111,9 @@ def __init__(

if function_registry is None:
if allow_hallucinated_python and python_hallucination_function is None:
from .builtins import run_cell
from .tools import run_python

python_hallucination_function = run_cell
python_hallucination_function = run_python

self.function_registry = FunctionRegistry(python_hallucination_function=python_hallucination_function)
else:
Expand Down
11 changes: 11 additions & 0 deletions chatlab/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .files import chat_functions as file_functions
from .python import get_python_docs, run_python
from .shell import chat_functions as shell_functions

__all__ = [
"file_functions",
"get_python_docs",
"run_python",
"shell_functions",
]

File renamed without changes.
48 changes: 48 additions & 0 deletions chatlab/tools/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Let models pick and show color palettes to you."""
import hashlib
from typing import List, Optional
from pydantic import BaseModel, validator, Field

from IPython.display import display


class Palette(BaseModel):
"""A palette of colors for the user to see."""

colors: List[str] = Field(..., description="A list of CSS colors to display.")
name: Optional[str] = None

@validator("colors", each_item=True)
def check_color_validity(cls, v):
if not isinstance(v, str):
raise ValueError("Each color must be a string representation of a CSS color.")
if not all(c.isalnum() or c in "#.,()% " for c in v):
raise ValueError(
"Color contains invalid characters. Only alphanumeric and CSS color specific characters are allowed."
)
return v

def _repr_html_(self):
html = "<div>"
for color in self.colors:
html += f'<div style="background-color:{color}; width:50px; height:50px; display:inline-block;"></div>'
html += "</div>"

return html

def __repr__(self):
"""Returns a string representation of the palette."""
return f"Palette({self.colors}, {self.name})"


def _generate_palette_name(colors: List[str]) -> str:
hash_object = hashlib.sha1("".join(colors).encode())
return f"palette-{hash_object.hexdigest()}"


def show_colors(colors: List[str]):
"""Shows a list of CSS colors for the user in their notebook."""
palette = Palette(colors=colors)

display(palette)
return "Displayed colors for user."
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit fc31e06

Please sign in to comment.