|
| 1 | +"""Unit tests for endpoints utility functions.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import pytest |
| 5 | + |
| 6 | +import constants |
| 7 | +from configuration import AppConfig |
| 8 | + |
| 9 | +from models.requests import QueryRequest |
| 10 | +from utils import endpoints |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def input_file(tmp_path): |
| 15 | + """Create file manually using the tmp_path fixture.""" |
| 16 | + filename = os.path.join(tmp_path, "prompt.txt") |
| 17 | + with open(filename, "wt") as fout: |
| 18 | + fout.write("this is prompt!") |
| 19 | + return filename |
| 20 | + |
| 21 | + |
| 22 | +def test_get_default_system_prompt(): |
| 23 | + """Test that default system prompt is returned when other prompts are not provided.""" |
| 24 | + config_dict = { |
| 25 | + "name": "foo", |
| 26 | + "service": { |
| 27 | + "host": "localhost", |
| 28 | + "port": 8080, |
| 29 | + "auth_enabled": False, |
| 30 | + "workers": 1, |
| 31 | + "color_log": True, |
| 32 | + "access_log": True, |
| 33 | + }, |
| 34 | + "llama_stack": { |
| 35 | + "api_key": "xyzzy", |
| 36 | + "url": "http://x.y.com:1234", |
| 37 | + "use_as_library_client": False, |
| 38 | + }, |
| 39 | + "user_data_collection": { |
| 40 | + "feedback_disabled": True, |
| 41 | + }, |
| 42 | + "mcp_servers": [], |
| 43 | + "customization": None, |
| 44 | + } |
| 45 | + |
| 46 | + # no customization provided |
| 47 | + cfg = AppConfig() |
| 48 | + cfg.init_from_dict(config_dict) |
| 49 | + |
| 50 | + # no system prompt in query request |
| 51 | + query_request = QueryRequest(query="query", system_prompt=None) |
| 52 | + |
| 53 | + # default system prompt needs to be returned |
| 54 | + system_prompt = endpoints.get_system_prompt(query_request, cfg) |
| 55 | + assert system_prompt == constants.DEFAULT_SYSTEM_PROMPT |
| 56 | + |
| 57 | + |
| 58 | +def test_get_customized_system_prompt(): |
| 59 | + """Test that customized system prompt is used when system prompt is not provided in query.""" |
| 60 | + config_dict = { |
| 61 | + "name": "foo", |
| 62 | + "service": { |
| 63 | + "host": "localhost", |
| 64 | + "port": 8080, |
| 65 | + "auth_enabled": False, |
| 66 | + "workers": 1, |
| 67 | + "color_log": True, |
| 68 | + "access_log": True, |
| 69 | + }, |
| 70 | + "llama_stack": { |
| 71 | + "api_key": "xyzzy", |
| 72 | + "url": "http://x.y.com:1234", |
| 73 | + "use_as_library_client": False, |
| 74 | + }, |
| 75 | + "user_data_collection": { |
| 76 | + "feedback_disabled": True, |
| 77 | + }, |
| 78 | + "mcp_servers": [], |
| 79 | + "customization": { |
| 80 | + "system_prompt": "This is system prompt", |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + # no customization provided |
| 85 | + cfg = AppConfig() |
| 86 | + cfg.init_from_dict(config_dict) |
| 87 | + |
| 88 | + # no system prompt in query request |
| 89 | + query_request = QueryRequest(query="query", system_prompt=None) |
| 90 | + |
| 91 | + # default system prompt needs to be returned |
| 92 | + system_prompt = endpoints.get_system_prompt(query_request, cfg) |
| 93 | + assert system_prompt == "This is system prompt" |
| 94 | + |
| 95 | + |
| 96 | +def test_get_query_system_prompt(): |
| 97 | + """Test that system prompt from query is returned.""" |
| 98 | + config_dict = { |
| 99 | + "name": "foo", |
| 100 | + "service": { |
| 101 | + "host": "localhost", |
| 102 | + "port": 8080, |
| 103 | + "auth_enabled": False, |
| 104 | + "workers": 1, |
| 105 | + "color_log": True, |
| 106 | + "access_log": True, |
| 107 | + }, |
| 108 | + "llama_stack": { |
| 109 | + "api_key": "xyzzy", |
| 110 | + "url": "http://x.y.com:1234", |
| 111 | + "use_as_library_client": False, |
| 112 | + }, |
| 113 | + "user_data_collection": { |
| 114 | + "feedback_disabled": True, |
| 115 | + }, |
| 116 | + "mcp_servers": [], |
| 117 | + "customization": None, |
| 118 | + } |
| 119 | + |
| 120 | + # no customization provided |
| 121 | + cfg = AppConfig() |
| 122 | + cfg.init_from_dict(config_dict) |
| 123 | + |
| 124 | + # system prompt defined in query request |
| 125 | + system_prompt = "System prompt defined in query" |
| 126 | + query_request = QueryRequest(query="query", system_prompt=system_prompt) |
| 127 | + |
| 128 | + # default system prompt needs to be returned |
| 129 | + system_prompt = endpoints.get_system_prompt(query_request, cfg) |
| 130 | + assert system_prompt == system_prompt |
| 131 | + |
| 132 | + |
| 133 | +def test_get_query_system_prompt_not_customized_one(): |
| 134 | + """Test that system prompt from query is returned even when customized one is specified.""" |
| 135 | + config_dict = { |
| 136 | + "name": "foo", |
| 137 | + "service": { |
| 138 | + "host": "localhost", |
| 139 | + "port": 8080, |
| 140 | + "auth_enabled": False, |
| 141 | + "workers": 1, |
| 142 | + "color_log": True, |
| 143 | + "access_log": True, |
| 144 | + }, |
| 145 | + "llama_stack": { |
| 146 | + "api_key": "xyzzy", |
| 147 | + "url": "http://x.y.com:1234", |
| 148 | + "use_as_library_client": False, |
| 149 | + }, |
| 150 | + "user_data_collection": { |
| 151 | + "feedback_disabled": True, |
| 152 | + }, |
| 153 | + "mcp_servers": [], |
| 154 | + "customization": { |
| 155 | + "system_prompt": "This is system prompt", |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + # no customization provided |
| 160 | + cfg = AppConfig() |
| 161 | + cfg.init_from_dict(config_dict) |
| 162 | + |
| 163 | + # system prompt defined in query request |
| 164 | + system_prompt = "System prompt defined in query" |
| 165 | + query_request = QueryRequest(query="query", system_prompt=system_prompt) |
| 166 | + |
| 167 | + # default system prompt needs to be returned |
| 168 | + system_prompt = endpoints.get_system_prompt(query_request, cfg) |
| 169 | + assert system_prompt == system_prompt |
0 commit comments