|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +from next_gen_ui_agent import NextGenUIAgent |
| 4 | +from next_gen_ui_agent.types import InputData |
| 5 | +from next_gen_ui_llama_stack_embedded import init_inference_from_env |
| 6 | + |
| 7 | + |
| 8 | +if True: |
| 9 | + # Ollama models: |
| 10 | + LLAMASTACK_CONFIG_PATH_DEFAULT = "tests/ai_eval_components/llamastack-ollama.yaml" |
| 11 | + #INFERENCE_MODEL_DEFAULT = "granite3.2:2b" |
| 12 | + #INFERENCE_MODEL_DEFAULT = "granite3.3:2b" |
| 13 | + INFERENCE_MODEL_DEFAULT="granite3.3:8b" |
| 14 | + #INFERENCE_MODEL_DEFAULT="ollama/llama3.2:latest" |
| 15 | + |
| 16 | +else: |
| 17 | + # Gemini API models: |
| 18 | + LLAMASTACK_CONFIG_PATH_DEFAULT = "tests/ai_eval_components/llamastack-m.gemini.yaml" |
| 19 | + INFERENCE_MODEL_DEFAULT="gemini/gemini-2.0-flash-lite" |
| 20 | + #INFERENCE_MODEL_DEFAULT="gemini/gemini-2.0-flash" |
| 21 | + #INFERENCE_MODEL_DEFAULT="gemini/gemini-2.5-flash-lite" |
| 22 | + #INFERENCE_MODEL_DEFAULT="gemini/gemini-2.5-flash" |
| 23 | + os.environ["INFERENCE_MODEL"] = INFERENCE_MODEL_DEFAULT |
| 24 | + |
| 25 | +BY_PREVIOUS_COMPONENT_PROMPT_PRFIX = '\nKeep UI component and its configuration consistent with previously shown ' |
| 26 | +BY_PREVIOUS_COMPONENT_PROMPT_SUFFIX = ' and only update it to match the "User Query" and "Data" if applicable pushing requested info up, otherwise replace it with another one.' |
| 27 | + |
| 28 | +#TOYSTORY_DATA = '{"movie": {"title": "Toy Story", "pictureUrl": "https://example.com/poster.jpg", "imdb" : { "rating": 8.3 }, "countries": ["USA"], "actors": ["Tom Hanks", "Tim Allen", "Jim Varney", "Don Rickles"], "released": 1995}}' |
| 29 | +TOYSTORY_DATA = '''{ |
| 30 | + "movie": { |
| 31 | + "languages": [ |
| 32 | + "English" |
| 33 | + ], |
| 34 | + "year": 1995, |
| 35 | + "pictureUrl": "https://image.tmdb.org/t/p/w440_and_h660_face/uXDfjJbdP4ijW5hWSBrPrlKpxab.jpg", |
| 36 | + "runtime": 81, |
| 37 | + "movieId": "1", |
| 38 | + "imdb": { |
| 39 | + "votes": 591836, |
| 40 | + "id": "0114709", |
| 41 | + "rating": 8.3 |
| 42 | + }, |
| 43 | + "countries": [ |
| 44 | + "USA", |
| 45 | + "Germany", |
| 46 | + "Czech Republic" |
| 47 | + ], |
| 48 | + "trailerUrl": "https://www.youtube.com/watch?v=v-PjgYDrg70", |
| 49 | + "title": "Toy Story", |
| 50 | + "url": "https://themoviedb.org/movie/862", |
| 51 | + "revenue": 373554033, |
| 52 | + "tmdbId": "862", |
| 53 | + "plot": "A cowboy doll is profoundly threatened and jealous when a new spaceman figure supplants him as top toy in a boy's room.", |
| 54 | + "released": "1995-11-22", |
| 55 | + "budget": 30000000 |
| 56 | + }, |
| 57 | + "actors": [ |
| 58 | + "Jim Varney", |
| 59 | + "Tim Allen", |
| 60 | + "Tom Hanks", |
| 61 | + "Don Rickles" |
| 62 | + ] |
| 63 | +}''' |
| 64 | + |
| 65 | +PROMPT_MOVIE_INFO = "Show me basic info about Toy Story." |
| 66 | +PROMPT_MOVIE_RELEASED = "When was the movie released?" |
| 67 | +PROMPT_MOVIE_POSTER = "Show me the poster only." |
| 68 | +PROMPT_MOVIE_SUBSCRIPTIONS = "Show me my subscriptions" |
| 69 | + |
| 70 | +def open_results_file(): |
| 71 | + """ |
| 72 | + Generate a valid filename from INFERENCE_MODEL_DEFAULT and open it for writing. |
| 73 | + Keeps only the part after the last '/' and adds .txt suffix. |
| 74 | + If file exists, it will be replaced. |
| 75 | + Creates the file in the 'one_component_results' subfolder relative to this script's directory. |
| 76 | + |
| 77 | + Returns: |
| 78 | + file: Open file handle for writing |
| 79 | + """ |
| 80 | + import os |
| 81 | + # Get the directory where this script is located |
| 82 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 83 | + results_dir = os.path.join(script_dir, 'one_component_results') |
| 84 | + |
| 85 | + # Create the subfolder if it doesn't exist |
| 86 | + os.makedirs(results_dir, exist_ok=True) |
| 87 | + |
| 88 | + # Extract the part after the last '/' |
| 89 | + filename = INFERENCE_MODEL_DEFAULT.split('/')[-1] |
| 90 | + # Add .txt suffix |
| 91 | + full_filename = os.path.join(results_dir, f"{filename}.txt") |
| 92 | + |
| 93 | + print(f"\nWriting results to {full_filename} ...\n") |
| 94 | + |
| 95 | + # Open file for writing, replacing if it exists |
| 96 | + return open(full_filename, 'w') |
| 97 | + |
| 98 | +# open results file |
| 99 | +results_file = open_results_file() |
| 100 | + |
| 101 | +def print_response(title, llm_response): |
| 102 | + response_text = f"{title}{llm_response[0].model_dump_json(indent=2)}\n\n" |
| 103 | + print(response_text, end='') |
| 104 | + results_file.write(response_text) |
| 105 | + results_file.flush() |
| 106 | + |
| 107 | +def print_header(title): |
| 108 | + header_text = f"{title}\n\n" |
| 109 | + print(header_text, end='') |
| 110 | + results_file.write(header_text) |
| 111 | + results_file.flush() |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + |
| 115 | + inference = init_inference_from_env( |
| 116 | + default_model=INFERENCE_MODEL_DEFAULT, |
| 117 | + default_config_file=LLAMASTACK_CONFIG_PATH_DEFAULT, |
| 118 | + ) |
| 119 | + |
| 120 | + agent = NextGenUIAgent(config = {"inference": inference, "unsupported_components": True}) |
| 121 | + |
| 122 | + print_header("*** 1. Current: Info about Toy Story. Previous: subscriptions table - change of the data entity ***") |
| 123 | + |
| 124 | + llm_response = asyncio.run(agent.component_selection( |
| 125 | + input={ |
| 126 | + "user_prompt": PROMPT_MOVIE_INFO+BY_PREVIOUS_COMPONENT_PROMPT_PRFIX+"'table' with title 'My Subscriptions' and fields ['subscription.id', 'subscription.endDate']"+BY_PREVIOUS_COMPONENT_PROMPT_SUFFIX, |
| 127 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 128 | + }, |
| 129 | + )) |
| 130 | + |
| 131 | + print_response("Response one previous component: ", llm_response) |
| 132 | + |
| 133 | + llm_response = asyncio.run(agent.component_selection( |
| 134 | + input={ |
| 135 | + "user_prompt": PROMPT_MOVIE_INFO, |
| 136 | + "previous_user_prompts": [PROMPT_MOVIE_SUBSCRIPTIONS], |
| 137 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 138 | + }, |
| 139 | + )) |
| 140 | + |
| 141 | + print_response("Response multipleprevious prompts: ", llm_response) |
| 142 | + |
| 143 | + llm_response = asyncio.run(agent.component_selection( |
| 144 | + input={ |
| 145 | + "user_prompt": PROMPT_MOVIE_INFO, |
| 146 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 147 | + }, |
| 148 | + )) |
| 149 | + |
| 150 | + print_response("Response no history: ", llm_response) |
| 151 | + |
| 152 | +# ----------------- 2 ----------------- |
| 153 | + print_header("*** 2. Current: When was the movie released? Previous: Info about Toy Story - additional info asked about data entity from previous conversation step ***") |
| 154 | + |
| 155 | + llm_response = asyncio.run(agent.component_selection( |
| 156 | + input={ |
| 157 | + "user_prompt": PROMPT_MOVIE_RELEASED+BY_PREVIOUS_COMPONENT_PROMPT_PRFIX+"'one-card' with title 'Toy Story' and fields ['movie.title', 'movie.pictureUrl', 'movie.imdb.rating', 'movie.imdb.year']"+BY_PREVIOUS_COMPONENT_PROMPT_SUFFIX, |
| 158 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 159 | + }, |
| 160 | + )) |
| 161 | + |
| 162 | + print_response("Response one previous component: ", llm_response) |
| 163 | + |
| 164 | + llm_response = asyncio.run(agent.component_selection( |
| 165 | + input={ |
| 166 | + "user_prompt": PROMPT_MOVIE_RELEASED, |
| 167 | + "previous_user_prompts": [PROMPT_MOVIE_INFO], |
| 168 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 169 | + }, |
| 170 | + )) |
| 171 | + |
| 172 | + print_response("Response multiple previous prompts: ", llm_response) |
| 173 | + |
| 174 | + llm_response = asyncio.run(agent.component_selection( |
| 175 | + input={ |
| 176 | + "user_prompt": PROMPT_MOVIE_RELEASED, |
| 177 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 178 | + }, |
| 179 | + )) |
| 180 | + |
| 181 | + print_response("Response no history: ", llm_response) |
| 182 | + |
| 183 | +# ----------------- 3 ----------------- |
| 184 | + print_header("*** 3. Current: Show me the poster only. Previous: Info about Toy Story - the same data entity, but user prompt asks for different UI component ***") |
| 185 | + |
| 186 | + llm_response = asyncio.run(agent.component_selection( |
| 187 | + input={ |
| 188 | + "user_prompt": PROMPT_MOVIE_POSTER+BY_PREVIOUS_COMPONENT_PROMPT_PRFIX+"'one-card' with title 'Toy Story' and fields ['movie.title', 'movie.released', 'movie.imdb.rating']"+BY_PREVIOUS_COMPONENT_PROMPT_SUFFIX, |
| 189 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 190 | + }, |
| 191 | + )) |
| 192 | + |
| 193 | + print_response("Response one previous component: ", llm_response) |
| 194 | + |
| 195 | + llm_response = asyncio.run(agent.component_selection( |
| 196 | + input={ |
| 197 | + "user_prompt": PROMPT_MOVIE_POSTER, |
| 198 | + "previous_user_prompts": [PROMPT_MOVIE_INFO], |
| 199 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 200 | + }, |
| 201 | + )) |
| 202 | + |
| 203 | + print_response("Response multiple previous prompts: ", llm_response) |
| 204 | + |
| 205 | + llm_response = asyncio.run(agent.component_selection( |
| 206 | + input={ |
| 207 | + "user_prompt": PROMPT_MOVIE_POSTER, |
| 208 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 209 | + }, |
| 210 | + )) |
| 211 | + |
| 212 | + print_response("Response no history: ", llm_response) |
| 213 | + |
| 214 | +# ----------------- 4 ----------------- |
| 215 | + print_header("*** 4. Current: When was the movie released? Previous: Poster only, Info about Toy Story, Subscriptions table - multiple previous conversation steps including entity change and then component type change to another and back ***") |
| 216 | + |
| 217 | + llm_response = asyncio.run(agent.component_selection( |
| 218 | + input={ |
| 219 | + "user_prompt": PROMPT_MOVIE_RELEASED+BY_PREVIOUS_COMPONENT_PROMPT_PRFIX+"'image' with title 'Toy Story Poster' and fields ['movie.pictureUrl']"+BY_PREVIOUS_COMPONENT_PROMPT_SUFFIX, |
| 220 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 221 | + }, |
| 222 | + )) |
| 223 | + |
| 224 | + print_response("Response one previous component: ", llm_response) |
| 225 | + |
| 226 | + llm_response = asyncio.run(agent.component_selection( |
| 227 | + input={ |
| 228 | + "user_prompt": PROMPT_MOVIE_RELEASED, |
| 229 | + "previous_user_prompts": [PROMPT_MOVIE_POSTER, PROMPT_MOVIE_INFO, PROMPT_MOVIE_SUBSCRIPTIONS], |
| 230 | + #"previous_user_prompts": [PROMPT_MOVIE_POSTER], |
| 231 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 232 | + }, |
| 233 | + )) |
| 234 | + |
| 235 | + print_response("Response multiple previous prompts: ", llm_response) |
| 236 | + |
| 237 | + llm_response = asyncio.run(agent.component_selection( |
| 238 | + input={ |
| 239 | + "user_prompt": PROMPT_MOVIE_RELEASED, |
| 240 | + "input_data": [InputData({"id": "1", "data": TOYSTORY_DATA})], |
| 241 | + }, |
| 242 | + )) |
| 243 | + |
| 244 | + print_response("Response no history: ", llm_response) |
| 245 | + |
0 commit comments