# Semantic Kernel

Semantic Kernel is an open-source SDK from Microsoft that connects LLMs with programming languages including C#, Python, and Java. The framework enables developers to build AI applications by integrating AI services, data sources, and custom logic into cohesive solutions.

This guide covers capturing telemetry from Semantic Kernel applications using InteractiveAI. While this documentation focuses on Python, the integration principles apply to other supported languages including C# and Java.

### Prerequisites

* InteractiveAI account with API credentials
* LLM provider credentials (OpenAI, Ollama, or other supported provider)

***

### Installation

```bash
pip install interactiveai openlit semantic-kernel
```

***

### Configuration

Set your API credentials as environment variables:

```python
import os

# InteractiveAI credentials
# Obtain keys from Settings > API Keys in the dashboard
os.environ["INTERACTIVEAI_PUBLIC_KEY"] = "pk-ia-..."
os.environ["INTERACTIVEAI_SECRET_KEY"] = "sk-ia-..."
os.environ["INTERACTIVEAI_HOST"] = "https://app.interactiveai.com"

# Model provider credentials
os.environ["OPENAI_API_KEY"] = "sk-proj-..."
os.environ["OPENAI_CHAT_MODEL_ID"] = "gpt-4o"
```

Initialize the client and confirm connectivity:

```python
from interactiveai import Interactive

client = Interactive(
    public_key=os.environ["INTERACTIVEAI_PUBLIC_KEY"],
    secret_key=os.environ["INTERACTIVEAI_SECRET_KEY"],
    host=os.environ.get("INTERACTIVEAI_HOST", "https://app.interactiveai.com")
)

if client.auth_check():
    print("Connection established")
else:
    print("Authentication failed - verify credentials")
```

***

### Enabling Trace Capture

OpenLIT provides automatic instrumentation for Semantic Kernel. Connect it to the InteractiveAI tracer:

```python
import openlit

openlit.init(tracer=client._otel_tracer, disable_batch=True)
```

The `disable_batch=True` flag processes traces immediately rather than buffering them.

***

### Building a Semantic Kernel Application

Create a kernel and add a chat completion service:

```python
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

kernel = Kernel()

kernel.add_service(
    OpenAIChatCompletion(),
)
```

Define a prompt template and register it as a function:

```python
from semantic_kernel.prompt_template import InputVariable, PromptTemplateConfig

prompt = """{{$input}}
Provide a clear and concise answer to the question above.
"""

prompt_template_config = PromptTemplateConfig(
    template=prompt,
    name="answer",
    template_format="semantic-kernel",
    input_variables=[
        InputVariable(name="input", description="The user input", is_required=True),
    ]
)

answer_function = kernel.add_function(
    function_name="answerFunc",
    plugin_name="answerPlugin",
    prompt_template_config=prompt_template_config,
)
```

***

### Running the Application

Invoke the function with a sample question:

```python
input_text = "What are the main benefits of observability in AI systems?"

response = await kernel.invoke(answer_function, input=input_text)

print(response)
```

OpenLIT captures this interaction and routes the trace data to InteractiveAI automatically.

***

### Enriching Traces with Context

Combine OpenLIT instrumentation with the InteractiveAI SDK to attach identifiers and metadata:

```python
from interactiveai import Interactive
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

client = Interactive(
    public_key=os.environ["INTERACTIVEAI_PUBLIC_KEY"],
    secret_key=os.environ["INTERACTIVEAI_SECRET_KEY"],
    host=os.environ.get("INTERACTIVEAI_HOST", "https://app.interactiveai.com")
)

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())

async def run_with_context():
    with client.start_as_current_span(name="semantic-kernel-task") as span:
        
        client.update_current_trace(
            user_id="user_123",
            session_id="session_abc",
            tags=["semantic-kernel", "qa"],
            metadata={"category": "general", "environment": "production"}
        )
        
        input_text = "Explain the concept of prompt engineering."
        response = await kernel.invoke(answer_function, input=input_text)
        
        client.update_current_trace(
            input=input_text,
            output=str(response)
        )
        
        return response

    client.flush()

result = await run_with_context()
print(result)
```

***

### Trace Visibility

The InteractiveAI dashboard displays:

* Kernel function invocations and plugin calls
* LLM requests with prompts and completions
* Prompt template rendering and variable substitution
* Token consumption and latency metrics


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.interactive.ai/integrations/ai-frameworks/semantic-kernel.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
