-
Notifications
You must be signed in to change notification settings - Fork 912
/
example.py
137 lines (112 loc) · 4.13 KB
/
example.py
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
131
132
133
134
135
136
137
### Step 0: Setup
import os
import time
import yaml
from julep import Julep # or AsyncJulep
client = Julep(api_key=os.environ["JULEP_API_KEY"])
### Step 1: Create an Agent
agent = client.agents.create(
name="Storytelling Agent",
model="claude-3.5-sonnet",
about="You are a creative storyteller that crafts engaging stories on a myriad of topics.",
)
### Step 2: Create a Task that generates a story and comic strip
task_yaml = """
name: Storyteller
description: Create a story based on an idea.
tools:
- name: research_wikipedia
integration:
provider: wikipedia
method: search
main:
# Step 1: Generate plot idea
- prompt:
- role: system
content: You are {{agent.name}}. {{agent.about}}
- role: user
content: >
Based on the idea '{{_.idea}}', generate a list of 5 plot ideas. Go crazy and be as creative as possible. Return your output as a list of long strings inside ```yaml tags at the end of your response.
unwrap: true
- evaluate:
plot_ideas: load_yaml(_.split('```yaml')[1].split('```')[0].strip())
# Step 2: Extract research fields from the plot ideas
- prompt:
- role: system
content: You are {{agent.name}}. {{agent.about}}
- role: user
content: >
Here are some plot ideas for a story:
{% for idea in _.plot_ideas %}
- {{idea}}
{% endfor %}
To develop the story, we need to research for the plot ideas.
What should we research? Write down wikipedia search queries for the plot ideas you think are interesting.
Return your output as a yaml list inside ```yaml tags at the end of your response.
unwrap: true
settings:
model: gpt-4o-mini
temperature: 0.7
- evaluate:
research_queries: load_yaml(_.split('```yaml')[1].split('```')[0].strip())
# Step 3: Research each plot idea
- foreach:
in: _.research_queries
do:
tool: research_wikipedia
arguments:
query: _
- evaluate:
wikipedia_results: 'NEWLINE.join([f"- {doc.metadata.title}: {doc.metadata.summary}" for item in _ for doc in item.documents])'
# Step 4: Think and deliberate
- prompt:
- role: system
content: You are {{agent.name}}. {{agent.about}}
- role: user
content: |-
Before we write the story, let's think and deliberate. Here are some plot ideas:
{% for idea in outputs[1].plot_ideas %}
- {{idea}}
{% endfor %}
Here are the results from researching the plot ideas on Wikipedia:
{{_.wikipedia_results}}
Think about the plot ideas critically. Combine the plot ideas with the results from Wikipedia to create a detailed plot for a story.
Write down all your notes and thoughts.
Then finally write the plot as a yaml object inside ```yaml tags at the end of your response. The yaml object should have the following structure:
```yaml
title: "<string>"
characters:
- name: "<string>"
about: "<string>"
synopsis: "<string>"
scenes:
- title: "<string>"
description: "<string>"
characters:
- name: "<string>"
role: "<string>"
plotlines:
- "<string>"```
Make sure the yaml is valid and the characters and scenes are not empty. Also take care of semicolons and other gotchas of writing yaml.
unwrap: true
- evaluate:
plot: "load_yaml(_.split('```yaml')[1].split('```')[0].strip())"
"""
task = client.tasks.create(
agent_id=agent.id,
**yaml.safe_load(task_yaml)
)
### Step 3: Execute the Task
execution = client.executions.create(
task_id=task.id,
input={"idea": "A cat who learns to fly"}
)
# 🎉 Watch as the story and comic panels are generated
while (result := client.executions.get(execution.id)).status not in ['succeeded', 'failed']:
print(result.status, result.output)
time.sleep(1)
# 📦 Once the execution is finished, retrieve the results
if result.status == "succeeded":
print(result.output)
else:
raise Exception(result.error)