Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 104 additions & 155 deletions python/notebooks/istio_agent.ipynb

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions python/src/kagent/prompts/__init__.py

This file was deleted.

594 changes: 0 additions & 594 deletions python/src/kagent/prompts/_istio_crd.py

This file was deleted.

92 changes: 0 additions & 92 deletions python/src/kagent/prompts/base.py

This file was deleted.

24 changes: 0 additions & 24 deletions python/src/kagent/prompts/models.py

This file was deleted.

3 changes: 2 additions & 1 deletion python/src/kagent/tools/istio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ._istio_crds import generate_resource
from ._istioctl import proxy_config, verify_install

__all__ = ["verify_install", "proxy_config"]
__all__ = ["verify_install", "proxy_config", "generate_resource"]
92 changes: 92 additions & 0 deletions python/src/kagent/tools/istio/_istio_crds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from enum import Enum
from typing import Annotated

from autogen_core.models import SystemMessage, UserMessage
from autogen_core.tools import FunctionTool
from autogen_ext.models.openai import OpenAIChatCompletionClient

from .prompts import (
AUTH_POLICY_PROMPT,
GATEWAY_PROMPT,
PEER_AUTHENTICATION_PROMPT,
VIRTUAL_SERVICE_PROMPT,
IstioResources,
)


def get_model_client():
# TODO: We should have a way to configure externally somehow.
return OpenAIChatCompletionClient(
model="gpt-4o-mini",
)


async def _generate_crd(system_prompt: str, policy_description: str) -> str:
"""
Asynchronously generates a Custom Resource Definition (CRD) based on the provided system prompt and policy description.

Args:
system_prompt (str): The system prompt to guide the CRD generation.
policy_description (str): The description of the policy to be included in the CRD.

Returns:
str: The generated CRD content or an error message if the generation fails.

Raises:
Exception: If there is an error during the CRD generation process.
"""
try:
model_client = get_model_client()
result = await model_client.create(
messages=[SystemMessage(content=system_prompt), UserMessage(content=policy_description, source="user")],
json_output=True,
)
return result.content
except Exception as e:
return f"Error generating policy: {str(e)}"


async def _generate_gateway_crd(
policy_description: Annotated[str, "Detailed description of the Gateway to generate YAML for"],
) -> str:
return await _generate_crd(GATEWAY_PROMPT, policy_description)


async def _generate_auth_policy_crd(
policy_description: Annotated[str, "Detailed description of the AuthorizationPolicy to generate YAML for"],
) -> str:
return await _generate_crd(AUTH_POLICY_PROMPT, policy_description)


async def _generate_peer_auth_crd(
policy_description: Annotated[str, "Detailed description of the PeerAuthentication to generate YAML for"],
) -> str:
return await _generate_crd(PEER_AUTHENTICATION_PROMPT, policy_description)


async def _generate_virtual_service_crd(
policy_description: Annotated[str, "Detailed description of the VirtualService to generate YAML for"],
) -> str:
return await _generate_crd(VIRTUAL_SERVICE_PROMPT, policy_description)

async def _generate_istio_resource(
istio_resource: Annotated[IstioResources, "Type of resources to generate"],
policy_description: Annotated[str, "Detailed description of the resource to generate YAML for"],
) -> str:
if istio_resource == IstioResources.AUTH_POLICY:
return await _generate_auth_policy_crd(policy_description)
elif istio_resource == IstioResources.GATEWAY:
return await _generate_gateway_crd(policy_description)
elif istio_resource == IstioResources.PEER_AUTHENTICATION:
return await _generate_peer_auth_crd(policy_description)
elif istio_resource == IstioResources.VIRTUAL_SERVICE:
return await _generate_virtual_service_crd(policy_description)
else:
return "Unsupported Istio resource type"


generate_resource = FunctionTool(
_generate_istio_resource,
description="Generates an Istio resource YAML configuration from a detailed description",
name="generate_istio_resource",
)
11 changes: 11 additions & 0 deletions python/src/kagent/tools/istio/prompts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .auth_policy import AUTH_POLICY_PROMPT
from .gateway import GATEWAY_PROMPT
from .peer_auth import PEER_AUTHENTICATION_PROMPT
from .virtual_service import VIRTUAL_SERVICE_PROMPT

__all__ = [
"AUTH_POLICY_PROMPT",
"GATEWAY_PROMPT",
"PEER_AUTHENTICATION_PROMPT",
"VIRTUAL_SERVICE_PROMPT",
]
Loading