From e78fc1a4a7cd4e0d2f4c36bf99e158060dd1797d Mon Sep 17 00:00:00 2001 From: Grey_D Date: Sun, 19 Mar 2023 19:39:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Update=20playwright=20fo?= =?UTF-8?q?r=20interface=20consistency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now the playwright version has the same interface as the API version. --- example_chatgpt_playwright.py | 6 ++-- utils/chatgpt_browser.py | 67 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 utils/chatgpt_browser.py diff --git a/example_chatgpt_playwright.py b/example_chatgpt_playwright.py index 4d5c613..5346344 100644 --- a/example_chatgpt_playwright.py +++ b/example_chatgpt_playwright.py @@ -1,11 +1,13 @@ from chatgpt_wrapper import ChatGPT - +from llm_handle.parser import extract_cmd +from task_handle.cmd_execution import execute_cmd import os if __name__ == "__main__": bot = ChatGPT() conversations = bot.get_history() + print(conversations) # structure of conversation: # {conversation_id (str): {'id': conversation_id, 'title': conversation_title, 'create_time': conversation_create_time'}} @@ -17,5 +19,5 @@ if __name__ == "__main__": ## Try to ask a question in this conversation question = "What is the meaning of life?" - success, response, message = bot.ask("Hello, world!") + response = bot.ask("Hello, world!") print(response) diff --git a/utils/chatgpt_browser.py b/utils/chatgpt_browser.py new file mode 100644 index 0000000..44893bd --- /dev/null +++ b/utils/chatgpt_browser.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- + +import json +import re +import time +from uuid import uuid1 +import datetime + +import loguru +import requests + +from chatgpt_wrapper import ChatGPT +from chatgpt_wrapper.config import Config + +logger = loguru.logger + + +class ChatGPTBrowser: + """ + The ChatGPT Wrapper based on browser (playwright). + It keeps the same interface as ChatGPT. + """ + def __init__(self, model=None): + config = Config() + if model is not None: + config.set('chat.model', model) + self.bot = ChatGPT(config) + + def get_authorization(self): + # TODO: get authorization from browser + return + + def get_latest_message_id(self, conversation_id): + # TODO: get latest message id from browser + return + + def get_conversation_history(self, limit=20, offset=0): + # Get the conversation id in the history + return self.bot.get_history(limit, offset) + + def send_new_message(self, message): + # 发送新会话窗口消息,返回会话id + response = self.bot.ask(message) + latest_uuid = self.get_conversation_history(limit=1, offset=0).keys()[0] + return response, latest_uuid + + def send_message(self, message, conversation_id): + # 发送会话窗口消息 + # TODO: send message from browser + return + + def extract_code_fragments(self, text): + code_fragments = re.findall(r"```(.*?)```", text, re.DOTALL) + return code_fragments + + + def delete_conversation(self, conversation_id=None): + # delete conversation with its uuid + if conversation_id is not None: + self.bot.delete_conversation(conversation_id) + +if __name__ == "__main__": + chatgptBrowser_session = ChatGPTBrowser() + text, conversation_id = chatgptBrowser_session.send_new_message( + "I am a new tester for RESTful APIs." + ) +