feat: 🎸 Update playwright for interface consistency

Now the playwright version has the same interface as the API version.
This commit is contained in:
Grey_D
2023-03-19 19:39:57 +08:00
parent a3e4d5696c
commit e78fc1a4a7
2 changed files with 71 additions and 2 deletions

View File

@@ -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)

67
utils/chatgpt_browser.py Normal file
View File

@@ -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."
)