ag-cortex 0.1.0

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 (162) hide show
  1. package/.agent/commands/test-browser.md +339 -0
  2. package/.agent/rules/00-constitution.md +46 -0
  3. package/.agent/rules/project-rules.md +49 -0
  4. package/.agent/skills/agent-browser/SKILL.md +223 -0
  5. package/.agent/skills/agent-native-architecture/SKILL.md +435 -0
  6. package/.agent/skills/agent-native-architecture/references/action-parity-discipline.md +409 -0
  7. package/.agent/skills/agent-native-architecture/references/agent-execution-patterns.md +467 -0
  8. package/.agent/skills/agent-native-architecture/references/agent-native-testing.md +582 -0
  9. package/.agent/skills/agent-native-architecture/references/architecture-patterns.md +478 -0
  10. package/.agent/skills/agent-native-architecture/references/dynamic-context-injection.md +338 -0
  11. package/.agent/skills/agent-native-architecture/references/files-universal-interface.md +301 -0
  12. package/.agent/skills/agent-native-architecture/references/from-primitives-to-domain-tools.md +359 -0
  13. package/.agent/skills/agent-native-architecture/references/mcp-tool-design.md +506 -0
  14. package/.agent/skills/agent-native-architecture/references/mobile-patterns.md +871 -0
  15. package/.agent/skills/agent-native-architecture/references/product-implications.md +443 -0
  16. package/.agent/skills/agent-native-architecture/references/refactoring-to-prompt-native.md +317 -0
  17. package/.agent/skills/agent-native-architecture/references/self-modification.md +269 -0
  18. package/.agent/skills/agent-native-architecture/references/shared-workspace-architecture.md +680 -0
  19. package/.agent/skills/agent-native-architecture/references/system-prompt-design.md +250 -0
  20. package/.agent/skills/agent-native-reviewer/SKILL.md +246 -0
  21. package/.agent/skills/andrew-kane-gem-writer/SKILL.md +184 -0
  22. package/.agent/skills/andrew-kane-gem-writer/references/database-adapters.md +231 -0
  23. package/.agent/skills/andrew-kane-gem-writer/references/module-organization.md +121 -0
  24. package/.agent/skills/andrew-kane-gem-writer/references/rails-integration.md +183 -0
  25. package/.agent/skills/andrew-kane-gem-writer/references/resources.md +119 -0
  26. package/.agent/skills/andrew-kane-gem-writer/references/testing-patterns.md +261 -0
  27. package/.agent/skills/ankane-readme-writer/SKILL.md +50 -0
  28. package/.agent/skills/architecture-strategist/SKILL.md +52 -0
  29. package/.agent/skills/best-practices-researcher/SKILL.md +100 -0
  30. package/.agent/skills/bug-reproduction-validator/SKILL.md +67 -0
  31. package/.agent/skills/code-simplicity-reviewer/SKILL.md +85 -0
  32. package/.agent/skills/coding-tutor/.claude-plugin/plugin.json +9 -0
  33. package/.agent/skills/coding-tutor/README.md +37 -0
  34. package/.agent/skills/coding-tutor/commands/quiz-me.md +1 -0
  35. package/.agent/skills/coding-tutor/commands/sync-tutorials.md +25 -0
  36. package/.agent/skills/coding-tutor/commands/teach-me.md +1 -0
  37. package/.agent/skills/coding-tutor/skills/coding-tutor/SKILL.md +214 -0
  38. package/.agent/skills/coding-tutor/skills/coding-tutor/scripts/create_tutorial.py +202 -0
  39. package/.agent/skills/coding-tutor/skills/coding-tutor/scripts/index_tutorials.py +203 -0
  40. package/.agent/skills/coding-tutor/skills/coding-tutor/scripts/quiz_priority.py +190 -0
  41. package/.agent/skills/coding-tutor/skills/coding-tutor/scripts/setup_tutorials.py +132 -0
  42. package/.agent/skills/compound-docs/SKILL.md +510 -0
  43. package/.agent/skills/compound-docs/assets/critical-pattern-template.md +34 -0
  44. package/.agent/skills/compound-docs/assets/resolution-template.md +93 -0
  45. package/.agent/skills/compound-docs/references/yaml-schema.md +65 -0
  46. package/.agent/skills/compound-docs/schema.yaml +176 -0
  47. package/.agent/skills/create-agent-skills/SKILL.md +299 -0
  48. package/.agent/skills/create-agent-skills/references/api-security.md +226 -0
  49. package/.agent/skills/create-agent-skills/references/be-clear-and-direct.md +531 -0
  50. package/.agent/skills/create-agent-skills/references/best-practices.md +404 -0
  51. package/.agent/skills/create-agent-skills/references/common-patterns.md +595 -0
  52. package/.agent/skills/create-agent-skills/references/core-principles.md +437 -0
  53. package/.agent/skills/create-agent-skills/references/executable-code.md +175 -0
  54. package/.agent/skills/create-agent-skills/references/iteration-and-testing.md +474 -0
  55. package/.agent/skills/create-agent-skills/references/official-spec.md +185 -0
  56. package/.agent/skills/create-agent-skills/references/recommended-structure.md +168 -0
  57. package/.agent/skills/create-agent-skills/references/skill-structure.md +372 -0
  58. package/.agent/skills/create-agent-skills/references/using-scripts.md +113 -0
  59. package/.agent/skills/create-agent-skills/references/using-templates.md +112 -0
  60. package/.agent/skills/create-agent-skills/references/workflows-and-validation.md +510 -0
  61. package/.agent/skills/create-agent-skills/templates/router-skill.md +73 -0
  62. package/.agent/skills/create-agent-skills/templates/simple-skill.md +33 -0
  63. package/.agent/skills/create-agent-skills/workflows/add-reference.md +96 -0
  64. package/.agent/skills/create-agent-skills/workflows/add-script.md +93 -0
  65. package/.agent/skills/create-agent-skills/workflows/add-template.md +74 -0
  66. package/.agent/skills/create-agent-skills/workflows/add-workflow.md +120 -0
  67. package/.agent/skills/create-agent-skills/workflows/audit-skill.md +138 -0
  68. package/.agent/skills/create-agent-skills/workflows/create-domain-expertise-skill.md +605 -0
  69. package/.agent/skills/create-agent-skills/workflows/create-new-skill.md +191 -0
  70. package/.agent/skills/create-agent-skills/workflows/get-guidance.md +121 -0
  71. package/.agent/skills/create-agent-skills/workflows/upgrade-to-router.md +161 -0
  72. package/.agent/skills/create-agent-skills/workflows/verify-skill.md +204 -0
  73. package/.agent/skills/data-integrity-guardian/SKILL.md +70 -0
  74. package/.agent/skills/data-migration-expert/SKILL.md +97 -0
  75. package/.agent/skills/deployment-verification-agent/SKILL.md +159 -0
  76. package/.agent/skills/design-implementation-reviewer/SKILL.md +85 -0
  77. package/.agent/skills/design-iterator/SKILL.md +197 -0
  78. package/.agent/skills/dhh-rails-reviewer/SKILL.md +45 -0
  79. package/.agent/skills/dhh-rails-style/SKILL.md +184 -0
  80. package/.agent/skills/dhh-rails-style/references/architecture.md +653 -0
  81. package/.agent/skills/dhh-rails-style/references/controllers.md +303 -0
  82. package/.agent/skills/dhh-rails-style/references/frontend.md +510 -0
  83. package/.agent/skills/dhh-rails-style/references/gems.md +266 -0
  84. package/.agent/skills/dhh-rails-style/references/models.md +359 -0
  85. package/.agent/skills/dhh-rails-style/references/testing.md +338 -0
  86. package/.agent/skills/dspy-ruby/SKILL.md +594 -0
  87. package/.agent/skills/dspy-ruby/assets/config-template.rb +359 -0
  88. package/.agent/skills/dspy-ruby/assets/module-template.rb +326 -0
  89. package/.agent/skills/dspy-ruby/assets/signature-template.rb +143 -0
  90. package/.agent/skills/dspy-ruby/references/core-concepts.md +265 -0
  91. package/.agent/skills/dspy-ruby/references/optimization.md +623 -0
  92. package/.agent/skills/dspy-ruby/references/providers.md +305 -0
  93. package/.agent/skills/every-style-editor/SKILL.md +134 -0
  94. package/.agent/skills/every-style-editor/references/EVERY_WRITE_STYLE.md +529 -0
  95. package/.agent/skills/figma-design-sync/SKILL.md +166 -0
  96. package/.agent/skills/file-todos/SKILL.md +251 -0
  97. package/.agent/skills/file-todos/assets/todo-template.md +155 -0
  98. package/.agent/skills/framework-docs-researcher/SKILL.md +83 -0
  99. package/.agent/skills/frontend-design/SKILL.md +42 -0
  100. package/.agent/skills/gemini-imagegen/SKILL.md +237 -0
  101. package/.agent/skills/gemini-imagegen/requirements.txt +2 -0
  102. package/.agent/skills/gemini-imagegen/scripts/compose_images.py +168 -0
  103. package/.agent/skills/gemini-imagegen/scripts/edit_image.py +157 -0
  104. package/.agent/skills/gemini-imagegen/scripts/gemini_images.py +265 -0
  105. package/.agent/skills/gemini-imagegen/scripts/generate_image.py +147 -0
  106. package/.agent/skills/gemini-imagegen/scripts/multi_turn_chat.py +215 -0
  107. package/.agent/skills/git-history-analyzer/SKILL.md +42 -0
  108. package/.agent/skills/git-worktree/SKILL.md +302 -0
  109. package/.agent/skills/git-worktree/scripts/worktree-manager.sh +345 -0
  110. package/.agent/skills/julik-frontend-races-reviewer/SKILL.md +222 -0
  111. package/.agent/skills/kieran-python-reviewer/SKILL.md +104 -0
  112. package/.agent/skills/kieran-rails-reviewer/SKILL.md +86 -0
  113. package/.agent/skills/kieran-typescript-reviewer/SKILL.md +95 -0
  114. package/.agent/skills/lint/SKILL.md +16 -0
  115. package/.agent/skills/pattern-recognition-specialist/SKILL.md +57 -0
  116. package/.agent/skills/performance-oracle/SKILL.md +110 -0
  117. package/.agent/skills/pr-comment-resolver/SKILL.md +69 -0
  118. package/.agent/skills/rclone/SKILL.md +150 -0
  119. package/.agent/skills/rclone/scripts/check_setup.sh +60 -0
  120. package/.agent/skills/repo-research-analyst/SKILL.md +113 -0
  121. package/.agent/skills/security-sentinel/SKILL.md +93 -0
  122. package/.agent/skills/skill-creator/SKILL.md +209 -0
  123. package/.agent/skills/skill-creator/scripts/init_skill.py +304 -0
  124. package/.agent/skills/skill-creator/scripts/package_skill.py +112 -0
  125. package/.agent/skills/skill-creator/scripts/quick_validate.py +72 -0
  126. package/.agent/skills/spec-flow-analyzer/SKILL.md +113 -0
  127. package/.agent/skills/test-agent/SKILL.md +4 -0
  128. package/.agent/workflows/agent-native-audit.md +277 -0
  129. package/.agent/workflows/ask-user-question.md +21 -0
  130. package/.agent/workflows/changelog.md +137 -0
  131. package/.agent/workflows/compound.md +202 -0
  132. package/.agent/workflows/create-agent-skill.md +8 -0
  133. package/.agent/workflows/deepen-plan-research.md +334 -0
  134. package/.agent/workflows/deepen-plan-synthesis.md +182 -0
  135. package/.agent/workflows/deepen-plan.md +79 -0
  136. package/.agent/workflows/feature-video.md +342 -0
  137. package/.agent/workflows/generate-command.md +162 -0
  138. package/.agent/workflows/heal-skill.md +142 -0
  139. package/.agent/workflows/lfg.md +20 -0
  140. package/.agent/workflows/plan-analysis.md +67 -0
  141. package/.agent/workflows/plan-next-steps.md +63 -0
  142. package/.agent/workflows/plan-review.md +33 -0
  143. package/.agent/workflows/plan-synthesis.md +106 -0
  144. package/.agent/workflows/plan.md +49 -0
  145. package/.agent/workflows/report-bug.md +150 -0
  146. package/.agent/workflows/reproduce-bug.md +99 -0
  147. package/.agent/workflows/resolve-parallel.md +34 -0
  148. package/.agent/workflows/resolve-pr-parallel.md +49 -0
  149. package/.agent/workflows/resolve-todo-parallel.md +35 -0
  150. package/.agent/workflows/review-analysis.md +145 -0
  151. package/.agent/workflows/review-synthesis.md +262 -0
  152. package/.agent/workflows/review.md +64 -0
  153. package/.agent/workflows/ship.md +90 -0
  154. package/.agent/workflows/test-command.md +3 -0
  155. package/.agent/workflows/triage.md +310 -0
  156. package/.agent/workflows/work.md +157 -0
  157. package/.agent/workflows/xcode-test.md +332 -0
  158. package/LICENSE +22 -0
  159. package/README.md +49 -0
  160. package/bin/ag-cortex.js +54 -0
  161. package/lib/core.js +165 -0
  162. package/package.json +31 -0
@@ -0,0 +1,594 @@
1
+ ---
2
+ name: dspy-ruby
3
+ description: This skill should be used when working with DSPy.rb, a Ruby framework for building type-safe, composable LLM applications. Use this when implementing predictable AI features, creating LLM signatures and modules, configuring language model providers (OpenAI, Google Gemini, Ollama), building agent systems with tools, optimizing prompts, or testing LLM-powered functionality in Ruby applications.
4
+ ---
5
+
6
+ # DSPy.rb Expert
7
+
8
+ ## Overview
9
+
10
+ DSPy.rb is a Ruby framework that enables developers to **program LLMs, not prompt them**. Instead of manually crafting prompts, define application requirements through type-safe, composable modules that can be tested, optimized, and version-controlled like regular code.
11
+
12
+ This skill provides comprehensive guidance on:
13
+ - Creating type-safe signatures for LLM operations
14
+ - Building composable modules and workflows
15
+ - Configuring multiple LLM providers
16
+ - Implementing agents with tools
17
+ - Testing and optimizing LLM applications
18
+ - Production deployment patterns
19
+
20
+ ## Core Capabilities
21
+
22
+ ### 1. Type-Safe Signatures
23
+
24
+ Create input/output contracts for LLM operations with runtime type checking.
25
+
26
+ **When to use**: Defining any LLM task, from simple classification to complex analysis.
27
+
28
+ **Quick reference**:
29
+ ```ruby
30
+ class EmailClassificationSignature < DSPy::Signature
31
+ description "Classify customer support emails"
32
+
33
+ input do
34
+ const :email_subject, String
35
+ const :email_body, String
36
+ end
37
+
38
+ output do
39
+ const :category, T.enum(["Technical", "Billing", "General"])
40
+ const :priority, T.enum(["Low", "Medium", "High"])
41
+ end
42
+ end
43
+ ```
44
+
45
+ **Templates**: See `assets/signature-template.rb` for comprehensive examples including:
46
+ - Basic signatures with multiple field types
47
+ - Vision signatures for multimodal tasks
48
+ - Sentiment analysis signatures
49
+ - Code generation signatures
50
+
51
+ **Best practices**:
52
+ - Always provide clear, specific descriptions
53
+ - Use enums for constrained outputs
54
+ - Include field descriptions with `desc:` parameter
55
+ - Prefer specific types over generic String when possible
56
+
57
+ **Full documentation**: See `references/core-concepts.md` sections on Signatures and Type Safety.
58
+
59
+ ### 2. Composable Modules
60
+
61
+ Build reusable, chainable modules that encapsulate LLM operations.
62
+
63
+ **When to use**: Implementing any LLM-powered feature, especially complex multi-step workflows.
64
+
65
+ **Quick reference**:
66
+ ```ruby
67
+ class EmailProcessor < DSPy::Module
68
+ def initialize
69
+ super
70
+ @classifier = DSPy::Predict.new(EmailClassificationSignature)
71
+ end
72
+
73
+ def forward(email_subject:, email_body:)
74
+ @classifier.forward(
75
+ email_subject: email_subject,
76
+ email_body: email_body
77
+ )
78
+ end
79
+ end
80
+ ```
81
+
82
+ **Templates**: See `assets/module-template.rb` for comprehensive examples including:
83
+ - Basic modules with single predictors
84
+ - Multi-step pipelines that chain modules
85
+ - Modules with conditional logic
86
+ - Error handling and retry patterns
87
+ - Stateful modules with history
88
+ - Caching implementations
89
+
90
+ **Module composition**: Chain modules together to create complex workflows:
91
+ ```ruby
92
+ class Pipeline < DSPy::Module
93
+ def initialize
94
+ super
95
+ @step1 = Classifier.new
96
+ @step2 = Analyzer.new
97
+ @step3 = Responder.new
98
+ end
99
+
100
+ def forward(input)
101
+ result1 = @step1.forward(input)
102
+ result2 = @step2.forward(result1)
103
+ @step3.forward(result2)
104
+ end
105
+ end
106
+ ```
107
+
108
+ **Full documentation**: See `references/core-concepts.md` sections on Modules and Module Composition.
109
+
110
+ ### 3. Multiple Predictor Types
111
+
112
+ Choose the right predictor for your task:
113
+
114
+ **Predict**: Basic LLM inference with type-safe inputs/outputs
115
+ ```ruby
116
+ predictor = DSPy::Predict.new(TaskSignature)
117
+ result = predictor.forward(input: "data")
118
+ ```
119
+
120
+ **ChainOfThought**: Adds automatic reasoning for improved accuracy
121
+ ```ruby
122
+ predictor = DSPy::ChainOfThought.new(TaskSignature)
123
+ result = predictor.forward(input: "data")
124
+ # Returns: { reasoning: "...", output: "..." }
125
+ ```
126
+
127
+ **ReAct**: Tool-using agents with iterative reasoning
128
+ ```ruby
129
+ predictor = DSPy::ReAct.new(
130
+ TaskSignature,
131
+ tools: [SearchTool.new, CalculatorTool.new],
132
+ max_iterations: 5
133
+ )
134
+ ```
135
+
136
+ **CodeAct**: Dynamic code generation (requires `dspy-code_act` gem)
137
+ ```ruby
138
+ predictor = DSPy::CodeAct.new(TaskSignature)
139
+ result = predictor.forward(task: "Calculate factorial of 5")
140
+ ```
141
+
142
+ **When to use each**:
143
+ - **Predict**: Simple tasks, classification, extraction
144
+ - **ChainOfThought**: Complex reasoning, analysis, multi-step thinking
145
+ - **ReAct**: Tasks requiring external tools (search, calculation, API calls)
146
+ - **CodeAct**: Tasks best solved with generated code
147
+
148
+ **Full documentation**: See `references/core-concepts.md` section on Predictors.
149
+
150
+ ### 4. LLM Provider Configuration
151
+
152
+ Support for OpenAI, Google Gemini, Ollama, and OpenRouter.
153
+
154
+ **Quick configuration examples**:
155
+ ```ruby
156
+ # OpenAI
157
+ DSPy.configure do |c|
158
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
159
+ api_key: ENV['OPENAI_API_KEY'])
160
+ end
161
+
162
+ # Google Gemini (Advanced)
163
+ DSPy.configure do |c|
164
+ c.lm = DSPy::LM.new('google/gemini-1.5-pro-latest',
165
+ api_key: ENV['GOOGLE_API_KEY'])
166
+ end
167
+
168
+ # Google Gemini
169
+ DSPy.configure do |c|
170
+ c.lm = DSPy::LM.new('gemini/gemini-1.5-pro',
171
+ api_key: ENV['GOOGLE_API_KEY'])
172
+ end
173
+
174
+ # Local Ollama (free, private)
175
+ DSPy.configure do |c|
176
+ c.lm = DSPy::LM.new('ollama/llama3.1')
177
+ end
178
+ ```
179
+
180
+ **Templates**: See `assets/config-template.rb` for comprehensive examples including:
181
+ - Environment-based configuration
182
+ - Multi-model setups for different tasks
183
+ - Configuration with observability (OpenTelemetry, Langfuse)
184
+ - Retry logic and fallback strategies
185
+ - Budget tracking
186
+ - Rails initializer patterns
187
+
188
+ **Provider compatibility matrix**:
189
+
190
+ | Feature | OpenAI | Google Gemini | Ollama |
191
+ |---------|--------|-----------|--------|
192
+ | Structured Output | ✅ | ✅ | ✅ |
193
+ | Vision (Images) | ✅ | ✅ | ⚠️ Limited |
194
+ | Image URLs | ✅ | ❌ | ❌ |
195
+ | Tool Calling | ✅ | ✅ | Varies |
196
+
197
+ **Cost optimization strategy**:
198
+ - Development: Ollama (free) or gpt-4o-mini (cheap)
199
+ - Testing: gpt-4o-mini with temperature=0.0
200
+ - Production simple tasks: gpt-4o-mini, gemini-flash, gemini-1.5-flash
201
+ - Production complex tasks: gpt-4o, gemini-1.5-pro, gemini-1.5-pro
202
+
203
+ **Full documentation**: See `references/providers.md` for all configuration options, provider-specific features, and troubleshooting.
204
+
205
+ ### 5. Multimodal & Vision Support
206
+
207
+ Process images alongside text using the unified `DSPy::Image` interface.
208
+
209
+ **Quick reference**:
210
+ ```ruby
211
+ class VisionSignature < DSPy::Signature
212
+ description "Analyze image and answer questions"
213
+
214
+ input do
215
+ const :image, DSPy::Image
216
+ const :question, String
217
+ end
218
+
219
+ output do
220
+ const :answer, String
221
+ end
222
+ end
223
+
224
+ predictor = DSPy::Predict.new(VisionSignature)
225
+ result = predictor.forward(
226
+ image: DSPy::Image.from_file("path/to/image.jpg"),
227
+ question: "What objects are visible?"
228
+ )
229
+ ```
230
+
231
+ **Image loading methods**:
232
+ ```ruby
233
+ # From file
234
+ DSPy::Image.from_file("path/to/image.jpg")
235
+
236
+ # From URL (OpenAI only)
237
+ DSPy::Image.from_url("https://example.com/image.jpg")
238
+
239
+ # From base64
240
+ DSPy::Image.from_base64(base64_data, mime_type: "image/jpeg")
241
+ ```
242
+
243
+ **Provider support**:
244
+ - OpenAI: Full support including URLs
245
+ - Google Gemini: Base64 or file loading only
246
+ - Ollama: Limited multimodal depending on model
247
+
248
+ **Full documentation**: See `references/core-concepts.md` section on Multimodal Support.
249
+
250
+ ### 6. Testing LLM Applications
251
+
252
+ Write standard RSpec tests for LLM logic.
253
+
254
+ **Quick reference**:
255
+ ```ruby
256
+ RSpec.describe EmailClassifier do
257
+ before do
258
+ DSPy.configure do |c|
259
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
260
+ api_key: ENV['OPENAI_API_KEY'])
261
+ end
262
+ end
263
+
264
+ it 'classifies technical emails correctly' do
265
+ classifier = EmailClassifier.new
266
+ result = classifier.forward(
267
+ email_subject: "Can't log in",
268
+ email_body: "Unable to access account"
269
+ )
270
+
271
+ expect(result[:category]).to eq('Technical')
272
+ expect(result[:priority]).to be_in(['High', 'Medium', 'Low'])
273
+ end
274
+ end
275
+ ```
276
+
277
+ **Testing patterns**:
278
+ - Mock LLM responses for unit tests
279
+ - Use VCR for deterministic API testing
280
+ - Test type safety and validation
281
+ - Test edge cases (empty inputs, special characters, long texts)
282
+ - Integration test complete workflows
283
+
284
+ **Full documentation**: See `references/optimization.md` section on Testing.
285
+
286
+ ### 7. Optimization & Improvement
287
+
288
+ Automatically improve prompts and modules using optimization techniques.
289
+
290
+ **MIPROv2 optimization**:
291
+ ```ruby
292
+ require 'dspy/mipro'
293
+
294
+ # Define evaluation metric
295
+ def accuracy_metric(example, prediction)
296
+ example[:expected_output][:category] == prediction[:category] ? 1.0 : 0.0
297
+ end
298
+
299
+ # Prepare training data
300
+ training_examples = [
301
+ {
302
+ input: { email_subject: "...", email_body: "..." },
303
+ expected_output: { category: 'Technical' }
304
+ },
305
+ # More examples...
306
+ ]
307
+
308
+ # Run optimization
309
+ optimizer = DSPy::MIPROv2.new(
310
+ metric: method(:accuracy_metric),
311
+ num_candidates: 10
312
+ )
313
+
314
+ optimized_module = optimizer.compile(
315
+ EmailClassifier.new,
316
+ trainset: training_examples
317
+ )
318
+ ```
319
+
320
+ **A/B testing different approaches**:
321
+ ```ruby
322
+ # Test ChainOfThought vs ReAct
323
+ approach_a_score = evaluate_approach(ChainOfThoughtModule, test_set)
324
+ approach_b_score = evaluate_approach(ReActModule, test_set)
325
+ ```
326
+
327
+ **Full documentation**: See `references/optimization.md` section on Optimization.
328
+
329
+ ### 8. Observability & Monitoring
330
+
331
+ Track performance, token usage, and behavior in production.
332
+
333
+ **OpenTelemetry integration**:
334
+ ```ruby
335
+ require 'opentelemetry/sdk'
336
+
337
+ OpenTelemetry::SDK.configure do |c|
338
+ c.service_name = 'my-dspy-app'
339
+ c.use_all
340
+ end
341
+
342
+ # DSPy automatically creates traces
343
+ ```
344
+
345
+ **Langfuse tracing**:
346
+ ```ruby
347
+ DSPy.configure do |c|
348
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
349
+ api_key: ENV['OPENAI_API_KEY'])
350
+
351
+ c.langfuse = {
352
+ public_key: ENV['LANGFUSE_PUBLIC_KEY'],
353
+ secret_key: ENV['LANGFUSE_SECRET_KEY']
354
+ }
355
+ end
356
+ ```
357
+
358
+ **Custom monitoring**:
359
+ - Token tracking
360
+ - Performance monitoring
361
+ - Error rate tracking
362
+ - Custom logging
363
+
364
+ **Full documentation**: See `references/optimization.md` section on Observability.
365
+
366
+ ## Quick Start Workflow
367
+
368
+ ### For New Projects
369
+
370
+ 1. **Install DSPy.rb and provider gems**:
371
+ ```bash
372
+ gem install dspy dspy-openai # or dspy-gemini
373
+ ```
374
+
375
+ 2. **Configure LLM provider** (see `assets/config-template.rb`):
376
+ ```ruby
377
+ require 'dspy'
378
+
379
+ DSPy.configure do |c|
380
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
381
+ api_key: ENV['OPENAI_API_KEY'])
382
+ end
383
+ ```
384
+
385
+ 3. **Create a signature** (see `assets/signature-template.rb`):
386
+ ```ruby
387
+ class MySignature < DSPy::Signature
388
+ description "Clear description of task"
389
+
390
+ input do
391
+ const :input_field, String, desc: "Description"
392
+ end
393
+
394
+ output do
395
+ const :output_field, String, desc: "Description"
396
+ end
397
+ end
398
+ ```
399
+
400
+ 4. **Create a module** (see `assets/module-template.rb`):
401
+ ```ruby
402
+ class MyModule < DSPy::Module
403
+ def initialize
404
+ super
405
+ @predictor = DSPy::Predict.new(MySignature)
406
+ end
407
+
408
+ def forward(input_field:)
409
+ @predictor.forward(input_field: input_field)
410
+ end
411
+ end
412
+ ```
413
+
414
+ 5. **Use the module**:
415
+ ```ruby
416
+ module_instance = MyModule.new
417
+ result = module_instance.forward(input_field: "test")
418
+ puts result[:output_field]
419
+ ```
420
+
421
+ 6. **Add tests** (see `references/optimization.md`):
422
+ ```ruby
423
+ RSpec.describe MyModule do
424
+ it 'produces expected output' do
425
+ result = MyModule.new.forward(input_field: "test")
426
+ expect(result[:output_field]).to be_a(String)
427
+ end
428
+ end
429
+ ```
430
+
431
+ ### For Rails Applications
432
+
433
+ 1. **Add to Gemfile**:
434
+ ```ruby
435
+ gem 'dspy'
436
+ gem 'dspy-openai' # or other provider
437
+ ```
438
+
439
+ 2. **Create initializer** at `config/initializers/dspy.rb` (see `assets/config-template.rb` for full example):
440
+ ```ruby
441
+ require 'dspy'
442
+
443
+ DSPy.configure do |c|
444
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
445
+ api_key: ENV['OPENAI_API_KEY'])
446
+ end
447
+ ```
448
+
449
+ 3. **Create modules in** `app/llm/` directory:
450
+ ```ruby
451
+ # app/llm/email_classifier.rb
452
+ class EmailClassifier < DSPy::Module
453
+ # Implementation here
454
+ end
455
+ ```
456
+
457
+ 4. **Use in controllers/services**:
458
+ ```ruby
459
+ class EmailsController < ApplicationController
460
+ def classify
461
+ classifier = EmailClassifier.new
462
+ result = classifier.forward(
463
+ email_subject: params[:subject],
464
+ email_body: params[:body]
465
+ )
466
+ render json: result
467
+ end
468
+ end
469
+ ```
470
+
471
+ ## Common Patterns
472
+
473
+ ### Pattern: Multi-Step Analysis Pipeline
474
+
475
+ ```ruby
476
+ class AnalysisPipeline < DSPy::Module
477
+ def initialize
478
+ super
479
+ @extract = DSPy::Predict.new(ExtractSignature)
480
+ @analyze = DSPy::ChainOfThought.new(AnalyzeSignature)
481
+ @summarize = DSPy::Predict.new(SummarizeSignature)
482
+ end
483
+
484
+ def forward(text:)
485
+ extracted = @extract.forward(text: text)
486
+ analyzed = @analyze.forward(data: extracted[:data])
487
+ @summarize.forward(analysis: analyzed[:result])
488
+ end
489
+ end
490
+ ```
491
+
492
+ ### Pattern: Agent with Tools
493
+
494
+ ```ruby
495
+ class ResearchAgent < DSPy::Module
496
+ def initialize
497
+ super
498
+ @agent = DSPy::ReAct.new(
499
+ ResearchSignature,
500
+ tools: [
501
+ WebSearchTool.new,
502
+ DatabaseQueryTool.new,
503
+ SummarizerTool.new
504
+ ],
505
+ max_iterations: 10
506
+ )
507
+ end
508
+
509
+ def forward(question:)
510
+ @agent.forward(question: question)
511
+ end
512
+ end
513
+
514
+ class WebSearchTool < DSPy::Tool
515
+ def call(query:)
516
+ results = perform_search(query)
517
+ { results: results }
518
+ end
519
+ end
520
+ ```
521
+
522
+ ### Pattern: Conditional Routing
523
+
524
+ ```ruby
525
+ class SmartRouter < DSPy::Module
526
+ def initialize
527
+ super
528
+ @classifier = DSPy::Predict.new(ClassifySignature)
529
+ @simple_handler = SimpleModule.new
530
+ @complex_handler = ComplexModule.new
531
+ end
532
+
533
+ def forward(input:)
534
+ classification = @classifier.forward(text: input)
535
+
536
+ if classification[:complexity] == 'Simple'
537
+ @simple_handler.forward(input: input)
538
+ else
539
+ @complex_handler.forward(input: input)
540
+ end
541
+ end
542
+ end
543
+ ```
544
+
545
+ ### Pattern: Retry with Fallback
546
+
547
+ ```ruby
548
+ class RobustModule < DSPy::Module
549
+ MAX_RETRIES = 3
550
+
551
+ def forward(input, retry_count: 0)
552
+ begin
553
+ @predictor.forward(input)
554
+ rescue DSPy::ValidationError => e
555
+ if retry_count < MAX_RETRIES
556
+ sleep(2 ** retry_count)
557
+ forward(input, retry_count: retry_count + 1)
558
+ else
559
+ # Fallback to default or raise
560
+ raise
561
+ end
562
+ end
563
+ end
564
+ end
565
+ ```
566
+
567
+ ## Resources
568
+
569
+ This skill includes comprehensive reference materials and templates:
570
+
571
+ ### References (load as needed for detailed information)
572
+
573
+ - [core-concepts.md](./references/core-concepts.md): Complete guide to signatures, modules, predictors, multimodal support, and best practices
574
+ - [providers.md](./references/providers.md): All LLM provider configurations, compatibility matrix, cost optimization, and troubleshooting
575
+ - [optimization.md](./references/optimization.md): Testing patterns, optimization techniques, observability setup, and monitoring
576
+
577
+ ### Assets (templates for quick starts)
578
+
579
+ - [signature-template.rb](./assets/signature-template.rb): Examples of signatures including basic, vision, sentiment analysis, and code generation
580
+ - [module-template.rb](./assets/module-template.rb): Module patterns including pipelines, agents, error handling, caching, and state management
581
+ - [config-template.rb](./assets/config-template.rb): Configuration examples for all providers, environments, observability, and production patterns
582
+
583
+ ## When to Use This Skill
584
+
585
+ Trigger this skill when:
586
+ - Implementing LLM-powered features in Ruby applications
587
+ - Creating type-safe interfaces for AI operations
588
+ - Building agent systems with tool usage
589
+ - Setting up or troubleshooting LLM providers
590
+ - Optimizing prompts and improving accuracy
591
+ - Testing LLM functionality
592
+ - Adding observability to AI applications
593
+ - Converting from manual prompt engineering to programmatic approach
594
+ - Debugging DSPy.rb code or configuration issues