Skip to content

Commit d2850de

Browse files
committed
Version3 for gpt4free model
1. Add the gpt4free model for app, user can choose use gpt4free or openai model. 2. In the gpt4free model, we'll use HuggingFace embedding for document which is no need openai api key. 3. Clean and refactor code.
1 parent 922ffea commit d2850de

File tree

11 files changed

+146
-124
lines changed

11 files changed

+146
-124
lines changed

Pipfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ faiss-cpu = "==1.7.4"
1414
tiktoken = "==0.4.0"
1515
tenacity = "==8.1.0"
1616
google-search-results = "==2.4.2"
17+
google-cloud-aiplatform = ">=1.26.0"
18+
sentence-transformers = "*"
1719

1820
[dev-packages]
1921

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818

1919
### docGPT-V2 vs docGPT-V3
2020

21-
In docGPT-V3, we introduced the usage of `gpt4free`, allowing users to use the application **for free without entering any API key or making payments** (For more information about [`gpt4free`](https://github.com/xtekky/gpt4free), please refer to the source project).
21+
In docGPT-V3, we introduced the usage of `gpt4free`, allowing users to use the application **for free without entering any API key or making payments**.
22+
23+
If you want to use the `gpt4free` free model, you need to select a `Provider` (default is `g4f.provider.ChatgptAi`).
24+
25+
(For more information about [`gpt4free`](https://github.com/xtekky/gpt4free), please refer to the source project).
2226

2327
* Version2
2428
* Utilizes the **`openai` model**
@@ -33,7 +37,7 @@ In docGPT-V3, we introduced the usage of `gpt4free`, allowing users to use the a
3337
* `openai`: Stable access to the `openai` model by providing an API key
3438

3539
<p align="center">
36-
<img src="img/2023-08-29-11-51-13.png" width="70%">
40+
<img src="img/2023-08-29-13-39-00.png" width="70%">
3741
</p>
3842

3943
If you like this project, please give it a ⭐`Star` to support the developers~
@@ -97,8 +101,8 @@ With Langchain, we can create our own ChatGPT model that can be general-purpose
97101

98102
* Visit the [application](https://docgpt-app.streamlit.app/).
99103

100-
* Enter your API keys:
101-
* `OpenAI API Key`: Required.
104+
* Enter your API keys: (This step is optional in version V3, you can choose to skip it and use the `gpt4free` free model)
105+
* `OpenAI API Key`: Make sure you still have usage left
102106
* `SERPAPI API Key`: Optional. If you want to ask questions about content not appearing in the PDF document, you need this key.
103107

104108
* Upload a PDF file from your local machine.

README.zh-TW.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
### docGPT-V2 vs docGPT-V3
1919

20-
我們在 docGPT-V3 引入 `gpt4free` 的調用,讓使用者可以在不輸入任何 api key、付費的情況下,**免費使用**該應用程序
20+
我們在 docGPT-V3 引入 `gpt4free` 的調用,讓使用者可以在不輸入任何 api key、付費的情況下,**免費使用**該應用程序。
21+
22+
如果您要使用 `gpt4free` 免費模型,您需要選擇 `Provider` (預設是 `g4f.provider.ChatgptAi`)。
23+
2124
(更多 [`gpt4free`](https://github.com/xtekky/gpt4free) 的資訊請參考源專案)
2225

2326
* Version2
@@ -33,7 +36,7 @@
3336
* `openai`: 帶入 api key,穩定調用 `openai` 模型
3437

3538
<p align="center">
36-
<img src="img/2023-08-29-11-51-13.png" width="70%">
39+
<img src="img/2023-08-29-13-39-00.png" width="70%">
3740
</p>
3841

3942
如果您喜歡這個專案,請給予⭐`Star`以支持開發者~
@@ -100,8 +103,8 @@
100103

101104
* 前往[應用程序](https://docgpt-app.streamlit.app/)
102105

103-
* 輸入您的 `API_KEY`:
104-
* `OpenAI API KEY`: 必須設定
106+
* 輸入您的 `API_KEY` (此步驟在V3版本,可以選擇忽略,使用 `gpt4free` 免費模型):
107+
* `OpenAI API KEY`: 務必確認是否還有使用量
105108
* `SERPAPI API KEY`: 根據您需求,如果您要問**PDF文檔沒有出現**的內容,您就需要用此 KEY
106109

107110
* 上傳來自本地的 PDF 檔案

agent/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

app.py

Lines changed: 31 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,46 @@
88
import langchain
99
import streamlit as st
1010
from langchain.cache import InMemoryCache
11-
from langchain.chat_models import ChatOpenAI
1211
from streamlit import logger
1312
from streamlit_chat import message
1413

15-
from agent import AgentHelper
16-
from docGPT import DocGPT, OpenAiAPI, SerpAPI, GPT4Free
14+
from docGPT import GPT4Free, create_doc_gpt
1715
from model import PDFLoader
18-
import g4f
19-
from tenacity import retry, stop_after_attempt
2016

2117
langchain.llm_cache = InMemoryCache()
2218

2319
OPENAI_API_KEY = ''
2420
SERPAPI_API_KEY = ''
25-
agent_ = None
21+
model = None
2622

2723
st.session_state.openai_api_key = None
2824
st.session_state.serpapi_api_key = None
2925
st.session_state.g4f_provider = None
3026
app_logger = logger.get_logger(__name__)
3127

3228

33-
def theme():
29+
def theme() -> None:
3430
st.set_page_config(page_title="DocGPT")
3531
icon, title = st.columns([3, 20])
3632
with icon:
3733
st.image('./img/chatbot.png')
3834
with title:
3935
st.title('PDF Chatbot')
36+
37+
with st.sidebar:
38+
39+
with st.expander(':orange[How to use?]'):
40+
st.markdown(
41+
"""
42+
1. Enter your API keys: (You can choose to skip it and use the `gpt4free` free model)
43+
* `OpenAI API Key`: Make sure you still have usage left
44+
* `SERPAPI API Key`: Optional. If you want to ask questions about content not appearing in the PDF document, you need this key.
45+
2. Upload a PDF file from your local machine.
46+
3. Start asking questions!
47+
4. More details.(https://github.com/Lin-jun-xiang/docGPT-streamlit)
48+
5. If you have any questions, feel free to leave comments and engage in discussions.(https://github.com/Lin-jun-xiang/docGPT-streamlit/issues)
49+
"""
50+
)
4051

4152

4253
def load_api_key() -> None:
@@ -80,7 +91,7 @@ def load_api_key() -> None:
8091
)
8192

8293

83-
def upload_and_process_pdf():
94+
def upload_and_process_pdf() -> list:
8495
upload_file = st.file_uploader('#### Upload a PDF file:', type='pdf')
8596
if upload_file:
8697
temp_file = tempfile.NamedTemporaryFile(delete=False)
@@ -98,79 +109,30 @@ def upload_and_process_pdf():
98109

99110

100111
@lru_cache(maxsize=20)
101-
def get_response(query: str):
112+
def get_response(query: str) -> str:
102113
try:
103-
if agent_.agent_ is not None:
104-
response = agent_.query(query)
114+
if model is not None:
115+
response = model.run(query)
105116
return response
106117
except Exception as e:
107-
app_logger.info(e)
118+
app_logger.info(f'{__file__}: {e}')
119+
return (
120+
'Something wrong in docGPT...\n'
121+
'1. If you are using gpt4free model, '
122+
'try to select the different provider.\n'
123+
'2. If you are using openai model, '
124+
'check your usage for openai api key.'
125+
)
108126

109127

110128
theme()
111129
load_api_key()
112130

113131
doc_container = st.container()
114132

115-
116133
with doc_container:
117134
docs = upload_and_process_pdf()
118-
119-
if docs:
120-
docGPT = DocGPT(docs=docs)
121-
docGPT_tool, calculate_tool, search_tool, llm_tool = [None]*4
122-
agent_ = AgentHelper()
123-
124-
if OpenAiAPI.is_valid():
125-
# Use openai llm model
126-
docGPT.llm = ChatOpenAI(
127-
temperature=0.2,
128-
max_tokens=6000,
129-
model_name='gpt-3.5-turbo-16k'
130-
)
131-
agent_.llm = ChatOpenAI(
132-
temperature=0.2,
133-
max_tokens=6000,
134-
model_name='gpt-3.5-turbo-16k'
135-
)
136-
docGPT.create_qa_chain(
137-
chain_type='refine',
138-
)
139-
140-
docGPT_tool = agent_.create_doc_chat(docGPT)
141-
calculate_tool = agent_.get_calculate_chain
142-
llm_tool = agent_.create_llm_chain()
143-
144-
if SerpAPI.is_valid():
145-
search_tool = agent_.get_searp_chain
146-
else:
147-
# Use gpt4free llm model
148-
docGPT.llm = GPT4Free(
149-
provider=GPT4Free().PROVIDER_MAPPING[
150-
st.session_state.g4f_provider
151-
]
152-
)
153-
agent_.llm = GPT4Free(
154-
provider=GPT4Free().PROVIDER_MAPPING[
155-
st.session_state.g4f_provider
156-
]
157-
)
158-
docGPT.create_qa_chain(
159-
chain_type='refine',
160-
)
161-
docGPT_tool = agent_.create_doc_chat(docGPT)
162-
try:
163-
tools = [
164-
docGPT_tool,
165-
search_tool,
166-
# llm_tool, # This will cause agent confuse
167-
calculate_tool
168-
]
169-
agent_.initialize(tools)
170-
except Exception as e:
171-
app_logger.info(e)
172-
173-
135+
model = create_doc_gpt(docs)
174136
st.write('---')
175137

176138
if 'response' not in st.session_state:

docGPT/__init__.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,69 @@
1-
from .docGPT import DocGPT, GPT4Free
1+
import os
2+
3+
import openai
4+
import streamlit as st
5+
from langchain.chat_models import ChatOpenAI
6+
from streamlit import logger
7+
8+
from .agent import AgentHelper
29
from .check_api_key import OpenAiAPI, SerpAPI
10+
from .docGPT import DocGPT, GPT4Free
11+
12+
openai.api_key = os.getenv('OPENAI_API_KEY')
13+
os.environ['SERPAPI_API_KEY'] = os.getenv('SERPAPI_API_KEY')
14+
module_logger = logger.get_logger(__name__)
15+
16+
17+
def create_doc_gpt(docs):
18+
if not docs:
19+
return
20+
21+
docGPT = DocGPT(docs=docs)
22+
23+
try:
24+
if OpenAiAPI.is_valid():
25+
# Use openai llm model with agent
26+
docGPT_tool, calculate_tool, search_tool, llm_tool = [None] * 4
27+
agent_ = AgentHelper()
28+
29+
llm_model = ChatOpenAI(
30+
temperature=0.2,
31+
max_tokens=6000,
32+
model_name='gpt-3.5-turbo-16k'
33+
)
34+
docGPT.llm = llm_model
35+
agent_.llm = llm_model
36+
with st.spinner('Running...'):
37+
docGPT.create_qa_chain(
38+
chain_type='refine',
39+
)
40+
docGPT_tool = agent_.create_doc_chat(docGPT)
41+
calculate_tool = agent_.get_calculate_chain
42+
llm_tool = agent_.create_llm_chain()
43+
44+
if SerpAPI.is_valid():
45+
search_tool = agent_.get_searp_chain
46+
47+
tools = [
48+
docGPT_tool,
49+
search_tool,
50+
# llm_tool, # This will cause agent confuse
51+
calculate_tool
52+
]
53+
agent_.initialize(tools)
54+
return agent_ if agent_ is not None else None
55+
else:
56+
# Use gpt4free llm model without agent
57+
llm_model = GPT4Free(
58+
provider=GPT4Free().PROVIDER_MAPPING[
59+
st.session_state.g4f_provider
60+
]
61+
)
62+
docGPT.llm = llm_model
63+
with st.spinner('Running...(free model will take more time)'):
64+
docGPT.create_qa_chain(
65+
chain_type='refine',
66+
)
67+
return docGPT
68+
except Exception as e:
69+
module_logger.info(f'{__file__}: {e}')

agent/agent.py renamed to docGPT/agent.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from langchain.agents import AgentType, Tool, initialize_agent
77
from langchain.callbacks import get_openai_callback
88
from langchain.chains import LLMChain
9-
from langchain.chat_models import ChatOpenAI
109
from langchain.prompts import PromptTemplate
1110

1211
openai.api_key = os.getenv('OPENAI_API_KEY')
@@ -86,12 +85,15 @@ def initialize(self, tools):
8685
verbose=True
8786
)
8887

89-
def query(self, query: str) -> Optional[str]:
88+
def run(self, query: str) -> Optional[str]:
9089
response = None
9190
with get_openai_callback() as callback:
92-
# TODO: The true result will hide in 'Observation'
93-
# https://github.com/hwchase17/langchain/issues/4916
94-
# https://python.langchain.com/docs/modules/agents/how_to/intermediate_steps
95-
response = self.agent_.run(query)
91+
try:
92+
response = self.agent_.run(query)
93+
except ValueError as e:
94+
response = 'Something wrong in agent: ' + str(e)
95+
if not response.startswith("Could not parse LLM output: `"):
96+
raise e
97+
9698
print(callback)
9799
return response

docGPT/check_api_key.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class OpenAiAPI(ApiKey):
2020
@classmethod
2121
def is_valid(cls) -> str:
2222
if not st.session_state['openai_api_key']:
23-
st.error('⚠️ :red[You have not pass OpenAI API key.] Use default key')
23+
st.error('⚠️ :red[You have not pass OpenAI API key.] Use default model')
2424
return
2525

2626
openai.api_key = os.getenv('OPENAI_API_KEY')
@@ -57,4 +57,3 @@ def is_valid(cls) -> str:
5757
'[Check your usage](https://serpapi.com/dashboard)'
5858
)
5959
print(f'Test error\n{e}')
60-

0 commit comments

Comments
 (0)