context-compiler-example-integrations 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. context_compiler_example_integrations/README.md +71 -0
  2. context_compiler_example_integrations/__init__.py +1 -0
  3. context_compiler_example_integrations/examples/__init__.py +12 -0
  4. context_compiler_example_integrations/examples/_shared/__init__.py +1 -0
  5. context_compiler_example_integrations/examples/_shared/litellm_request.py +61 -0
  6. context_compiler_example_integrations/examples/_shared/provider_mode.py +91 -0
  7. context_compiler_example_integrations/examples/checkpoint_continuation/README.md +77 -0
  8. context_compiler_example_integrations/examples/checkpoint_continuation/example.py +169 -0
  9. context_compiler_example_integrations/examples/checkpoint_continuation/fastapi/README.md +85 -0
  10. context_compiler_example_integrations/examples/checkpoint_continuation/fastapi/app.py +205 -0
  11. context_compiler_example_integrations/examples/execution_authorization/README.md +31 -0
  12. context_compiler_example_integrations/examples/execution_authorization/expense_approval/README.md +77 -0
  13. context_compiler_example_integrations/examples/execution_authorization/expense_approval/__init__.py +1 -0
  14. context_compiler_example_integrations/examples/execution_authorization/expense_approval/example.py +168 -0
  15. context_compiler_example_integrations/examples/execution_authorization/expense_approval/fastapi/README.md +97 -0
  16. context_compiler_example_integrations/examples/execution_authorization/expense_approval/fastapi/__init__.py +1 -0
  17. context_compiler_example_integrations/examples/execution_authorization/expense_approval/fastapi/app.py +312 -0
  18. context_compiler_example_integrations/examples/execution_authorization/expense_approval/fastapi/model_approval.py +98 -0
  19. context_compiler_example_integrations/examples/gateway_middleware/README.md +49 -0
  20. context_compiler_example_integrations/examples/gateway_middleware/customer_support_routing/README.md +40 -0
  21. context_compiler_example_integrations/examples/gateway_middleware/customer_support_routing/__init__.py +1 -0
  22. context_compiler_example_integrations/examples/gateway_middleware/customer_support_routing/example.py +220 -0
  23. context_compiler_example_integrations/examples/prompt_construction/README.md +27 -0
  24. context_compiler_example_integrations/examples/prompt_construction/litellm/README.md +197 -0
  25. context_compiler_example_integrations/examples/prompt_construction/litellm/basic.py +392 -0
  26. context_compiler_example_integrations/examples/prompt_construction/litellm/confirmation_helper.py +91 -0
  27. context_compiler_example_integrations/examples/prompt_construction/litellm/with_directive_drafter.py +524 -0
  28. context_compiler_example_integrations/examples/prompt_construction/writing_assistant/README.md +92 -0
  29. context_compiler_example_integrations/examples/prompt_construction/writing_assistant/__init__.py +1 -0
  30. context_compiler_example_integrations/examples/prompt_construction/writing_assistant/example.py +205 -0
  31. context_compiler_example_integrations/examples/retrieval_filtering/README.md +65 -0
  32. context_compiler_example_integrations/examples/retrieval_filtering/chromadb_hr_policy_lookup/README.md +120 -0
  33. context_compiler_example_integrations/examples/retrieval_filtering/chromadb_hr_policy_lookup/__init__.py +1 -0
  34. context_compiler_example_integrations/examples/retrieval_filtering/chromadb_hr_policy_lookup/example.py +320 -0
  35. context_compiler_example_integrations/examples/retrieval_filtering/hr_policy_lookup/README.md +123 -0
  36. context_compiler_example_integrations/examples/retrieval_filtering/hr_policy_lookup/__init__.py +1 -0
  37. context_compiler_example_integrations/examples/retrieval_filtering/hr_policy_lookup/example.py +300 -0
  38. context_compiler_example_integrations/examples/schema_selection/README.md +41 -0
  39. context_compiler_example_integrations/examples/schema_selection/litellm_response_format/response_format.py +218 -0
  40. context_compiler_example_integrations/examples/schema_selection/ollama_structured_output/README.md +60 -0
  41. context_compiler_example_integrations/examples/schema_selection/ollama_structured_output/example.py +171 -0
  42. context_compiler_example_integrations/examples/schema_selection/refund_intake/README.md +95 -0
  43. context_compiler_example_integrations/examples/schema_selection/refund_intake/__init__.py +1 -0
  44. context_compiler_example_integrations/examples/schema_selection/refund_intake/example.py +180 -0
  45. context_compiler_example_integrations/examples/tool_gating/README.md +55 -0
  46. context_compiler_example_integrations/examples/tool_gating/calendar_admin/README.md +34 -0
  47. context_compiler_example_integrations/examples/tool_gating/calendar_admin/__init__.py +1 -0
  48. context_compiler_example_integrations/examples/tool_gating/calendar_admin/example.py +195 -0
  49. context_compiler_example_integrations/examples/tool_gating/mcp_calendar_admin/README.md +93 -0
  50. context_compiler_example_integrations/examples/tool_gating/mcp_calendar_admin/__init__.py +1 -0
  51. context_compiler_example_integrations/examples/tool_gating/mcp_calendar_admin/example.py +250 -0
  52. context_compiler_example_integrations/examples/tool_gating/mcp_calendar_admin/live_model.py +369 -0
  53. context_compiler_example_integrations/reference_integrations/__init__.py +12 -0
  54. context_compiler_example_integrations/reference_integrations/litellm_proxy/README.md +251 -0
  55. context_compiler_example_integrations/reference_integrations/litellm_proxy/_checkpoint_support.py +146 -0
  56. context_compiler_example_integrations/reference_integrations/litellm_proxy/config.example.yaml +23 -0
  57. context_compiler_example_integrations/reference_integrations/litellm_proxy/context_compiler_precall_hook.py +163 -0
  58. context_compiler_example_integrations/reference_integrations/litellm_proxy/context_compiler_precall_hook_with_directive_drafter.py +289 -0
  59. context_compiler_example_integrations/reference_integrations/openwebui_pipe/README.md +233 -0
  60. context_compiler_example_integrations/reference_integrations/openwebui_pipe/open_webui_pipe.py +750 -0
  61. context_compiler_example_integrations/reference_integrations/openwebui_pipe/open_webui_pipe_with_directive_drafter.py +1045 -0
  62. context_compiler_example_integrations-0.1.0.dist-info/METADATA +110 -0
  63. context_compiler_example_integrations-0.1.0.dist-info/RECORD +65 -0
  64. context_compiler_example_integrations-0.1.0.dist-info/WHEEL +4 -0
  65. context_compiler_example_integrations-0.1.0.dist-info/licenses/LICENSE +173 -0
@@ -0,0 +1,233 @@
1
+ # Open WebUI Pipe Integration
2
+
3
+ Saved compiler state changes which turns the pipe handles locally and what it
4
+ forwards downstream. These examples show Open WebUI pipe behavior with and
5
+ without Directive Drafter preprocessing.
6
+
7
+ ## Core behavior
8
+
9
+ - The pipe handles directive-only turns locally and returns a fixed response.
10
+ - The pipe forwards normal chat turns to the backend model.
11
+ - When compiler state is non-empty, passthrough includes exactly one compiler-owned
12
+ `[[cc_state]]` system message in the forwarded request.
13
+ - Conflicting or ambiguous updates ask for clarification before state changes.
14
+ - The pipe handles exact `show state` locally. The pipe treats near matches such
15
+ as `show state please` as normal chat input.
16
+
17
+ The forwarded `[[cc_state]]` block makes both kinds of authoritative state
18
+ runtime-visible:
19
+
20
+ - premise = authoritative factual or request context
21
+ - policy = explicit behavioral constraint
22
+
23
+ Example forwarded state block with both:
24
+
25
+ ```text
26
+ [[cc_state]]
27
+ Premise: draft is a board update summarizing quarterly results
28
+ Use: concise_style
29
+ Prohibit: slang
30
+ ```
31
+
32
+ The host injects this block into the forwarded request. Premise is not only a
33
+ syntax or lifecycle rule here; it is part of the runtime-visible state that
34
+ shapes the downstream request.
35
+
36
+ ## Files
37
+
38
+ - `open_webui_pipe.py`: basic integration, no Directive Drafter layer (recommended/default).
39
+ - `open_webui_pipe_with_directive_drafter.py`: optional/experimental Directive Drafter layer (heuristic check first, then optional model fallback) before `engine.step(...)`.
40
+
41
+ ## Setup
42
+
43
+ Quick start for the base pipe:
44
+
45
+ 1. Import `open_webui_pipe.py` as a Function by URL.
46
+ 2. Enable the function.
47
+ 3. Set `BASE_MODEL_ID` to a valid Open WebUI model id.
48
+ 4. Turn on `SHOW_CONTEXT_COMPILER_TRACE=true` if you want easy in-chat verification.
49
+ 5. Select the pipe model in chat.
50
+
51
+ Open WebUI is a separate runtime and must already be installed/configured separately.
52
+ Open WebUI also needs at least one real backend model/provider configured (for example Ollama or OpenAI) so `BASE_MODEL_ID` resolves to an actual model.
53
+ Note: The `PROVIDER` environment contract used in LiteLLM examples/demos does not apply to OpenWebUI. OpenWebUI manages providers via its own connection settings and model IDs.
54
+
55
+ These examples require `context-compiler>=0.8.3`.
56
+
57
+ ### Configuration
58
+
59
+ - Open: `http://localhost:3000/admin/functions`
60
+ - Verify `BASE_MODEL_ID` matches an existing Open WebUI model id exactly
61
+ - Example: `BASE_MODEL_ID = llama3.1:8b`
62
+ - Model ids are configured in: `Admin Panel → Settings → Models`
63
+
64
+ If using `open_webui_pipe_with_directive_drafter.py`:
65
+
66
+ - Install directive-drafter support if needed:
67
+ `pip install "context-compiler>=0.8.3" "context-compiler-directive-drafter>=0.1.2"`
68
+ - Set `PREPROCESSOR_PROMPT_PROFILE=default` for heuristic-first behavior
69
+ - Optionally set `PREPROCESSOR_MODEL_ID` to use a separate fallback drafting model
70
+ - If `PREPROCESSOR_MODEL_ID` is unset, fallback uses `BASE_MODEL_ID`
71
+ - Use `llama` only for LLM-only fallback drafting with Llama-family models
72
+
73
+ Model fallback output is structurally validated before handoff. This does not prove that the model interpreted the user correctly. The automated fallback path is experimental pending a separate source-aware acceptance policy and reviewed drafting workflow.
74
+
75
+ ### Docker/manual install fallback
76
+
77
+ If frontmatter dependency installs are disabled, offline, or unavailable:
78
+
79
+ 1. Open a shell in the Open WebUI container:
80
+ - `docker exec -it <openwebui-container> sh`
81
+
82
+ 1. Install the package manually:
83
+
84
+ - Minimal pipe: `pip install "context-compiler>=0.8.3"`
85
+ - Directive Drafter pipe: `pip install "context-compiler>=0.8.3" "context-compiler-directive-drafter>=0.1.2"`
86
+
87
+ 1. Import and enable the function in Open WebUI, then configure valves.
88
+
89
+ ### Finding valid model ids
90
+
91
+ Use the Open WebUI model picker/list to copy exact model ids for `BASE_MODEL_ID`
92
+ (and optional `PREPROCESSOR_MODEL_ID` for the directive-drafter pipe).
93
+
94
+ ## Verify behavior
95
+
96
+ ### Before you start
97
+
98
+ Use a real Open WebUI runtime that you control locally.
99
+
100
+ Use a backend model/provider that you can observe during verification.
101
+ A local backend or request-capturing proxy is recommended because it makes
102
+ it easier to confirm when model calls occur.
103
+
104
+ For the easiest verification, enable `SHOW_CONTEXT_COMPILER_TRACE=true` in the
105
+ function valves before testing. The trace is appended to normal responses and
106
+ gives a quick view of what happened on each turn.
107
+
108
+ If you also have a local proxy or stub that records backend requests, you can
109
+ use that as an optional advanced check to confirm the exact forwarded
110
+ `[[cc_state]]` system message.
111
+
112
+ ### Base pipe
113
+
114
+ Use this pipe when you want the simplest Open WebUI integration path.
115
+
116
+ Suggested verification:
117
+
118
+ - Send `use docker` and confirm you get `State updated: Use docker.` with trace showing a local turn
119
+ - Send a normal prompt such as `what should I run?` and confirm trace shows a forwarded turn with compiler state included
120
+ - Send `use kubectl instead of docker` and confirm Open WebUI asks for clarification instead of changing state
121
+ - Optionally send `show state` and confirm the state summary is returned locally
122
+
123
+ Advanced check:
124
+
125
+ - If you have a local proxy or stub, inspect the forwarded request and confirm it contains exactly one `[[cc_state]]` system message.
126
+ - Confirm that saved premise appears as `Premise: ...` and saved policy appears as `Use: ...` or `Prohibit: ...`.
127
+
128
+ ### Directive-drafter pipe
129
+
130
+ Use this pipe when you want the same runtime behavior plus Directive Drafter preprocessing.
131
+
132
+ Suggested verification:
133
+
134
+ - Send `use docker` and confirm you get `State updated: Use docker.` with trace showing a local turn
135
+ - Send `set premise to concise replies` and confirm Open WebUI clarifies locally with `Use 'set premise <value>'.`
136
+ - Send `please use docker` and confirm either:
137
+ - the Directive Drafter converts it into a local state update, or
138
+ - trace shows the turn followed the normal compiler path without a silent state change
139
+ - Send `use kubectl instead of docker`, then reply `yes`, and confirm the saved clarification flow resumes locally
140
+ - Send `use docker and prohibit peanuts` and confirm the pipe responds locally that multiple directives are not supported and must be submitted separately
141
+ - Send a normal prompt such as `what should I run?` and confirm trace shows a forwarded turn with compiler state included
142
+
143
+ Advanced check:
144
+
145
+ - If you have a local proxy or stub, inspect the forwarded request and confirm it contains exactly one `[[cc_state]]` system message reflecting the active state.
146
+ - Confirm that saved premise appears as `Premise: ...` and saved policy appears as `Use: ...` or `Prohibit: ...`.
147
+
148
+ ### Optional extra checks
149
+
150
+ If you want a slightly broader manual pass:
151
+
152
+ - verify chat isolation with separate real chat ids
153
+ - verify state is lost after restart because these examples do not use external persistence
154
+ - verify non-text input is bypassed
155
+
156
+ ### Notes
157
+
158
+ - Trace is the easiest way to verify behavior from the Open WebUI chat output.
159
+ - Forwarded-request inspection is optional and most useful when you already have a local proxy or stub.
160
+ - Exact `show state` is a local-state check and does not rely on trace output.
161
+
162
+ ## Limits
163
+
164
+ - No durable external persistence
165
+ - No multi-worker or cross-process guarantees
166
+ - No Redis, DB, or external storage for checkpoints
167
+ - No Filters or Pipelines
168
+ - No production hardening
169
+
170
+ ## Behavioral comparisons
171
+
172
+ ### Case 1
173
+
174
+ - prompt(s): `clear state` → `change premise to formal tone`
175
+ - base model: “To adjust the tone… provide the original content…”
176
+ - basic pipe: `No premise exists yet. Use 'set premise ...' first.`
177
+ - directive-drafter pipe: `No premise exists yet. Use 'set premise ...' first.`
178
+ - why this matters: lifecycle rule is enforced in a fixed, repeatable way; base model drifts into generic rewriting help.
179
+
180
+ ### Case 2
181
+
182
+ - prompt(s): `clear state` → `use docker` → `prohibit docker`
183
+ - base model: generic Docker/prohibition guidance text
184
+ - basic pipe: `'docker' is already in use. Only one policy per item is allowed. Use 'reset policies' to change it.`
185
+ - directive-drafter pipe: same conflict clarify
186
+ - why this matters: the app asks before applying a conflicting change.
187
+
188
+ ### Case 3
189
+
190
+ - prompt(s): `clear state` → `use podman instead of docker`
191
+ - base model: generic “how to switch to Podman” tutorial
192
+ - basic pipe: `No exact policy found for "docker". Replacement requires an exact policy match...`
193
+ - directive-drafter pipe: same replacement clarify
194
+ - why this matters: the app only replaces a policy when the old item already exists.
195
+
196
+ ### Case 4
197
+
198
+ - prompt(s): `clear state` → `set premise to concise replies`
199
+ - base model: accepts conversational style phrasing
200
+ - basic pipe: `Did you mean 'set premise concise replies'?`
201
+ - directive-drafter pipe: same clarify (near-miss is not rewritten)
202
+ - why this matters: near-miss text is not silently rewritten.
203
+
204
+ ### Case 5
205
+
206
+ - prompt(s): `clear state` → `change premise concise replies`
207
+ - base model: generic “please clarify changes” response
208
+ - basic pipe: `Did you mean 'change premise to concise replies'?`
209
+ - directive-drafter pipe: same clarify (near-miss is passed through unchanged)
210
+ - why this matters: the app waits for explicit, valid directive text before changing state.
211
+
212
+ ## Compatibility
213
+
214
+ Tested target: Open WebUI `v0.8.12`.
215
+ Validated at runtime on stock Docker Open WebUI with a real backend model provider.
216
+
217
+ Compatibility note: OpenWebUI `0.9.x` changed `Users.get_user_by_id` to async.
218
+ These examples support both sync (`0.8.x`) and async (`0.9.x`) user lookup.
219
+
220
+ ## Troubleshooting
221
+
222
+ - `BASE_MODEL_ID is required`: set a valid Open WebUI model id in the function valves, or enable `ALLOW_MISSING_BASE_MODEL_FOR_DEBUG=true` only for local testing.
223
+ - `BASE_MODEL_ID was not found in Open WebUI models`: copy the exact id from `Admin Panel → Settings → Models`.
224
+ - `PREPROCESSOR_MODEL_ID was not found in Open WebUI models`: set a valid fallback model id or leave it unset to default to `BASE_MODEL_ID`.
225
+ - `PREPROCESSOR_MODEL_ID must not match the selected pipe model id`: choose a real backend model id, not the pipe model id itself.
226
+ - `PREPROCESSOR_MODEL_ID is invalid or not configured in Open WebUI`: the fallback route hit a missing model; fix the configured fallback model or unset it to reuse `BASE_MODEL_ID`.
227
+ - `ALLOW_MISSING_BASE_MODEL_FOR_DEBUG=true`: directive-only updates still run locally, but passthrough returns a deterministic debug message instead of calling a downstream model.
228
+ - imports fail after function upload: install `context-compiler>=0.8.3` in the Open WebUI runtime, and add `context-compiler-directive-drafter>=0.1.2` only for the Directive Drafter pipe, because the copied function runs from a temp/cached location.
229
+
230
+ ## Fallback notes
231
+
232
+ - Fallback drafting uses `PREPROCESSOR_MODEL_ID` first, while the main passthrough path still forwards with `BASE_MODEL_ID`.
233
+ - If the fallback model returns `model not found`, the pipe normalizes that into the deterministic `PREPROCESSOR_MODEL_ID` misconfiguration message above.