diff --git a/Makefile b/Makefile index 4d0eebfb7d1489..df211aa4d0fe64 100644 --- a/Makefile +++ b/Makefile @@ -802,7 +802,7 @@ save-load-state: examples/save-load-state/save-load-state.cpp ggml.o llama.o $(C $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<) $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) -server: examples/server/server.cpp examples/server/utils.hpp examples/server/python-parser.hpp examples/server/yaml-parser.hpp examples/server/function-call.hpp examples/server/tree_sitter/libtree-sitter.a examples/server/yaml-cpp/libyaml-cpp.a examples/server/httplib.h common/json.hpp examples/server/index.html.hpp examples/server/index.js.hpp examples/server/completion.js.hpp examples/server/json-schema-to-grammar.mjs.hpp common/stb_image.h ggml.o llama.o func_scanner.o func_parser.o $(COMMON_DEPS) grammar-parser.o $(OBJS) +server: examples/server/server.cpp examples/server/utils.hpp examples/server/python-parser.hpp examples/server/function-call-parser.hpp examples/server/function-call.hpp examples/server/tree_sitter/libtree-sitter.a examples/server/yaml-cpp/libyaml-cpp.a examples/server/httplib.h common/json.hpp examples/server/index.html.hpp examples/server/index.js.hpp examples/server/completion.js.hpp examples/server/json-schema-to-grammar.mjs.hpp common/stb_image.h ggml.o llama.o func_scanner.o func_parser.o $(COMMON_DEPS) grammar-parser.o $(OBJS) $(CXX) $(CXXFLAGS) -c $< -I examples/server/tree_sitter -I examples/server/yaml-cpp -o $(call GET_OBJ_FILE, $<) $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) $(call GET_OBJ_FILE, $<) -Iexamples/server -o $@ $(LDFLAGS) $(LWINSOCK2) diff --git a/examples/server/yaml-parser.hpp b/examples/server/function-call-parser.hpp similarity index 69% rename from examples/server/yaml-parser.hpp rename to examples/server/function-call-parser.hpp index 37cfbbc71022b8..344714e410699a 100644 --- a/examples/server/yaml-parser.hpp +++ b/examples/server/function-call-parser.hpp @@ -3,7 +3,7 @@ #include using json = nlohmann::ordered_json; - +using namespace std; static json get_value(string target_arg_type, const YAML::Node & node) { json arg_json; @@ -94,3 +94,52 @@ std::vector rubra_fc_yaml_tool_extractor(const std::string& source_string, return calls; } + +std::string generate_uuid() { + static std::random_device rd; + static std::mt19937 generator(rd()); + static std::uniform_int_distribution distribution(0, 15); + + const char *v = "0123456789abcdef"; + std::stringstream uuid; + + for (int i = 0; i < 8; ++i) { + uuid << v[distribution(generator)]; + } + return uuid.str(); +} + +std::vector rubra_fc_json_tool_extractor(const std::string& output_str) { + std::vector result; + if (output_str.find("") == std::string::npos) { + return result; + } + + std::string str_to_parse = output_str.substr(output_str.find("") + std::string("").length()); + std::istringstream stream(str_to_parse); + std::string line; + std::vector function_call_json; + + try { + while (std::getline(stream, line)) { + json fc = json::parse(line); + if (fc["arguments"].is_string()) { + fc["arguments"] = json::parse(fc["arguments"].get()); + } + function_call_json.push_back(fc); + } + } catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + } + + for (const auto& fc : function_call_json) { + json func_call; + func_call["id"] = generate_uuid(); + func_call["name"] = fc["name"]; + func_call["kwargs"] = fc["arguments"]; + func_call["type"] = "function"; + result.push_back(func_call); + } + + return result; +} \ No newline at end of file diff --git a/examples/server/function-call.hpp b/examples/server/function-call.hpp index 796b754903048b..4c643e9ede9b0b 100644 --- a/examples/server/function-call.hpp +++ b/examples/server/function-call.hpp @@ -243,7 +243,7 @@ string rubra_format_typescript_function_call_str(const std::vector &functi for (const auto& def : function_definitions) { final_str += def + "\n\n"; } - final_str += "Use the following format if using a tool:\n<>[toolname1(arg1=value1, arg2=value2, ...), toolname2(arg1=value1, arg2=value2, ...)]\nYou can choose to respond with 1 or more tool calls at once, or with a chat message back to the user. Only make tool calls once you have all the details to fill in the required params. Feel free to ask the user for more info when appropriate. Any tool call you make must match the name of a function(s) provided above."; + final_str += "You can choose to respond with one or more tool calls at once, or with a chat message back to the user. Ensure you have all necessary details before making tool calls. If additional information is needed, ask the user appropriately. Any tool call you make must correspond to the functions listed above.\nIf you decide to call tools, format your response in JSONL. Start with the keyword `` followed by the JSON object:\n`{\"name\": \"\", \"arguments\": {\"\": \"\", \"\": \"\", ...}}`"; return final_str; } @@ -546,17 +546,38 @@ static string construct_yaml_tool_call_str(const json & tool_calls, nlohmann::or } +std::string construct_json_tool_call_str(const json& tool_calls, nlohmann::ordered_map & func_observation_map) { + std::string tool_call_str; + bool first = true; + for (const auto& tool_call : tool_calls) { + std::string tool_call_id = tool_call["id"]; + func_observation_map[tool_call_id] = ""; // Initialize with empty value, updated later from the message with tool role + + if (!first) { + tool_call_str += "\n"; + } + json tc = tool_call["function"]; + if (tc["arguments"].is_string()) { + tc["arguments"] = json::parse(tc["arguments"].get()); + } + tool_call_str += tc.dump(); + first = false; + } + + return std::string("") + tool_call_str; +} + const vector expand_messages(const json & body, json &tool_name_map, json& func_arg_type) { string function_str = ""; if (body.contains("tools") && !body["tools"].empty()) { - // function_str = rubra_format_typescript_function_call_str(body["tools"], tool_name_map); - function_str = rubra_format_yaml_function_call_str(body["tools"], func_arg_type); + function_str = rubra_format_typescript_function_call_str(body["tools"], tool_name_map); + // function_str = rubra_format_yaml_function_call_str(body["tools"], func_arg_type); } // If 'tool' is not set or empty, check 'functions' else if (body.contains("functions") && !body["functions"].empty()) { - // function_str = rubra_format_typescript_function_call_str(body["functions"], tool_name_map); - function_str = rubra_format_yaml_function_call_str(body["functions"], func_arg_type); + function_str = rubra_format_typescript_function_call_str(body["functions"], tool_name_map); + // function_str = rubra_format_yaml_function_call_str(body["functions"], func_arg_type); } if (function_str != "") { @@ -601,7 +622,7 @@ const vector expand_messages(const json & body, json &tool_name_map, json& else if (body["messages"][i]["role"] == "assistant" and body["messages"][i].contains("tool_calls")){ // convert OpenAI function call format to Rubra format // string tool_call_str = construct_python_tool_call_str(body["messages"][i]["tool_calls"], func_observation_map); - string tool_call_str = construct_yaml_tool_call_str(body["messages"][i]["tool_calls"], func_observation_map); + string tool_call_str = construct_json_tool_call_str(body["messages"][i]["tool_calls"], func_observation_map); json function_call; function_call["role"] = "function"; function_call["content"] = tool_call_str; diff --git a/examples/server/utils.hpp b/examples/server/utils.hpp index 3ade196f215d49..dedb617999b631 100644 --- a/examples/server/utils.hpp +++ b/examples/server/utils.hpp @@ -6,7 +6,7 @@ #include "json.hpp" #include "python-parser.hpp" #include "function-call.hpp" -#include "yaml-parser.hpp" +#include "function-call-parser.hpp" #include #include @@ -448,7 +448,8 @@ static json format_final_response_oaicompat(const json & request, json result, c std::string content = json_value(result, "content", std::string("")); // std::vector parsed_content = parsePythonFunctionCalls(content, request["tool_name_map"]); - std::vector parsed_content = rubra_fc_yaml_tool_extractor(content, request["func_arg_type"]); + // std::vector parsed_content = rubra_fc_yaml_tool_extractor(content, request["func_arg_type"]); + std::vector parsed_content = rubra_fc_json_tool_extractor(content); std::string finish_reason = "length"; if (stopped_word || stopped_eos) { @@ -475,6 +476,7 @@ static json format_final_response_oaicompat(const json & request, json result, c json tool_call; tool_call["id"] = pc["id"]; tool_call["type"] = "function"; + tool_call["function"] = json{ {"name" , pc["name"]}, {"arguments" , pc["kwargs"].dump()}, @@ -531,7 +533,9 @@ static std::vector format_partial_response_oaicompat(json request ,json re std::string content = json_value(result, "content", std::string("")); // std::vector parsed_content = parsePythonFunctionCalls(content, request["tool_name_map"]); - std::vector parsed_content = rubra_fc_yaml_tool_extractor(content, request["func_arg_type"]); + // std::vector parsed_content = rubra_fc_yaml_tool_extractor(content, request["func_arg_type"]); + std::vector parsed_content = rubra_fc_json_tool_extractor(content); + std::time_t t = std::time(0); if (!parsed_content.empty()) { std::vector res; diff --git a/test_llamacpp.ipynb b/test_llamacpp.ipynb index 2a704aa4fa0899..5a874bb76faeb0 100644 --- a/test_llamacpp.ipynb +++ b/test_llamacpp.ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -117,7 +117,7 @@ " \"content\": div(tool_call.function.arguments),\n", " \"tool_call_id\": tool_call.id\n", " })\n", - " print(f\"Observation: {msgs[-1]['content']}\")\n", + " print(f\"Observation: {msgs[-1]}\")\n", " \n", " return msgs\n", "\n", @@ -399,7 +399,7 @@ " l = 0\n", " while res_next.message.tool_calls and len(res_next.message.tool_calls) > 0:\n", " msgs = insert_tool_response(res_next, msgs)\n", - "\n", + " print(msgs)\n", " res_next = chat_method(model=\"gpt-4-0125-preview\", functions=functions, msgs=msgs)\n", " # for m in msgs:\n", " # print(m)\n", @@ -423,7 +423,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -434,7 +434,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -444,13 +444,21 @@ "Pointing to URL: http://localhost:8019/v1/\n", "\n", "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"origin\":\"San Francisco\",\"destination\":\"Cupertino\",\"mode\":\"drive\"}', name='calculate_distance')\n", - "Observation: Distance is 50 miles.\n", + "Tool Call: Function(arguments='{\"origin\":\"San Francisco\",\"destination\":\"Cupertino\",\"mode\":\"driving\"}', name='calculate_distance')\n", + "Observation: {'role': 'tool', 'tool_call_id': '8eefe744', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'What is the distance between San Francisco and Cupertino by driving and by air from both directions?'}, {'role': 'assistant', 'tool_calls': [{'id': '8eefe744', 'function': {'name': 'calculate_distance', 'arguments': '{\"origin\":\"San Francisco\",\"destination\":\"Cupertino\",\"mode\":\"driving\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': '8eefe744', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 0\n", "\n", + "[AI calling functions]:\n", + "Tool Call: Function(arguments='{\"origin\":\"San Francisco\",\"destination\":\"Cupertino\",\"mode\":\"air\"}', name='calculate_distance')\n", + "Observation: {'role': 'tool', 'tool_call_id': '8a948772', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'What is the distance between San Francisco and Cupertino by driving and by air from both directions?'}, {'role': 'assistant', 'tool_calls': [{'id': '8eefe744', 'function': {'name': 'calculate_distance', 'arguments': '{\"origin\":\"San Francisco\",\"destination\":\"Cupertino\",\"mode\":\"driving\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': '8eefe744', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}, {'role': 'assistant', 'tool_calls': [{'id': '8a948772', 'function': {'name': 'calculate_distance', 'arguments': '{\"origin\":\"San Francisco\",\"destination\":\"Cupertino\",\"mode\":\"air\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': '8a948772', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}]\n", + "Pointing to URL: http://localhost:8019/v1/\n", + "Loop 1\n", + "\n", "[AI response]:\n", - " The distance between San Francisco and Cupertino by driving is 50 miles.\n" + " The driving distance between San Francisco and Cupertino is 50 miles. The air distance from San Francisco to Cupertino is also 50 miles.\n" ] } ], @@ -466,7 +474,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -476,20 +484,14 @@ "Pointing to URL: http://localhost:8019/v1/\n", "\n", "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"premiums\":[1.0,3.0,5.0]}', name='latest_insurance_premium')\n" - ] - }, - { - "ename": "KeyError", - "evalue": "'content'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[45], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# user_query0 = \"create a 3d model with length 8, width 5, and height 4\"\u001b[39;00m\n\u001b[1;32m 2\u001b[0m user_query0 \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwhat\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ms the latest insurance premium of 1, 3 ,5\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 3\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_query0\u001b[49m\u001b[43m)\u001b[49m\n", - "Cell \u001b[0;32mIn[19], line 386\u001b[0m, in \u001b[0;36mrun_completion\u001b[0;34m(chat_method, user_query, msgs)\u001b[0m\n\u001b[1;32m 384\u001b[0m l \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m 385\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--> 386\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 388\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 389\u001b[0m \u001b[38;5;66;03m# for m in msgs:\u001b[39;00m\n\u001b[1;32m 390\u001b[0m \u001b[38;5;66;03m# print(m)\u001b[39;00m\n", - "Cell \u001b[0;32mIn[19], line 105\u001b[0m, in \u001b[0;36minsert_tool_response\u001b[0;34m(res, msgs)\u001b[0m\n\u001b[1;32m 98\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;124mdivision\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 99\u001b[0m msgs\u001b[38;5;241m.\u001b[39mappend({\n\u001b[1;32m 100\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 101\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;124mdivision\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 102\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcontent\u001b[39m\u001b[38;5;124m\"\u001b[39m: div(tool_call\u001b[38;5;241m.\u001b[39mfunction\u001b[38;5;241m.\u001b[39marguments),\n\u001b[1;32m 103\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 104\u001b[0m })\n\u001b[0;32m--> 105\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mObservation: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[43mmsgs\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mcontent\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 107\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m msgs\n", - "\u001b[0;31mKeyError\u001b[0m: 'content'" + "Tool Call: Function(arguments='{\"object_name\":\"rectangle\",\"dimensions\":{\"length\":8,\"width\":5,\"height\":4}}', name='create_3d_model')\n", + "Observation: {'role': 'assistant', 'tool_calls': [{'id': '0ee56562', 'function': {'name': 'create_3d_model', 'arguments': '{\"object_name\":\"rectangle\",\"dimensions\":{\"length\":8,\"width\":5,\"height\":4}}'}, 'type': 'function'}]}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'create a 3d model with length 8, width 5, and height 4'}, {'role': 'assistant', 'tool_calls': [{'id': '0ee56562', 'function': {'name': 'create_3d_model', 'arguments': '{\"object_name\":\"rectangle\",\"dimensions\":{\"length\":8,\"width\":5,\"height\":4}}'}, 'type': 'function'}]}]\n", + "Pointing to URL: http://localhost:8019/v1/\n", + "Loop 0\n", + "\n", + "[AI response]:\n", + " A 3D model of a rectangle with dimensions 8x5x4 has been successfully created.\n" ] } ], @@ -501,7 +503,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -511,25 +513,21 @@ "Pointing to URL: http://localhost:8019/v1/\n", "\n", "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"number_to_buy\":\"3\"}', name='orderUmbrella')\n", - "Observation: Order placed. the price is 10 dollars.\n", + "Tool Call: Function(arguments='{\"number_to_buy\":3}', name='orderUmbrella')\n", + "Observation: {'role': 'tool', 'tool_call_id': '19e16a34', 'name': 'orderUmbrella', 'content': 'Order placed. the price is 10 dollars.'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'now order 3 umbrellas for me and generate a password of length 8'}, {'role': 'assistant', 'tool_calls': [{'id': '19e16a34', 'function': {'name': 'orderUmbrella', 'arguments': '{\"number_to_buy\":3}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': '19e16a34', 'name': 'orderUmbrella', 'content': 'Order placed. the price is 10 dollars.'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 0\n", "\n", "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"length\":\"8\"}', name='generate_password')\n", - "Observation: Password generated: e1bd5f66\n", + "Tool Call: Function(arguments='{\"length\":8}', name='generate_password')\n", + "Observation: {'role': 'tool', 'tool_call_id': 'c14327a0', 'name': 'generate_password', 'content': 'Password generated: 85f87d3d'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'now order 3 umbrellas for me and generate a password of length 8'}, {'role': 'assistant', 'tool_calls': [{'id': '19e16a34', 'function': {'name': 'orderUmbrella', 'arguments': '{\"number_to_buy\":3}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': '19e16a34', 'name': 'orderUmbrella', 'content': 'Order placed. the price is 10 dollars.'}, {'role': 'assistant', 'tool_calls': [{'id': 'c14327a0', 'function': {'name': 'generate_password', 'arguments': '{\"length\":8}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': 'c14327a0', 'name': 'generate_password', 'content': 'Password generated: 85f87d3d'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 1\n", "\n", - "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"origin\":\"New York\",\"destination\":\"Los Angeles\",\"mode\":\"car\"}', name='calculate_distance')\n", - "Observation: Distance is 50 miles.\n", - "Pointing to URL: http://localhost:8019/v1/\n", - "Loop 2\n", - "\n", "[AI response]:\n", - " Your order for 3 umbrellas has been placed, and a password 'e1bd5f66' was generated. The price for the umbrellas is 10 dollars. The distance from New York to Los Angeles is 50 miles if you travel by car.\n" + " Your order for 3 umbrellas has been placed at 10 dollars each. A secure password '85f87d3d' has also been generated for you.\n" ] } ], @@ -547,7 +545,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -558,30 +556,27 @@ "\n", "[AI calling functions]:\n", "Tool Call: Function(arguments='{\"a\":\"4\",\"b\":\"6\"}', name='addition')\n", - "Observation: 10.0\n", + "Observation: {'role': 'tool', 'name': 'addition', 'content': '10.0', 'tool_call_id': '89cdcf84'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': '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'}, {'role': 'assistant', 'tool_calls': [{'id': '89cdcf84', 'function': {'name': 'addition', 'arguments': '{\"a\":\"4\",\"b\":\"6\"}'}, 'type': 'function'}]}, {'role': 'tool', 'name': 'addition', 'content': '10.0', 'tool_call_id': '89cdcf84'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 0\n", "\n", "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"a\":\"10\",\"b\":\"2\"}', name='addition')\n", - "Observation: 12.0\n", + "Tool Call: Function(arguments='{\"a\":\"10.0\",\"b\":\"5\"}', name='multiplication')\n", + "Observation: {'role': 'tool', 'name': 'multiplication', 'content': '50.0', 'tool_call_id': '890a70de'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': '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'}, {'role': 'assistant', 'tool_calls': [{'id': '89cdcf84', 'function': {'name': 'addition', 'arguments': '{\"a\":\"4\",\"b\":\"6\"}'}, 'type': 'function'}]}, {'role': 'tool', 'name': 'addition', 'content': '10.0', 'tool_call_id': '89cdcf84'}, {'role': 'assistant', 'tool_calls': [{'id': '890a70de', 'function': {'name': 'multiplication', 'arguments': '{\"a\":\"10.0\",\"b\":\"5\"}'}, 'type': 'function'}]}, {'role': 'tool', 'name': 'multiplication', 'content': '50.0', 'tool_call_id': '890a70de'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 1\n", "\n", "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"a\":\"12\",\"b\":\"5\"}', name='multiplication')\n", - "Observation: 60.0\n", + "Tool Call: Function(arguments='{\"a\":\"50.0\",\"b\":\"2\"}', name='division')\n", + "Observation: {'role': 'tool', 'name': 'division', 'content': '25.0', 'tool_call_id': '6519faf3'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': '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'}, {'role': 'assistant', 'tool_calls': [{'id': '89cdcf84', 'function': {'name': 'addition', 'arguments': '{\"a\":\"4\",\"b\":\"6\"}'}, 'type': 'function'}]}, {'role': 'tool', 'name': 'addition', 'content': '10.0', 'tool_call_id': '89cdcf84'}, {'role': 'assistant', 'tool_calls': [{'id': '890a70de', 'function': {'name': 'multiplication', 'arguments': '{\"a\":\"10.0\",\"b\":\"5\"}'}, 'type': 'function'}]}, {'role': 'tool', 'name': 'multiplication', 'content': '50.0', 'tool_call_id': '890a70de'}, {'role': 'assistant', 'tool_calls': [{'id': '6519faf3', 'function': {'name': 'division', 'arguments': '{\"a\":\"50.0\",\"b\":\"2\"}'}, 'type': 'function'}]}, {'role': 'tool', 'name': 'division', 'content': '25.0', 'tool_call_id': '6519faf3'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 2\n", "\n", - "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"a\":\"60\",\"b\":\"2\"}', name='division')\n", - "Observation: 30.0\n", - "Pointing to URL: http://localhost:8019/v1/\n", - "Loop 3\n", - "\n", "[AI response]:\n", - " The result of adding 4 and 6 is 10. Adding 2 more gives 12. Multiplying 12 by 5 results in 60. Dividing 60 by 2 gives 30.\n" + " The result of adding 4 and 6 is 10. After multiplying this by 5, the result is 50. Finally, dividing this by 2 gives 25.\n" ] } ], @@ -601,7 +596,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -611,20 +606,29 @@ "Pointing to URL: http://localhost:8019/v1/\n", "\n", "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"location\":\"Boston, MA\",\"unit\":\"f\"}', name='getCurrentWeather')\n", + "Tool Call: Function(arguments='{\"location\":\"Boston\",\"unit\":\"f\"}', name='getCurrentWeather')\n", "\n", - "Observation: temprature is 60 degree\n", + "Observation: {'role': 'tool', 'tool_call_id': 'de8f0d7b', 'name': 'getCurrentWeather', 'content': 'temprature is 56 degree'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': \"check the weather in boston, calculate the distance from boston to NYC for me only if it's less than 100 degrees Fahrenheit\"}, {'role': 'assistant', 'tool_calls': [{'id': 'de8f0d7b', 'function': {'name': 'getCurrentWeather', 'arguments': '{\"location\":\"Boston\",\"unit\":\"f\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': 'de8f0d7b', 'name': 'getCurrentWeather', 'content': 'temprature is 56 degree'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 0\n", "\n", "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"origin\":\"Boston, MA\",\"destination\":\"New York City, NY\",\"mode\":\"car\"}', name='calculate_distance')\n", - "Observation: Distance is 50 miles.\n", + "Tool Call: Function(arguments='{\"origin\":\"Boston\",\"destination\":\"New York City\",\"mode\":\"car\"}', name='calculate_distance')\n", + "Observation: {'role': 'tool', 'tool_call_id': '361acf3f', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': \"check the weather in boston, calculate the distance from boston to NYC for me only if it's less than 100 degrees Fahrenheit\"}, {'role': 'assistant', 'tool_calls': [{'id': 'de8f0d7b', 'function': {'name': 'getCurrentWeather', 'arguments': '{\"location\":\"Boston\",\"unit\":\"f\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': 'de8f0d7b', 'name': 'getCurrentWeather', 'content': 'temprature is 56 degree'}, {'role': 'assistant', 'tool_calls': [{'id': '361acf3f', 'function': {'name': 'calculate_distance', 'arguments': '{\"origin\":\"Boston\",\"destination\":\"New York City\",\"mode\":\"car\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': '361acf3f', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 1\n", "\n", + "[AI calling functions]:\n", + "Tool Call: Function(arguments='{\"a\":\"56\",\"b\":\"100\"}', name='subtraction')\n", + "Observation: {'role': 'tool', 'name': 'subtraction', 'content': '-44.0', 'tool_call_id': '7a78eb34'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': \"check the weather in boston, calculate the distance from boston to NYC for me only if it's less than 100 degrees Fahrenheit\"}, {'role': 'assistant', 'tool_calls': [{'id': 'de8f0d7b', 'function': {'name': 'getCurrentWeather', 'arguments': '{\"location\":\"Boston\",\"unit\":\"f\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': 'de8f0d7b', 'name': 'getCurrentWeather', 'content': 'temprature is 56 degree'}, {'role': 'assistant', 'tool_calls': [{'id': '361acf3f', 'function': {'name': 'calculate_distance', 'arguments': '{\"origin\":\"Boston\",\"destination\":\"New York City\",\"mode\":\"car\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': '361acf3f', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}, {'role': 'assistant', 'tool_calls': [{'id': '7a78eb34', 'function': {'name': 'subtraction', 'arguments': '{\"a\":\"56\",\"b\":\"100\"}'}, 'type': 'function'}]}, {'role': 'tool', 'name': 'subtraction', 'content': '-44.0', 'tool_call_id': '7a78eb34'}]\n", + "Pointing to URL: http://localhost:8019/v1/\n", + "Loop 2\n", + "\n", "[AI response]:\n", - " The weather in Boston is 60 degrees Fahrenheit. The distance from Boston to New York City is 50 miles.\n" + " The weather in Boston is 56 degrees Fahrenheit. The distance from Boston to New York City is 50 miles. The difference between 56 and 100 is -44.0 degrees Fahrenheit.\n" ] } ], @@ -635,7 +639,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -645,20 +649,29 @@ "Pointing to URL: http://localhost:8019/v1/\n", "\n", "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"location\":\"Boston, MA\",\"unit\":\"f\"}', name='getCurrentWeather')\n", + "Tool Call: Function(arguments='{\"location\":\"Boston\",\"unit\":\"f\"}', name='getCurrentWeather')\n", "\n", - "Observation: temprature is 60 degree\n", + "Observation: {'role': 'tool', 'tool_call_id': 'defe6bc9', 'name': 'getCurrentWeather', 'content': 'temprature is 56 degree'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': \"check the weather in boston, calculate the distance from boston to NYC for me only if it's greater than 100 degrees Fahrenheit\"}, {'role': 'assistant', 'tool_calls': [{'id': 'defe6bc9', 'function': {'name': 'getCurrentWeather', 'arguments': '{\"location\":\"Boston\",\"unit\":\"f\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': 'defe6bc9', 'name': 'getCurrentWeather', 'content': 'temprature is 56 degree'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 0\n", "\n", "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"origin\":\"Boston, MA\",\"destination\":\"New York City, NY\",\"mode\":\"car\"}', name='calculate_distance')\n", - "Observation: Distance is 50 miles.\n", + "Tool Call: Function(arguments='{\"origin\":\"Boston\",\"destination\":\"New York City\",\"mode\":\"car\"}', name='calculate_distance')\n", + "Observation: {'role': 'tool', 'tool_call_id': '6b7dec34', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': \"check the weather in boston, calculate the distance from boston to NYC for me only if it's greater than 100 degrees Fahrenheit\"}, {'role': 'assistant', 'tool_calls': [{'id': 'defe6bc9', 'function': {'name': 'getCurrentWeather', 'arguments': '{\"location\":\"Boston\",\"unit\":\"f\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': 'defe6bc9', 'name': 'getCurrentWeather', 'content': 'temprature is 56 degree'}, {'role': 'assistant', 'tool_calls': [{'id': '6b7dec34', 'function': {'name': 'calculate_distance', 'arguments': '{\"origin\":\"Boston\",\"destination\":\"New York City\",\"mode\":\"car\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': '6b7dec34', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 1\n", "\n", + "[AI calling functions]:\n", + "Tool Call: Function(arguments='{\"a\":\"56\",\"b\":\"100\"}', name='subtraction')\n", + "Observation: {'role': 'tool', 'name': 'subtraction', 'content': '-44.0', 'tool_call_id': 'a53ba368'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': \"check the weather in boston, calculate the distance from boston to NYC for me only if it's greater than 100 degrees Fahrenheit\"}, {'role': 'assistant', 'tool_calls': [{'id': 'defe6bc9', 'function': {'name': 'getCurrentWeather', 'arguments': '{\"location\":\"Boston\",\"unit\":\"f\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': 'defe6bc9', 'name': 'getCurrentWeather', 'content': 'temprature is 56 degree'}, {'role': 'assistant', 'tool_calls': [{'id': '6b7dec34', 'function': {'name': 'calculate_distance', 'arguments': '{\"origin\":\"Boston\",\"destination\":\"New York City\",\"mode\":\"car\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': '6b7dec34', 'name': 'calculate_distance', 'content': 'Distance is 50 miles.'}, {'role': 'assistant', 'tool_calls': [{'id': 'a53ba368', 'function': {'name': 'subtraction', 'arguments': '{\"a\":\"56\",\"b\":\"100\"}'}, 'type': 'function'}]}, {'role': 'tool', 'name': 'subtraction', 'content': '-44.0', 'tool_call_id': 'a53ba368'}]\n", + "Pointing to URL: http://localhost:8019/v1/\n", + "Loop 2\n", + "\n", "[AI response]:\n", - " The weather in Boston is 60 degrees Fahrenheit. The distance from Boston to New York City is 50 miles.\n" + " The weather in Boston is 56 degrees Fahrenheit. The distance from Boston to New York City is 50 miles. The difference between 56 and 100 is -44.0 degrees Fahrenheit.\n" ] } ], @@ -676,7 +689,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -687,26 +700,16 @@ "\n", "[AI calling functions]:\n", "Tool Call: Function(arguments='{\"directory\":\"documents\"}', name='list_files')\n", - "Observation: File list:\n", - "report.docx\n", - "task.txt\n", - "notes.txt\n", + "Observation: {'role': 'tool', 'tool_call_id': 'e8b5f43a', 'name': 'list_files', 'content': 'File list:\\nreport.docx\\ntask.txt\\nnotes.txt'}\n", + "[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': \"check the size of all files in the 'documents' directory.\"}, {'role': 'assistant', 'tool_calls': [{'id': 'e8b5f43a', 'function': {'name': 'list_files', 'arguments': '{\"directory\":\"documents\"}'}, 'type': 'function'}]}, {'role': 'tool', 'tool_call_id': 'e8b5f43a', 'name': 'list_files', 'content': 'File list:\\nreport.docx\\ntask.txt\\nnotes.txt'}]\n", "Pointing to URL: http://localhost:8019/v1/\n", "Loop 0\n", "\n", - "[AI calling functions]:\n", - "Tool Call: Function(arguments='{\"filename\":\"report.docx\"}', name='get_file_size')\n", - "Observation: the size is 100 bytes.\n", - "Pointing to URL: http://localhost:8019/v1/\n", - "Loop 1\n", - "\n", "[AI response]:\n", " The files in the 'documents' directory are:\n", "- report.docx\n", "- task.txt\n", - "- notes.txt\n", - "\n", - "The size of 'report.docx' is 100 bytes.\n" + "- notes.txt\n" ] } ],