-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchains_sequential.py
More file actions
71 lines (52 loc) · 1.81 KB
/
chains_sequential.py
File metadata and controls
71 lines (52 loc) · 1.81 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
import os
from dotenv import load_dotenv, find_dotenv
import google.generativeai as genai
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SequentialChain
load_dotenv(find_dotenv())
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
llm_model = "gemini-1.5-flash"
open_ai = ChatGoogleGenerativeAI(model=llm_model, temperature=0.7)
template = """
As a children's book writer, please come up with a simple and short (90 words)
lullaby based on the location
{location}
and the main character {name}
STORY:
"""
prompt = PromptTemplate(input_variables=["location", "name"], template=template)
chain_story = LLMChain(llm=open_ai,
prompt=prompt,
output_key='story',
verbose=True)
story = chain_story({"location": "in the forest", "name": "Bobby"})
print(story)
# ======= Sequential Chain =====
# chain to translate the story to Portuguese
template_update = """
Translate the {story} into {language}. Make sure
the language is simple and fun.
TRANSLATION:
"""
prompt_translate = PromptTemplate(input_variables=['story', 'language'],
template=template_update)
chain_translate = LLMChain(
llm=open_ai,
prompt=prompt_translate,
output_key='translated'
)
# ==== Create the Sequential Chain ===
overall_chain = SequentialChain(
chains=[chain_story, chain_translate],
input_variables=['location', 'name', 'language'],
output_variables=['story', 'translated'],
verbose=True
)
response = overall_chain({
'location': 'in the forest',
'name': 'Bobby',
'language': 'Portuguese'
})
print(f"English Version ====> { response['story']} \n \n")
print(f"Translated Version ====> { response['translated']}")