-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequentialchain.py
More file actions
50 lines (33 loc) · 1.35 KB
/
Copy pathsequentialchain.py
File metadata and controls
50 lines (33 loc) · 1.35 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
from dotenv import load_dotenv
load_dotenv()
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
# Simple sequential execution function
def run_sequential_chain(topic):
"""Run the chains sequentially and return results"""
# Generate explanation from topic
explanation = explanation_chain.invoke({'topic': topic})
# Generate summary from explanation
summary = summary_chain.invoke({'explanation': explanation})
return {
"explanation": explanation,
"summary": summary
}
llm = ChatOpenAI(
model = "gpt-4o-mini",
temperature = 0.7
)
explanation_prompt = PromptTemplate.from_template("Explain {topic} in a detailed paragraph suitable for a beginner.")
summary_prompt = PromptTemplate.from_template("Summarize the following paragraph in 2 short sentences:\n {explanation}")
explanation_chain = explanation_prompt | llm | StrOutputParser()
summary_chain = summary_prompt | llm | StrOutputParser()
# Get user input
topic = input("Enter a topic: ")
# Run the sequential chain
print("\n\033[1m> Running sequential chain...\033[0m")
result = run_sequential_chain(topic)
print("\033[1m> Finished!\033[0m")
# Display results
print("\nFull Explanation:\n", result["explanation"])
print("\nSummary:\n", result["summary"])