|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +## Documentation |
| 4 | + |
| 5 | +- `CONTRIBUTING.md`: Contains the rules for contributing to the project. |
| 6 | +- `STYLEGUIDE.md`: Contains the code style guide. It is mandatory for all python contributions. |
| 7 | + |
| 8 | +## `ai-data` - Common Library |
| 9 | + |
| 10 | +### Core Components |
| 11 | + |
| 12 | +`src/ai_data` is the core Python package for shared constants, paths, and models: |
| 13 | + |
| 14 | +- `cache`: disk-based response caching with `@cached_method` decorator. |
| 15 | +- `clients`: singleton API clients with rate limiting (WizClient, MikaClient). |
| 16 | +- `credentials`: singleton environment configuration classes for external services. |
| 17 | +- `llm_utils`: LLM client abstractions (`ModelGateway`, `LlmConfig`, model enums). |
| 18 | +- `paths`: shared project paths. |
| 19 | +- `types`: shared data models. |
| 20 | +- `utils`: shared async utilities (JSON, file helpers). |
| 21 | + |
| 22 | +### Paths |
| 23 | + |
| 24 | +- Use `AI_DATA_ROOT` for the project root. |
| 25 | +- See `paths.py` for other paths. |
| 26 | + |
| 27 | +### Types |
| 28 | + |
| 29 | +`Struct` is the base model (Pydantic) with stricter rules: |
| 30 | + |
| 31 | +- Forbids extra fields. |
| 32 | +- Validates defaults and assignments. |
| 33 | +- Strips whitespace from string fields. |
| 34 | +- Uses enum values for enum fields. |
| 35 | +- Provides a deterministic SHA256 `signature` for change detection. |
| 36 | + |
| 37 | +### Credentials |
| 38 | + |
| 39 | +`GlobalCredentials` loads from `.env`, environment variables, or device authentication. |
| 40 | + |
| 41 | +```python |
| 42 | +from ai_data.credentials.base import GlobalCredentials |
| 43 | + |
| 44 | +class MyConfig(GlobalCredentials): |
| 45 | + api_key: str = Field(validation_alias="MY_API_KEY") |
| 46 | + |
| 47 | +MyConfig() # loads from .env then environment |
| 48 | +MyConfig(sources=[".env"]) # loads from .env only |
| 49 | +MyConfig(sources=["environment"]) # loads from os.environ only |
| 50 | +MyConfig(sources=["device"]) # loads from device authentication |
| 51 | +MyConfig().api_key |
| 52 | +``` |
| 53 | + |
| 54 | +**Available credentials:** |
| 55 | + |
| 56 | +- `VertexCredentials`: `VERTEX_AI_PROJECT`, `VERTEX_AI_LOCATION`, `GOOGLE_VERTEX_AI_SA` (service account JSON) |
| 57 | +- `WizCredentials`: Wiz API credentials |
| 58 | + |
| 59 | +`WizCredentials` env var prefix selection via `WizEnv`: |
| 60 | + |
| 61 | +- `WizEnv.WIZ`: `WIZ_*` |
| 62 | +- `WizEnv.WIZ_DEMO`: `WIZ_DEMO_*` |
| 63 | +- `WizEnv.WIZ_DEMO_ADVANCED`: `WIZ_DEMO_ADVANCED_*` |
| 64 | +- `WizEnv.WIZ_TEST`: `WIZ_TEST_*` |
| 65 | + |
| 66 | +### Cache |
| 67 | + |
| 68 | +Disk-based response caching using `@cached_method` decorator for async methods. |
| 69 | + |
| 70 | +```python |
| 71 | +from ai_data.cache import cached_method |
| 72 | + |
| 73 | +class MyClient: |
| 74 | + @cached_method(namespace_arg="model", ttl="1h") |
| 75 | + async def fetch(self, model: str, query: str) -> dict: |
| 76 | + return await self._api_call(model, query) |
| 77 | + |
| 78 | +# Normal call (uses cache) |
| 79 | +result = await client.fetch("gpt4", "query") |
| 80 | + |
| 81 | +# Force refresh (skips cache read, overwrites cache) |
| 82 | +result = await client.fetch("gpt4", "query", use_cache=False) |
| 83 | +``` |
| 84 | + |
| 85 | +TTL units: `s` (seconds), `m` (minutes), `h` (hours), `d` (days), `w` (weeks). |
| 86 | + |
| 87 | +### Utils |
| 88 | + |
| 89 | +`ai_data.utils` provides async file helpers: |
| 90 | + |
| 91 | +```python |
| 92 | +from ai_data.utils import load_json, save_json, read_file, write_file |
| 93 | + |
| 94 | +# JSON |
| 95 | +data = await load_json("config.json") |
| 96 | +await save_json("out.json", data) |
| 97 | + |
| 98 | +# Text files |
| 99 | +content = await read_file("file.txt") |
| 100 | +await write_file("file.txt", content) |
| 101 | +``` |
| 102 | + |
| 103 | +### ModelGateway |
| 104 | + |
| 105 | +User-facing LLM client with flexible API, structured output, and caching. |
| 106 | + |
| 107 | +```python |
| 108 | +from ai_data import ModelGateway, ModelName |
| 109 | +from pydantic import BaseModel |
| 110 | + |
| 111 | +gateway = ModelGateway() |
| 112 | + |
| 113 | +# Plain text completion |
| 114 | +result = await gateway.completion("What is 2+2?") |
| 115 | +print(result.content) # "4" |
| 116 | + |
| 117 | +# Structured output |
| 118 | +class Answer(BaseModel): |
| 119 | + value: int |
| 120 | + explanation: str |
| 121 | + |
| 122 | +result = await gateway.completion("What is 2+2?", output_type=Answer) |
| 123 | +print(result.content.value) # 4 |
| 124 | + |
| 125 | +# With specific model |
| 126 | +result = await gateway.completion("Hello", model=ModelName.HAIKU_4_5) |
| 127 | + |
| 128 | +# Batch completions |
| 129 | +results = await gateway.batch_completion(["Q1", "Q2"], output_type=Answer) |
| 130 | +``` |
| 131 | + |
| 132 | +### WizClient |
| 133 | + |
| 134 | +Singleton Wiz API client with rate limiting and async support. |
| 135 | + |
| 136 | +```python |
| 137 | +from ai_data import WizClient |
| 138 | +from ai_data.clients.wiz import GqlQuery |
| 139 | + |
| 140 | +client = WizClient() |
| 141 | + |
| 142 | +# Single query with GqlQuery dataclass |
| 143 | +query = GqlQuery(query="query { viewer { id } }", variables={}) |
| 144 | +result = await WizClient.gql_query(query) |
| 145 | + |
| 146 | +# Batch queries (concurrent, rate-limited) |
| 147 | +results = await WizClient.batch_gql_query([ |
| 148 | + GqlQuery(query=query1, variables=vars1), |
| 149 | + GqlQuery(query=query2, variables=vars2), |
| 150 | +]) |
| 151 | + |
| 152 | +WizClient.override(max_concurrency=2) # Replaces the existing singleton |
| 153 | +WizClient.clear() # Deletes the global singleton instance |
| 154 | +``` |
| 155 | + |
| 156 | +### MikaClient |
| 157 | + |
| 158 | +Extends `WizClient` with Mika AI Assistant methods. |
| 159 | + |
| 160 | +```python |
| 161 | +from ai_data import MikaClient |
| 162 | + |
| 163 | +MikaClient() # Creates singleton |
| 164 | +response = await MikaClient.query_mika("What are my critical issues?") |
| 165 | +results = await MikaClient.batch_query_mika([ |
| 166 | + "What are my critical issues?", |
| 167 | + "Show me recent vulnerabilities", |
| 168 | +]) |
| 169 | +``` |
0 commit comments