From 88ab8742788ddc446564fa770d546957832dbcdf Mon Sep 17 00:00:00 2001 From: Kyle Kelley Date: Thu, 12 Sep 2024 13:39:42 -0700 Subject: [PATCH] adapt to `from_json` from `jiter` --- chatlab/views/tools.py | 17 +++-- notebooks/cooking.ipynb | 145 ++++++++++++---------------------------- 2 files changed, 53 insertions(+), 109 deletions(-) diff --git a/chatlab/views/tools.py b/chatlab/views/tools.py index f8a9a9c..d586be7 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 instructor.dsl.partialjson import JSONParser +from jiter import from_json class ToolArguments(AutoUpdate): @@ -74,11 +74,11 @@ def update(self) -> None: def render(self): if self.custom_render is not None: - # We use the same definition as was in the original function try: - parser = JSONParser() - possible_args = parser.parse(self.arguments) - + possible_args = from_json(self.arguments.encode("utf-8"), partial_mode="trailing-strings") + except Exception: + return None + try: Model = extract_model_from_function(self.name, self.custom_render) # model = Model.model_validate(possible_args) model = Model(**possible_args) @@ -188,8 +188,11 @@ def render(self): if self.custom_render is not None: # We use the same definition as was in the original function try: - parser = JSONParser() - possible_args = parser.parse(self.arguments) + possible_args = from_json(self.arguments.encode("utf-8"), partial_mode="trailing-strings") + except Exception: + return None + + try: Model = extract_model_from_function(self.name, self.custom_render) # model = Model.model_validate(possible_args) diff --git a/notebooks/cooking.ipynb b/notebooks/cooking.ipynb index 106782a..103f573 100644 --- a/notebooks/cooking.ipynb +++ b/notebooks/cooking.ipynb @@ -84,7 +84,7 @@ } }, "children": [ - "2 tablespoons of Olive oil" + "2 tbsp of Olive oil" ], "tagName": "li" }, @@ -168,37 +168,21 @@ } }, "children": [ - "Wash the potatoes thoroughly and pat them dry with a kitchen towel." + "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." ], "tagName": "p" }, - { - "attributes": {}, - "children": [], - "tagName": "span" - } - ], - "tagName": "div" - }, - { - "attributes": {}, - "children": [ { "attributes": { "style": { - "lineHeight": "1.5", - "margin": "5px 0" + "color": "#d9534f", + "fontWeight": "bold" } }, "children": [ - "Prick the potatoes all over with a fork. This allows steam to escape during the cooking process." + "Note: Easy" ], "tagName": "p" - }, - { - "attributes": {}, - "children": [], - "tagName": "span" } ], "tagName": "div" @@ -214,37 +198,21 @@ } }, "children": [ - "Rub the potatoes with olive oil. This helps to crisp the skin." + "Rub the potatoes with olive oil and sprinkle them generously with sea salt. This will give them a crispy skin." ], "tagName": "p" }, - { - "attributes": {}, - "children": [], - "tagName": "span" - } - ], - "tagName": "div" - }, - { - "attributes": {}, - "children": [ { "attributes": { "style": { - "lineHeight": "1.5", - "margin": "5px 0" + "color": "#d9534f", + "fontWeight": "bold" } }, "children": [ - "Sprinkle sea salt over the potatoes. The salt not only seasons the potatoes but also helps to crisp up the skin." + "Note: Easy" ], "tagName": "p" - }, - { - "attributes": {}, - "children": [], - "tagName": "span" } ], "tagName": "div" @@ -260,37 +228,21 @@ } }, "children": [ - "Place the potatoes directly on the middle shelf of the oven. This ensures even cooking." + "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." ], "tagName": "p" }, - { - "attributes": {}, - "children": [], - "tagName": "span" - } - ], - "tagName": "div" - }, - { - "attributes": {}, - "children": [ { "attributes": { "style": { - "lineHeight": "1.5", - "margin": "5px 0" + "color": "#d9534f", + "fontWeight": "bold" } }, "children": [ - "Bake for about 20 minutes, then reduce the oven temperature to 190°C (375°F) and continue baking for another 60-90 minutes, or until the potatoes are cooked through and the skin is crispy." + "Note: Medium" ], "tagName": "p" - }, - { - "attributes": {}, - "children": [], - "tagName": "span" } ], "tagName": "div" @@ -306,37 +258,21 @@ } }, "children": [ - "Remove the potatoes from the oven and let them cool for a few minutes." + "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." ], "tagName": "p" }, - { - "attributes": {}, - "children": [], - "tagName": "span" - } - ], - "tagName": "div" - }, - { - "attributes": {}, - "children": [ { "attributes": { "style": { - "lineHeight": "1.5", - "margin": "5px 0" + "color": "#d9534f", + "fontWeight": "bold" } }, "children": [ - "Slice open the top of each potato, being careful not to cut all the way through. Fluff up the inside with a fork." + "Note: Medium" ], "tagName": "p" - }, - { - "attributes": {}, - "children": [], - "tagName": "span" } ], "tagName": "div" @@ -352,14 +288,21 @@ } }, "children": [ - "Serve hot, with your choice of fillings or toppings. Popular options include butter, cheese, baked beans, and coleslaw." + "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." ], "tagName": "p" }, { - "attributes": {}, - "children": [], - "tagName": "span" + "attributes": { + "style": { + "color": "#d9534f", + "fontWeight": "bold" + } + }, + "children": [ + "Note: Easy" + ], + "tagName": "p" } ], "tagName": "div" @@ -374,10 +317,10 @@ "tagName": "div" }, "text/html": [ - "

British Baked Potato

Ingredients

  • 4 of Large potatoes
  • 2 tablespoons of Olive oil
  • to taste of Sea salt

Steps

Preheat your oven to 220°C (430°F).

Wash the potatoes thoroughly and pat them dry with a kitchen towel.

Prick the potatoes all over with a fork. This allows steam to escape during the cooking process.

Rub the potatoes with olive oil. This helps to crisp the skin.

Sprinkle sea salt over the potatoes. The salt not only seasons the potatoes but also helps to crisp up the skin.

Place the potatoes directly on the middle shelf of the oven. This ensures even cooking.

Bake for about 20 minutes, then reduce the oven temperature to 190°C (375°F) and continue baking for another 60-90 minutes, or until the potatoes are cooked through and the skin is crispy.

Remove the potatoes from the oven and let them cool for a few minutes.

Slice open the top of each potato, being careful not to cut all the way through. Fluff up the inside with a fork.

Serve hot, with your choice of fillings or toppings. Popular options include butter, cheese, baked beans, and coleslaw.

" + "

British Baked Potato

Ingredients

  • 4 of Large potatoes
  • 2 tbsp of Olive oil
  • to taste of Sea salt

Steps

Preheat your oven to 220°C (430°F).

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.

Note: Easy

Rub the potatoes with olive oil and sprinkle them generously with sea salt. This will give them a crispy skin.

Note: Easy

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.

Note: Medium

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.

Note: Medium

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.

Note: Easy

" ], "text/plain": [ - "

British Baked Potato

Ingredients

  • 4 of Large potatoes
  • 2 tablespoons of Olive oil
  • to taste of Sea salt

Steps

Preheat your oven to 220°C (430°F).

Wash the potatoes thoroughly and pat them dry with a kitchen towel.

Prick the potatoes all over with a fork. This allows steam to escape during the cooking process.

Rub the potatoes with olive oil. This helps to crisp the skin.

Sprinkle sea salt over the potatoes. The salt not only seasons the potatoes but also helps to crisp up the skin.

Place the potatoes directly on the middle shelf of the oven. This ensures even cooking.

Bake for about 20 minutes, then reduce the oven temperature to 190°C (375°F) and continue baking for another 60-90 minutes, or until the potatoes are cooked through and the skin is crispy.

Remove the potatoes from the oven and let them cool for a few minutes.

Slice open the top of each potato, being careful not to cut all the way through. Fluff up the inside with a fork.

Serve hot, with your choice of fillings or toppings. Popular options include butter, cheese, baked beans, and coleslaw.

" + "

British Baked Potato

Ingredients

  • 4 of Large potatoes
  • 2 tbsp of Olive oil
  • to taste of Sea salt

Steps

Preheat your oven to 220°C (430°F).

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.

Note: Easy

Rub the potatoes with olive oil and sprinkle them generously with sea salt. This will give them a crispy skin.

Note: Easy

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.

Note: Medium

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.

Note: Medium

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.

Note: Easy

" ] }, "metadata": {}, @@ -386,31 +329,22 @@ { "data": { "text/markdown": [ - "I've stored the recipe for a classic British Baked Potato for you. Here's how to make it:\n", + "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 tablespoons Olive oil\n", + "- 2 tbsp Olive oil\n", "- Sea salt, to taste\n", "\n", "#### 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.\n", - "3. **Prick the potatoes** all over with a fork. This allows steam to escape during the cooking process.\n", - "4. **Rub the potatoes** with olive oil. This helps to crisp the skin.\n", - "5. **Sprinkle sea salt** over the potatoes. The salt not only seasons the potatoes but also helps to crisp up the skin.\n", - "6. **Place the potatoes** directly on the middle shelf of the oven. This ensures even cooking.\n", - "7. **Bake for about 20 minutes**, then reduce the oven temperature to 190°C (375°F) and continue baking for another 60-90 minutes, or until the potatoes are cooked through and the skin is crispy.\n", - "8. **Remove the potatoes** from the oven and let them cool for a few minutes.\n", - "9. **Slice open the top** of each potato, being careful not to cut all the way through. Fluff up the inside with a fork.\n", - "10. **Serve hot**, with your choice of fillings or toppings. Popular options include butter, cheese, baked beans, and coleslaw.\n", - "\n", - "Enjoy your delicious British Baked Potato!" + "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" ], "text/plain": [ - "AssistantMessageView(display_id='ffde1008-79b2-4a67-8dde-825246b9291a', content=\"I've stored the recipe for a classic British Baked Potato for you. Here's how to make it:\\n\\n### British Baked Potato\\n\\n#### Ingredients:\\n- 4 Large potatoes\\n- 2 tablespoons 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.\\n3. **Prick the potatoes** all over with a fork. This allows steam to escape during the cooking process.\\n4. **Rub the potatoes** with olive oil. This helps to crisp the skin.\\n5. **Sprinkle sea salt** over the potatoes. The salt not only seasons the potatoes but also helps to crisp up the skin.\\n6. **Place the potatoes** directly on the middle shelf of the oven. This ensures even cooking.\\n7. **Bake for about 20 minutes**, then reduce the oven temperature to 190°C (375°F) and continue baking for another 60-90 minutes, or until the potatoes are cooked through and the skin is crispy.\\n8. **Remove the potatoes** from the oven and let them cool for a few minutes.\\n9. **Slice open the top** of each potato, being careful not to cut all the way through. Fluff up the inside with a fork.\\n10. **Serve hot**, with your choice of fillings or toppings. Popular options include butter, cheese, baked beans, and coleslaw.\\n\\nEnjoy your delicious British Baked Potato!\", 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\", finished=False, has_displayed=True)" ] }, "metadata": {}, @@ -537,6 +471,13 @@ "\n", "await chat(\"Let's make a baked potato like they would in Britain.\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {