Module: Agentic::Capabilities::Examples

Defined in:
lib/agentic/capabilities/examples.rb

Overview

Example capabilities for common tasks

Class Method Summary collapse

Class Method Details

.register_allvoid

This method returns an undefined value.

Register all example capabilities



14
15
16
17
18
19
20
21
22
# File 'lib/agentic/capabilities/examples.rb', line 14

def register_all
  register_text_generation
  register_web_search
  register_data_analysis
  register_code_generation
  register_summarization
  register_brainstorming
  register_structured_extraction
end

.register_brainstormingCapabilitySpecification

Register a brainstorming capability

Returns:



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/agentic/capabilities/examples.rb', line 339

def register_brainstorming
  spec = CapabilitySpecification.new(
    name: "brainstorming",
    description: "Generates creative ideas for a topic",
    version: "1.0.0",
    inputs: {
      topic: {
        type: "string",
        required: true,
        description: "The topic to brainstorm about"
      },
      num_ideas: {
        type: "integer",
        description: "Number of ideas to generate"
      },
      creativity: {
        type: "number",
        description: "Creativity level (0.0-1.0)"
      }
    },
    outputs: {
      ideas: {
        type: "array",
        required: true,
        description: "The generated ideas"
      },
      themes: {
        type: "array",
        description: "Common themes across the ideas"
      }
    },
    dependencies: [
      {name: "text_generation", version: "1.0.0"}
    ]
  )

  provider = CapabilityProvider.new(
    capability: spec,
    implementation: lambda do |inputs|
      topic = inputs[:topic]
      num_ideas = inputs[:num_ideas] || 10
      creativity = inputs[:creativity] || 0.7

      # Get the text generation capability
      text_gen_provider = registry.get_provider("text_generation")

      # Generate ideas
      ideas_prompt = "Brainstorm #{num_ideas} creative ideas about: #{topic}"
      ideas_response = text_gen_provider.execute(
        prompt: ideas_prompt,
        temperature: creativity
      )[:response]

      # Parse ideas
      ideas = ideas_response.split("\n").map { |line| line.sub(/^\d+\.\s*/, "") }.map(&:strip).reject(&:empty?)

      # Identify themes
      themes_prompt = "Identify 3-5 common themes in the following ideas:\n\n#{ideas.join("\n")}"
      themes_response = text_gen_provider.execute(prompt: themes_prompt)[:response]
      themes = themes_response.split("\n").map(&:strip).reject(&:empty?)

      {
        ideas: ideas,
        themes: themes
      }
    end
  )

  registry.register(spec, provider)
end

.register_code_generationCapabilitySpecification

Register a code generation capability

Returns:



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/agentic/capabilities/examples.rb', line 200

def register_code_generation
  spec = CapabilitySpecification.new(
    name: "code_generation",
    description: "Generates code based on requirements",
    version: "1.0.0",
    inputs: {
      requirements: {
        type: "string",
        required: true,
        description: "The code requirements"
      },
      language: {
        type: "string",
        required: true,
        description: "The programming language"
      },
      include_comments: {
        type: "boolean",
        description: "Whether to include comments in the code"
      }
    },
    outputs: {
      code: {
        type: "string",
        required: true,
        description: "The generated code"
      },
      explanation: {
        type: "string",
        description: "Explanation of the code"
      }
    },
    dependencies: [
      {name: "text_generation", version: "1.0.0"}
    ]
  )

  provider = CapabilityProvider.new(
    capability: spec,
    implementation: lambda do |inputs|
      requirements = inputs[:requirements]
      language = inputs[:language]
      include_comments = inputs[:include_comments] || false

      # Get the text generation capability
      text_gen_provider = registry.get_provider("text_generation")

      # Generate code
      comments_instruction = include_comments ? "Include detailed comments." : "Keep comments minimal."
      code_prompt = "Generate #{language} code for the following requirements:\n\n#{requirements}\n\n#{comments_instruction}"
      code = text_gen_provider.execute(prompt: code_prompt)[:response]

      # Generate explanation if needed
      explanation = nil
      if include_comments
        explanation_prompt = "Explain the following #{language} code:\n\n#{code}"
        explanation = text_gen_provider.execute(prompt: explanation_prompt)[:response]
      end

      result = {code: code}
      result[:explanation] = explanation if explanation

      result
    end
  )

  registry.register(spec, provider)
end

.register_data_analysisCapabilitySpecification

Register a data analysis capability

Returns:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/agentic/capabilities/examples.rb', line 135

def register_data_analysis
  spec = CapabilitySpecification.new(
    name: "data_analysis",
    description: "Analyzes data and extracts insights",
    version: "1.0.0",
    inputs: {
      data: {
        type: "object",
        required: true,
        description: "The data to analyze"
      },
      analysis_type: {
        type: "string",
        description: "The type of analysis to perform"
      }
    },
    outputs: {
      insights: {
        type: "array",
        required: true,
        description: "The extracted insights"
      },
      summary: {
        type: "string",
        required: true,
        description: "A summary of the analysis"
      }
    },
    dependencies: [
      {name: "text_generation", version: "1.0.0"}
    ]
  )

  provider = CapabilityProvider.new(
    capability: spec,
    implementation: lambda do |inputs|
      data = inputs[:data]
      analysis_type = inputs[:analysis_type] || "basic"

      # Get the text generation capability
      text_gen_provider = registry.get_provider("text_generation")

      # Generate insights based on the data
      insights_prompt = "Analyze the following data using #{analysis_type} analysis and provide key insights:\n\n#{data.inspect}"
      insights_response = text_gen_provider.execute(prompt: insights_prompt)[:response]

      # Parse insights
      insights = insights_response.split("\n").map(&:strip).reject(&:empty?)

      # Generate summary
      summary_prompt = "Summarize the following insights in one paragraph:\n\n#{insights.join("\n")}"
      summary = text_gen_provider.execute(prompt: summary_prompt)[:response]

      {
        insights: insights,
        summary: summary
      }
    end
  )

  registry.register(spec, provider)
end

.register_structured_extractionCapabilitySpecification

Register a structured extraction capability

Returns:



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/agentic/capabilities/examples.rb', line 412

def register_structured_extraction
  spec = CapabilitySpecification.new(
    name: "structured_extraction",
    description: "Extracts structured data from text",
    version: "1.0.0",
    inputs: {
      content: {
        type: "string",
        required: true,
        description: "The content to extract from"
      },
      schema: {
        type: "object",
        required: true,
        description: "The schema to extract"
      }
    },
    outputs: {
      data: {
        type: "object",
        required: true,
        description: "The extracted data"
      },
      confidence: {
        type: "number",
        description: "Confidence score for the extraction"
      }
    },
    dependencies: [
      {name: "text_generation", version: "1.0.0"}
    ]
  )

  provider = CapabilityProvider.new(
    capability: spec,
    implementation: lambda do |inputs|
      content = inputs[:content]
      schema = inputs[:schema]

      # Get the text generation capability
      text_gen_provider = registry.get_provider("text_generation")

      # Create extraction prompt
      schema_str = JSON.pretty_generate(schema)
      extraction_prompt = "      Extract structured data from the following content according to this schema:\n      \n      \#{schema_str}\n      \n      Content:\n      \#{content}\n      \n      Return the data as valid JSON.\n      PROMPT\n\n      # Generate extraction\n      extraction_response = text_gen_provider.execute(prompt: extraction_prompt)[:response]\n\n      # Parse JSON (with error handling)\n      data = nil\n      begin\n        # Clean the response to ensure it's valid JSON\n        json_str = extraction_response.gsub(/```json|```/, \"\").strip\n        data = JSON.parse(json_str)\n      rescue JSON::ParserError\n        # Fallback extraction for malformed JSON\n        data = extract_fallback(extraction_response)\n      end\n\n      # Calculate confidence based on schema match\n      confidence = calculate_confidence(data, schema)\n\n      {\n        data: data,\n        confidence: confidence\n      }\n    end\n  )\n\n  registry.register(spec, provider)\nend\n"

.register_summarizationCapabilitySpecification

Register a summarization capability

Returns:



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/agentic/capabilities/examples.rb', line 271

def register_summarization
  spec = CapabilitySpecification.new(
    name: "summarization",
    description: "Summarizes text content",
    version: "1.0.0",
    inputs: {
      content: {
        type: "string",
        required: true,
        description: "The content to summarize"
      },
      max_length: {
        type: "integer",
        description: "Maximum length of the summary"
      },
      format: {
        type: "string",
        description: "Format of the summary (paragraph, bullets, etc.)"
      }
    },
    outputs: {
      summary: {
        type: "string",
        required: true,
        description: "The generated summary"
      },
      key_points: {
        type: "array",
        description: "Key points from the content"
      }
    },
    dependencies: [
      {name: "text_generation", version: "1.0.0"}
    ]
  )

  provider = CapabilityProvider.new(
    capability: spec,
    implementation: lambda do |inputs|
      content = inputs[:content]
      max_length = inputs[:max_length]
      format = inputs[:format] || "paragraph"

      # Get the text generation capability
      text_gen_provider = registry.get_provider("text_generation")

      # Generate summary
      length_instruction = max_length ? "Keep the summary under #{max_length} words." : ""
      summary_prompt = "Summarize the following content in #{format} format. #{length_instruction}\n\n#{content}"
      summary = text_gen_provider.execute(prompt: summary_prompt)[:response]

      # Extract key points
      key_points_prompt = "Extract 3-5 key points from the following content:\n\n#{content}"
      key_points_response = text_gen_provider.execute(prompt: key_points_prompt)[:response]
      key_points = key_points_response.split("\n").map(&:strip).reject(&:empty?)

      {
        summary: summary,
        key_points: key_points
      }
    end
  )

  registry.register(spec, provider)
end

.register_text_generationCapabilitySpecification

Register a text generation capability

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/agentic/capabilities/examples.rb', line 26

def register_text_generation
  spec = CapabilitySpecification.new(
    name: "text_generation",
    description: "Generates text based on a prompt",
    version: "1.0.0",
    inputs: {
      prompt: {
        type: "string",
        required: true,
        description: "The prompt to generate text from"
      },
      max_tokens: {
        type: "integer",
        description: "Maximum number of tokens to generate"
      },
      temperature: {
        type: "number",
        description: "Sampling temperature (0.0-1.0)"
      }
    },
    outputs: {
      response: {
        type: "string",
        required: true,
        description: "The generated text"
      }
    }
  )

  provider = CapabilityProvider.new(
    capability: spec,
    implementation: lambda do |inputs|
      # Get the LLM client
      llm_config = LlmConfig.new
      llm_config.max_tokens = inputs[:max_tokens] if inputs[:max_tokens]
      llm_config.temperature = inputs[:temperature] if inputs[:temperature]

      client = Agentic.client(llm_config)

      # Generate text
      response = client.complete(prompt: inputs[:prompt])

      {response: response.to_s}
    end
  )

  registry.register(spec, provider)
end

.register_web_searchCapabilitySpecification

Register a web search capability

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/agentic/capabilities/examples.rb', line 77

def register_web_search
  spec = CapabilitySpecification.new(
    name: "web_search",
    description: "Searches the web for information",
    version: "1.0.0",
    inputs: {
      query: {
        type: "string",
        required: true,
        description: "The search query"
      },
      num_results: {
        type: "integer",
        description: "Number of results to return"
      }
    },
    outputs: {
      results: {
        type: "array",
        required: true,
        description: "The search results"
      },
      sources: {
        type: "array",
        description: "The sources of the results"
      }
    }
  )

  provider = CapabilityProvider.new(
    capability: spec,
    implementation: lambda do |inputs|
      # This is a mock implementation
      # In a real implementation, you would use a search API or web scraping

      query = inputs[:query]
      num_results = inputs[:num_results] || 3

      results = num_results.times.map do |i|
        "Result #{i + 1} for query: #{query}"
      end

      sources = num_results.times.map do |i|
        "https://example.com/result#{i + 1}"
      end

      {
        results: results,
        sources: sources
      }
    end
  )

  registry.register(spec, provider)
end