Skip to content

Commit 7dcb0e8

Browse files
committed
Reduce memory: move usually unused module from global to local scope
1 parent 7fd30f9 commit 7dcb0e8

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

app.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
os.chdir(os.path.dirname(os.path.abspath(__file__)))
66
os.environ['SERPAPI_API_KEY'] = ''
77

8-
import langchain
98
import streamlit as st
10-
from langchain.cache import InMemoryCache
119
from streamlit import logger
1210
from streamlit_chat import message
1311

1412
from docGPT import GPT4Free, create_doc_gpt
1513
from model import DocumentLoader
1614

17-
langchain.llm_cache = InMemoryCache()
18-
1915
OPENAI_API_KEY = ''
2016
SERPAPI_API_KEY = ''
2117
model = None

docGPT/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def create_doc_gpt(
3939
docGPT.create_qa_chain(chain_type='refine', verbose=False)
4040
docGPT_tool = agent_.create_doc_chat(docGPT)
4141
calculate_tool = agent_.get_calculate_chain
42-
llm_tool = agent_.create_llm_chain()
42+
# llm_tool = agent_.create_llm_chain()
4343

4444
if SerpAPI.is_valid():
4545
search_tool = agent_.get_searp_chain

docGPT/agent.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import Optional
33

44
import openai
5-
from langchain import LLMMathChain, SerpAPIWrapper
65
from langchain.agents import AgentType, Tool, initialize_agent
76
from langchain.callbacks import get_openai_callback
87
from langchain.chains import LLMChain
@@ -29,6 +28,8 @@ def llm(self, llm) -> None:
2928

3029
@property
3130
def get_calculate_chain(self) -> Tool:
31+
from langchain import LLMMathChain
32+
3233
llm_math_chain = LLMMathChain.from_llm(llm=self.llm, verbose=True)
3334
tool = Tool(
3435
name='Calculator',
@@ -39,6 +40,8 @@ def get_calculate_chain(self) -> Tool:
3940

4041
@property
4142
def get_searp_chain(self) -> Tool:
43+
from langchain import SerpAPIWrapper
44+
4245
search = SerpAPIWrapper()
4346
tool = Tool(
4447
name='Search',

docGPT/check_api_key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import openai
55
import streamlit as st
6-
from langchain import SerpAPIWrapper
76

87

98
class ApiKey(ABC):
@@ -45,6 +44,7 @@ def is_valid(cls) -> str:
4544
if not st.session_state['serpapi_api_key']:
4645
st.warning('⚠️ You have not pass SerpAPI key. (You cannot ask current events.)')
4746
return
47+
from langchain import SerpAPIWrapper
4848

4949
os.environ['SERPAPI_API_KEY'] = os.getenv('SERPAPI_API_KEY')
5050
try:

docGPT/docGPT.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
import openai
88
from langchain.callbacks import get_openai_callback
99
from langchain.callbacks.manager import CallbackManagerForLLMRun
10-
from langchain.chains import ConversationalRetrievalChain, RetrievalQA
10+
from langchain.chains import RetrievalQA
1111
from langchain.embeddings import HuggingFaceEmbeddings
1212
from langchain.embeddings.openai import OpenAIEmbeddings
1313
from langchain.llms.base import LLM
14-
from langchain.memory import ConversationBufferMemory
1514
from langchain.prompts import PromptTemplate
1615
from langchain.vectorstores import FAISS
1716
from streamlit import logger
@@ -68,8 +67,11 @@ def __init__(
6867
super().__init__(chain_type, retriever, llm)
6968

7069
@property
71-
def create_qa_chain(self) -> ConversationalRetrievalChain:
70+
def create_qa_chain(self):
7271
# TODO: cannot use conversation qa chain
72+
from langchain.chains import ConversationalRetrievalChain
73+
from langchain.memory import ConversationBufferMemory
74+
7375
memory = ConversationBufferMemory(
7476
memory_key='chat_history',
7577
return_messages=True

0 commit comments

Comments
 (0)