-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchains_story.py
More file actions
33 lines (25 loc) · 991 Bytes
/
chains_story.py
File metadata and controls
33 lines (25 loc) · 991 Bytes
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
import os
from dotenv import find_dotenv, load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Load API Key from .env
load_dotenv(find_dotenv())
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
# Initialize Google Gemini AI Model
chat = ChatGoogleGenerativeAI(model="gemini-1.5-flash",
temperature=0.7,
google_api_key=GOOGLE_API_KEY)
# Define Prompt Template
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)
# Create LLMChain
chain_story = LLMChain(llm=chat, prompt=prompt, verbose=True)
# Generate Story
story = chain_story({"location": "Zanzibar", "name": "Maya"})
# Print the generated story
print(story['text'])