agentensor 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
agentensor/__init__.py ADDED
@@ -0,0 +1 @@
1
+ """Example module."""
agentensor/loss.py ADDED
@@ -0,0 +1,44 @@
1
+ """Loss functions."""
2
+
3
+ from dataclasses import dataclass
4
+ from typing import Any
5
+ from pydantic_ai import models
6
+ from pydantic_evals.evaluators import EvaluationReason, Evaluator, EvaluatorContext
7
+ from pydantic_evals.evaluators.llm_as_a_judge import judge_input_output, judge_output
8
+ from agentensor.tensor import TextTensor
9
+
10
+
11
+ @dataclass
12
+ class LLMTensorJudge(Evaluator[TextTensor, TextTensor, Any]):
13
+ """LLM judge for text tensors.
14
+
15
+ Adapted from pydantic_evals.evaluators.common.LLMJudge.
16
+ """
17
+
18
+ rubric: str
19
+ model: models.Model | models.KnownModelName | None = None
20
+ include_input: bool = True
21
+
22
+ async def evaluate(
23
+ self,
24
+ ctx: EvaluatorContext[TextTensor, TextTensor, Any],
25
+ ) -> EvaluationReason:
26
+ """Evaluate the text tensor."""
27
+ if self.include_input:
28
+ grading_output = await judge_input_output(
29
+ ctx.inputs.text, ctx.output.text, self.rubric, self.model
30
+ )
31
+ else:
32
+ grading_output = await judge_output(
33
+ ctx.output.text, self.rubric, self.model
34
+ )
35
+ return EvaluationReason(
36
+ value=grading_output.pass_, reason=grading_output.reason
37
+ )
38
+
39
+ def build_serialization_arguments(self) -> dict[str, Any]:
40
+ """Build serialization arguments."""
41
+ result = super().build_serialization_arguments()
42
+ if (model := result.get("model")) and isinstance(model, models.Model):
43
+ result["model"] = f"{model.system}:{model.model_name}"
44
+ return result
agentensor/module.py ADDED
@@ -0,0 +1,26 @@
1
+ """Module class."""
2
+
3
+ from dataclasses import dataclass
4
+ from pydantic_graph.nodes import BaseNode, DepsT, NodeRunEndT, StateT
5
+ from agentensor.tensor import TextTensor
6
+
7
+
8
+ @dataclass
9
+ class ModuleState:
10
+ """State of the graph."""
11
+
12
+ input: TextTensor
13
+
14
+
15
+ class AgentModule(BaseNode[StateT, DepsT, NodeRunEndT]):
16
+ """Agent module."""
17
+
18
+ @classmethod
19
+ def get_params(cls) -> list[TextTensor]:
20
+ """Get the parameters of the module."""
21
+ params = []
22
+ for base in cls.__mro__:
23
+ for _, attr in base.__dict__.items():
24
+ if isinstance(attr, TextTensor) and attr.requires_grad:
25
+ params.append(attr)
26
+ return params
agentensor/optim.py ADDED
@@ -0,0 +1,39 @@
1
+ """Optimizer module."""
2
+
3
+ from pydantic_ai import Agent
4
+ from pydantic_graph import Graph
5
+ from agentensor.module import AgentModule
6
+ from agentensor.tensor import TextTensor
7
+
8
+
9
+ class Optimizer:
10
+ """Optimizer class."""
11
+
12
+ def __init__(self, graph: Graph) -> None:
13
+ """Initialize the optimizer."""
14
+ self.params: list[TextTensor] = [
15
+ param
16
+ for node in graph.get_nodes()
17
+ for param in node.get_params() # type: ignore[attr-defined]
18
+ if issubclass(node, AgentModule)
19
+ ]
20
+ self.agent: Agent = Agent(
21
+ model="openai:gpt-4o-mini",
22
+ system_prompt="Rewrite the system prompt given the feedback.",
23
+ )
24
+
25
+ def step(self) -> None:
26
+ """Step the optimizer."""
27
+ for param in self.params:
28
+ if not param.text_grad:
29
+ continue
30
+ param.text = self.optimize(param.text, param.text_grad)
31
+
32
+ def zero_grad(self) -> None:
33
+ """Zero the gradients."""
34
+ for param in self.params:
35
+ param.text_grad = ""
36
+
37
+ def optimize(self, text: str, grad: str) -> str:
38
+ """Optimize the text."""
39
+ return self.agent.run_sync(f"Feedback: {grad}\nText: {text}").data
agentensor/tensor.py ADDED
@@ -0,0 +1,53 @@
1
+ """Example module."""
2
+
3
+ from __future__ import annotations
4
+ from pydantic_ai import Agent
5
+
6
+
7
+ class TextTensor:
8
+ """A tensor that represents a text."""
9
+
10
+ def __init__(
11
+ self,
12
+ text: str,
13
+ parents: list[TextTensor] | None = None,
14
+ requires_grad: bool = False,
15
+ ) -> None:
16
+ """Initialize a TextTensor."""
17
+ self.text = text
18
+ self.requires_grad = requires_grad
19
+ self.text_grad = ""
20
+ self.agent = Agent(
21
+ model="openai:gpt-4o-mini", system_prompt="Answer the user's question."
22
+ )
23
+ self.parents: list[TextTensor] = parents or []
24
+
25
+ def backward(self, grad: str = "") -> None:
26
+ """Backward pass for the TextTensor.
27
+
28
+ Args:
29
+ grad (str, optional): The gradient to backpropagate. Defaults to "".
30
+ """
31
+ if not grad:
32
+ return
33
+
34
+ if self.requires_grad:
35
+ self.text_grad = grad
36
+ for parent in self.parents:
37
+ if not parent.requires_grad:
38
+ continue
39
+ grad_to_parent = self.calc_grad(parent.text, self.text, grad)
40
+ parent.backward(grad_to_parent)
41
+
42
+ def calc_grad(self, input_text: str, output_text: str, grad: str) -> str:
43
+ """Calculate the gradient for the TextTensor."""
44
+ return self.agent.run_sync(
45
+ f"Here is the input: \n\n>{input_text}\n\nI got this "
46
+ f"output: \n\n>{output_text}\n\nHere is the feedback: \n\n"
47
+ f">{grad}\n\nHow should I improve the input to get a "
48
+ f"better output?"
49
+ ).data
50
+
51
+ def __str__(self) -> str:
52
+ """Return the text as a string."""
53
+ return self.text
agentensor/train.py ADDED
@@ -0,0 +1,66 @@
1
+ """Trainer."""
2
+
3
+ from typing import Any
4
+ from pydantic_evals import Dataset
5
+ from pydantic_graph import Graph
6
+ from agentensor.module import AgentModule, ModuleState
7
+ from agentensor.optim import Optimizer
8
+ from agentensor.tensor import TextTensor
9
+
10
+
11
+ class Trainer:
12
+ """Trainer."""
13
+
14
+ def __init__(
15
+ self,
16
+ graph: Graph[ModuleState, None, TextTensor],
17
+ start_node: type[AgentModule],
18
+ dataset: Dataset[TextTensor, TextTensor, Any],
19
+ optimizer: Optimizer,
20
+ epochs: int,
21
+ stop_threshold: float = 0.95,
22
+ ):
23
+ """Initialize the trainer."""
24
+ self.graph = graph
25
+ self.start_node = start_node
26
+ self.dataset = dataset
27
+ self.optimizer = optimizer
28
+ self.epochs = epochs
29
+ self.stop_threshold = stop_threshold
30
+
31
+ async def step(self, x: TextTensor) -> TextTensor:
32
+ """Step the optimizer."""
33
+ state = ModuleState(input=x)
34
+ result = await self.graph.run(self.start_node(), state=state) # type: ignore[arg-type]
35
+ return result.output
36
+
37
+ def train(self) -> None:
38
+ """Train the model."""
39
+ for i in range(self.epochs):
40
+ report = self.dataset.evaluate_sync(self.step)
41
+ report.print(
42
+ include_input=True, include_output=True, include_durations=True
43
+ )
44
+
45
+ # Backward those failed cases
46
+ for case in report.cases:
47
+ losses = []
48
+ for evaluator in case.assertions.values():
49
+ if not evaluator.value:
50
+ assert evaluator.reason
51
+ losses.append(evaluator.reason)
52
+ if losses:
53
+ case.output.backward(" ".join(losses))
54
+
55
+ self.optimizer.step()
56
+ self.optimizer.zero_grad()
57
+
58
+ print(f"Epoch {i + 1}")
59
+ for param in self.optimizer.params:
60
+ print(param.text) # pragma: no cover
61
+ print()
62
+ performance = report.averages().assertions
63
+ assert performance is not None
64
+ if performance >= self.stop_threshold:
65
+ print("Optimization complete.")
66
+ break
@@ -0,0 +1,40 @@
1
+ Metadata-Version: 2.4
2
+ Name: agentensor
3
+ Version: 0.0.1
4
+ Summary: Add your description here
5
+ License: MIT License
6
+
7
+ Copyright (c) 2025 Shaojie Jiang
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+ License-File: LICENSE
27
+ Requires-Python: >=3.12
28
+ Requires-Dist: logfire>=3.14.0
29
+ Requires-Dist: pydantic-ai>=0.0.55
30
+ Description-Content-Type: text/markdown
31
+
32
+ # AgenTensor
33
+
34
+ [![CI](https://github.com/ShaojieJiang/agentensor/actions/workflows/ci.yml/badge.svg?event=push)](https://github.com/ShaojieJiang/agentensor/actions/workflows/ci.yml?query=branch%3Amain)
35
+ [![Coverage](https://coverage-badge.samuelcolvin.workers.dev/ShaojieJiang/agentensor.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/ShaojieJiang/agentensor)
36
+ [![PyPI](https://img.shields.io/pypi/v/agentensor.svg)](https://pypi.python.org/pypi/agentensor)
37
+
38
+ ## TODO
39
+
40
+ - [ ] Add parameter saving
@@ -0,0 +1,10 @@
1
+ agentensor/__init__.py,sha256=q_xtykXlbfOMT4e0rlS-LqNVewgcSDKKJN8EAN4ekXA,22
2
+ agentensor/loss.py,sha256=Gtit_dgCRaT4-KzwaahoB9Prik64ZD60CuSaXa6HFLg,1531
3
+ agentensor/module.py,sha256=4sq14LdGUwIZSpxsAQzuSVEZXq0xsGYpuYrfdIx_5Cw,682
4
+ agentensor/optim.py,sha256=_YQDi5AX1vqAlRXEw3TYaty3_BoFVCHrWZIrSv8wXgU,1217
5
+ agentensor/tensor.py,sha256=9yMxugXJy8aLniPPe3TSv52Ui5ll6QHD4IwPzcqRKwM,1686
6
+ agentensor/train.py,sha256=-pdhyJgoqj-wprL9gz1g4-jI0ZOoPwJUBbf_gGVBxpU,2179
7
+ agentensor-0.0.1.dist-info/METADATA,sha256=TXVk6ma6mRb9AtD8wLjMtDavll29bI4ob7pxqncAle8,1989
8
+ agentensor-0.0.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
+ agentensor-0.0.1.dist-info/licenses/LICENSE,sha256=UMhWnNnpcD5pmbMMRSZGXPtDuafJDTSZtzuvH245gAo,1070
10
+ agentensor-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Shaojie Jiang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.