kiln-ai 0.5.5__tar.gz → 0.6.1__tar.gz

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.

Potentially problematic release.


This version of kiln-ai might be problematic. Click here for more details.

Files changed (74) hide show
  1. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/PKG-INFO +1 -2
  2. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/__init__.py +9 -1
  3. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/base_adapter.py +24 -35
  4. kiln_ai-0.6.1/kiln_ai/adapters/data_gen/__init__.py +11 -0
  5. kiln_ai-0.6.1/kiln_ai/adapters/data_gen/data_gen_prompts.py +73 -0
  6. kiln_ai-0.6.1/kiln_ai/adapters/data_gen/data_gen_task.py +185 -0
  7. kiln_ai-0.6.1/kiln_ai/adapters/data_gen/test_data_gen_task.py +293 -0
  8. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/langchain_adapters.py +39 -7
  9. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/ml_model_list.py +55 -1
  10. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/prompt_builders.py +66 -0
  11. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/repair/test_repair_task.py +4 -1
  12. kiln_ai-0.6.1/kiln_ai/adapters/test_langchain_adapter.py +124 -0
  13. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/test_ml_model_list.py +56 -0
  14. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/test_prompt_adaptors.py +52 -18
  15. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/test_prompt_builders.py +97 -7
  16. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/test_saving_adapter_results.py +16 -6
  17. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/test_structured_output.py +33 -5
  18. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/datamodel/__init__.py +28 -7
  19. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/datamodel/json_schema.py +1 -0
  20. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/datamodel/test_models.py +44 -8
  21. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/utils/config.py +3 -2
  22. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/utils/test_config.py +7 -0
  23. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/pyproject.toml +1 -1
  24. kiln_ai-0.5.5/kiln_ai/adapters/test_langchain_adapter.py +0 -51
  25. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/.gitignore +0 -0
  26. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/.python-version +0 -0
  27. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/LICENSE.txt +0 -0
  28. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/README.md +0 -0
  29. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/index.html +0 -0
  30. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/adapters/base_adapter.html +0 -0
  31. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/adapters/langchain_adapters.html +0 -0
  32. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/adapters/ml_model_list.html +0 -0
  33. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/adapters/prompt_builders.html +0 -0
  34. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/adapters/repair/repair_task.html +0 -0
  35. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/adapters/repair.html +0 -0
  36. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/adapters.html +0 -0
  37. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/datamodel/basemodel.html +0 -0
  38. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/datamodel/json_schema.html +0 -0
  39. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/datamodel.html +0 -0
  40. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/utils/config.html +0 -0
  41. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/utils/formatting.html +0 -0
  42. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai/utils.html +0 -0
  43. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/kiln_ai.html +0 -0
  44. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/core_library_docs/search.js +0 -0
  45. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/index.html +0 -0
  46. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/adapters/base_adapter.html +0 -0
  47. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/adapters/langchain_adapters.html +0 -0
  48. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/adapters/ml_model_list.html +0 -0
  49. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/adapters/prompt_builders.html +0 -0
  50. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/adapters/repair/repair_task.html +0 -0
  51. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/adapters/repair.html +0 -0
  52. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/adapters.html +0 -0
  53. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/datamodel/basemodel.html +0 -0
  54. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/datamodel/json_schema.html +0 -0
  55. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/datamodel.html +0 -0
  56. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/utils/config.html +0 -0
  57. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/utils/formatting.html +0 -0
  58. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai/utils.html +0 -0
  59. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/kiln_ai.html +0 -0
  60. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/docs/kiln_core_docs/search.js +0 -0
  61. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/__init__.py +0 -0
  62. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/repair/__init__.py +0 -0
  63. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/adapters/repair/repair_task.py +0 -0
  64. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/datamodel/basemodel.py +0 -0
  65. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/datamodel/test_basemodel.py +0 -0
  66. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/datamodel/test_datasource.py +0 -0
  67. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/datamodel/test_example_models.py +0 -0
  68. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/datamodel/test_json_schema.py +0 -0
  69. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/datamodel/test_nested_save.py +0 -0
  70. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/datamodel/test_output_rating.py +0 -0
  71. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/utils/__init__.py +0 -0
  72. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/kiln_ai/utils/formatting.py +0 -0
  73. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/setup.cfg +0 -0
  74. {kiln_ai-0.5.5 → kiln_ai-0.6.1}/uv.lock +0 -0
@@ -1,13 +1,12 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: kiln-ai
3
- Version: 0.5.5
3
+ Version: 0.6.1
4
4
  Summary: Kiln AI
5
5
  Project-URL: Homepage, https://getkiln.ai
6
6
  Project-URL: Repository, https://github.com/Kiln-AI/kiln
7
7
  Project-URL: Documentation, https://kiln-ai.github.io/Kiln/kiln_core_docs/kiln_ai.html
8
8
  Project-URL: Issues, https://github.com/Kiln-AI/kiln/issues
9
9
  Author-email: "Steve Cosman, Chesterfield Laboratories Inc" <scosman@users.noreply.github.com>
10
- License-File: LICENSE.txt
11
10
  Classifier: Intended Audience :: Developers
12
11
  Classifier: License :: OSI Approved :: MIT License
13
12
  Classifier: Programming Language :: Python :: 3.10
@@ -12,7 +12,14 @@ The prompt_builders submodule contains classes that build prompts for use with t
12
12
  The repair submodule contains an adapter for the repair task.
13
13
  """
14
14
 
15
- from . import base_adapter, langchain_adapters, ml_model_list, prompt_builders, repair
15
+ from . import (
16
+ base_adapter,
17
+ data_gen,
18
+ langchain_adapters,
19
+ ml_model_list,
20
+ prompt_builders,
21
+ repair,
22
+ )
16
23
 
17
24
  __all__ = [
18
25
  "base_adapter",
@@ -20,4 +27,5 @@ __all__ = [
20
27
  "ml_model_list",
21
28
  "prompt_builders",
22
29
  "repair",
30
+ "data_gen",
23
31
  ]
@@ -24,6 +24,12 @@ class AdapterInfo:
24
24
  prompt_builder_name: str
25
25
 
26
26
 
27
+ @dataclass
28
+ class RunOutput:
29
+ output: Dict | str
30
+ intermediate_outputs: Dict[str, str] | None
31
+
32
+
27
33
  class BaseAdapter(metaclass=ABCMeta):
28
34
  """Base class for AI model adapters that handle task execution.
29
35
 
@@ -36,22 +42,6 @@ class BaseAdapter(metaclass=ABCMeta):
36
42
  kiln_task (Task): The task configuration and metadata
37
43
  output_schema (dict | None): JSON schema for validating structured outputs
38
44
  input_schema (dict | None): JSON schema for validating structured inputs
39
-
40
- Example:
41
- ```python
42
- class CustomAdapter(BaseAdapter):
43
- async def _run(self, input: Dict | str) -> Dict | str:
44
- # Implementation for specific model
45
- pass
46
-
47
- def adapter_info(self) -> AdapterInfo:
48
- return AdapterInfo(
49
- adapter_name="custom",
50
- model_name="model-1",
51
- model_provider="provider",
52
- prompt_builder_name="simple"
53
- )
54
- ```
55
45
  """
56
46
 
57
47
  def __init__(
@@ -85,21 +75,23 @@ class BaseAdapter(metaclass=ABCMeta):
85
75
  validate_schema(input, self.input_schema)
86
76
 
87
77
  # Run
88
- result = await self._run(input)
78
+ run_output = await self._run(input)
89
79
 
90
80
  # validate output
91
81
  if self.output_schema is not None:
92
- if not isinstance(result, dict):
93
- raise RuntimeError(f"structured response is not a dict: {result}")
94
- validate_schema(result, self.output_schema)
82
+ if not isinstance(run_output.output, dict):
83
+ raise RuntimeError(
84
+ f"structured response is not a dict: {run_output.output}"
85
+ )
86
+ validate_schema(run_output.output, self.output_schema)
95
87
  else:
96
- if not isinstance(result, str):
88
+ if not isinstance(run_output.output, str):
97
89
  raise RuntimeError(
98
- f"response is not a string for non-structured task: {result}"
90
+ f"response is not a string for non-structured task: {run_output.output}"
99
91
  )
100
92
 
101
93
  # Generate the run and output
102
- run = self.generate_run(input, input_source, result)
94
+ run = self.generate_run(input, input_source, run_output)
103
95
 
104
96
  # Save the run if configured to do so, and we have a path to save to
105
97
  if Config.shared().autosave_runs and self.kiln_task.path is not None:
@@ -118,27 +110,23 @@ class BaseAdapter(metaclass=ABCMeta):
118
110
  pass
119
111
 
120
112
  @abstractmethod
121
- async def _run(self, input: Dict | str) -> Dict | str:
113
+ async def _run(self, input: Dict | str) -> RunOutput:
122
114
  pass
123
115
 
124
116
  def build_prompt(self) -> str:
125
- prompt = self.prompt_builder.build_prompt()
126
- adapter_instructions = self.adapter_specific_instructions()
127
- if adapter_instructions is not None:
128
- prompt += f"# Format Instructions\n\n{adapter_instructions}\n\n"
129
- return prompt
130
-
131
- # override for adapter specific instructions (e.g. tool calling, json format, etc)
132
- def adapter_specific_instructions(self) -> str | None:
133
- return None
117
+ return self.prompt_builder.build_prompt()
134
118
 
135
119
  # create a run and task output
136
120
  def generate_run(
137
- self, input: Dict | str, input_source: DataSource | None, output: Dict | str
121
+ self, input: Dict | str, input_source: DataSource | None, run_output: RunOutput
138
122
  ) -> TaskRun:
139
123
  # Convert input and output to JSON strings if they are dictionaries
140
124
  input_str = json.dumps(input) if isinstance(input, dict) else input
141
- output_str = json.dumps(output) if isinstance(output, dict) else output
125
+ output_str = (
126
+ json.dumps(run_output.output)
127
+ if isinstance(run_output.output, dict)
128
+ else run_output.output
129
+ )
142
130
 
143
131
  # If no input source is provided, use the human data source
144
132
  if input_source is None:
@@ -159,6 +147,7 @@ class BaseAdapter(metaclass=ABCMeta):
159
147
  properties=self._properties_for_task_output(),
160
148
  ),
161
149
  ),
150
+ intermediate_outputs=run_output.intermediate_outputs,
162
151
  )
163
152
 
164
153
  exclude_fields = {
@@ -0,0 +1,11 @@
1
+ """
2
+ # Data Generation
3
+
4
+ A task to generate synthetic data for Kiln Tasks. This generates the inputs, which then can be run through the task.
5
+
6
+ Optional human guidance can be provided to guide the generation process.
7
+ """
8
+
9
+ from . import data_gen_task
10
+
11
+ __all__ = ["data_gen_task"]
@@ -0,0 +1,73 @@
1
+ # The contents of this file are adapted from the promptwrite library (https://github.com/StacklokLabs/promptwright),
2
+ # which was adapted from the pluto library (https://github.com/redotvideo/pluto).
3
+ # These libraries are licensed under the Apache License 2.0. Any modifications
4
+ # are licensed under the kiln AI Core license (MIT at time of writing). See /libs/core/LICENSE.txt for details.
5
+
6
+
7
+ TREE_GENERATION_PROMPT = """I want to train a large language model and I am using another, bigger large language model to generate training data for this. However, if we always ask the bigger model to generate training data with the same prompt, it will end up generating very repetitive training samples. Therefore, we will slightly modify our prompt for each sampling procedure according to some aspects. For instance, when asking the model to generate news articles, we could modify the prompt to let the model tell news articles about particular topics, such as business or politics. To further generate training data, we will do this recursively, and generate submodifications to the prompt. For instance, within the domain of business, we could adapt the prompt to generate news about the stock market or business scandals, and within politics, we could ask the model to generate articles for subtopics like elections or climate policy. We do this recursively, and therefore, we get a tree-like structure of topics.
8
+ Your job is the following: I will give you a path of nodes down the topic tree - you should then come up with a list of new subtopics for this given node and return it as a python list. Here are a few examples of what your outputs should look like, related to the news example I just gave you:
9
+
10
+ Example 1:
11
+ node path: "News Topics" -> "Sports" -> "Football"
12
+ desired number of subtopics: 5
13
+ subtopics: ["College Football", "Football Stadiums", "Health Consequences Football", "Seattle Seahawks", "Football Sponsorships"]
14
+
15
+ Example 2:
16
+ node path: "News Topics" -> "Entertainment" -> "Movies" -> "Star Portraits"
17
+ desired number of subtopics: 8
18
+ subtopics: ["Tom Hanks", "Meryl Streep", "Leonardo DiCaprio", "Jennifer Lawrence", "Denzel Washington", "Charlize Theron", "Robert Downey Jr.", "Emma Stone"]
19
+
20
+
21
+ Here are three new examples, this time for generating smalltalk topics for a friendly chat assistant:
22
+
23
+ Example 1:
24
+ node path: "Small Talk Topics"
25
+ desired number of subtopics: 7
26
+ subtopics: ["Weather", "Weekend Plans", "Hobbies", "Family", "Books", "Food", "Music"]
27
+
28
+ Example 2:
29
+ node path: "Small Talk Topics" -> "Family"
30
+ desired number of subtopics: 5
31
+ subtopics: ["Parents", "Grandparents", "Siblings", "Family Traditions", "Family Vacations"]
32
+
33
+ Example 3:
34
+ node path: "Small Talk Topics" -> "Hobbies" -> "Cooking"
35
+ desired number of subtopics: 6
36
+ subtopics: ["Recipes", "Asian Food", "Favourite Dishes", "Cookbooks", "Kitchen Gadgets", "Vegan Cooking"]
37
+
38
+ The user message will contain the following:
39
+ - The system prompt for the model we want to train as system_prompt.
40
+ - The node path as node_path. It will be formated as a list of strings from most general to most specific. For example, the node_path for Example 3 above would be ["Small Talk Topics", "Hobbies", "Cooking"]. If empty, the node path is the root node.
41
+ - The desired number of subtopics for this node as num_subtopics. Return exactly this number of subtopics.
42
+ - Optionally, it may contain human_guidance, which is a string that contains additional instructions for how to generate the subtopics.
43
+ - Optionally, it may contain existing_topics, which is a list of subtopics that already exist at this node. You should not generate subtopics that are in this list.
44
+
45
+
46
+ When generating subtopics, remain somewhat vague. Things can only be tangentially related and they don't have to be interpreted in a single way. Importantly, make sure that the subtopics fit the system prompt.
47
+ """
48
+
49
+
50
+ SAMPLE_GENERATION_PROMPT = """I want to train a large language model and you should help me generate training data for it.
51
+
52
+ Your job is to generate a list of potential inputs to the provided system prompt. They should be diverse and relevant to the system prompt, and the topic if provided.
53
+
54
+ In the user message we'll provide the following:
55
+ - The system prompt as system_prompt
56
+ - A potential topic to generate samples for. This will be a list of strings from most general to most specific. For example, the topic ["Small Talk Topics", "Hobbies", "Cooking"] would represent the topic "Cooking" in the "Hobbies" category of "Small Talk Topics". The list may be empty, in which case you should generate samples using the system prompt alone.
57
+ - The number of samples to generate as num_samples. If greater than 1, generate a range of samples that are diverse and relevant to the system prompt, and the topic if provided.
58
+ - The user message may optionally contain human_guidance, which is a string that contains additional instructions for how to generate the samples.
59
+
60
+ The output must be formatted:
61
+ - in the provided structured format, as an object with a single property "generated_samples" that maps to a list of generated samples that would be inputs to the provided system prompt.
62
+ - With the correct number of samples (num_samples).
63
+ - Do not include any other text or break the schema in any way.
64
+
65
+ Example inputs:
66
+ - system_prompt: "You are an assistant that classifies the tone of a tweet. You should output one of the following labels: 'positive', 'negative', 'neutral'."
67
+ - topic: ["Technology", "New iPhone Event"]
68
+ - num_samples: 2
69
+ Example output: {"generated_samples": ["New iPhone looks amazing! I need that camera.", "Another boring event from Apple.", "New iPhone looks interesting, but I'm waiting for reviews."]}
70
+
71
+
72
+ Note how the output of this task is data to input to the system prompt, not the expected output of the system prompt.
73
+ """
@@ -0,0 +1,185 @@
1
+ import json
2
+
3
+ from pydantic import BaseModel
4
+
5
+ from kiln_ai.adapters.prompt_builders import SimplePromptBuilder
6
+ from kiln_ai.datamodel import Project, Task
7
+
8
+ from .data_gen_prompts import (
9
+ SAMPLE_GENERATION_PROMPT,
10
+ TREE_GENERATION_PROMPT,
11
+ )
12
+
13
+
14
+ class DataGenCategoriesTaskInput(BaseModel):
15
+ """Input model for generating categories/subtopics.
16
+
17
+ Attributes:
18
+ node_path: List of strings representing the hierarchical path to current node
19
+ system_prompt: System prompt to guide the AI generation
20
+ num_subtopics: Number of subtopics to generate
21
+ human_guidance: Optional human guidance to influence generation
22
+ existing_topics: Optional list of existing topics to avoid duplication
23
+ """
24
+
25
+ node_path: list[str]
26
+ system_prompt: str
27
+ num_subtopics: int
28
+ human_guidance: str | None = None
29
+ existing_topics: list[str] | None = None
30
+
31
+ @classmethod
32
+ def from_task(
33
+ cls,
34
+ task: Task,
35
+ node_path: list[str] = [],
36
+ num_subtopics: int = 6,
37
+ human_guidance: str | None = None,
38
+ existing_topics: list[str] | None = None,
39
+ ) -> "DataGenCategoriesTaskInput":
40
+ """Create a DataGenCategoriesTaskInput instance from a Task.
41
+
42
+ Args:
43
+ task: The source Task object
44
+ node_path: Path to current node in topic hierarchy
45
+ num_subtopics: Number of subtopics to generate
46
+ human_guidance: Optional guidance for generation
47
+ existing_topics: Optional list of existing topics
48
+
49
+ Returns:
50
+ A new DataGenCategoriesTaskInput instance
51
+ """
52
+ prompt_builder = SimplePromptBuilder(task=task)
53
+ return cls(
54
+ node_path=node_path,
55
+ num_subtopics=num_subtopics,
56
+ human_guidance=human_guidance,
57
+ existing_topics=existing_topics,
58
+ system_prompt=prompt_builder.build_prompt(),
59
+ )
60
+
61
+
62
+ class DataGenCategoriesTaskOutput(BaseModel):
63
+ """Output model for generated categories/subtopics.
64
+
65
+ Attributes:
66
+ subtopics: List of generated subtopic strings
67
+ """
68
+
69
+ subtopics: list[str]
70
+
71
+
72
+ class DataGenCategoriesTask(Task, parent_of={}):
73
+ """Task for generating hierarchical categories/subtopics.
74
+
75
+ Generates synthetic data categories which can be used to generate
76
+ training data for model learning.
77
+ """
78
+
79
+ def __init__(self):
80
+ # Keep the typechecker happy. TODO: shouldn't need this or parent_of above.
81
+ tmp_project = Project(name="DataGen")
82
+ super().__init__(
83
+ name="DataGen",
84
+ parent=tmp_project,
85
+ description="A task which generates synthetic data categories, which in turn are used to generate training data for a model to learn from.",
86
+ instruction=TREE_GENERATION_PROMPT,
87
+ input_json_schema=json.dumps(
88
+ DataGenCategoriesTaskInput.model_json_schema()
89
+ ),
90
+ output_json_schema=json.dumps(
91
+ DataGenCategoriesTaskOutput.model_json_schema()
92
+ ),
93
+ )
94
+
95
+
96
+ class DataGenSampleTaskInput(BaseModel):
97
+ """Input model for generating data samples for a kiln task.
98
+
99
+ Attributes:
100
+ topic: List of strings representing the topic path
101
+ system_prompt: System prompt to guide the AI generation
102
+ num_samples: Number of samples to generate
103
+ human_guidance: Optional human guidance to influence generation
104
+ """
105
+
106
+ topic: list[str]
107
+ system_prompt: str
108
+ num_samples: int
109
+ human_guidance: str | None = None
110
+
111
+ @classmethod
112
+ def from_task(
113
+ cls,
114
+ task: Task,
115
+ topic: list[str] = [],
116
+ num_samples: int = 8,
117
+ human_guidance: str | None = None,
118
+ ) -> "DataGenSampleTaskInput":
119
+ """Create a DataGenSampleTaskInput instance from a Task.
120
+
121
+ Args:
122
+ task: The source Task object
123
+ topic: Topic path for sample generation
124
+ num_samples: Number of samples to generate
125
+ human_guidance: Optional guidance for generation
126
+
127
+ Returns:
128
+ A new DataGenSampleTaskInput instance
129
+ """
130
+ prompt_builder = SimplePromptBuilder(task=task)
131
+ return cls(
132
+ topic=topic,
133
+ num_samples=num_samples,
134
+ human_guidance=human_guidance,
135
+ system_prompt=prompt_builder.build_prompt(),
136
+ )
137
+
138
+
139
+ def list_json_schema_for_task(task: Task) -> str:
140
+ """Generate a JSON schema for a list of task inputs (json schema)
141
+
142
+ Args:
143
+ task: Task object whose input schema will be used
144
+
145
+ Returns:
146
+ JSON string representing the schema for a list of task inputs
147
+ """
148
+ if task.input_json_schema:
149
+ items_schema = json.loads(task.input_json_schema)
150
+ else:
151
+ items_schema = {"type": "string"}
152
+
153
+ list_schema = {
154
+ "type": "array",
155
+ "items": items_schema,
156
+ }
157
+
158
+ top_level_schema = {
159
+ "type": "object",
160
+ "properties": {
161
+ "generated_samples": list_schema,
162
+ },
163
+ "required": ["generated_samples"],
164
+ }
165
+
166
+ return json.dumps(top_level_schema)
167
+
168
+
169
+ class DataGenSampleTask(Task, parent_of={}):
170
+ """Task for generating data samples for a given topic.
171
+
172
+ Generates synthetic data samples based on provided topics and subtopics.
173
+ """
174
+
175
+ def __init__(self, target_task: Task, num_samples: int = 8):
176
+ # Keep the typechecker happy. TODO: shouldn't need this or parent_of above.
177
+ tmp_project = Project(name="DataGenSample")
178
+ super().__init__(
179
+ name="DataGenSample",
180
+ parent=tmp_project,
181
+ description="A task which generates synthetic data samples for a given topic (and optional subtopic).",
182
+ instruction=SAMPLE_GENERATION_PROMPT,
183
+ input_json_schema=json.dumps(DataGenSampleTaskInput.model_json_schema()),
184
+ output_json_schema=list_json_schema_for_task(target_task),
185
+ )