Why does ChatGPT speak serious nonsense? How can we improve it? | Text with code
As we all know, the model on the OpenAI platform
ChatGPT
currently has two major pain points: 1. The data it learns are all up to 2021, so it is impossible to give what happened after 2022. 2. Sometimes there will be some common-sense mistakes, that is, it will talk nonsense in a serious manner, such as asking it some history and mathematics questions, and it may answer the wrong answer like that .
The reason is mainly that ChatGPT is a "generative reply". It has learned a lot of human knowledge and human guidance and reward strategies in advance. It essentially relies on a neural network model with a huge amount of parameters, and its training process is based on its corpus. The training data itself is not all high-quality text, so there may be factual errors.ChatGPT
The answers given are only based on the best results generated by its understanding. However, for certain training knowledge blind spots, it may reason according to the literal meaning, resulting in the phenomenon of "serious nonsense".
Therefore, in order to solve the above two problems, we can use search engines toChatGPT
provide accurate and timely auxiliary information to the Internet. All in all, the overall solution is to throw the question-related information crawled on the search engine to CharGPT, and let it help us integrate and output the answers we want.
Article directory
1. Fast track
If readers with code proficiency want to debug the complete code directly, they can go to my repository to download the complete and runnable one directly, and due to the reasons stipulated by the CSDN community, the complete content cannot be written on it, so the blogger puts all the code and blog on it. This repo is asking for Star! : https://github.com/lsl1229840757/chatbot .
2. Limitations and factual errors of ChatGPT history
In the abstract, the blogger has analyzed in detail that ChatGPT (1) cannot answer what will happen after 2022; (2) will make some common sense mistakes, these two major problems. Let us take a chestnut to see:
When we asked him which team to win the 2022 World Cup, it said it couldn't be predicted.
When we asked him what hook three-strand four-string five is, he would tell us solemnly that this is the tuning method of the ancient Chinese instrument Qin:
3. Effect demonstration
There is no harm without comparison, let's not talk much, and directly show the effect:
- Ask about the 2022 World Cup Champion Team
- Ask about the Pythagorean Theorem
- view recent news
5. Code example
import warnings
import openai
from selenium import webdriver
from selenium.webdriver.common.by import By
warnings.filterwarnings('ignore')
class ChatGPT(object):
def __init__(self, api_key) -> None:
self._api_key = None
self.api_key = api_key
options = webdriver.ChromeOptions()
options.add_argument('headless') # 设置不弹出浏览器
options.add_experimental_option('excludeSwitches',
['enable-logging']) # 控制台不输出
self.browser = webdriver.Chrome(options=options)
@property
def api_key(self):
return self._api_key
@api_key.setter
def api_key(self, api_key):
self._api_key = api_key
# Load your API key
openai.api_key = api_key
def _grabbing_data(self, wd):
url = f'https://www.baidu.com/s?wd={
wd}'
self.browser.get(url)
self.browser.execute_script(
'window.scrollTo(0, document.body.scrollHeight)')
data = []
results = self.browser.find_elements(By.CLASS_NAME, 'c-border') \
+ self.browser.find_elements(By.CLASS_NAME, 'result-op') \
+ self.browser.find_elements(By.CLASS_NAME, 'result')
for result in results[:5]:
a_link = result.find_element(By.TAG_NAME, 'a')
href = a_link.get_attribute('href')
data.append(result.text + f'来源:{
href}')
return data
def _format_prompt(self, prompt):
print(f'正在百度中搜索{
prompt}相关资料...')
data = self._grabbing_data(prompt)
data = '\n'.join(data)
prompt = f'请根据以下辅助信息回答问题: {
data[:1000]}\n 请问:{
prompt}'
return prompt
def get_answer(self, prompt):
prompt = self._format_prompt(prompt)
response = openai.Completion.create(model="text-davinci-003",
prompt=prompt,
temperature=0.6,
max_tokens=2048)
return response['choices'][0]['text']
def chat(self):
flag = True
print('欢迎使用百度版ChatGPT, 我使用了百度作为资料搜索支撑引擎, 如果您想退出请输入exit!')
while flag:
prompt = input()
if prompt != 'exit':
print(self.get_answer(prompt))
else:
print('再见!')
flag = False
4. References
- https://cloud.tencent.com/developer/article/2190154
- https://platform.openai.com/account/api-keys
- https://platform.openai.com/docs/introduction/overview