File size: 944 Bytes
ad31616 |
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 langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
llm = ChatOpenAI(api_key=api_key)
def create_script(prompt, topic, topia):
print(f'Writing script about {topic} {topia}')
final_prompt = ChatPromptTemplate.from_messages([
("system", '{prompt}'),
("user", '{topic}'),
("user", '{topia}')
])
output_parser = StrOutputParser()
chain = final_prompt | llm | output_parser
response = chain.invoke({"prompt": prompt, "topic": topic, "topia": topia})
return response
def save_script(script, path, topic, topia):
filename = f'{topic}-{topia}.txt'
file_path = os.path.join(path, filename)
print(f'Saving script to {file_path}')
with open(file_path, "w") as f:
f.write(script)
|