-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
89 lines (70 loc) · 2.21 KB
/
app.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
from apikey import apikey
import streamlit as st
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
# from langchain.chains import LLMChain, SequentialChain
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory
from langchain.utilities import WikipediaAPIWrapper
os.environ['OPENAI_API_KEY'] = apikey
# App framework
st.title('🦜️🔗 Book GPT Recommender 📚')
prompt = st.text_input('Give me a topic, you\'ll get back some good books:')
# Prompt templates
title_template = PromptTemplate(
input_variables=['topic'],
template='give me some book titles about {topic}'
)
script_template = PromptTemplate(
input_variables=['title', 'wikipedia_research'],
template='write me a summary script of the given topic based on this title TITLE: {title} while leveraging this wikipedia research: {wikipedia_research}'
)
# Memory
title_memory = ConversationBufferMemory(
input_key='topic',
memory_key='chat_history'
)
script_memory = ConversationBufferMemory(
input_key='title',
memory_key='chat_history'
)
# Llms
llm = OpenAI(temperature=0.9)
title_chain = LLMChain(
llm=llm,
prompt=title_template,
verbose=True,
output_key='title',
memory=title_memory
)
script_chain = LLMChain(
llm=llm,
prompt=script_template,
verbose=True,
output_key='script',
memory=script_memory
)
# sequential_chain = SequentialChain(
# chains=[title_chain, script_chain],
# input_variables=['topic'],
# output_variables=['title', 'script'],
# verbose=True
# )
wiki = WikipediaAPIWrapper()
# Show response to the screen if there's a prompt
if prompt:
# response = sequential_chain({'topic': prompt})
# st.write(response['title'])
# st.write(response['script'])
title = title_chain.run(prompt)
wiki_research = wiki.run(prompt)
script = script_chain.run(title=title, wikipedia_research=wiki_research)
st.write(title)
st.write(script)
with st.expander('Title History'):
st.info(title_memory.buffer)
with st.expander('Script History'):
st.info(script_memory.buffer)
with st.expander('Wikipedia Research'):
st.info(wiki_research)