-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path14_functional_flowsteps.py
53 lines (44 loc) · 1.6 KB
/
14_functional_flowsteps.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
# pylint: skip-file
import os
from llmflows.flows import Flow, ChatFlowStep, FunctionalFlowStep
from llmflows.llms import OpenAIChat, MessageHistory
from llmflows.prompts import PromptTemplate
openai_api_key = os.environ.get("OPENAI_API_KEY", "<your-api-key>")
def capitalize_first_letters(lyrics: str) -> str:
"""Capitalize the first letter of each word in a string."""
return lyrics.title()
title_message_history = MessageHistory()
title_message_history.system_prompt = "You write song titles"
lyrics_message_history = MessageHistory()
title_message_history.system_prompt = "You write song lyrics"
# Create flowsteps
title_flowstep = ChatFlowStep(
name="Title Flowstep",
llm=OpenAIChat(api_key=openai_api_key),
message_history=title_message_history,
message_prompt_template=PromptTemplate("Write a good song title about {topic}"),
message_key="topic",
output_key="song_title",
)
lyrics_flowstep = ChatFlowStep(
name="Lyrics Flowstep",
llm=OpenAIChat(api_key=openai_api_key),
message_history=lyrics_message_history,
message_prompt_template=PromptTemplate(
"Write the lyrics of a song titled {song_title}"
),
message_key="song_title",
output_key="lyrics",
)
capitalizer_flowstep = FunctionalFlowStep(
name="Capitalizer Flowstep",
flowstep_fn=capitalize_first_letters,
output_key="capitalized_lyrics",
)
# Connect flowsteps
title_flowstep.connect(lyrics_flowstep)
lyrics_flowstep.connect(capitalizer_flowstep)
# Create and run Flow
songwriting_flow = Flow(title_flowstep)
result = songwriting_flow.start(topic="love", verbose=True)
print(result)