preamble
Today I'm going to teach you aboutHow to implement the nonebot plugin of ChatGpt
Note that this article involves asynchronous crawler, json file read and write knowledge points
intend
1. Get developer key
Get the address of the key:Account API Keys - OpenAI API
As you can see in the picture, I have created a key, you can also click on theCreate new secret key
button to create a new key, be careful, do not give away your key!
2. Magic
In the process of obtaining the key we still need to use magic, and the proxy must be foreign, as long as the key to get it, the subsequent steps do not need to use magic
commencement
1. Find the interface
I originally wanted to teach you to dock the official interface to OpenAI, but thought that most students may not be "magic", if there is no "magic" experience will be greatly reduced, so we have to use other big brother to help us complete the agent! The process
I found this gem of a website while surfing the webGPT3.5 ()
You can see that this site is required to provide us with the key, I here shallow explanation of the main ideas of this program
The overall idea is probably this
Here, we should have the key, we just opened the site, press F12 to open the developer debugging tools
Here we can see the requested api address:/question
We can see that he's a post request
Then we open the source analysis, it is not difficult to see that the data is json format, and there are two parameters, akey
Onequestion
Now that we've found the interface, the next step is the big one - writing the code!
2. Happy coding
Let's get to the good stuff.
from nonebot import on_keyword from import T_State from .v11 import GroupMessageEvent, Bot, Message import httpx import json ''' Implementing qq group chat calling chatgpt write by munchkin source at 2023/3/5 ''' chatgpt = on_keyword({"#gpt"}) @() async def yy(bot: Bot, event: GroupMessageEvent, state: T_State): get_msg = str().strip() # Get the link sent by the user user_question = get_msg.strip("#gpt") msg_id = event.message_id # Get the message id user_id = event.user_id file_name = "" try: with open(file_name, "r", encoding="UTF-8") as f: person_data = (f) # Read the data try: user_data = person_data[str(user_id)] except KeyError: user_data = "" form_data = {'key': 'Fill in your own key', 'question': user_data + f"----{user_question}"} async with () as client: headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE' } url = f"/question" data = await (url=url, headers=headers, json=form_data, timeout=None) # of requests for data response = () try: # Circumvent content acquisition failures res_ai = str(response['content']) try: user_data = person_data[str(user_id)] + f"----{user_question}" question = user_data + f'\n{res_ai}' person_data[str(user_id)] = question except KeyError: person_data[str(user_id)] = f'----{user_question}' with open(file_name, "w", encoding="UTF-8") as f: (person_data, f, ensure_ascii=False) ai_res = str(response['content']).strip("\n") except KeyError: # Reset session person_data[user_id] = "" with open(file_name, "w", encoding="UTF-8") as f: (person_data, f, ensure_ascii=False) ai_res = 'Sorry, content acquisition failed, please make sure your question does not have illegal components, if not it may be that your session history has reached its limit, please change your questioning method or try again' except FileNotFoundError: with open(file_name, "w", encoding="UTF-8") as f: ({user_id: f'----{user_question}'}, f, ensure_ascii=False) form_data = {'key': 'Fill in your own key', 'question': user_question} async with () as client: headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE' } url = f"/question" data = await (url=url, headers=headers, json=form_data, timeout=None) # of requests for data response = () ai_res = str(response['content']).strip("\n") res = f"[CQ:reply,id={msg_id}]{ai_res}" await (Message(res))
I'm also going to go straight to the full code here
Next, I'll pick out some code that I think is more interesting to study and talk about it
try: with open(file_name, "r", encoding="UTF-8") as f: #① person_data = (f) # Read the data try: user_data = person_data[str(user_id)] except KeyError: user_data = "" form_data = {'key': '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', 'question': user_data + f"----{user_question}"}
For example, in this paragraph, in ① this place, I read a json file used to store the user's session, so some people may ask, why read such a file? Or what is the purpose of this file?
In fact, in the early version of the session file is not available, after the group's test, I found a small problem, that is, the conversation is not continuous, for example, I want to play with the gpt idioms solitaire, but the session is not continuous ah, so I found a file to store the user's session method, the file structure is probably this way
It's storing each person's session data separately so that the conversation has continuity
The continuity problem was solved, but a new problem arose - the session was too long, gpt didn't know how to answer or couldn't get the content properly, so what to do, so I came up with the following method to solve the problem
try: # Circumvent content acquisition failures res_ai = str(response['content']) try: user_data = person_data[str(user_id)] + f"----{user_question}" question = user_data + f'\n{res_ai}' person_data[str(user_id)] = question except KeyError: person_data[str(user_id)] = f'----{user_question}' with open(file_name, "w", encoding="UTF-8") as f: (person_data, f, ensure_ascii=False) ai_res = str(response['content']).strip("\n") except KeyError: # Reset session person_data[user_id] = "" with open(file_name, "w", encoding="UTF-8") as f: (person_data, f, ensure_ascii=False) ai_res = 'Sorry, content acquisition failed, please make sure your question does not have illegal components, if not it may be that your session history has reached its limit, please change your questioning method or try again'
As you can see, I've written a try statement here to clear the session when it fails to get the content and return a message to the user so they can ask the question again.
These are the parts of the code that I think need to be explained, I'm sure you can see where I'm going with this, oh yeah.Remember to replace the key
For more information on the nonebot plugin chatgpt follow my other related posts!