forked from julep-ai/julep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
130 lines (108 loc) · 3.64 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const { Julep } = require('@julep/sdk');
const yaml = require('js-yaml');
const readline = require('readline');
const client = new Julep({ apiKey: 'your_julep_api_key' });
async function createAgent() {
const agent = await client.agents.create({
name: "Storytelling Agent",
model: "gpt-4",
about: "You are a creative storytelling agent that can craft engaging stories and generate comic panels based on ideas.",
});
// 🛠️ Add an image generation tool (DALL·E) to the agent
await client.agents.tools.create(agent.id, {
name: "image_generator",
description: "Use this tool to generate images based on descriptions.",
integration: {
provider: "dalle",
method: "generate_image",
setup: {
api_key: "your_openai_api_key",
},
},
});
return agent;
}
const taskYaml = `
name: Story and Comic Creator
description: Create a story based on an idea and generate a 4-panel comic strip illustrating the story.
main:
# Step 1: Generate a story and outline into 4 panels
- prompt:
- role: system
content: You are {{agent.name}}. {{agent.about}}
- role: user
content: >
Based on the idea '{{_.idea}}', write a short story suitable for a 4-panel comic strip.
Provide the story and a numbered list of 4 brief descriptions for each panel illustrating key moments in the story.
unwrap: true
# Step 2: Extract the panel descriptions and story
- evaluate:
story: _.split('1. ')[0].trim()
panels: _.match(/\\d+\\.\\s*(.*?)(?=\\d+\\.\\s*|$)/g)
# Step 3: Generate images for each panel using the image generator tool
- foreach:
in: _.panels
do:
tool: image_generator
arguments:
description: _
# Step 4: Generate a catchy title for the story
- prompt:
- role: system
content: You are {{agent.name}}. {{agent.about}}
- role: user
content: >
Based on the story below, generate a catchy title.
Story: {{outputs[1].story}}
unwrap: true
# Step 5: Return the story, the generated images, and the title
- return:
title: outputs[3]
story: outputs[1].story
comic_panels: outputs[2].map(output => output.image.url)
`;
async function createTask(agent) {
const task = await client.tasks.create(agent.id, yaml.load(taskYaml));
return task;
}
async function executeTask(task) {
const execution = await client.executions.create(task.id, {
input: { idea: "A cat who learns to fly" }
});
// 🎉 Watch as the story and comic panels are generated
for await (const transition of client.executions.transitions.stream(execution.id)) {
console.log(transition);
}
// 📦 Once the execution is finished, retrieve the results
const result = await client.executions.get(execution.id);
return result;
}
async function chatWithAgent(agent) {
const session = await client.sessions.create({ agent_id: agent.id });
// 💬 Send messages to the agent
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const chat = async () => {
rl.question("Enter a message (or 'quit' to exit): ", async (message) => {
if (message.toLowerCase() === 'quit') {
rl.close();
return;
}
const response = await client.sessions.chat(session.id, { message });
console.log(response);
chat();
});
};
chat();
}
// Run the example
async function runExample() {
const agent = await createAgent();
const task = await createTask(agent);
const result = await executeTask(task);
console.log("Task Result:", result);
await chatWithAgent(agent);
}
runExample().catch(console.error);