-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path4-chains.py
More file actions
127 lines (105 loc) · 2.69 KB
/
4-chains.py
File metadata and controls
127 lines (105 loc) · 2.69 KB
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
from dotenv import load_dotenv, find_dotenv
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain, SequentialChain
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
"""
Load OpenAI API key
"""
_ = load_dotenv(find_dotenv())
"""
Model
"""
chat_llm = ChatOpenAI(
temperature=0.0,
streaming=True,
callbacks=[StreamingStdOutCallbackHandler()]
)
"""
Simple Sequatial Chain
"""
template1 = """
Sebutkan satu jenis oleh-oleh khas dari kota {city}.
Jawaban maksimal 3 kata.
"""
prompt1 = ChatPromptTemplate.from_template(template1)
chain1 = LLMChain(
llm=chat_llm,
prompt=prompt1,
)
template2 = """
Jelaskan secara rinci resep untuk membuat {food}.
"""
prompt2 = ChatPromptTemplate.from_template(template2)
chain2 = LLMChain(
llm=chat_llm,
prompt=prompt2
)
overall_chain = SimpleSequentialChain(
chains=[chain1, chain2],
verbose=True
)
city = "Palembang"
# response = overall_chain.run(city)
# print(response)
"""
Sequential Chain
"""
user_review = """
This leaf blower is pretty amazing. It has four settings:\
candle blower, gentle breeze, windy city, and tornado. \
It arrived in two days, just in time for my wife's \
anniversary present. \
I think my wife liked it so much she was speechless. \
So far I've been the only one using it, and I've been \
using it every other morning to clear the leaves on our lawn. \
It's slightly more expensive than the other leaf blowers \
out there, but I think it's worth it for the extra features.
"""
# Chain1: input=Review, output=Indonesian_Review
prompt1 = ChatPromptTemplate.from_template(
"""
Translate the following review to Indonesian:
{review}
"""
)
chain1 = LLMChain(
llm=chat_llm,
prompt=prompt1,
output_key="indonesian_review",
verbose=True
)
# Chain2: input=Review, output=Sentiment
prompt2 = ChatPromptTemplate.from_template(
"""
Is the following review positive or negative?
{review}
Answer with either "positive" or "negative"
"""
)
chain2 = LLMChain(
llm=chat_llm,
prompt=prompt2,
output_key="sentiment",
verbose=True
)
# Chain3: input=Indonesian_Review, output=Summary
prompt3 = ChatPromptTemplate.from_template(
"""
Summarize the following Indonesian review in 1 sentence:
{indonesian_review}
"""
)
chain3 = LLMChain(
llm=chat_llm,
prompt=prompt3,
output_key="summary",
verbose=True
)
overall_seq_chain = SequentialChain(
chains=[chain1, chain2, chain3],
input_variables=["review"],
output_variables=["indonesian_review", "sentiment", "summary"],
verbose=True
)
overall_seq_chain(user_review)