Spaces:
Runtime error
Runtime error
Commit
·
5b4830a
1
Parent(s):
196a397
Understanding Langchain by building script generator
Browse files- .gitignore +1 -0
- README.md +29 -1
- apikey.py +1 -0
- app.py +59 -0
- requirements.txt +101 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
__pycache__
|
README.md
CHANGED
@@ -10,4 +10,32 @@ pinned: false
|
|
10 |
license: mit
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
license: mit
|
11 |
---
|
12 |
|
13 |
+
# First LangChain tutorial
|
14 |
+
This is the first LangChain tutorial hands-on.
|
15 |
+
It's a Youtube script generator using OpenAI LLM.
|
16 |
+
It covers the following topics:
|
17 |
+
* Streamlist for Web App development
|
18 |
+
* LLMChain
|
19 |
+
* PromptTemplate
|
20 |
+
* SimpleSequentialChain
|
21 |
+
* SequentialChain
|
22 |
+
* ConversationalMemoryBuffer
|
23 |
+
* WikipediaAPIWrapper
|
24 |
+
|
25 |
+
|
26 |
+
### Pre-requisites
|
27 |
+
|
28 |
+
$ pip install openai langchain streamlit wikipedia chromadb tiktoken
|
29 |
+
|
30 |
+
Or
|
31 |
+
|
32 |
+
$ pip install -r requirements.txt
|
33 |
+
|
34 |
+
* Install the above Python packages
|
35 |
+
* Replace the API key in the file `apikey.py` with your own OpenAI API
|
36 |
+
### Running
|
37 |
+
$ streamlit run app.py
|
38 |
+
|
39 |
+
## Reference
|
40 |
+
* Langchain Docs: https://python.langchain.com/en/latest/index.html
|
41 |
+
* Streamlist: https://streamlit.io/
|
apikey.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
apikey = 'sk-MySuperS3cr3tApiKey'
|
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from apikey import apikey
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
from langchain.llms import OpenAI
|
6 |
+
from langchain.prompts import PromptTemplate
|
7 |
+
from langchain.chains import LLMChain, SequentialChain
|
8 |
+
from langchain.memory import ConversationBufferMemory
|
9 |
+
from langchain.utilities import WikipediaAPIWrapper
|
10 |
+
|
11 |
+
os.environ['OPENAI_API_KEY'] = apikey
|
12 |
+
|
13 |
+
st.title('🦜🔗 LangChain Tutorial - YouTube GPT Creator - by Raseel ')
|
14 |
+
prompt = st.text_input('Prompt: Write me a Youtube Video Script about')
|
15 |
+
|
16 |
+
# Prompt Templates
|
17 |
+
title_template = PromptTemplate(
|
18 |
+
input_variables = ['topic'],
|
19 |
+
template='Write me a Youtube Video title about {topic}'
|
20 |
+
)
|
21 |
+
|
22 |
+
script_template = PromptTemplate(
|
23 |
+
input_variables = ['title', 'wikipedia_research'],
|
24 |
+
template='Write me a Youtube Video script based on the title, TITLE {title} while leveraging its Wikipedia researc: {wikipedia_research}'
|
25 |
+
)
|
26 |
+
|
27 |
+
# Memory
|
28 |
+
title_memory = ConversationBufferMemory(input_key='topic', memory_key='chat_history')
|
29 |
+
script_memory = ConversationBufferMemory(input_key='title', memory_key='chat_history')
|
30 |
+
|
31 |
+
# LLMs
|
32 |
+
llm = OpenAI(temperature=0.9)
|
33 |
+
title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True, output_key='title', memory=title_memory)
|
34 |
+
script_chain = LLMChain(llm=llm, prompt=script_template, verbose=True, output_key='script', memory=script_memory)
|
35 |
+
|
36 |
+
# Tools
|
37 |
+
wiki = WikipediaAPIWrapper()
|
38 |
+
|
39 |
+
# sequential_chain = SequentialChain(chains=[title_chain, script_chain],
|
40 |
+
# input_variables=['topic'],
|
41 |
+
# output_variables=['title', 'script'],
|
42 |
+
# verbose=True)
|
43 |
+
|
44 |
+
if prompt:
|
45 |
+
title = title_chain.run(prompt)
|
46 |
+
wiki_research = wiki.run(prompt)
|
47 |
+
script = script_chain.run(title=title, wikipedia_research=wiki_research)
|
48 |
+
|
49 |
+
st.write(title)
|
50 |
+
st.write(script)
|
51 |
+
|
52 |
+
with st.expander('Title History:'):
|
53 |
+
st.info(title_memory.buffer)
|
54 |
+
|
55 |
+
with st.expander('Script History:'):
|
56 |
+
st.info(script_memory.buffer)
|
57 |
+
|
58 |
+
with st.expander('Wikipedia Research:'):
|
59 |
+
st.info(wiki_research)
|
requirements.txt
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiohttp==3.8.4
|
2 |
+
aiosignal==1.3.1
|
3 |
+
altair==5.0.1
|
4 |
+
anyio==3.7.0
|
5 |
+
async-timeout==4.0.2
|
6 |
+
attrs==23.1.0
|
7 |
+
backoff==2.2.1
|
8 |
+
beautifulsoup4==4.12.2
|
9 |
+
blinker==1.6.2
|
10 |
+
cachetools==5.3.1
|
11 |
+
certifi==2023.5.7
|
12 |
+
charset-normalizer==3.1.0
|
13 |
+
chromadb==0.3.26
|
14 |
+
click==8.1.3
|
15 |
+
clickhouse-connect==0.6.1
|
16 |
+
coloredlogs==15.0.1
|
17 |
+
dataclasses-json==0.5.7
|
18 |
+
decorator==5.1.1
|
19 |
+
duckdb==0.8.0
|
20 |
+
exceptiongroup==1.1.1
|
21 |
+
fastapi==0.96.0
|
22 |
+
flatbuffers==23.5.26
|
23 |
+
frozenlist==1.3.3
|
24 |
+
gitdb==4.0.10
|
25 |
+
GitPython==3.1.31
|
26 |
+
greenlet==2.0.2
|
27 |
+
h11==0.14.0
|
28 |
+
hnswlib==0.7.0
|
29 |
+
httptools==0.5.0
|
30 |
+
humanfriendly==10.0
|
31 |
+
idna==3.4
|
32 |
+
importlib-metadata==6.6.0
|
33 |
+
Jinja2==3.1.2
|
34 |
+
jsonschema==4.17.3
|
35 |
+
langchain==0.0.193
|
36 |
+
langchainplus-sdk==0.0.4
|
37 |
+
lz4==4.3.2
|
38 |
+
markdown-it-py==2.2.0
|
39 |
+
MarkupSafe==2.1.3
|
40 |
+
marshmallow==3.19.0
|
41 |
+
marshmallow-enum==1.5.1
|
42 |
+
mdurl==0.1.2
|
43 |
+
monotonic==1.6
|
44 |
+
mpmath==1.3.0
|
45 |
+
multidict==6.0.4
|
46 |
+
mypy-extensions==1.0.0
|
47 |
+
numexpr==2.8.4
|
48 |
+
numpy==1.24.3
|
49 |
+
onnxruntime==1.15.0
|
50 |
+
openai==0.27.8
|
51 |
+
openapi-schema-pydantic==1.2.4
|
52 |
+
overrides==7.3.1
|
53 |
+
packaging==23.1
|
54 |
+
pandas==2.0.2
|
55 |
+
Pillow==9.5.0
|
56 |
+
posthog==3.0.1
|
57 |
+
protobuf==4.23.2
|
58 |
+
pulsar-client==3.2.0
|
59 |
+
pyarrow==12.0.0
|
60 |
+
pydantic==1.10.8
|
61 |
+
pydeck==0.8.1b0
|
62 |
+
Pygments==2.15.1
|
63 |
+
Pympler==1.0.1
|
64 |
+
pyrsistent==0.19.3
|
65 |
+
python-dateutil==2.8.2
|
66 |
+
python-dotenv==1.0.0
|
67 |
+
pytz==2023.3
|
68 |
+
pytz-deprecation-shim==0.1.0.post0
|
69 |
+
PyYAML==6.0
|
70 |
+
regex==2023.6.3
|
71 |
+
requests==2.31.0
|
72 |
+
rich==13.4.1
|
73 |
+
six==1.16.0
|
74 |
+
smmap==5.0.0
|
75 |
+
sniffio==1.3.0
|
76 |
+
soupsieve==2.4.1
|
77 |
+
SQLAlchemy==2.0.15
|
78 |
+
starlette==0.27.0
|
79 |
+
streamlit==1.23.1
|
80 |
+
sympy==1.12
|
81 |
+
tenacity==8.2.2
|
82 |
+
tiktoken==0.4.0
|
83 |
+
tokenizers==0.13.3
|
84 |
+
toml==0.10.2
|
85 |
+
toolz==0.12.0
|
86 |
+
tornado==6.3.2
|
87 |
+
tqdm==4.65.0
|
88 |
+
typing-inspect==0.9.0
|
89 |
+
typing_extensions==4.6.3
|
90 |
+
tzdata==2023.3
|
91 |
+
tzlocal==4.3
|
92 |
+
urllib3==2.0.3
|
93 |
+
uvicorn==0.22.0
|
94 |
+
uvloop==0.17.0
|
95 |
+
validators==0.20.0
|
96 |
+
watchfiles==0.19.0
|
97 |
+
websockets==11.0.3
|
98 |
+
wikipedia==1.4.0
|
99 |
+
yarl==1.9.2
|
100 |
+
zipp==3.15.0
|
101 |
+
zstandard==0.21.0
|