kiln-ai 0.5.4__tar.gz → 0.6.0__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.4 → kiln_ai-0.6.0}/PKG-INFO +41 -7
  2. kiln_ai-0.6.0/README.md +58 -0
  3. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/base_adapter.py +24 -35
  4. kiln_ai-0.6.0/kiln_ai/adapters/data_gen/data_gen_prompts.py +73 -0
  5. kiln_ai-0.6.0/kiln_ai/adapters/data_gen/data_gen_task.py +117 -0
  6. kiln_ai-0.6.0/kiln_ai/adapters/data_gen/test_data_gen_task.py +292 -0
  7. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/langchain_adapters.py +39 -7
  8. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/ml_model_list.py +68 -1
  9. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/prompt_builders.py +66 -0
  10. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/repair/test_repair_task.py +4 -1
  11. kiln_ai-0.6.0/kiln_ai/adapters/test_langchain_adapter.py +124 -0
  12. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/test_ml_model_list.py +56 -0
  13. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/test_prompt_adaptors.py +54 -18
  14. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/test_prompt_builders.py +97 -7
  15. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/test_saving_adapter_results.py +16 -6
  16. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/test_structured_output.py +33 -5
  17. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/datamodel/__init__.py +28 -7
  18. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/datamodel/json_schema.py +1 -0
  19. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/datamodel/test_models.py +44 -8
  20. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/utils/config.py +3 -2
  21. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/utils/test_config.py +7 -0
  22. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/pyproject.toml +1 -1
  23. kiln_ai-0.5.4/README.md +0 -23
  24. kiln_ai-0.5.4/kiln_ai/adapters/test_langchain_adapter.py +0 -51
  25. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/.gitignore +0 -0
  26. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/.python-version +0 -0
  27. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/LICENSE.txt +0 -0
  28. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/index.html +0 -0
  29. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/adapters/base_adapter.html +0 -0
  30. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/adapters/langchain_adapters.html +0 -0
  31. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/adapters/ml_model_list.html +0 -0
  32. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/adapters/prompt_builders.html +0 -0
  33. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/adapters/repair/repair_task.html +0 -0
  34. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/adapters/repair.html +0 -0
  35. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/adapters.html +0 -0
  36. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/datamodel/basemodel.html +0 -0
  37. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/datamodel/json_schema.html +0 -0
  38. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/datamodel.html +0 -0
  39. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/utils/config.html +0 -0
  40. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/utils/formatting.html +0 -0
  41. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai/utils.html +0 -0
  42. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/kiln_ai.html +0 -0
  43. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/core_library_docs/search.js +0 -0
  44. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/index.html +0 -0
  45. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/adapters/base_adapter.html +0 -0
  46. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/adapters/langchain_adapters.html +0 -0
  47. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/adapters/ml_model_list.html +0 -0
  48. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/adapters/prompt_builders.html +0 -0
  49. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/adapters/repair/repair_task.html +0 -0
  50. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/adapters/repair.html +0 -0
  51. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/adapters.html +0 -0
  52. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/datamodel/basemodel.html +0 -0
  53. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/datamodel/json_schema.html +0 -0
  54. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/datamodel.html +0 -0
  55. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/utils/config.html +0 -0
  56. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/utils/formatting.html +0 -0
  57. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai/utils.html +0 -0
  58. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/kiln_ai.html +0 -0
  59. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/docs/kiln_core_docs/search.js +0 -0
  60. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/__init__.py +0 -0
  61. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/__init__.py +0 -0
  62. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/repair/__init__.py +0 -0
  63. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/adapters/repair/repair_task.py +0 -0
  64. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/datamodel/basemodel.py +0 -0
  65. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/datamodel/test_basemodel.py +0 -0
  66. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/datamodel/test_datasource.py +0 -0
  67. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/datamodel/test_example_models.py +0 -0
  68. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/datamodel/test_json_schema.py +0 -0
  69. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/datamodel/test_nested_save.py +0 -0
  70. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/datamodel/test_output_rating.py +0 -0
  71. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/utils/__init__.py +0 -0
  72. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/kiln_ai/utils/formatting.py +0 -0
  73. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/setup.cfg +0 -0
  74. {kiln_ai-0.5.4 → kiln_ai-0.6.0}/uv.lock +0 -0
@@ -1,13 +1,12 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: kiln-ai
3
- Version: 0.5.4
3
+ Version: 0.6.0
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
@@ -31,6 +30,12 @@ Description-Content-Type: text/markdown
31
30
 
32
31
  # kiln_ai
33
32
 
33
+ <p align="center">
34
+ <picture>
35
+ <img width="205" alt="Kiln AI Logo" src="https://github.com/user-attachments/assets/5fbcbdf7-1feb-45c9-bd73-99a46dd0a47f">
36
+ </picture>
37
+ </p>
38
+
34
39
  [![PyPI - Version](https://img.shields.io/pypi/v/kiln-ai.svg?logo=pypi&label=PyPI&logoColor=gold)](https://pypi.org/project/kiln-ai)
35
40
  [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/kiln-ai.svg)](https://pypi.org/project/kiln-ai)
36
41
  [![Docs](https://img.shields.io/badge/docs-pdoc-blue)](https://kiln-ai.github.io/Kiln/kiln_core_docs/index.html)
@@ -43,12 +48,41 @@ Description-Content-Type: text/markdown
43
48
  pip install kiln_ai
44
49
  ```
45
50
 
46
- ## About Kiln AI
51
+ ## About
52
+
53
+ This package is the Kiln AI core library. There is also a separate desktop application and server package. Learn more about Kiln AI at [getkiln.ai](https://getkiln.ai)
54
+
55
+ - Github: [github.com/Kiln-AI/kiln](https://github.com/Kiln-AI/kiln)
56
+ - Core Library Docs: [https://kiln-ai.github.io/Kiln/kiln_core_docs/index.html](https://kiln-ai.github.io/Kiln/kiln_core_docs/index.html)
57
+
58
+ ## Quick Start
47
59
 
48
- Learn more about Kiln AI at [getkiln.ai](https://getkiln.ai)
60
+ ```python
61
+ from kiln_ai.datamodel import Project
49
62
 
50
- This package is the Kiln AI core library. There is also a separate desktop application and server package.
63
+ print("Reading Kiln project")
64
+ project = Project.load_from_file("path/to/project.kiln")
65
+ print("Project: ", project.name, " - ", project.description)
51
66
 
52
- Github: [github.com/Kiln-AI/kiln](https://github.com/Kiln-AI/kiln)
67
+ task = project.tasks()[0]
68
+ print("Task: ", task.name, " - ", task.description)
69
+ print("Total dataset size:", len(task.runs()))
53
70
 
54
- Docs: [https://kiln-ai.github.io/Kiln/kiln_core_docs/index.html](https://kiln-ai.github.io/Kiln/kiln_core_docs/index.html)
71
+ # ... app specific code using the typed kiln datamodel
72
+
73
+ # Alternatively, load data into pandas or a similar tool:
74
+ import glob
75
+ import json
76
+ import pandas as pd
77
+ from pathlib import Path
78
+
79
+ dataitem_glob = str(task.path.parent) + "/runs/*/task_run.kiln"
80
+
81
+ dfs = []
82
+ for file in glob.glob(dataitem_glob):
83
+ js = json.loads(Path(file).read_text())
84
+ df = pd.json_normalize(js)
85
+ dfs.append(df)
86
+ final_df = pd.concat(dfs, ignore_index=True)
87
+ print(final_df)
88
+ ```
@@ -0,0 +1,58 @@
1
+ # kiln_ai
2
+
3
+ <p align="center">
4
+ <picture>
5
+ <img width="205" alt="Kiln AI Logo" src="https://github.com/user-attachments/assets/5fbcbdf7-1feb-45c9-bd73-99a46dd0a47f">
6
+ </picture>
7
+ </p>
8
+
9
+ [![PyPI - Version](https://img.shields.io/pypi/v/kiln-ai.svg?logo=pypi&label=PyPI&logoColor=gold)](https://pypi.org/project/kiln-ai)
10
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/kiln-ai.svg)](https://pypi.org/project/kiln-ai)
11
+ [![Docs](https://img.shields.io/badge/docs-pdoc-blue)](https://kiln-ai.github.io/Kiln/kiln_core_docs/index.html)
12
+
13
+ ---
14
+
15
+ ## Installation
16
+
17
+ ```console
18
+ pip install kiln_ai
19
+ ```
20
+
21
+ ## About
22
+
23
+ This package is the Kiln AI core library. There is also a separate desktop application and server package. Learn more about Kiln AI at [getkiln.ai](https://getkiln.ai)
24
+
25
+ - Github: [github.com/Kiln-AI/kiln](https://github.com/Kiln-AI/kiln)
26
+ - Core Library Docs: [https://kiln-ai.github.io/Kiln/kiln_core_docs/index.html](https://kiln-ai.github.io/Kiln/kiln_core_docs/index.html)
27
+
28
+ ## Quick Start
29
+
30
+ ```python
31
+ from kiln_ai.datamodel import Project
32
+
33
+ print("Reading Kiln project")
34
+ project = Project.load_from_file("path/to/project.kiln")
35
+ print("Project: ", project.name, " - ", project.description)
36
+
37
+ task = project.tasks()[0]
38
+ print("Task: ", task.name, " - ", task.description)
39
+ print("Total dataset size:", len(task.runs()))
40
+
41
+ # ... app specific code using the typed kiln datamodel
42
+
43
+ # Alternatively, load data into pandas or a similar tool:
44
+ import glob
45
+ import json
46
+ import pandas as pd
47
+ from pathlib import Path
48
+
49
+ dataitem_glob = str(task.path.parent) + "/runs/*/task_run.kiln"
50
+
51
+ dfs = []
52
+ for file in glob.glob(dataitem_glob):
53
+ js = json.loads(Path(file).read_text())
54
+ df = pd.json_normalize(js)
55
+ dfs.append(df)
56
+ final_df = pd.concat(dfs, ignore_index=True)
57
+ print(final_df)
58
+ ```
@@ -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,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,117 @@
1
+ import json
2
+
3
+ from kiln_ai.adapters.prompt_builders import SimplePromptBuilder
4
+ from kiln_ai.datamodel import Project, Task
5
+ from pydantic import BaseModel
6
+
7
+ from .data_gen_prompts import (
8
+ SAMPLE_GENERATION_PROMPT,
9
+ TREE_GENERATION_PROMPT,
10
+ )
11
+
12
+
13
+ class DataGenCategoriesTaskInput(BaseModel):
14
+ node_path: list[str]
15
+ system_prompt: str
16
+ num_subtopics: int
17
+ human_guidance: str | None = None
18
+ existing_topics: list[str] | None = None
19
+
20
+ @classmethod
21
+ def from_task(
22
+ cls,
23
+ task: Task,
24
+ node_path: list[str] = [],
25
+ num_subtopics: int = 6,
26
+ human_guidance: str | None = None,
27
+ existing_topics: list[str] | None = None,
28
+ ) -> "DataGenCategoriesTaskInput":
29
+ prompt_builder = SimplePromptBuilder(task=task)
30
+ return cls(
31
+ node_path=node_path,
32
+ num_subtopics=num_subtopics,
33
+ human_guidance=human_guidance,
34
+ existing_topics=existing_topics,
35
+ system_prompt=prompt_builder.build_prompt(),
36
+ )
37
+
38
+
39
+ class DataGenCategoriesTaskOutput(BaseModel):
40
+ subtopics: list[str]
41
+
42
+
43
+ class DataGenCategoriesTask(Task, parent_of={}):
44
+ def __init__(self):
45
+ # Keep the typechecker happy. TODO: shouldn't need this or parent_of above.
46
+ tmp_project = Project(name="DataGen")
47
+ super().__init__(
48
+ name="DataGen",
49
+ parent=tmp_project,
50
+ description="A task which generates synthetic data categories, which in turn are used to generate training data for a model to learn from.",
51
+ instruction=TREE_GENERATION_PROMPT,
52
+ input_json_schema=json.dumps(
53
+ DataGenCategoriesTaskInput.model_json_schema()
54
+ ),
55
+ output_json_schema=json.dumps(
56
+ DataGenCategoriesTaskOutput.model_json_schema()
57
+ ),
58
+ )
59
+
60
+
61
+ class DataGenSampleTaskInput(BaseModel):
62
+ topic: list[str]
63
+ system_prompt: str
64
+ num_samples: int
65
+ human_guidance: str | None = None
66
+
67
+ @classmethod
68
+ def from_task(
69
+ cls,
70
+ task: Task,
71
+ topic: list[str] = [],
72
+ num_samples: int = 8,
73
+ human_guidance: str | None = None,
74
+ ) -> "DataGenSampleTaskInput":
75
+ prompt_builder = SimplePromptBuilder(task=task)
76
+ return cls(
77
+ topic=topic,
78
+ num_samples=num_samples,
79
+ human_guidance=human_guidance,
80
+ system_prompt=prompt_builder.build_prompt(),
81
+ )
82
+
83
+
84
+ def list_json_schema_for_task(task: Task) -> str:
85
+ if task.input_json_schema:
86
+ items_schema = json.loads(task.input_json_schema)
87
+ else:
88
+ items_schema = {"type": "string"}
89
+
90
+ list_schema = {
91
+ "type": "array",
92
+ "items": items_schema,
93
+ }
94
+
95
+ top_level_schema = {
96
+ "type": "object",
97
+ "properties": {
98
+ "generated_samples": list_schema,
99
+ },
100
+ "required": ["generated_samples"],
101
+ }
102
+
103
+ return json.dumps(top_level_schema)
104
+
105
+
106
+ class DataGenSampleTask(Task, parent_of={}):
107
+ def __init__(self, target_task: Task, num_samples: int = 8):
108
+ # Keep the typechecker happy. TODO: shouldn't need this or parent_of above.
109
+ tmp_project = Project(name="DataGenSample")
110
+ super().__init__(
111
+ name="DataGenSample",
112
+ parent=tmp_project,
113
+ description="A task which generates synthetic data samples for a given topic (and optional subtopic).",
114
+ instruction=SAMPLE_GENERATION_PROMPT,
115
+ input_json_schema=json.dumps(DataGenSampleTaskInput.model_json_schema()),
116
+ output_json_schema=list_json_schema_for_task(target_task),
117
+ )