Skip to content

Instantly share code, notes, and snippets.

View cedricvidal's full-sized avatar

Cedric Vidal cedricvidal

View GitHub Profile
@cedricvidal
cedricvidal / basic_agent.py
Last active May 28, 2025 20:40
OpenAI Agents SDK with Azure AI Foundry and Keyless authentication
# uv add asyncio azure-identity dotenv-azd openai-agents
from openai import AsyncAzureOpenAI
from agents import Agent, Runner, set_default_openai_client, set_tracing_export_api_key
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from dotenv import load_dotenv
from dotenv_azd import load_azd_env
import asyncio
import os
@cedricvidal
cedricvidal / json-diff.py
Last active May 22, 2025 17:49
Python CLI to create a JSON diff using the patchdiff package
#!/usr/bin/env python
import rich_click as click
import json
from pathlib import Path
from dotenv import load_dotenv
from sys import stdout
from patchdiff import diff as patch_diff
load_dotenv()
@cedricvidal
cedricvidal / gist:69e2dbfbe37bd718b9917578875c3a62
Created January 23, 2025 01:40
Source .env variables and execute command or docker image
# print env vars
cat .azure/test/.env | xargs -i echo {}
# use for a command and merge into current env
env $(cat .azure/test/.env | xargs) env
# use as a docker run --env-file
docker run -it --env-file <(cat .azure/test/.env | xargs -i echo {}) ubuntu env
@cedricvidal
cedricvidal / litellm_patched.py
Last active October 7, 2024 23:24
LiteLLM Monkey Patched to support Azure Keyless authentication using `DefaultAzureCredential` and `get_bearer_token_provider`
#!/usr/local/bin/python3.12
# -*- coding: utf-8 -*-
import re
import sys
# LiteLLM doesn't support DefaultAzureCredential and get_bearer_token_provider
# There has been a discussion on the subject but no clear path forward:
# See https://github.com/BerriAI/litellm/issues/4417
# This monkey patches LiteLLM to force it to use the DefaultAzureCredential and get_bearer_token_provider
@cedricvidal
cedricvidal / dedup_env.sh
Created August 22, 2024 14:50
Deduplicate environment variables from multiple sources with Bash 4+
#!/bin/bash
dedup_env() {
local -A env_ary
while IFS== read -r key value; do
value=$(echo "$value" | sed 's/^ *"//' | sed 's/" *$//')
env_ary[$key]=$value
done <<EOM
$(cat $*)
EOM
@cedricvidal
cedricvidal / array-filtering.bicep
Created August 21, 2024 09:45
Bicep array filtering example
var deployments = [
{
name: 'text-embedding-ada-002'
format: 'OpenAI'
}
{
name: 'llama'
format: 'serverless'
}
]
@cedricvidal
cedricvidal / hf_constant_hash_hack.py
Last active May 19, 2024 18:17
Huggingface hack to prevent fingerprint issues with functions
from typing import Any
def constant_hash(func):
"""
Wraps a function and changes its hashing behavior to a constant.
This allows Hugginface to only use functiona parameters for hashing and nothing else in
the function's captured context
"""
return ConstantHash(func)
@cedricvidal
cedricvidal / open_llm_openai.py
Last active March 7, 2024 21:56
Consume Azure AI Pay As You Go (PAYG) Open Model endpoint (Llama 2, ...)
# Those endpoints don't use the usual Azure OpenAI scheme, they use the OpenAI scheme.
# They also take the model field to route to the proper deployment, but I haven't verified this works
# Tested with openai 1.13.3
from openai import OpenAI
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(filename)s:%(funcName)s:%(lineno)d - %(message)s',
@cedricvidal
cedricvidal / uuid5_sha256.py
Created February 11, 2024 18:38
Python UUID5 using SHA-256
# Generate a UUID5 from a name using SHA-256
def uuid5_sha256(namespace, name):
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
if isinstance(name, str):
name = bytes(name, "utf-8")
from uuid import UUID
import hashlib
hash = hashlib.sha256(namespace.bytes + name).digest()
return UUID(bytes=hash[:16], version=5)
@cedricvidal
cedricvidal / logging.py
Created March 28, 2023 19:55 — forked from kingspp/logging.py
Python Comprehensive Logging using YAML Configuration
import os
import yaml
import logging.config
import logging
import coloredlogs
def setup_logging(default_path='logging.yaml', default_level=logging.INFO, env_key='LOG_CFG'):
"""
| **@author:** Prathyush SP
| Logging Setup