From 22c0405d5a1884eac41b90ad2008e509aeffb5df Mon Sep 17 00:00:00 2001 From: FantWu <3383715710@qq.com> Date: Mon, 6 Mar 2023 21:16:23 +0800 Subject: [PATCH] Introducing "auto" mode. --- chatGPTEx/chatHistory.json | 72 ++++------------------------------ chatGPTEx/evaluator.py | 49 +++++++++++++++++++++++ chatGPTEx/evaluator_prompt.txt | 19 +++++++++ chatGPTEx/main.py | 3 ++ chatGPTEx/search.py | 1 - chatGPTEx/templates/index.html | 1 + 6 files changed, 80 insertions(+), 65 deletions(-) create mode 100644 chatGPTEx/evaluator.py create mode 100644 chatGPTEx/evaluator_prompt.txt diff --git a/chatGPTEx/chatHistory.json b/chatGPTEx/chatHistory.json index bdc5e25..b8bcbd3 100644 --- a/chatGPTEx/chatHistory.json +++ b/chatGPTEx/chatHistory.json @@ -6,83 +6,27 @@ }, { "role": "user", - "content": "write a quick sort algorithms" + "content": "Summariaze our diaglog" }, { "role": "assistant", - "content": "Sure, I can help you with that!\n\nQuick sort is a popular sorting algorithm that uses divide and conquer strategy. Here's a basic implementation of quick sort in Python:\n\n```\ndef quick_sort(arr):\n if len(arr) <= 1:\n return arr\n else:\n pivot = arr[0]\n less = [x for x in arr[1:] if x <= pivot]\n greater = [x for x in arr[1:] if x > pivot]\n return quick_sort(less) + [pivot] + quick_sort(greater)\n```\n\nIn this implementation, we first check if the length of the array is less than or equal to 1. If so, we just return the array as it is already sorted. Otherwise, we select the first element of the array as the pivot and create two sub-arrays: one containing elements less than or equal to the pivot and another containing elements greater than the pivot. We then recursively apply quick sort to both sub-arrays and concatenate the sorted sub-arrays with the pivot element in between to get the final sorted array.\n\nI hope that helps! Let me know if you have any questions." + "content": "Multitasking is a computer operating system technology that allows multiple programs to run simultaneously on a computer, improving efficiency and resource utilization. The CPU's time is divided into several time slices, each of which can be allocated to a program, and these programs run simultaneously on the computer without interfering with each other. This technology can enable a computer to handle multiple tasks at the same time, thereby increasing its throughput. The main advantage of multitasking is to improve CPU and device utilization, enabling multiple programs to share computer resources. However, multitasking's disadvantages may include uneven resource utilization and interference between programs." }, { "role": "user", - "content": "hello" + "content": "Chat History info: [{'role': 'system', 'content': 'You are ExChatGPT, a web-based large language model, Respond conversationally'}, {'role': 'user', 'content': 'Summariaze our diaglog'}, {'role': 'assistant', 'content': \"Multitasking is a computer operating system technology that allows multiple programs to run simultaneously on a computer, improving efficiency and resource utilization. The CPU's time is divided into several time slices, each of which can be allocated to a program, and these programs run simultaneously on the computer without interfering with each other. This technology can enable a computer to handle multiple tasks at the same time, thereby increasing its throughput. The main advantage of multitasking is to improve CPU and device utilization, enabling multiple programs to share computer resources. However, multitasking's disadvantages may include uneven resource utilization and interference between programs.\"}]\n Query: current Time: 2023-03-06 20:35:19.380861\nQuery:\u4f60\u597d" }, { "role": "assistant", - "content": "Hello! How can I assist you today?" + "content": "\u4f60\u597d\uff01\u6709\u4ec0\u4e48\u6211\u53ef\u4ee5\u5e2e\u52a9\u60a8\u7684\u5417\uff1f" }, { "role": "user", - "content": "write a quick sort in python" + "content": "\u4f60\u597d" }, { "role": "assistant", - "content": "Sure, here's an implementation of quick sort in Python:\n\n```python\ndef quick_sort(arr):\n if len(arr) <= 1:\n return arr\n else:\n pivot = arr[0]\n less = [x for x in arr[1:] if x <= pivot]\n greater = [x for x in arr[1:] if x > pivot]\n return quick_sort(less) + [pivot] + quick_sort(greater)\n```\n\nIn this implementation, we first check if the length of the array is less than or equal to 1. If so, we just return the array as it is already sorted. Otherwise, we select the first element of the array as the pivot and create two sub-arrays: one containing elements less than or equal to the pivot and another containing elements greater than the pivot. We then recursively apply quick sort to both sub-arrays and concatenate the sorted sub-arrays with the pivot element in between to get the final sorted array.\n\nI hope that helps! Let me know if you have any questions." - }, - { - "role": "user", - "content": "write a quick sort in python" - }, - { - "role": "assistant", - "content": "Sure, here's an implementation of quick sort in Python:\n\n```python\ndef quick_sort(arr):\n if len(arr) <= 1:\n return arr\n else:\n pivot = arr[0]\n less = [x for x in arr[1:] if x <= pivot]\n greater = [x for x in arr[1:] if x > pivot]\n return quick_sort(less) + [pivot] + quick_sort(greater)\n```\n\nIn this implementation, we first check if the length of the array is less than or equal to 1. If so, we just return the array as it is already sorted. Otherwise, we select the first element of the array as the pivot and create two sub-arrays: one containing elements less than or equal to the pivot and another containing elements greater than the pivot. We then recursively apply quick sort to both sub-arrays and concatenate the sorted sub-arrays with the pivot element in between to get the final sorted array.\n\nI hope that helps! Let me know if you have any questions." - }, - { - "role": "user", - "content": "write a java for quick sort" - }, - { - "role": "assistant", - "content": "Sure, here's an implementation of quick sort in Java:\n\n```java\npublic static void quickSort(int[] arr, int low, int high) {\n if (low < high) {\n int pivotIndex = partition(arr, low, high);\n quickSort(arr, low, pivotIndex - 1);\n quickSort(arr, pivotIndex + 1, high);\n }\n}\n\npublic static int partition(int[] arr, int low, int high) {\n int pivot = arr[high];\n int i = low - 1;\n for (int j = low; j < high; j++) {\n if (arr[j] < pivot) {\n i++;\n int temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n }\n }\n int temp = arr[i + 1];\n arr[i + 1] = arr[high];\n arr[high] = temp;\n return i + 1;\n}\n```\n\nIn this implementation, we first check if the low index is less than the high index. If so, we select the pivot element (in this case, the last element of the array) and partition the array into two sub-arrays: one containing elements less than the pivot and another containing elements greater than or equal to the pivot. We then recursively apply quick sort to both sub-arrays until the entire array is sorted.\n\nI hope that helps! Let me know if you have any questions." - }, - { - "role": "user", - "content": "write a java for quick sort" - }, - { - "role": "assistant", - "content": "Sure, here's an implementation of quick sort in Java:\n\n```java\npublic static void quickSort(int[] arr, int low, int high) {\n if (low < high) {\n int pivotIndex = partition(arr, low, high);\n quickSort(arr, low, pivotIndex - 1);\n quickSort(arr, pivotIndex + 1, high);\n }\n}\n\npublic static int partition(int[] arr, int low, int high) {\n int pivot = arr[high];\n int i = low - 1;\n for (int j = low; j < high; j++) {\n if (arr[j] < pivot) {\n i++;\n int temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n }\n }\n int temp = arr[i + 1];\n arr[i + 1] = arr[high];\n arr[high] = temp;\n return i + 1;\n}\n```\n\nIn this implementation, we first check if the low index is less than the high index. If so, we select the pivot element (in this case, the last element of the array) and partition the array into two sub-arrays: one containing elements less than the pivot and another containing elements greater than or equal to the pivot. We then recursively apply quick sort to both sub-arrays until the entire array is sorted.\n\nI hope that helps! Let me know if you have any questions." - }, - { - "role": "user", - "content": "write a quick sort in python" - }, - { - "role": "assistant", - "content": "Sure, here is a Python implementation of quick sort algorithm:\n\n```python\ndef quick_sort(arr):\n if len(arr) <= 1:\n return arr\n else:\n pivot = arr[0]\n less = [x for x in arr[1:] if x <= pivot]\n greater = [x for x in arr[1:] if x > pivot]\n return quick_sort(less) + [pivot] + quick_sort(greater)\n```\n\nIn this implementation, we first check if the length of the array is less than or equal to 1. If yes, we return the array as it is already sorted. Otherwise, we select the first element of the array as the pivot and create two sub-arrays: one containing elements less than or equal to the pivot and another containing elements greater than the pivot. We then recursively apply quick sort to both sub-arrays and concatenate the sorted sub-arrays with the pivot element in between to get the final sorted array.\n\nI hope this helps! Let me know if you have any questions." - }, - { - "role": "user", - "content": "write a $ln(x) = $ in math format" - }, - { - "role": "assistant", - "content": "The equation $ln(x)=$ in math notation is:\n\n$$\\ln(x)=\\ldots$$\n\nThe natural logarithm function $\\ln(x)$ is the inverse of the exponential function $e^x$. It is defined as the logarithm to the base $e$, where $e$ is the mathematical constant approximately equal to $2.71828$. The natural logarithm function is commonly used in many areas of mathematics and science, particularly in calculus and statistics." - }, - { - "role": "user", - "content": "write a quick sort in python" - }, - { - "role": "assistant", - "content": "Sure, here is a Python implementation of quick sort algorithm:\n\n```python\ndef quick_sort(arr):\n if len(arr) <= 1:\n return arr\n else:\n pivot = arr[0]\n less = [x for x in arr[1:] if x <= pivot]\n greater = [x for x in arr[1:] if x > pivot]\n return quick_sort(less) + [pivot] + quick_sort(greater)\n```\n\nIn this implementation, we first check if the length of the array is less than or equal to 1. If yes, we return the array as it is already sorted. Otherwise, we select the first element of the array as the pivot and create two sub-arrays: one containing elements less than or equal to the pivot and another containing elements greater than the pivot. We then recursively apply quick sort to both sub-arrays and concatenate the sorted sub-arrays with the pivot element in between to get the final sorted array.\n\nI hope this helps! Let me know if you have any questions." - }, - { - "role": "user", - "content": "write a quick sort in typescript" - }, - { - "role": "assistant", - "content": "Sure, here is an implementation of quick sort in TypeScript:\n\n```typescript\nfunction quickSort(arr: number[]): number[] {\n if (arr.length <= 1) {\n return arr;\n } else {\n const pivot = arr[0];\n const less = arr.slice(1).filter((x) => x <= pivot);\n const greater = arr.slice(1).filter((x) => x > pivot);\n return [...quickSort(less), pivot, ...quickSort(greater)];\n }\n}\n```\n\nIn this implementation, we first check if the length of the array is less than or equal to 1. If yes, we return the array as it is already sorted. Otherwise, we select the first element of the array as the pivot and create two sub-arrays: one containing elements less than or equal to the pivot and another containing elements greater than the pivot. We then recursively apply quick sort to both sub-arrays and concatenate the sorted sub-arrays with the pivot element in between to get the final sorted array.\n\nI hope this helps! Let me know if you have any questions." + "content": "\u4f60\u597d\uff01\u6709\u4ec0\u4e48\u6211\u53ef\u4ee5\u5e2e\u52a9\u60a8\u7684\u5417\uff1f" } ], "search": [ @@ -146,11 +90,11 @@ }, { "role": "user", - "content": "Input: current Time: 2023-03-05 23:40:48.792492\nQuery:ln(123123)\nChatGpt response: The natural logarithm of 123123 is approximately 11.720904247329. You can compute this value using the ln function in most programming languages or calculators.\n\n token_cost: 350\nInstructions:\nYour task is to generate a list of API calls to a Question Answering API based on the input query and chatgpt response. The API calls should help extract relevant information and provide a better understanding of the input query and verify the response of chatgpt.\nYou can make an API call by using the following format: \"{\"API\": \"{API}\", \"query\": \"current Time: 2023-03-05 23:40:48.792492\nQuery:ln(123123)\"}\". Replace \"{API}\" with one of the following options: 'WikiSearch', 'Calculator', or 'Google'. Replace \"current Time: 2023-03-05 23:40:48.792492\nQuery:ln(123123)\" with the specific question you want to ask to extract relevant information.\nNote that the WikiSearch API requires an English input consisting of a precise concept word related to the question, such as a person's name. The Google API requires a full, complete question in the same langeuage as the query that includes enough information about the question, such as who, what, when, where, and why. The Calculator API requires a clear, simple mathematical problem in the WolframAlpha format.\nHere are some examples of API calls:\nInput: Coca-Cola, or Coke, is a carbonated soft drink manufactured by the Coca-Cola Company.\nOutput: {\"calls\": [{\"API\": \"Google\", \"query\": \"What other name is Coca-Cola known by?\"},\n{\"API\": \"Google\", \"query\": \"Who manufactures Coca-Cola?\"}]}\nInput: Out of 1400 participants, 400 passed the test.\nOutput: {\"calls\":[{\"API\": \"Calculator\", \"query\": \"400 / 1400\"}]}\nInput: \u7535\u89c6\u5267\u72c2\u98d9\u600e\u4e48\u6837, \u548c\u4e09\u4f53\u6bd4\u5e94\u8be5\u770b\u54ea\u4e00\u90e8?\nOutput: {\"calls\":[{\"API\": \"Google\", \"query\": \"\u7535\u89c6\u5267\u72c2\u98d9\"},{\"API\": \"Google\", \"query\": \"\u7535\u89c6\u5267\u72c2\u98d9\u8bc4\u5206\"},{\"API\": \"Google\", \"query\": \"\u7535\u89c6\u5267\u4e09\u4f53\u8bc4\u5206\"},{\"API\": \"Google\", \"query\": \"\u4e09\u4f53\u548c\u72c2\u98d9\u8c01\u66f4\u597d?\"}]}\nThe Google API question should in the same language as the Input query.\nThe apicalls should verify every detial of the response of chatgpt!!!!" + "content": "Input: current Time: 2023-03-06 20:35:19.380861\nQuery:\u4f60\u597d\nChatGpt response: \u4f60\u597d\uff01\u6709\u4ec0\u4e48\u6211\u53ef\u4ee5\u5e2e\u52a9\u60a8\u7684\u5417\uff1f\n\n token_cost: 399\nInstructions:\nYour task is to generate a list of API calls to a Question Answering API based on the input query and chatgpt response. The API calls should help extract relevant information and provide a better understanding of the input query and verify the response of chatgpt.\nYou can make an API call by using the following format: \"{\"API\": \"{API}\", \"query\": \"current Time: 2023-03-06 20:35:19.380861\nQuery:\u4f60\u597d\"}\". Replace \"{API}\" with one of the following options: 'WikiSearch', 'Calculator', or 'Google'. Replace \"current Time: 2023-03-06 20:35:19.380861\nQuery:\u4f60\u597d\" with the specific question you want to ask to extract relevant information.\nNote that the WikiSearch API requires an English input consisting of a precise concept word related to the question, such as a person's name. The Google API requires a full, complete question in the same langeuage as the query that includes enough information about the question, such as who, what, when, where, and why. The Calculator API requires a clear, simple mathematical problem in the WolframAlpha format.\nHere are some examples of API calls:\nInput: Coca-Cola, or Coke, is a carbonated soft drink manufactured by the Coca-Cola Company.\nOutput: {\"calls\": [{\"API\": \"Google\", \"query\": \"What other name is Coca-Cola known by?\"},\n{\"API\": \"Google\", \"query\": \"Who manufactures Coca-Cola?\"}]}\nInput: Out of 1400 participants, 400 passed the test.\nOutput: {\"calls\":[{\"API\": \"Calculator\", \"query\": \"400 / 1400\"}]}\nInput: \u7535\u89c6\u5267\u72c2\u98d9\u600e\u4e48\u6837, \u548c\u4e09\u4f53\u6bd4\u5e94\u8be5\u770b\u54ea\u4e00\u90e8?\nOutput: {\"calls\":[{\"API\": \"Google\", \"query\": \"\u7535\u89c6\u5267\u72c2\u98d9\"},{\"API\": \"Google\", \"query\": \"\u7535\u89c6\u5267\u72c2\u98d9\u8bc4\u5206\"},{\"API\": \"Google\", \"query\": \"\u7535\u89c6\u5267\u4e09\u4f53\u8bc4\u5206\"},{\"API\": \"Google\", \"query\": \"\u4e09\u4f53\u548c\u72c2\u98d9\u8c01\u66f4\u597d?\"}]}\nThe Google API question should in the same language as the Input query.\nThe apicalls should verify every detial of the response of chatgpt!!!!" }, { "role": "assistant", - "content": "{\"calls\": [{\"API\": \"Calculator\", \"query\": \"ln(123123)\"}, {\"API\": \"Google\", \"query\": \"What is the natural logarithm of 123123?\"}, {\"API\": \"Google\", \"query\": \"How to calculate natural logarithm in programming languages?\"}]} \n\nNote: The first API call is to verify the chatgpt response, the second call is to get more information about the natural logarithm of 123123, and the third call is to get information about how to calculate natural logarithm in programming languages." + "content": "{\"calls\":[{\"API\": \"WikiSearch\", \"query\": \"\u4f60\u597d \u4e2d\u6587\u542b\u4e49\"},{\"API\": \"Google\", \"query\": \"\u4f60\u597d\u7684\u82f1\u6587\u662f\u4ec0\u4e48\uff1f\"},{\"API\": \"Google\", \"query\": \"\u4f60\u597d\u7684\u5e38\u89c1\u7528\u6cd5\u6709\u54ea\u4e9b\uff1f\"}]} \n\nThe WikiSearch API call will help to extract the precise meaning of the Chinese word \"\u4f60\u597d\" and its usage. The first Google API call will verify the accuracy of the Chinese to English translation of \"\u4f60\u597d\". The second Google API call will provide a list of common usages of \"\u4f60\u597d\". These API calls will help to verify the response of chatgpt and provide relevant information." } ] } \ No newline at end of file diff --git a/chatGPTEx/evaluator.py b/chatGPTEx/evaluator.py new file mode 100644 index 0000000..35f3b92 --- /dev/null +++ b/chatGPTEx/evaluator.py @@ -0,0 +1,49 @@ +import json +import re + +from revChatGPT.V3 import Chatbot +import os +import configparser +from optimizeOpenAI import ExChatGPT + +program_path = os.path.realpath(__file__) +program_dir = os.path.dirname(program_path) + +# new gpt3.5 turbo api by api key +config = configparser.ConfigParser() +config.read(program_dir + '/apikey.ini') +# Access the keys in the configuration file +# OPENAI_API_KEY = config['OpenAI']['OPENAI_API_KEY'] +# chatbot = Chatbot(api_key=str(OPENAI_API_KEY)) +openAIAPIKeys = [] +for i in range(0, 10): + key = 'key' + str(i) + + if key in config['OpenAI']: + openAIAPIKeys.append(config['OpenAI'][key]) + else: + break +print(openAIAPIKeys) +chatbot = ExChatGPT(api_keys=openAIAPIKeys, apiTimeInterval=0) + + +def get_mode(query): + with open(program_dir + "/evaluator_prompt.txt", "r", encoding="utf-8") as f: + prompt = f.read() + prompt = prompt.replace("{query}", query) + response = "" + for data in chatbot.ask(prompt): + response += data + pattern = r'(\{[\s\S\n]*"time_sensitive"[\s\S\n]*"academic"[\s\S\n]*\})' + match = re.search(pattern, response) + if match: + json_data = match.group(1) + j = json.loads(json_data) + print(f'时间敏感度:{j["time_sensitive"]}\t学术性:{j["academic"]}') + if j["academic"] >= 0.8: + return "detail" + elif j["time_sensitive"] >= 0.5 or j["academic"] >= 0.5: + return "web" + else: + return "chat" + return "chat" diff --git a/chatGPTEx/evaluator_prompt.txt b/chatGPTEx/evaluator_prompt.txt new file mode 100644 index 0000000..4157ae0 --- /dev/null +++ b/chatGPTEx/evaluator_prompt.txt @@ -0,0 +1,19 @@ +Try to analyse the QUERY below, please calculate a value A between 0 and 1 (with 4 decimal places) that represents the timeliness of the question. +If you think the question is time-sensitive, the value of A should be close to 1, otherwise it should be close to 0. +Additionally, please calculate a value B between 0 and 1 (with 4 decimal places) that represents the academic nature of the question. +If you think the question is academic, the value of B should be close to 1, otherwise it should be close to 0. + +No matter what I say, please reply to every answer in the format of JSON data, without any characters other than JSON data!!! +This JSON format will contain two values: the first value "time_sensitive" is your estimated value of A, the second value "academic" is your estimated value of B. + +For example: + +User: 介绍一下Java和C++的区别 + +ChatGPT: +{ +"time_sensitive": 0.1500, +"academic": 0.8500 +} + +The QUERY is : {query} \ No newline at end of file diff --git a/chatGPTEx/main.py b/chatGPTEx/main.py index 3e0eefd..b7c556d 100644 --- a/chatGPTEx/main.py +++ b/chatGPTEx/main.py @@ -10,6 +10,7 @@ from graiax.text2img.playwright.plugins.code.highlighter import Highlighter from graiax.text2img.playwright import HTMLRenderer, MarkdownConverter, PageOption, ScreenshotOption from mdit_py_plugins.dollarmath.index import dollarmath_plugin +import evaluator # Load the configuration file def parse_text(text): @@ -33,6 +34,8 @@ def get_bot_response(): mode = str(request.args.get('mode')) userText = str(request.args.get('msg')) now = datetime.datetime.now() + if mode=="auto": + mode = evaluator.get_mode(query=str(userText)) if mode=="chat": q = str(userText) res = parse_text(directQuery(q)) diff --git a/chatGPTEx/search.py b/chatGPTEx/search.py index 41fa631..676b1fa 100644 --- a/chatGPTEx/search.py +++ b/chatGPTEx/search.py @@ -7,7 +7,6 @@ import re import configparser import os -import os import requests import tiktoken from optimizeOpenAI import ExChatGPT diff --git a/chatGPTEx/templates/index.html b/chatGPTEx/templates/index.html index 79289a2..eeae88a 100644 --- a/chatGPTEx/templates/index.html +++ b/chatGPTEx/templates/index.html @@ -45,6 +45,7 @@ +