Skip to content

Commit

Permalink
Model rebuild for openai plugin predicate context.
Browse files Browse the repository at this point in the history
  • Loading branch information
moonbox3 committed Dec 19, 2024
1 parent 8464649 commit e1e9e74
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
# - Services.ONNX
# - Services.VERTEX_AI
# Please make sure you have configured your environment correctly for the selected chat completion service.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.OPENAI)
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.AZURE_OPENAI)

# Configure the function choice behavior. Here, we set it to Auto, where auto_invoke=False.
# With `FunctionChoiceBehavior(auto_invoke=False)`, the model may return tool call instructions
Expand Down
10 changes: 10 additions & 0 deletions python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from semantic_kernel.connectors.ai.open_ai.prompt_execution_settings.open_ai_prompt_execution_settings import (
OpenAIEmbeddingPromptExecutionSettings,
)
from semantic_kernel.connectors.openai_plugin.openai_function_execution_parameters import (
OpenAIFunctionExecutionParameters,
)
from semantic_kernel.data.record_definition.vector_store_model_decorator import vectorstoremodel
from semantic_kernel.data.record_definition.vector_store_model_definition import VectorStoreRecordDefinition
from semantic_kernel.data.record_definition.vector_store_record_fields import (
Expand Down Expand Up @@ -686,3 +689,10 @@ class DataModelClass(BaseModel):
key: Annotated[str, VectorStoreRecordKeyField()]

return DataModelClass


@fixture
def define_openai_predicate_context():
from semantic_kernel.connectors.openapi_plugin import OperationSelectionPredicateContext # noqa: F401

OpenAIFunctionExecutionParameters.model_rebuild()
2 changes: 1 addition & 1 deletion python/tests/samples/test_concepts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest
from pytest import mark, param

from samples.concepts.auto_function_calling.chat_completion_with_function_calling import (
from samples.concepts.auto_function_calling.chat_completion_with_auto_function_calling import (
main as chat_completion_with_function_calling,
)
from samples.concepts.auto_function_calling.functions_defined_in_json_prompt import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def predicate_callback(context):
return runner, operations, exec_settings


def test_predicate_callback_applied(openapi_runner_with_predicate_callback):
def test_predicate_callback_applied(openapi_runner_with_predicate_callback, define_openai_predicate_context):
_, operations, exec_settings = openapi_runner_with_predicate_callback

skipped_operations = []
Expand Down
8 changes: 4 additions & 4 deletions python/tests/unit/functions/test_kernel_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def test_from_object_class(custom_plugin_class):


@patch("semantic_kernel.connectors.openai_plugin.openai_utils.OpenAIUtils.parse_openai_manifest_for_openapi_spec_url")
async def test_from_openai_from_file(mock_parse_openai_manifest):
async def test_from_openai_from_file(mock_parse_openai_manifest, define_openai_predicate_context):
openai_spec_file = os.path.join(os.path.dirname(__file__), "../../assets/test_plugins")
with open(os.path.join(openai_spec_file, "TestOpenAIPlugin", "akv-openai.json")) as file:
openai_spec = file.read()
Expand Down Expand Up @@ -526,7 +526,7 @@ async def test_from_openai_from_file(mock_parse_openai_manifest):

@patch("httpx.AsyncClient.get")
@patch("semantic_kernel.connectors.openai_plugin.openai_utils.OpenAIUtils.parse_openai_manifest_for_openapi_spec_url")
async def test_from_openai_plugin_from_url(mock_parse_openai_manifest, mock_get):
async def test_from_openai_plugin_from_url(mock_parse_openai_manifest, mock_get, define_openai_predicate_context):
openai_spec_file_path = os.path.join(
os.path.dirname(__file__), "../../assets/test_plugins", "TestOpenAIPlugin", "akv-openai.json"
)
Expand Down Expand Up @@ -561,12 +561,12 @@ async def test_from_openai_plugin_from_url(mock_parse_openai_manifest, mock_get)
mock_get.assert_awaited_once_with(fake_plugin_url, headers={"User-Agent": HTTP_USER_AGENT})


async def test_from_openai_fail():
async def test_from_openai_fail(define_openai_predicate_context):
with raises(PluginInitializationError):
await KernelPlugin.from_openai(plugin_name="TestOpenAIPlugin")


async def test_from_openai_fail_json_parsing():
async def test_from_openai_fail_json_parsing(define_openai_predicate_context):
with raises(PluginInitializationError):
await KernelPlugin.from_openai(plugin_name="TestOpenAIPlugin", plugin_str="test")

Expand Down
2 changes: 1 addition & 1 deletion python/tests/unit/kernel/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ def func2(arg1: str) -> str:


@patch("semantic_kernel.connectors.openai_plugin.openai_utils.OpenAIUtils.parse_openai_manifest_for_openapi_spec_url")
async def test_add_plugin_from_openai(mock_parse_openai_manifest, kernel: Kernel):
async def test_add_plugin_from_openai(mock_parse_openai_manifest, kernel: Kernel, define_openai_predicate_context):
base_folder = os.path.join(os.path.dirname(__file__), "../../assets/test_plugins")
with open(os.path.join(base_folder, "TestOpenAIPlugin", "akv-openai.json")) as file:
openai_spec = file.read()
Expand Down

0 comments on commit e1e9e74

Please sign in to comment.