|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +import semantic_kernel as sk |
| 9 | + |
| 10 | +pytestmark = pytest.mark.skipif( |
| 11 | + sys.version_info < (3, 9), reason="Google Palm requires Python 3.9 or greater" |
| 12 | +) |
| 13 | + |
| 14 | +pytestmark = pytest.mark.skipif( |
| 15 | + "Python_Integration_Tests" in os.environ, |
| 16 | + reason="Google Palm integration tests are only set up to run locally", |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.asyncio |
| 21 | +async def test_text2text_generation_input_str(setup_gp_text_completion_function): |
| 22 | + kernel, text2text_function, simple_input = setup_gp_text_completion_function |
| 23 | + |
| 24 | + # Complete input string and print |
| 25 | + summary = await kernel.run_async(text2text_function, input_str=simple_input) |
| 26 | + |
| 27 | + output = str(summary).strip() |
| 28 | + print(f"Completion using input string: '{output}'") |
| 29 | + assert len(output) > 0 |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.asyncio |
| 33 | +async def test_text2text_generation_input_vars(setup_gp_text_completion_function): |
| 34 | + kernel, text2text_function, simple_input = setup_gp_text_completion_function |
| 35 | + |
| 36 | + # Complete input as context variable and print |
| 37 | + context_vars = sk.ContextVariables(simple_input) |
| 38 | + summary = await kernel.run_async(text2text_function, input_vars=context_vars) |
| 39 | + |
| 40 | + output = str(summary).strip() |
| 41 | + print(f"Completion using context variables: '{output}'") |
| 42 | + assert len(output) > 0 |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.asyncio |
| 46 | +async def test_text2text_generation_input_context(setup_gp_text_completion_function): |
| 47 | + kernel, text2text_function, simple_input = setup_gp_text_completion_function |
| 48 | + |
| 49 | + # Complete input context and print |
| 50 | + context = kernel.create_new_context() |
| 51 | + context["input"] = simple_input |
| 52 | + summary = await kernel.run_async(text2text_function, input_context=context) |
| 53 | + |
| 54 | + output = str(summary).strip() |
| 55 | + print(f"Completion using input context: '{output}'") |
| 56 | + assert len(output) > 0 |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +async def test_text2text_generation_input_context_with_vars( |
| 61 | + setup_gp_text_completion_function, |
| 62 | +): |
| 63 | + kernel, text2text_function, simple_input = setup_gp_text_completion_function |
| 64 | + |
| 65 | + # Complete input context with additional variables and print |
| 66 | + context = kernel.create_new_context() |
| 67 | + context["input"] = simple_input |
| 68 | + context_vars = sk.ContextVariables("running and") |
| 69 | + summary = await kernel.run_async( |
| 70 | + text2text_function, input_context=context, input_vars=context_vars |
| 71 | + ) |
| 72 | + |
| 73 | + output = str(summary).strip() |
| 74 | + print(f"Completion using context and additional variables: '{output}'") |
| 75 | + assert len(output) > 0 |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.asyncio |
| 79 | +async def test_text2text_generation_input_context_with_str( |
| 80 | + setup_gp_text_completion_function, |
| 81 | +): |
| 82 | + kernel, text2text_function, simple_input = setup_gp_text_completion_function |
| 83 | + |
| 84 | + # Complete input context with additional input string and print |
| 85 | + context = kernel.create_new_context() |
| 86 | + context["input"] = simple_input |
| 87 | + summary = await kernel.run_async( |
| 88 | + text2text_function, input_context=context, input_str="running and" |
| 89 | + ) |
| 90 | + |
| 91 | + output = str(summary).strip() |
| 92 | + print(f"Completion using context and additional string: '{output}'") |
| 93 | + assert len(output) > 0 |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.asyncio |
| 97 | +async def test_text2text_generation_input_context_with_vars_and_str( |
| 98 | + setup_gp_text_completion_function, |
| 99 | +): |
| 100 | + kernel, text2text_function, simple_input = setup_gp_text_completion_function |
| 101 | + |
| 102 | + # Complete input context with additional variables and string and print |
| 103 | + context = kernel.create_new_context() |
| 104 | + context["input"] = simple_input |
| 105 | + context_vars = sk.ContextVariables(variables={"input2": "running and"}) |
| 106 | + summary = await kernel.run_async( |
| 107 | + text2text_function, |
| 108 | + input_context=context, |
| 109 | + input_vars=context_vars, |
| 110 | + input_str="new text", |
| 111 | + ) |
| 112 | + |
| 113 | + output = str(summary).strip() |
| 114 | + print( |
| 115 | + f"Completion using context, additional variables, and additional string: '{output}'" |
| 116 | + ) |
| 117 | + assert len(output) > 0 |
0 commit comments