feat(builder): Built-in templates (#7511)

- Add "Medium Blogger" template to `graph_templates` folder
- Add `import_packaged_templates()` function in `data.graph`
- Call `import_packaged_templates()` in `AgentServer` lifecycle setup
- Fix `util.json:loads` typing and parameter forwarding
This commit is contained in:
Reinier van der Leer
2024-07-22 17:54:48 +02:00
committed by GitHub
parent 56b82369b6
commit a02b017cea
4 changed files with 156 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
import asyncio
import uuid
from pathlib import Path
from typing import Any, Literal
import prisma.types
@@ -260,3 +261,28 @@ async def create_graph(graph: Graph) -> Graph:
return created_graph
raise ValueError(f"Created graph {graph.id} v{graph.version} is not in DB")
# --------------------- Helper functions --------------------- #
TEMPLATES_DIR = Path(__file__).parent.parent.parent / "graph_templates"
async def import_packaged_templates() -> None:
templates_in_db = await get_graphs_meta(filter_by="template")
print("Loading templates...")
for template_file in TEMPLATES_DIR.glob("*.json"):
template_data = json.loads(template_file.read_bytes())
template = Graph.model_validate(template_data)
if not template.is_template:
print(f"WARNING: pre-packaged graph file {template_file} is not a template")
continue
if (
exists := next((t for t in templates_in_db if t.id == template.id), None)
) and exists.version >= template.version:
continue
await create_graph(template)
print(f"Loaded template '{template.name}' ({template.id})")

View File

@@ -45,6 +45,7 @@ class AgentServer(AppService):
async def lifespan(self, _: FastAPI):
await db.connect()
self.run_and_wait(block.initialize_blocks())
self.run_and_wait(graph_db.import_packaged_templates())
asyncio.create_task(self.event_broadcaster())
yield
await db.disconnect()

View File

@@ -11,5 +11,4 @@ def dumps(data) -> str:
return json.dumps(jsonable_encoder(data))
def loads(data) -> dict:
return json.loads(data)
loads = json.loads

View File

@@ -0,0 +1,128 @@
{
"id": "96450e28-1ec0-4420-82c4-a8837247fc05",
"version": 28,
"is_active": false,
"is_template": true,
"name": "Medium Blogger",
"description": "An Agent that conducts research on a given topic, then writes and publishes a SEO blog post on Medium.",
"nodes": [
{
"id": "382efac9-3def-4baf-b16a-d6d2512a5c8b",
"block_id": "b2c3d4e5-6f7g-8h9i-0j1k-l2m3n4o5p6q7",
"input_default": {
"query": "19th July 2024 Microsoft Blackout"
},
"metadata": {
"position": {
"x": -20.471811217638447,
"y": 128.91560744799943
}
}
},
{
"id": "12e9ec75-74cd-4965-9003-6ff651f1fbf9",
"block_id": "3f7b2dcb-4a78-4e3f-b0f1-88132e1b89df",
"input_default": {
"author_id": "",
"title": "Worldwide IT Blackout",
"content_format": "markdown",
"tags": [
"AI",
"LLM"
],
"publish_status": "public",
"license": "all-rights-reserved",
"notify_followers": false,
"api_key": ""
},
"metadata": {
"position": {
"x": 1674.8131635468642,
"y": 135.36034349603352
}
}
},
{
"id": "0cd8f670-8956-4942-ba28-aee732ec783f",
"block_id": "b2g2c3d4-5e6f-7g8h-9i0j-k1l2m3n4o5p6",
"input_default": {
"key": "TITLE"
},
"metadata": {
"position": {
"x": 1126.4031753175684,
"y": 134.2846124057806
}
}
},
{
"id": "4a15b6b9-036d-43d3-915a-7e931fbc6522",
"block_id": "b2g2c3d4-5e6f-7g8h-9i0j-k1l2m3n4o5p6",
"input_default": {
"key": "CONTENT"
},
"metadata": {
"position": {
"x": 1132.489453622949,
"y": 582.2118862549163
}
}
},
{
"id": "a6fbbb8a-bd43-4469-b8ed-67d54ae58eeb",
"block_id": "ed55ac19-356e-4243-a6cb-bc599e9b716f",
"input_default": {
"model": "gpt-4o-mini",
"api_key": "",
"sys_prompt": "You are a uniquely talented blog writer with an approachable style. You don't use big words or complex, overly verbose prose. Your content always feels uncannily valuable to read.\n\nYou write full-length blog posts based on the research provided to you by the user.\n\nWrite in a standard markdown format.\n\nAlways include the following Subtitle under the main post title:\n## This content is AI Generated",
"expected_format": {
"CONTENT": "full blog post content",
"TITLE": "Blog post title"
}
},
"metadata": {
"position": {
"x": 558.8191447209389,
"y": 137.96346625283394
}
}
}
],
"links": [
{
"id": "06cf41ad-50ba-4273-883a-c93fd8bfd5b6",
"source_id": "0cd8f670-8956-4942-ba28-aee732ec783f",
"sink_id": "12e9ec75-74cd-4965-9003-6ff651f1fbf9",
"source_name": "output",
"sink_name": "title"
},
{
"id": "f2d5cf87-dbb5-4d81-98cd-46c225d82d60",
"source_id": "a6fbbb8a-bd43-4469-b8ed-67d54ae58eeb",
"sink_id": "0cd8f670-8956-4942-ba28-aee732ec783f",
"source_name": "response",
"sink_name": "input"
},
{
"id": "26e379d8-3d8e-498f-8c2c-3567977105d4",
"source_id": "382efac9-3def-4baf-b16a-d6d2512a5c8b",
"sink_id": "a6fbbb8a-bd43-4469-b8ed-67d54ae58eeb",
"source_name": "results",
"sink_name": "prompt"
},
{
"id": "304f6a2f-c5ce-423a-b395-1ef1d827f8f5",
"source_id": "4a15b6b9-036d-43d3-915a-7e931fbc6522",
"sink_id": "12e9ec75-74cd-4965-9003-6ff651f1fbf9",
"source_name": "output",
"sink_name": "content"
},
{
"id": "90274d0b-98d9-4841-a4d7-9111e4fb5156",
"source_id": "a6fbbb8a-bd43-4469-b8ed-67d54ae58eeb",
"sink_id": "4a15b6b9-036d-43d3-915a-7e931fbc6522",
"source_name": "response",
"sink_name": "input"
}
]
}