Metadata-Version: 2.5
Name: smuod_common
Version: 0.9.0
Summary: Common utilities for SMUOD
Author-email: Martin Høy <martin.hoy@dnv.com>
License-Expression: MIT
License-File: LICENSE
Requires-Python: >=3.12
Requires-Dist: azure-identity>=1.26
Requires-Dist: colorama>=0.4
Requires-Dist: logfire[system-metrics]>=5.3
Requires-Dist: opentelemetry-exporter-otlp-proto-grpc>=1.45
Requires-Dist: opentelemetry-instrumentation-fastapi>=0.66b1
Requires-Dist: opentelemetry-instrumentation-httpx>=0.66b1
Requires-Dist: pydantic-settings>=2
Provides-Extra: langchain
Requires-Dist: langchain[openai]>=1.4; extra == 'langchain'
Provides-Extra: mssql
Requires-Dist: opentelemetry-instrumentation-sqlalchemy>=0.66b1; extra == 'mssql'
Requires-Dist: pyodbc>=5.3; extra == 'mssql'
Requires-Dist: sqlalchemy<2.1.0,>=2.0; extra == 'mssql'
Provides-Extra: postgres
Requires-Dist: opentelemetry-instrumentation-sqlalchemy>=0.66b1; extra == 'postgres'
Requires-Dist: psycopg[binary]>=3.3; extra == 'postgres'
Requires-Dist: sqlalchemy<2.1.0,>=2.0; extra == 'postgres'
Provides-Extra: pydantic-ai
Requires-Dist: httpx2>=2.16; extra == 'pydantic-ai'
Requires-Dist: pydantic-ai-harness>=0.61; extra == 'pydantic-ai'
Requires-Dist: pydantic-ai[typesafe]>=2.55; extra == 'pydantic-ai'
Provides-Extra: snowflake
Requires-Dist: snowflake-sqlalchemy>=2.1; extra == 'snowflake'
Provides-Extra: sqlalchemy
Requires-Dist: opentelemetry-instrumentation-sqlalchemy>=0.66b1; extra == 'sqlalchemy'
Requires-Dist: sqlalchemy<2.1.0,>=2.0; extra == 'sqlalchemy'
Description-Content-Type: text/markdown

# smuod_common - Common utilities for the SMUOD team

# About this project

This repository contains common utilities and helpers used across the SMUOD team's
projects, including Azure Foundry access, database connection management, Logging and
OpenTelemetry instrumentation.


## Installing

You can install this package using `uv`:

```bash
uv add smuod_common
```

## Configuration variables

Each configuration variable is searched for in the following locations (the first value
wins):

- environment variables in the current shell
- configuration (.env) file given by environment variable `CONFIG_FILE` or `config.env`
- defaults set in the `smuod_common.settings` module.

Relevant environment variables:
| Environment Variable | Description |
|--------------------|-------------|
| `CONFIG_FILE` | Path to the configuration file to load environment variables from (default: `config.env`). |
| `AZURE_FOUNDRY_NAME` | Name of the Azure Foundry environment to use (default: `smuod-foundry-sweden`) |
| `LOGLEVEL_CONSOLE` | Log level for console output (default: `INFO`). |
| `LOGLEVEL_OTEL` | Log level for OpenTelemetry output (default: `INFO`). |

# How to use this package

## Logging and OpenTelemetry instrumentation

Put the following code e.g. in your top level `__init__.py` file of your package.

```python
from smuod_common import configure_logging_console, configure_otel

configure_logging_console(__package__)
configure_otel()
```


## LLM example code

Both the Pydantic AI and Langchain integrations are implmented with a `get_model`
function to obtain the appropriate model instance. And `get_embedder` for embedding
models. 

The argument is a string that follows the Pydantic AI naming convention (e.g.
`provider:model-version`). If `provider` is omitted, the default provider is
`azure-responses`.


### Pydantic AI integration

The Pydantic AI integration is an optional dependency. Install the package with the
following command:

```bash
uv add "smuod_common[pydantic_ai]"
```

The Pydantic AI client is configured with automatic retries for return codes 429, 502,
503, 504 and connection failures. It will respect Retry-After headers (capped at 300
seconds per wait), otherwise it uses exponential backoff. Upon retries, a WARNING will
be logged. After 5 attempts, the client will stop retrying and raise an exception.

Examples:
```python
from smuod_common.pydantic_ai import get_embedder, get_model

embedder = get_embedder("text-embedding-3-large")
gpt6 = get_model("gpt-6-luna-2026-09-22")
grok = get_model("grok-4.7")
decision = get_model("typesafe:Decision-1")
```

```python
from pydantic_ai import Agent

from smuod_common.pydantic_ai import get_model

agent = Agent(get_model("gpt-5.6-luna-2026-07-09"))
response = await agent.run("Hello, world!")
```

```python
from pydantic_ai import Agent

from smuod_common.pydantic_ai import get_model

agent = Agent(
    get_model("typesafe:Decision-1"),
    output_type=bool,
    instructions="Does this request contain sensitive information?",
    # model_settings=DecisionModelSettings(decision_boolean_threshold=0.99),
)
result = await agent.run("My credit card number is 1234-5678-9012-3456.")
print(result.output)
print(result.response.provider_details)
```

### Langchain integration

The Langchain integration is an optional dependency. Install the package with the
following command:

```bash
uv add "smuod_common[langchain]"
```

```python
from langchain_core.output_parsers import StrOutputParser

from smuod_common.langchain import get_model

llm = get_model("gpt-6-luna-2026-09-22")
chain = llm | StrOutputParser()
chain.invoke("Hello, world!")
```

# Contributing

Contributions to this repository are welcome. Please follow these steps:

1. Clone the repository.
2. Create a new branch for your feature or bugfix.
3. Commit your changes with clear and descriptive messages.
4. Push your changes to the remote repository.
5. Open a pull request against the main repository.

Please ensure that your code follows the existing style and includes appropriate tests.
