|
| 1 | +import logging |
| 2 | +import time |
| 3 | + |
1 | 4 | import semantic_kernel as sk
|
2 | 5 |
|
| 6 | +logging.basicConfig(level=logging.DEBUG) |
| 7 | +logger = logging.getLogger() |
| 8 | + |
| 9 | + |
| 10 | +async def retry(func, retries=15, delay=1): |
| 11 | + for i in range(retries): |
| 12 | + try: |
| 13 | + result = str(await func()) |
| 14 | + if "Error" in result: |
| 15 | + raise ValueError(result) |
| 16 | + return result |
| 17 | + except Exception as e: |
| 18 | + logger.error(f"Retry {i + 1}: {e}") |
| 19 | + if i == retries - 1: # Last retry |
| 20 | + raise |
| 21 | + time.sleep(delay) |
| 22 | + |
3 | 23 |
|
4 | 24 | async def summarize_function_test(kernel: sk.Kernel):
|
5 | 25 | # Define semantic function using SK prompt template language
|
@@ -33,68 +53,80 @@ async def summarize_function_test(kernel: sk.Kernel):
|
33 | 53 | print()
|
34 | 54 |
|
35 | 55 | # Summarize input string and print
|
36 |
| - summary = await kernel.run_async(tldr_function, input_str=text_to_summarize) |
37 |
| - |
| 56 | + summary = await retry( |
| 57 | + lambda: kernel.run_async(tldr_function, input_str=text_to_summarize) |
| 58 | + ) |
38 | 59 | output = str(summary).strip()
|
39 | 60 | print(f"Summary using input string: '{output}'")
|
40 |
| - assert len(output.split(" ")) == 5 |
| 61 | + assert "humans" in output or "Humans" in output or "preserve" in output |
| 62 | + assert len(output) < 100 |
41 | 63 |
|
42 | 64 | # Summarize input as context variable and print
|
43 | 65 | context_vars = sk.ContextVariables(text_to_summarize)
|
44 |
| - summary = await kernel.run_async(tldr_function, input_vars=context_vars) |
45 |
| - |
| 66 | + summary = await retry( |
| 67 | + lambda: kernel.run_async(tldr_function, input_vars=context_vars) |
| 68 | + ) |
46 | 69 | output = str(summary).strip()
|
47 | 70 | print(f"Summary using context variables: '{output}'")
|
48 |
| - assert len(output.split(" ")) == 5 |
| 71 | + assert "humans" in output or "Humans" in output or "preserve" in output |
| 72 | + assert len(output) < 100 |
49 | 73 |
|
50 | 74 | # Summarize input context and print
|
51 | 75 | context = kernel.create_new_context()
|
52 | 76 | context["input"] = text_to_summarize
|
53 |
| - summary = await kernel.run_async(tldr_function, input_context=context) |
54 |
| - |
| 77 | + summary = await retry( |
| 78 | + lambda: kernel.run_async(tldr_function, input_context=context) |
| 79 | + ) |
55 | 80 | output = str(summary).strip()
|
56 | 81 | print(f"Summary using input context: '{output}'")
|
57 |
| - assert len(output.split(" ")) == 5 |
| 82 | + assert "humans" in output or "Humans" in output or "preserve" in output |
| 83 | + assert len(output) < 100 |
58 | 84 |
|
59 | 85 | # Summarize input context with additional variables and print
|
60 | 86 | context = kernel.create_new_context()
|
61 | 87 | context["input"] = text_to_summarize
|
62 | 88 | context_vars = sk.ContextVariables("4) All birds are robots.")
|
63 |
| - summary = await kernel.run_async( |
64 |
| - tldr_function, input_context=context, input_vars=context_vars |
| 89 | + summary = await retry( |
| 90 | + lambda: kernel.run_async( |
| 91 | + tldr_function, input_context=context, input_vars=context_vars |
| 92 | + ) |
65 | 93 | )
|
66 |
| - |
67 | 94 | output = str(summary).strip()
|
68 | 95 | print(f"Summary using context and additional variables: '{output}'")
|
69 |
| - assert len(output.split(" ")) == 5 |
| 96 | + assert "humans" in output or "Humans" in output or "preserve" in output |
| 97 | + assert len(output) < 100 |
70 | 98 |
|
71 | 99 | # Summarize input context with additional input string and print
|
72 | 100 | context = kernel.create_new_context()
|
73 | 101 | context["input"] = text_to_summarize
|
74 |
| - summary = await kernel.run_async( |
75 |
| - tldr_function, input_context=context, input_str="4) All birds are robots." |
| 102 | + summary = await retry( |
| 103 | + lambda: kernel.run_async( |
| 104 | + tldr_function, input_context=context, input_str="4) All birds are robots." |
| 105 | + ) |
76 | 106 | )
|
77 |
| - |
78 | 107 | output = str(summary).strip()
|
79 | 108 | print(f"Summary using context and additional string: '{output}'")
|
80 |
| - assert len(output.split(" ")) == 5 |
| 109 | + assert "humans" in output or "Humans" in output or "preserve" in output |
| 110 | + assert len(output) < 100 |
81 | 111 |
|
82 | 112 | # Summarize input context with additional variables and string and print
|
83 | 113 | context = kernel.create_new_context()
|
84 | 114 | context["input"] = text_to_summarize
|
85 | 115 | context_vars = sk.ContextVariables(variables={"input2": "4) All birds are robots."})
|
86 |
| - summary = await kernel.run_async( |
87 |
| - tldr_function, |
88 |
| - input_context=context, |
89 |
| - input_vars=context_vars, |
90 |
| - input_str="new text", |
| 116 | + summary = await retry( |
| 117 | + lambda: kernel.run_async( |
| 118 | + tldr_function, |
| 119 | + input_context=context, |
| 120 | + input_vars=context_vars, |
| 121 | + input_str="new text", |
| 122 | + ) |
91 | 123 | )
|
92 |
| - |
93 | 124 | output = str(summary).strip()
|
94 | 125 | print(
|
95 | 126 | f"Summary using context, additional variables, and additional string: '{output}'"
|
96 | 127 | )
|
97 |
| - assert len(output.split(" ")) == 5 |
| 128 | + assert "humans" in output or "Humans" in output or "preserve" in output |
| 129 | + assert len(output) < 100 |
98 | 130 |
|
99 | 131 |
|
100 | 132 | async def simple_summarization(kernel: sk.Kernel):
|
|
0 commit comments