diff --git a/chatlab/views/tools.py b/chatlab/views/tools.py index d586be7..610298b 100644 --- a/chatlab/views/tools.py +++ b/chatlab/views/tools.py @@ -15,7 +15,7 @@ from IPython.display import display from IPython.core.getipython import get_ipython -from jiter import from_json +from jiter import from_json class ToolArguments(AutoUpdate): @@ -193,7 +193,6 @@ def render(self): return None try: - Model = extract_model_from_function(self.name, self.custom_render) # model = Model.model_validate(possible_args) model = Model(**possible_args) diff --git a/notebooks/cooking.ipynb b/notebooks/cooking.ipynb index 103f573..d3db6ba 100644 --- a/notebooks/cooking.ipynb +++ b/notebooks/cooking.ipynb @@ -341,10 +341,15 @@ "#### Steps:\n", "1. **Preheat your oven to 220°C (430°F).**\n", "2. **Wash the potatoes thoroughly** and pat them dry with a kitchen towel. Prick them all over with a fork to allow steam to escape during cooking. *(Easy)*\n", - "3. **Rub the potatoes with olive oil** and sprinkle them" + "3. **Rub the potatoes with olive oil** and sprinkle them generously with sea salt. This will give them a crispy skin. *(Easy)*\n", + "4. **Place the potatoes directly on the middle shelf of the oven.** If you're worried about the oven getting dirty, you can place a baking sheet on the shelf below to catch any drips. *(Medium)*\n", + "5. **Bake for about 20 minutes,** then reduce the oven temperature to 190°C (375°F) and continue baking for another 60 minutes, or until the potatoes are cooked through and their skin is crispy. *(Medium)*\n", + "6. **Remove the potatoes from the oven** and let them cool for a few minutes. Slice open the top of each potato, fluff up the inside with a fork, and serve with your favorite toppings. *(Easy)*\n", + "\n", + "Enjoy your delicious British Baked Potato!" ], "text/plain": [ - "AssistantMessageView(display_id='d5f77c5d-d19f-4f0b-8d1e-20f4c948da6a', content=\"Here's your British Baked Potato recipe, all set and visually presented:\\n\\n### British Baked Potato\\n\\n#### Ingredients:\\n- 4 Large potatoes\\n- 2 tbsp Olive oil\\n- Sea salt, to taste\\n\\n#### Steps:\\n1. **Preheat your oven to 220°C (430°F).**\\n2. **Wash the potatoes thoroughly** and pat them dry with a kitchen towel. Prick them all over with a fork to allow steam to escape during cooking. *(Easy)*\\n3. **Rub the potatoes with olive oil** and sprinkle them\", finished=False, has_displayed=True)" + "AssistantMessageView(display_id='d5f77c5d-d19f-4f0b-8d1e-20f4c948da6a', content=\"Here's your British Baked Potato recipe, all set and visually presented:\\n\\n### British Baked Potato\\n\\n#### Ingredients:\\n- 4 Large potatoes\\n- 2 tbsp Olive oil\\n- Sea salt, to taste\\n\\n#### Steps:\\n1. **Preheat your oven to 220°C (430°F).**\\n2. **Wash the potatoes thoroughly** and pat them dry with a kitchen towel. Prick them all over with a fork to allow steam to escape during cooking. *(Easy)*\\n3. **Rub the potatoes with olive oil** and sprinkle them generously with sea salt. This will give them a crispy skin. *(Easy)*\\n4. **Place the potatoes directly on the middle shelf of the oven.** If you're worried about the oven getting dirty, you can place a baking sheet on the shelf below to catch any drips. *(Medium)*\\n5. **Bake for about 20 minutes,** then reduce the oven temperature to 190°C (375°F) and continue baking for another 60 minutes, or until the potatoes are cooked through and their skin is crispy. *(Medium)*\\n6. **Remove the potatoes from the oven** and let them cool for a few minutes. Slice open the top of each potato, fluff up the inside with a fork, and serve with your favorite toppings. *(Easy)*\\n\\nEnjoy your delicious British Baked Potato!\", finished=False, has_displayed=True)" ] }, "metadata": {}, diff --git a/notebooks/knowledge-graph.ipynb b/notebooks/knowledge-graph.ipynb index c5dab9a..c82219d 100644 --- a/notebooks/knowledge-graph.ipynb +++ b/notebooks/knowledge-graph.ipynb @@ -22,36 +22,6 @@ "execution_count": 2, "metadata": {}, "outputs": [ - { - "data": { - "text/markdown": [ - "nteract Papermill is a tool that allows users to parameterize and execute Jupyter Notebooks. It is particularly useful for automating notebooks and enabling more dynamic execution of notebook content. Papermill enhances the interactivity of Jupyter Notebooks by allowing parameters to be passed to a notebook, executing it from start to finish, and then saving the output as a new notebook. This functionality makes it easier to use notebooks for data processing, reporting, and more automated, reproducible workflows.\n", - "\n", - "Papermill works by reading a source notebook, injecting new parameters into it, executing the notebook with these parameters, and then saving the result as a new notebook. This process facilitates the reuse of notebooks for different data inputs or configurations without manually changing the notebooks each time.\n", - "\n", - "### Key Features of Papermill:\n", - "- **Parameterization**: Allows passing parameters into notebooks to change their behavior or inputs without altering the notebook code.\n", - "- **Execution**: Facilitates the automated running of notebooks, making it simpler to integrate notebooks into data pipelines or workflows.\n", - "- **Output Capture**: Saves the executed notebook with all the outputs, which is useful for keeping records of computations or for reporting purposes.\n", - "\n", - "### Contributors:\n", - "The development of Papermill is supported by a community of open-source contributors. These contributors include developers who contribute code, documentation, report issues, and help with the project's maintenance and enhancement. The contributors are part of the broader nteract community, which focuses on creating open-source tools for interactive computing.\n", - "\n", - "### Visualization:\n", - "To visualize Papermill and its contributors, let's create a simple knowledge graph that includes the following elements:\n", - "- The Papermill tool as the central node.\n", - "- Key features of Papermill as connected nodes.\n", - "- A generic \"Contributors\" node to represent the community supporting Papermill.\n", - "\n", - "Let's proceed to create this visualization." - ], - "text/plain": [ - "AssistantMessageView(display_id='40267d8f-d48a-488a-ad1a-c77e075d4171', content='nteract Papermill is a tool that allows users to parameterize and execute Jupyter Notebooks. It is particularly useful for automating notebooks and enabling more dynamic execution of notebook content. Papermill enhances the interactivity of Jupyter Notebooks by allowing parameters to be passed to a notebook, executing it from start to finish, and then saving the output as a new notebook. This functionality makes it easier to use notebooks for data processing, reporting, and more automated, reproducible workflows.\\n\\nPapermill works by reading a source notebook, injecting new parameters into it, executing the notebook with these parameters, and then saving the result as a new notebook. This process facilitates the reuse of notebooks for different data inputs or configurations without manually changing the notebooks each time.\\n\\n### Key Features of Papermill:\\n- **Parameterization**: Allows passing parameters into notebooks to change their behavior or inputs without altering the notebook code.\\n- **Execution**: Facilitates the automated running of notebooks, making it simpler to integrate notebooks into data pipelines or workflows.\\n- **Output Capture**: Saves the executed notebook with all the outputs, which is useful for keeping records of computations or for reporting purposes.\\n\\n### Contributors:\\nThe development of Papermill is supported by a community of open-source contributors. These contributors include developers who contribute code, documentation, report issues, and help with the project\\'s maintenance and enhancement. The contributors are part of the broader nteract community, which focuses on creating open-source tools for interactive computing.\\n\\n### Visualization:\\nTo visualize Papermill and its contributors, let\\'s create a simple knowledge graph that includes the following elements:\\n- The Papermill tool as the central node.\\n- Key features of Papermill as connected nodes.\\n- A generic \"Contributors\" node to represent the community supporting Papermill.\\n\\nLet\\'s proceed to create this visualization.', finished=True, has_displayed=True)" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, { "data": { "image/svg+xml": [ @@ -61,73 +31,119 @@ "\n", "\n", - "\n", - "\n", - "\n", + "\n", + "\n", + "\n", "\n", "\n", "1\n", - "\n", - "Papermill\n", + "\n", + "Recruitment\n", "\n", "\n", "\n", "2\n", - "\n", - "Parameterization\n", + "\n", + "Onboarding\n", "\n", "\n", "\n", "1->2\n", - "\n", - "\n", - "enables\n", + "\n", + "\n", + "leads to\n", "\n", "\n", "\n", "3\n", - "\n", - "Execution\n", + "\n", + "Training & Development\n", "\n", - "\n", + "\n", "\n", - "1->3\n", - "\n", - "\n", - "facilitates\n", + "2->3\n", + "\n", + "\n", + "followed by\n", "\n", "\n", "\n", "4\n", - "\n", - "Output Capture\n", + "\n", + "Performance Management\n", "\n", - "\n", + "\n", "\n", - "1->4\n", - "\n", - "\n", - "saves\n", + "3->4\n", + "\n", + "\n", + "supports\n", "\n", "\n", "\n", "5\n", - "\n", - "Contributors\n", + "\n", + "Succession Planning\n", "\n", - "\n", + "\n", "\n", - "1->5\n", - "\n", - "\n", - "supported by\n", + "4->5\n", + "\n", + "\n", + "informs\n", + "\n", + "\n", + "\n", + "6\n", + "\n", + "Compensation & Benefits\n", + "\n", + "\n", + "\n", + "4->6\n", + "\n", + "\n", + "affects\n", + "\n", + "\n", + "\n", + "7\n", + "\n", + "Employee Relations\n", + "\n", + "\n", + "\n", + "4->7\n", + "\n", + "\n", + "enhanced by\n", + "\n", + "\n", + "\n", + "8\n", + "\n", + "Offboarding\n", + "\n", + "\n", + "\n", + "5->8\n", + "\n", + "\n", + "precedes\n", + "\n", + "\n", + "\n", + "7->8\n", + "\n", + "\n", + "can lead to\n", "\n", "\n", "\n" ], "text/plain": [ - "" + "" ] }, "metadata": {}, @@ -137,30 +153,45 @@ "name": "stderr", "output_type": "stream", "text": [ - "Warning: gre is not a known color.\n", - "Warning: g is not a known color.\n", - "Warning: g is not a known color.\n", - "Warning: o is not a known color.\n", + "Warning: li is not a known color.\n", + "Warning: lightbl is not a known color.\n", + "Warning: li is not a known color.\n", + "Warning: lightg is not a known color.\n", + "Warning: li is not a known color.\n", + "Warning: lightgre is not a known color.\n", + "Warning: yell is not a known color.\n", + "Warning: or is not a known color.\n", + "Warning: p is not a known color.\n", + "Warning: li is not a known color.\n", + "Warning: lightcor is not a known color.\n", + "Warning: bla is not a known color.\n", + "Warning: bla is not a known color.\n", + "Warning: b is not a known color.\n", + "Warning: b is not a known color.\n", + "Warning: blac is not a known color.\n", "Warning: blac is not a known color.\n", - "Warning: bla is not a known color.\n" + "Warning: bl is not a known color.\n", + "Warning: bl is not a known color.\n" ] }, { "data": { "text/markdown": [ - "The knowledge graph above visualizes the core concept of nteract Papermill and its key features, along with the community of contributors that support it. Here's a breakdown of the visualization:\n", + "I've created a visual representation of HR processes to help you understand the flow and relationships between different activities within HR. This includes:\n", "\n", - "- **Papermill** is the central node, representing the tool itself.\n", - "- Connected to Papermill, we have its key features:\n", - " - **Parameterization**: Highlighting the ability to pass parameters to a notebook.\n", - " - **Execution**: Representing the automated running of notebooks.\n", - " - **Output Capture**: Indicating the saving of executed notebooks, including their outputs.\n", - "- **Contributors** are represented as a community node that supports the development and maintenance of Papermill.\n", + "- **Recruitment**, leading to\n", + "- **Onboarding**, followed by\n", + "- **Training & Development**, which supports\n", + "- **Performance Management**. This, in turn, informs\n", + "- **Succession Planning**, affects\n", + "- **Compensation & Benefits**, and is enhanced by\n", + "- **Employee Relations**, which can lead to\n", + "- **Offboarding**.\n", "\n", - "This graph encapsulates the functionality of Papermill and the collaborative effort behind its continued development and success in the open-source community." + "This visual should give you a clear overview of how HR processes interlink and flow from one to the next." ], "text/plain": [ - "AssistantMessageView(display_id='452a8025-9eb5-4fc7-9ffc-37e3d058c9dd', content=\"The knowledge graph above visualizes the core concept of nteract Papermill and its key features, along with the community of contributors that support it. Here's a breakdown of the visualization:\\n\\n- **Papermill** is the central node, representing the tool itself.\\n- Connected to Papermill, we have its key features:\\n - **Parameterization**: Highlighting the ability to pass parameters to a notebook.\\n - **Execution**: Representing the automated running of notebooks.\\n - **Output Capture**: Indicating the saving of executed notebooks, including their outputs.\\n- **Contributors** are represented as a community node that supports the development and maintenance of Papermill.\\n\\nThis graph encapsulates the functionality of Papermill and the collaborative effort behind its continued development and success in the open-source community.\", finished=False, has_displayed=True)" + "AssistantMessageView(display_id='26e8afcd-0796-4b0a-a3f0-55bd93c86f22', content=\"I've created a visual representation of HR processes to help you understand the flow and relationships between different activities within HR. This includes:\\n\\n- **Recruitment**, leading to\\n- **Onboarding**, followed by\\n- **Training & Development**, which supports\\n- **Performance Management**. This, in turn, informs\\n- **Succession Planning**, affects\\n- **Compensation & Benefits**, and is enhanced by\\n- **Employee Relations**, which can lead to\\n- **Offboarding**.\\n\\nThis visual should give you a clear overview of how HR processes interlink and flow from one to the next.\", finished=False, has_displayed=True)" ] }, "metadata": {}, @@ -168,10 +199,11 @@ } ], "source": [ + "import warnings\n", "from graphviz import Digraph\n", "from pydantic import BaseModel, Field\n", "from typing import List\n", - "from chatlab import Chat\n", + "from chatlab import Chat, system\n", "from chatlab.decorators import incremental_display\n", "\n", "class Node(BaseModel):\n", @@ -212,11 +244,314 @@ " return \"Stored graph, visual shown\"\n", "\n", "chat = Chat(\n", + " system(\"You are a helpful assistant that can create knowledge graphs and store them in a database. You only respond with the knowledge graph.\"),\n", " model=\"gpt-4-turbo-preview\",\n", " chat_functions=[store_knowledge_graph],\n", ")\n", "\n", - "await chat(\"Visually teach me about the nteract papermill and its contributors\", temperature=0.7)" + "await chat(\"Visually teach me about HR processes\", temperature=0.7)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/markdown": [ + "It seems like there might be a misunderstanding. Could you please specify the company or organization you're asking about the founders of?" + ], + "text/plain": [ + "AssistantMessageView(display_id='3c5d41fe-655a-4bbe-96ab-ad5d1934593c', content=\"It seems like there might be a misunderstanding. Could you please specify the company or organization you're asking about the founders of?\", finished=False, has_displayed=True)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "await chat(\"Who are the founders?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "display(\"above here\", update=True, display_id=\"230945832\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "1\n", + "\n", + "Cats\n", + "\n", + "\n", + "\n", + "2\n", + "\n", + "Felines\n", + "\n", + "\n", + "\n", + "1->2\n", + "\n", + "\n", + "are\n", + "\n", + "\n", + "\n", + "5\n", + "\n", + "Independent\n", + "\n", + "\n", + "\n", + "1->5\n", + "\n", + "\n", + "are often\n", + "\n", + "\n", + "\n", + "3\n", + "\n", + "Domestic\n", + "\n", + "\n", + "\n", + "2->3\n", + "\n", + "\n", + "can be\n", + "\n", + "\n", + "\n", + "4\n", + "\n", + "Predators\n", + "\n", + "\n", + "\n", + "2->4\n", + "\n", + "\n", + "are\n", + "\n", + "\n", + "\n", + "6\n", + "\n", + "Carnivores\n", + "\n", + "\n", + "\n", + "2->6\n", + "\n", + "\n", + "are\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Warning: light is not a known color.\n", + "Warning: l is not a known color.\n", + "Warning: lightg is not a known color.\n", + "Warning: li is not a known color.\n", + "Warning: lightg is not a known color.\n", + "Warning: y is not a known color.\n", + "Warning: or is not a known color.\n", + "Warning: bl is not a known color.\n", + "Warning: bla is not a known color.\n", + "Warning: b is not a known color.\n", + "Warning: bl is not a known color.\n" + ] + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "1\n", + "\n", + "Dogs\n", + "\n", + "\n", + "\n", + "2\n", + "\n", + "Canines\n", + "\n", + "\n", + "\n", + "1->2\n", + "\n", + "\n", + "are\n", + "\n", + "\n", + "\n", + "5\n", + "\n", + "Loyal\n", + "\n", + "\n", + "\n", + "1->5\n", + "\n", + "\n", + "are known as\n", + "\n", + "\n", + "\n", + "3\n", + "\n", + "Domestic\n", + "\n", + "\n", + "\n", + "2->3\n", + "\n", + "\n", + "can be\n", + "\n", + "\n", + "\n", + "4\n", + "\n", + "Pack Animals\n", + "\n", + "\n", + "\n", + "2->4\n", + "\n", + "\n", + "are\n", + "\n", + "\n", + "\n", + "6\n", + "\n", + "Carnivores\n", + "\n", + "\n", + "\n", + "2->6\n", + "\n", + "\n", + "are\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Warning: light is not a known color.\n", + "Warning: l is not a known color.\n", + "Warning: lightg is not a known color.\n", + "Warning: li is not a known color.\n", + "Warning: lightg is not a known color.\n", + "Warning: yell is not a known color.\n", + "Warning: o is not a known color.\n", + "Warning: orang is not a known color.\n", + "Warning: pi is not a known color.\n", + "Warning: blac is not a known color.\n", + "Warning: bl is not a known color.\n" + ] + }, + { + "data": { + "text/markdown": [ + "I've created two knowledge graphs for you:\n", + "\n", + "1. **About Cats**: This graph explores the characteristics of cats, highlighting their nature as felines, domestic animals, predators, independent beings, and carnivores.\n", + "\n", + "2. **About Dogs**: This graph delves into the world of dogs, showcasing their identity as canines, domestic animals, pack animals, known for their loyalty, and as carnivores.\n", + "\n", + "Both visuals should give you a clear overview of the unique attributes and classifications of cats and dogs." + ], + "text/plain": [ + "AssistantMessageView(display_id='ebbcdc3d-8843-4fca-aa65-f4972d36f638', content=\"I've created two knowledge graphs for you:\\n\\n1. **About Cats**: This graph explores the characteristics of cats, highlighting their nature as felines, domestic animals, predators, independent beings, and carnivores.\\n\\n2. **About Dogs**: This graph delves into the world of dogs, showcasing their identity as canines, domestic animals, pack animals, known for their loyalty, and as carnivores.\\n\\nBoth visuals should give you a clear overview of the unique attributes and classifications of cats and dogs.\", finished=False, has_displayed=True)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "with warnings.catch_warnings():\n", + " warnings.simplefilter(\"ignore\")\n", + " await chat(\"Ok now make two knowledge graphs. One about cats and one about dogs.\")" ] } ], diff --git a/tests/ordering.ipynb b/tests/ordering.ipynb index 5946221..323a214 100644 --- a/tests/ordering.ipynb +++ b/tests/ordering.ipynb @@ -74,7 +74,7 @@ "metadata": {}, "outputs": [], "source": [ - "assert function_message[\"content\"] == None\n", + "assert function_message[\"content\"] is None\n", "\n", "assert function_message[\"function_call\"][\"name\"] == \"sql\"\n", "\n", diff --git a/tests/test_registry.py b/tests/test_registry.py index 00a0b5b..b69e42b 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -176,7 +176,7 @@ def test_generate_function_schema_with_model_argument(): "x": {"type": "integer"}, "y": {"type": "string"}, "z": {"default": False, "type": "boolean"}, - "model": {"allOf": [{"$ref": "#/$defs/SimpleModel"}], "default": None}, + "model": {"$ref": "#/$defs/SimpleModel", "default": None}, }, "required": ["x", "y"], "$defs": { @@ -197,6 +197,7 @@ def test_generate_function_schema_with_model_argument(): } }, }, + "strict": None, } assert schema.model_dump() == expected_schema @@ -212,8 +213,8 @@ def test_generate_function_schema_with_model_and_nested_model_arguments(): "x": {"type": "integer"}, "y": {"type": "string"}, "z": {"default": False, "type": "boolean"}, - "model": {"allOf": [{"$ref": "#/$defs/SimpleModel"}], "default": None}, - "nested_model": {"allOf": [{"$ref": "#/$defs/NestedModel"}], "default": None}, + "model": {"$ref": "#/$defs/SimpleModel", "default": None}, + "nested_model": {"$ref": "#/$defs/NestedModel", "default": None}, }, "required": ["x", "y"], "$defs": { @@ -249,6 +250,7 @@ def test_generate_function_schema_with_model_and_nested_model_arguments(): }, }, }, + "strict": None, } assert schema.model_dump() == expected_schema @@ -268,6 +270,7 @@ def test_generate_function_schema_with_uuid_argument(): }, "required": ["x", "y"], }, + "strict": None, } assert schema.model_dump() == expected_schema @@ -383,7 +386,6 @@ async def test_function_registry_call_edge_cases(): async def test_function_registry_call_tool(): registry = FunctionRegistry() registry.register(simple_func, SimpleModel) - registry.register(simple_func_with_model_arg) tools = registry.tools @@ -416,7 +418,7 @@ async def test_function_registry_call_tool(): "x": {"type": "integer"}, "y": {"type": "string"}, "z": {"default": False, "type": "boolean"}, - "model": {"allOf": [{"$ref": "#/$defs/SimpleModel"}], "default": None}, + "model": {"$ref": "#/$defs/SimpleModel", "default": None}, }, "required": ["x", "y"], "$defs": {