Skip to content

Commit

Permalink
Hacky func streaming (#1)
Browse files Browse the repository at this point in the history
* hacky function call streaming

* remove

* minor fix to take care of case that the input function has no description or arguments is null

* test parser

* fix makefile to make sure the order of file linking works for ubuntu gcc/g++ 11.4

* add function name mapping to take care of input function name with hyphen-

* add a comment TODO for streaming chunks.
  • Loading branch information
tybalex committed Apr 17, 2024
1 parent 66691ed commit c3dd240
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions test_llamacpp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
" 'required': []\n",
" }\n",
" }\n",
<<<<<<< HEAD
=======
" print(e, model, prompt)\n"
]
Expand Down Expand Up @@ -172,6 +173,8 @@
" \"location\": {\"type\": \"string\", \"description\": \"The city and state e.g. San Francisco, CA\"},\n",
" \"unit\": {\"type\": \"string\", \"enum\": [\"c\", \"f\"]}\n",
>>>>>>> ba3724d (a first working version integrated tree_sitter with python parser code)
=======
>>>>>>> 7cb21bd (Hacky func streaming (#1))
" },\n",
" {\n",
" 'type': 'function',\n",
Expand Down Expand Up @@ -314,6 +317,9 @@
{
"cell_type": "code",
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> 7cb21bd (Hacky func streaming (#1))
"execution_count": 4,
"metadata": {},
"outputs": [],
Expand All @@ -326,9 +332,12 @@
{
"cell_type": "code",
"execution_count": 5,
<<<<<<< HEAD
=======
"execution_count": 177,
>>>>>>> ba3724d (a first working version integrated tree_sitter with python parser code)
=======
>>>>>>> 7cb21bd (Hacky func streaming (#1))
"metadata": {},
"outputs": [
{
Expand All @@ -348,22 +357,29 @@
"\n",
"[AI response]:\n",
" The distance between San Francisco and Cupertino by driving is 50 miles and 100 miles from Cupertino to San Francisco. When traveling by air, the distance is 150 miles from San Francisco to Cupertino and 200 miles from Cupertino to San Francisco.\n"
<<<<<<< HEAD
=======
" <<functions>>[get_stock_fundermentals(symbol=\"TSLA\")]\n",
"<<functions>>[get_stock_fundermentals(symbol=\"GOOG\")]\n"
>>>>>>> ba3724d (a first working version integrated tree_sitter with python parser code)
=======
>>>>>>> 7cb21bd (Hacky func streaming (#1))
]
}
],
"source": [
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> 7cb21bd (Hacky func streaming (#1))
"\n",
"get_mistral_rubra_response = partial(get_oai_response, api_key=local_api_key, base_url=local_base_url)\n",
"\n",
"user_query = \"What is the distance between San Francisco and Cupertino by driving and by air from both directions?\"\n",
"# user_query = \"What is four plus six? What is the result of that plus 2? Take the result and multiply by 5 and then divide by two\"\n",
"# user_query = \"what's the distance between SF and NYC? Use the result value to multiply by 8, and then divide by 2, and then minus 30\"\n",
"msgs = run_completion(get_mistral_rubra_response, user_query)\n"
<<<<<<< HEAD
=======
"system_prompt = \"You are a helpful assistant.\"\n",
"functions = [\n",
Expand Down Expand Up @@ -415,21 +431,62 @@
"res = get_mistral_rubra_response(user_query, \"mistral_rubra\", functions=functions, msgs=msgs)\n",
"print(res.message.content)"
>>>>>>> ba3724d (a first working version integrated tree_sitter with python parser code)
=======
>>>>>>> 7cb21bd (Hacky func streaming (#1))
]
},
{
"cell_type": "code",
<<<<<<< HEAD
<<<<<<< HEAD
"execution_count": 8,
=======
"execution_count": 183,
>>>>>>> ba3724d (a first working version integrated tree_sitter with python parser code)
=======
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pointing to URL: http://localhost:8019/v1/\n",
"\n",
"[AI calling functions]:\n",
"Tool Call: Function(arguments='{\"number_to_buy\":3}', name='orderUmbrella')\n",
"Tool Call: Function(arguments='{\"length\":8}', name='generate_password')\n",
"Pointing to URL: http://localhost:8019/v1/\n",
"Loop 0\n",
"\n",
"[AI response]:\n",
" Your order for 3 umbrellas has been placed, and the total price is 10 dollars. The generated password is 96ddefe8.\n"
]
}
],
"source": [
"user_query2 = \"now order 3 umbrellas for me and generate a password of length 8\"\n",
"msgs = run_completion(get_mistral_rubra_response, user_query2, msgs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simple Math Chaining"
]
},
{
"cell_type": "code",
"execution_count": 9,
>>>>>>> 7cb21bd (Hacky func streaming (#1))
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
<<<<<<< HEAD
<<<<<<< HEAD
"Pointing to URL: http://localhost:8019/v1/\n",
"\n",
Expand All @@ -445,18 +502,47 @@
"<ast.List object at 0x105649390>\n",
"[('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"
>>>>>>> ba3724d (a first working version integrated tree_sitter with python parser code)
=======
"Pointing to URL: http://localhost:8019/v1/\n",
"\n",
"[AI calling functions]:\n",
"Tool Call: Function(arguments='{\"a\":\"4\",\"b\":\"6\"}', name='addition')\n",
"Tool Call: Function(arguments='{\"a\":\"result\",\"b\":\"2\"}', name='addition')\n",
"Tool Call: Function(arguments='{\"a\":\"result\",\"b\":\"5\"}', name='multiplication')\n",
"Tool Call: Function(arguments='{\"a\":\"result\",\"b\":\"2\"}', name='division')\n"
]
},
{
"ename": "ValueError",
"evalue": "could not convert string to float: 'result'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[9], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m user_query3 \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUser tool to help me : What is four plus six? What is the result of that plus 2? Take the result and multiply by 5 and then divide by two\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 2\u001b[0m msgs \u001b[38;5;241m=\u001b[39m \u001b[43mrun_completion\u001b[49m\u001b[43m(\u001b[49m\u001b[43mget_mistral_rubra_response\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43muser_query3\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmsgs\u001b[49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[3], line 244\u001b[0m, in \u001b[0;36mrun_completion\u001b[0;34m(chat_method, user_query, msgs)\u001b[0m\n\u001b[1;32m 242\u001b[0m l \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m 243\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m res_next\u001b[38;5;241m.\u001b[39mmessage\u001b[38;5;241m.\u001b[39mtool_calls \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(res_next\u001b[38;5;241m.\u001b[39mmessage\u001b[38;5;241m.\u001b[39mtool_calls) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m--> 244\u001b[0m msgs \u001b[38;5;241m=\u001b[39m \u001b[43minsert_tool_response\u001b[49m\u001b[43m(\u001b[49m\u001b[43mres_next\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmsgs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 246\u001b[0m res_next \u001b[38;5;241m=\u001b[39m chat_method(model\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpt-4-0125-preview\u001b[39m\u001b[38;5;124m\"\u001b[39m, functions\u001b[38;5;241m=\u001b[39mfunctions, msgs\u001b[38;5;241m=\u001b[39mmsgs)\n\u001b[1;32m 247\u001b[0m \u001b[38;5;66;03m# for m in msgs:\u001b[39;00m\n\u001b[1;32m 248\u001b[0m \u001b[38;5;66;03m# print(m)\u001b[39;00m\n",
"Cell \u001b[0;32mIn[3], line 77\u001b[0m, in \u001b[0;36minsert_tool_response\u001b[0;34m(res, msgs)\u001b[0m\n\u001b[1;32m 72\u001b[0m msgs\u001b[38;5;241m.\u001b[39mappend({\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrole\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtool\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtool_call_id\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;28mstr\u001b[39m(assistant_message\u001b[38;5;241m.\u001b[39mtool_calls[i]\u001b[38;5;241m.\u001b[39mid), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m\"\u001b[39m: assistant_message\u001b[38;5;241m.\u001b[39mtool_calls[i]\u001b[38;5;241m.\u001b[39mfunction\u001b[38;5;241m.\u001b[39mname, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcontent\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOrder placed. the price is \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m(i\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m1\u001b[39m)\u001b[38;5;250m \u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;250m \u001b[39m\u001b[38;5;241m10\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m dollars.\u001b[39m\u001b[38;5;124m\"\u001b[39m})\n\u001b[1;32m 73\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m tool_call\u001b[38;5;241m.\u001b[39mfunction\u001b[38;5;241m.\u001b[39mname \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124maddition\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 74\u001b[0m msgs\u001b[38;5;241m.\u001b[39mappend({\n\u001b[1;32m 75\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrole\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtool\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 76\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124maddition\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m---> 77\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcontent\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[43madd\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtool_call\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfunction\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marguments\u001b[49m\u001b[43m)\u001b[49m,\n\u001b[1;32m 78\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtool_call_id\u001b[39m\u001b[38;5;124m\"\u001b[39m: tool_call\u001b[38;5;241m.\u001b[39mid\n\u001b[1;32m 79\u001b[0m })\n\u001b[1;32m 80\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m tool_call\u001b[38;5;241m.\u001b[39mfunction\u001b[38;5;241m.\u001b[39mname \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msubtraction\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 81\u001b[0m msgs\u001b[38;5;241m.\u001b[39mappend({\n\u001b[1;32m 82\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrole\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtool\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 83\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msubtraction\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 84\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcontent\u001b[39m\u001b[38;5;124m\"\u001b[39m: sub(tool_call\u001b[38;5;241m.\u001b[39mfunction\u001b[38;5;241m.\u001b[39marguments),\n\u001b[1;32m 85\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtool_call_id\u001b[39m\u001b[38;5;124m\"\u001b[39m: tool_call\u001b[38;5;241m.\u001b[39mid\n\u001b[1;32m 86\u001b[0m })\n",
"Cell \u001b[0;32mIn[3], line 8\u001b[0m, in \u001b[0;36madd\u001b[0;34m(args)\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21madd\u001b[39m(args: \u001b[38;5;28mstr\u001b[39m):\n\u001b[1;32m 7\u001b[0m args \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mloads(args)\n\u001b[0;32m----> 8\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mstr\u001b[39m(\u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43ma\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mfloat\u001b[39m(args[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m]))\n",
"\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'result'"
>>>>>>> 7cb21bd (Hacky func streaming (#1))
]
}
],
"source": [
<<<<<<< HEAD
<<<<<<< HEAD
"user_query2 = \"now order 3 umbrellas for me and generate a password of length 8\"\n",
"msgs = run_completion(get_mistral_rubra_response, user_query2, msgs)"
=======
"user_query3 = \"User tool to help me : What is four plus six? What is the result of that plus 2? Take the result and multiply by 5 and then divide by two\"\n",
"msgs = run_completion(get_mistral_rubra_response, user_query3, msgs)"
>>>>>>> 7cb21bd (Hacky func streaming (#1))
]
},
{
"cell_type": "markdown",
"metadata": {},
<<<<<<< HEAD
"source": [
"## Simple Math Chaining"
=======
Expand Down Expand Up @@ -612,6 +698,10 @@
"print(functions)\n"
]
>>>>>>> ba3724d (a first working version integrated tree_sitter with python parser code)
=======
"outputs": [],
"source": []
>>>>>>> 7cb21bd (Hacky func streaming (#1))
}
],
"metadata": {
Expand Down

0 comments on commit c3dd240

Please sign in to comment.