Amazing-Python-Scripts
43 строки · 1.6 Кб
1from langchain.llms import OpenAI
2from langchain.prompts import PromptTemplate
3from langchain.chains import LLMChain
4from langchain.tools import DuckDuckGoSearchRun
5
6# Function to generate video script
7
8
9def generate_script(prompt, video_length, creativity, api_key):
10
11# Template for generating 'Title'
12title_template = PromptTemplate(
13input_variables=['subject'],
14template='Please come up with a title for a YouTube video on the {subject}.'
15)
16
17# Template for generating 'Video Script' using search engine
18script_template = PromptTemplate(
19input_variables=['title', 'DuckDuckGo_Search', 'duration'],
20template='Create a script for a YouTube video based on this title for me. TITLE: {title} of duration: {duration} minutes using this search data {DuckDuckGo_Search} '
21)
22
23# Setting up OpenAI LLM
24llm = OpenAI(temperature=creativity, openai_api_key=api_key,
25model_name='gpt-3.5-turbo')
26
27# Creating chain for 'Title' & 'Video Script'
28title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True)
29script_chain = LLMChain(llm=llm, prompt=script_template, verbose=True)
30
31# https://python.langchain.com/docs/modules/agents/tools/integrations/ddg
32search = DuckDuckGoSearchRun()
33
34# Executing the chains we created for 'Title'
35title = title_chain.run(prompt)
36
37# Executing the chains we created for 'Video Script' by taking help of search engine 'DuckDuckGo'
38search_result = search.run(prompt)
39script = script_chain.run(
40title=title, DuckDuckGo_Search=search_result, duration=video_length)
41
42# Returning the output
43return search_result, title, script
44