From 3164d49455e5b9b3267f00434a14e6d9f83d6799 Mon Sep 17 00:00:00 2001 From: Adil Hafeez Date: Mon, 17 Mar 2025 15:27:47 -0700 Subject: [PATCH] adding python files --- tests/rest/generate_observations.py | 17 +++++++ tests/rest/generate_system_prompt.py | 72 ++++++++++++++++++++++++++++ tests/rest/model_test_adil.hurl | 22 +++++++++ tests/rest/model_test_adil.rest | 0 4 files changed, 111 insertions(+) create mode 100644 tests/rest/generate_observations.py create mode 100644 tests/rest/generate_system_prompt.py create mode 100644 tests/rest/model_test_adil.hurl delete mode 100644 tests/rest/model_test_adil.rest diff --git a/tests/rest/generate_observations.py b/tests/rest/generate_observations.py new file mode 100644 index 00000000..3663596a --- /dev/null +++ b/tests/rest/generate_observations.py @@ -0,0 +1,17 @@ +# Use the format `{"name": "function_name", "result": "function_result"}` for each tool call +tool_call_results = [ + {"name": "get_weather", "result": "37.1 f"} + # {"name": "get_stock_price", "result": "247.66 USD"} + # Add more results if needed +] + + +def build_observations(tool_call_results): + observations = "\n".join([repr(x) for x in tool_call_results]) + observations = f"\n{observations}\n" + return observations + + +if __name__ == "__main__": + observations = build_observations(tool_call_results) + print(repr(observations)) diff --git a/tests/rest/generate_system_prompt.py b/tests/rest/generate_system_prompt.py new file mode 100644 index 00000000..27a6c209 --- /dev/null +++ b/tests/rest/generate_system_prompt.py @@ -0,0 +1,72 @@ +import json +from typing import Any, Dict, List + + +ARCH_FUNCTION_TOOL_PROMPT = ( + "You are a helpful assistant designed to assist with the user query by making one or more function calls if needed." + "\n\nYou are provided with function signatures within XML tags:\n{tool_text}\n" + "\n\nYour task is to decide which functions are needed and collect missing parameters if necessary.\n\n" +) + +ARCH_FUNCTION_FORMAT_PROMPT = """ +Based on your analysis, provide your response in one of the following JSON formats: +1. If no functions are needed: +``` +{"response": "Your response text here"} +``` +2. If functions are needed but some required parameters are missing: +``` +{"required_functions": ["func_name1", "func_name2", ...], "clarification": "Text asking for missing parameters"} +``` +3. If functions are needed and all required parameters are available: +``` +{"tool_calls": [{"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},... (more tool calls as required)]} +``` +""".strip() + + +tools = [ + { + "name": "get_weather", + "description": "Determine weather in my location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["c", "f"]}, + }, + "required": ["location", "unit"], + }, + }, + { + "name": "get_stock_price", + "description": "Get the current stock price", + "parameters": { + "type": "object", + "properties": { + "symbol": {"type": "string", "description": "The stock symbol"} + }, + "required": ["symbol"], + }, + }, +] + + +def build_system_prompt(tools: List[Dict[str, Any]]) -> str: + tool_text = "" + for tool in tools: + tool_text += "\n" + json.dumps(tool) + + return ( + ARCH_FUNCTION_TOOL_PROMPT.format(tool_text=tool_text) + + ARCH_FUNCTION_FORMAT_PROMPT + ) + + +if __name__ == "__main__": + system_prompt = build_system_prompt(tools) + # print(repr(system_prompt.encode("unicode_escape").decode())) + print(json.dumps(system_prompt)) diff --git a/tests/rest/model_test_adil.hurl b/tests/rest/model_test_adil.hurl new file mode 100644 index 00000000..a16cdb92 --- /dev/null +++ b/tests/rest/model_test_adil.hurl @@ -0,0 +1,22 @@ +POST https://archfc.katanemo.dev/v1/chat/completions +Content-Type: application/json + +{ + "model": "Arch-Intent", + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant.\n\nYou task is to check if there are any tools that can be used to help the last user message in conversations according to the available tools listed below.\n\n\n{\"index\": \"T0\", \"type\": \"function\", \"function\": {\"name\": \"weather_forecast\", \"parameters\": {\"type\": \"object\", \"properties\": {\"city\": {\"type\": \"str\"}, \"days\": {\"type\": \"int\"}}, \"required\": [\"city\", \"days\"]}}}\n\n\nProvide your tool assessment for ONLY THE LAST USER MESSAGE in the above conversation:\n- First line must read 'Yes' or 'No'.\n- If yes, a second line must include a comma-separated list of tool indexes.\n" + }, + { "role": "user", "content": "how is the weather in seattle? Are there any tools can help?" } + ], + "stream": false +} + +HTTP 200 +[Asserts] +header "content-type" == "application/json" +jsonpath "$.model" matches /^Arch-Function/ +jsonpath "$.usage" != null +jsonpath "$.choices[0].message.content" matches /Yes/ +jsonpath "$.choices[0].message.role" == "assistant" diff --git a/tests/rest/model_test_adil.rest b/tests/rest/model_test_adil.rest deleted file mode 100644 index e69de29b..00000000