From 3cf8de4939f451c044a83fc6e8b0b155942fd161 Mon Sep 17 00:00:00 2001 From: Yingbei Date: Thu, 21 Mar 2024 15:08:29 -0700 Subject: [PATCH] update notebook --- test_llamacpp.ipynb | 112 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 92 insertions(+), 20 deletions(-) diff --git a/test_llamacpp.ipynb b/test_llamacpp.ipynb index 4121a9c84..2ef7032fd 100644 --- a/test_llamacpp.ipynb +++ b/test_llamacpp.ipynb @@ -193,6 +193,13 @@ "print(res)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### IGNORE the following for now." + ] + }, { "cell_type": "code", "execution_count": 7, @@ -268,35 +275,100 @@ "name": "stdout", "output_type": "stream", "text": [ - "[('get_current_weather', [], {'location': 'Boston, MA', 'api_key': 123456789, 'unit': 'fahrenheit'}), ('func', ['cde'], {'x': 1, 'b': '2', 'c': [1, 2, {'a': 1, 'b': 2}]})]\n" + "content: <>[get_current_weather(location='Boston, MA', api_key=123456789, unit='fahrenheit'), func(x= 1, b='2', c=123)]\n", + "[\"get_current_weather(location='Boston, MA', api_key=123456789, unit='fahrenheit')\", \" func(x= 1, b='2', c=123))\"]\n" + ] + }, + { + "ename": "SyntaxError", + "evalue": "unterminated string literal (detected at line 1) (, line 1)", + "output_type": "error", + "traceback": [ + "Traceback \u001b[0;36m(most recent call last)\u001b[0m:\n", + "\u001b[0m File \u001b[1;32m~/.pyenv/versions/3.10.12/envs/py310/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3548\u001b[0m in \u001b[1;35mrun_code\u001b[0m\n exec(code_obj, self.user_global_ns, self.user_ns)\u001b[0m\n", + "\u001b[0m Cell \u001b[1;32mIn[19], line 40\u001b[0m\n result_dict = parse_function_call(function_call.strip())\u001b[0m\n", + "\u001b[0m Cell \u001b[1;32mIn[19], line 22\u001b[0m in \u001b[1;35mparse_function_call\u001b[0m\n parsed_value = ast.literal_eval(value)\u001b[0m\n", + "\u001b[0m File \u001b[1;32m~/.pyenv/versions/3.10.12/lib/python3.10/ast.py:64\u001b[0m in \u001b[1;35mliteral_eval\u001b[0m\n node_or_string = parse(node_or_string.lstrip(\" \\t\"), mode='eval')\u001b[0m\n", + "\u001b[0;36m File \u001b[0;32m~/.pyenv/versions/3.10.12/lib/python3.10/ast.py:50\u001b[0;36m in \u001b[0;35mparse\u001b[0;36m\n\u001b[0;31m return compile(source, filename, mode, flags,\u001b[0;36m\n", + "\u001b[0;36m File \u001b[0;32m:1\u001b[0;36m\u001b[0m\n\u001b[0;31m 'Boston\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unterminated string literal (detected at line 1)\n" + ] + } + ], + "source": [ + "import json\n", + "import re\n", + "import ast\n", + "\n", + "content = \"<>[get_current_weather(location='Boston, MA', api_key=123456789, unit='fahrenheit'), func(x= 1, b='2', c=123)]\"\n", + "regex = re.compile(r\"<>\\[(.*?)\\]\", re.DOTALL)\n", + "matches = re.findall(regex, content)\n", + "\n", + "print(\"content:\", content)\n", + "\n", + "def parse_function_call(call):\n", + " func_name, args_str = call.split('(', 1)\n", + " args_str = args_str.rstrip(')')\n", + " args_list = args_str.split(',')\n", + " args_dict = {}\n", + " for arg in args_list:\n", + " key, value = arg.split('=')\n", + " key = key.strip()\n", + " value = value.strip()\n", + " try:\n", + " # Use ast.literal_eval to safely parse the string to its Python type\n", + " parsed_value = ast.literal_eval(value)\n", + " except ValueError as e:\n", + " # If parsing fails, keep the original string. \n", + " # This might happen if the value is a string that's not quoted as a Python literal.\n", + " print(f\"Error parsing value {value}: {e}\")\n", + " parsed_value = value\n", + " args_dict[key] = parsed_value\n", + " return {\"name\": func_name.strip(), \"arguments\": args_dict}\n", + "\n", + "result_dicts = []\n", + "for match in matches:\n", + " # Splitting each function call from the match. We add ')' back because it was used as a delimiter\n", + " function_calls = [f\"{func})\" for func in match.split('),') if func]\n", + " print(function_calls)\n", + " for function_call in function_calls:\n", + " # Removing the trailing ')' that was added for the last function call\n", + " if function_call.endswith(')'):\n", + " function_call = function_call[:-1]\n", + " result_dict = parse_function_call(function_call.strip())\n", + " result_dicts.append(result_dict)\n", + " print(result_dicts)\n", + "\n", + "res = json.dumps(result_dicts, ensure_ascii=False)\n", + "res" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'name': 'get_current_weather', 'args': [], 'kwargs': {'location': 'Boston, MA', 'api_key': 123456789, 'unit': 'fahrenheit'}}, {'name': 'func', 'args': ['cde'], 'kwargs': {'x': 1, 'b': '2', 'c': [1, 2, {'a': 1, 'b': 2}]}}]\n" ] } ], "source": [ "import ast\n", "\n", - "input_str = \"[get_current_weather(location='Boston, MA', api_key=123456789, unit='fahrenheit'), func('cde', x=1, b='2', c=[1, 2, {'a': 1, 'b': 2}])]\"\n", - "\n", + "raw_input_str = \"<>[get_current_weather(location='Boston, MA', api_key=123456789, unit='fahrenheit'), func('cde', x= 1, b='2', c=[1, 2, {'a': 1, 'b': 2}])]\"\n", + "# raw_input_str = \"<>[get_current_weather(location='Boston, MA', api_key=123456789, unit='fahrenheit'), func( x=1, b='2', c=123)]\"\n", + "input_str = raw_input_str.split('<>')[1]\n", "# Parse the string into an AST\n", "parsed_ast = ast.parse(input_str, mode='eval')\n", "\n", - "# Function to convert an AST node to a Python object\n", - "def ast_node_to_object(node):\n", - " if isinstance(node, ast.Constant):\n", - " return node.value\n", - " elif isinstance(node, ast.List):\n", - " return [ast_node_to_object(n) for n in node.elts]\n", - " elif isinstance(node, ast.Dict):\n", - " return {ast_node_to_object(key): ast_node_to_object(value) for key, value in zip(node.keys, node.values)}\n", - " elif isinstance(node, ast.Tuple):\n", - " return tuple(ast_node_to_object(n) for n in node.elts)\n", - " # Add more cases here as needed\n", - " return None\n", - "\n", "def find_calls(node):\n", " calls = []\n", " if isinstance(node, ast.Call): # If it's a function call\n", " calls.append(node)\n", + " return calls\n", " for child in ast.iter_child_nodes(node):\n", " calls.extend(find_calls(child))\n", " return calls\n", @@ -308,9 +380,9 @@ "for call in calls:\n", " if isinstance(call.func, ast.Name): # Ensure it's a named function\n", " function_name = call.func.id\n", - " args = [ast_node_to_object(arg) for arg in call.args] # Convert all positional arguments\n", - " kwargs = {kw.arg: ast_node_to_object(kw.value) for kw in call.keywords} # Convert all keyword arguments\n", - " functions.append((function_name, args, kwargs))\n", + " args = [ast.literal_eval(arg) for arg in call.args] # Convert all positional arguments\n", + " kwargs = {kw.arg: ast.literal_eval(kw.value) for kw in call.keywords} # Convert all keyword arguments\n", + " functions.append({\"name\": function_name, \"args\": args, \"kwargs\":kwargs})\n", "\n", "print(functions)\n" ] @@ -331,7 +403,7 @@ "source": [ "import ast\n", "\n", - "input_str = \"[get_current_weather(location='Boston, MA', api_key=123456789, unit='fahrenheit'), func('cde', x=1, b='2', c=[func_nested(1, 2), {'a': func_deep('value')}])]\"\n", + "input_str = \"[get_current_weather(location='Boston, MA', api_key=123456789, unit='fahrenheit'), func('cde', x= 1, b='2', c=[func_nested(1, 2), {'a': func_deep('value')}])]\"\n", "\n", "def ast_node_to_object(node):\n", " if isinstance(node, ast.Constant):\n",