Skip to content

Commit 93d81b9

Browse files
committed
Fix & Improve (see details)
1. The agent llm were used with `max_tokens=256`(default), we convert llm be gpt-3.5-turbo-16k with `max_tokens=6000` 2. After expand the max_tokens, the agent response(thought) will not incomplete.
1 parent 63c0dd4 commit 93d81b9

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

agent/agent.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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.llms import OpenAI
9+
from langchain.chat_models import ChatOpenAI
1010
from langchain.prompts import PromptTemplate
1111

1212
openai.api_key = os.getenv('OPENAI_API_KEY')
@@ -16,7 +16,11 @@
1616
class AgentHelper:
1717
"""Add agent to help docGPT can be perfonm better."""
1818
def __init__(self) -> None:
19-
self.llm = OpenAI(temperature=0)
19+
self.llm = ChatOpenAI(
20+
temperature=0.2,
21+
max_tokens=6000,
22+
model_name='gpt-3.5-turbo-16k'
23+
)
2024
self.agent_ = None
2125
self.tools = []
2226

@@ -32,7 +36,6 @@ def get_calculate_chain(self) -> Tool:
3236

3337
@property
3438
def get_searp_chain(self) -> Tool:
35-
3639
search = SerpAPIWrapper()
3740
tool = Tool(
3841
name='Search',
@@ -47,7 +50,7 @@ def create_doc_chat(self, docGPT) -> Tool:
4750
name='DocumentGPT',
4851
func=docGPT.run,
4952
description="""
50-
useful for when you need to answer questions from the context of PDF,
53+
useful for when you need to answer questions from the context of PDF
5154
"""
5255
)
5356
return tool
@@ -58,12 +61,12 @@ def create_llm_chain(self) -> Tool:
5861
input_variables = ['query'],
5962
template = '{query}'
6063
)
61-
llm_chain = LLMChain(llm=self.llm, prompt = prompt)
64+
llm_chain = LLMChain(llm=self.llm, prompt=prompt)
6265

6366
tool = Tool(
6467
name='LLM',
6568
func=llm_chain.run,
66-
description='useful for general purpose queries and logic'
69+
description='useful for general purpose queries and logic.'
6770
)
6871
return tool
6972

app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from docGPT import DocGPT
1515
from model import PDFLoader
1616

17-
1817
langchain.llm_cache = InMemoryCache()
1918

2019
OPENAI_API_KEY = ''
@@ -82,7 +81,7 @@ def load_api_key() -> None:
8281
temp_file_path = temp_file.name
8382

8483
docs = PDFLoader.load_documents(temp_file_path)
85-
docs = PDFLoader.split_documents(docs, chunk_size=2500, chunk_overlap=200)
84+
docs = PDFLoader.split_documents(docs, chunk_size=2000, chunk_overlap=200)
8685

8786
temp_file.close()
8887
if temp_file_path:
@@ -113,7 +112,8 @@ def load_api_key() -> None:
113112
tools = [
114113
docGPT_tool,
115114
search_tool,
116-
llm_tool
115+
# llm_tool, # This will cause agent confuse
116+
calculate_tool
117117
]
118118
agent_.initialize(tools)
119119
except Exception as e:

docGPT/docGPT.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
import openai
55
from langchain.callbacks import get_openai_callback
66
from langchain.chains import ConversationalRetrievalChain, RetrievalQA
7+
from langchain.chat_models import ChatOpenAI
78
from langchain.embeddings.openai import OpenAIEmbeddings
8-
from langchain.llms import OpenAI
99
from langchain.memory import ConversationBufferMemory
1010
from langchain.prompts import PromptTemplate
1111
from langchain.vectorstores import Chroma
12-
from langchain.chat_models import ChatOpenAI
13-
1412

1513
openai.api_key = os.getenv('OPENAI_API_KEY')
1614

@@ -84,8 +82,8 @@ def __init__(self, docs):
8482
self.qa_chain = None
8583
self.llm = ChatOpenAI(
8684
temperature=0.2,
87-
max_tokens=2000,
88-
model_name='gpt-3.5-turbo'
85+
max_tokens=6000,
86+
model_name='gpt-3.5-turbo-16k'
8987
)
9088

9189
self.prompt_template = """

model/data_connection.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import json
22
import os
3+
from typing import Iterator
34

45
from langchain.document_loaders import PyMuPDFLoader
56
from langchain.text_splitter import RecursiveCharacterTextSplitter
67

78

89
class PDFLoader:
910
@staticmethod
10-
def get_pdf_files(path: str) -> list:
11-
if path.endswith('.pdf'):
12-
return f'./PDF/uploaded/{path}'
13-
14-
else:
15-
file_names = os.listdir(f'./PDF/{path}')
16-
pdf_files = [name for name in file_names if name.endswith('.pdf')]
17-
18-
return pdf_files
11+
def get_pdf_files(path: str) -> Iterator[str]:
12+
try:
13+
yield from [
14+
file_name for file_name in os.listdir(f'{path}')
15+
if file_name.endswith('.pdf')
16+
]
17+
except FileNotFoundError as e:
18+
print(f'\033[31m{e}')
1919

2020
@staticmethod
2121
def load_documents(pdf_file: str) -> PyMuPDFLoader:

0 commit comments

Comments
 (0)