Files
pipewright/tests/test_plugins.py
T

86 lines
2.8 KiB
Python

"""A local plugin pack registering one of each: node type, provider, sandbox, harness."""
import textwrap
from pipewright import ops
PACK = textwrap.dedent('''
from pydantic import BaseModel
from pipewright import NodeType, Port, register_node, register_provider, register_sandbox, register_harness
from pipewright.models.base import ModelProvider
from pipewright.sandbox.fake import FakeProvider as FakeSandboxes
from pipewright.harness.base import Harness, Trajectory
class ShoutParams(BaseModel):
field: str = "text"
@register_node
class Shout(NodeType):
"""Upper-case a payload field."""
name = "mypack.shout"
kind = "map"
Params = ShoutParams
inputs = {"in": Port(kind="note")}
outputs = {"out": Port(kind="note")}
example = {"field": "text"}
async def process(self, item, ctx):
return item.derive(update_payload={self.params.field: item.payload[self.params.field].upper()})
class Echo(ModelProvider):
type = "echo"
async def complete(self, payload):
text = payload["messages"][-1]["content"]
return {"choices": [{"index": 0, "message": {"role": "assistant", "content": text[::-1]}}],
"usage": {"prompt_tokens": 1, "completion_tokens": 1}}
register_provider("echo", Echo)
register_sandbox("mybox", FakeSandboxes)
class Noop(Harness):
type = "noop"
async def run(self, task, sandbox, ctx):
return Trajectory(harness=self.name, status="completed", final=task.prompt)
register_harness("noop", Noop)
''')
GRAPH = """
name: plug
nodes:
notes: {type: source.inline, params: {kind: note, records: [{id: n1, text: hello}]}}
shout: {type: mypack.shout, inputs: {in: notes}}
rev: {type: llm.generate, inputs: {in: shout}, params: {model: rev, prompt: "{{ payload.text }}", output_field: r}}
agent: {type: agent.run, inputs: {in: rev}, params: {harness: h, pool: box, prompt: "{{ payload.r }}"}}
sink: {type: sink.jsonl, inputs: {in: agent}, params: {path: out/p.jsonl}}
"""
PROJECT = """
plugins: [mypack_nodes]
providers: {e: {type: echo}}
models: {rev: {provider: e, model: echo-1}}
sandboxes: {box: {type: mybox}}
harnesses: {h: {type: noop}}
"""
def test_local_plugin_pack(tmp_path):
(tmp_path / "mypack_nodes.py").write_text(PACK)
(tmp_path / "pipewright.yaml").write_text(PROJECT)
(tmp_path / "g.yaml").write_text(GRAPH)
chk = ops.check_graph(tmp_path / "g.yaml")
assert chk["ok"], chk
s = ops.run_graph(tmp_path / "g.yaml")
assert s["status"] == "completed", s["errors"]
item = ops.items(tmp_path / "g.yaml", "agent")["items"][0]
assert item["payload"]["r"] == "OLLEH" and item["labels"]["agent_status"] == "completed"
assert ops.nodes_describe("mypack.shout")["inputs"]["in"]["kind"] == "note"