From 3faf3630ced0ca3d7de0125be0901b713e0ef865 Mon Sep 17 00:00:00 2001 From: hp256 <971748116@qq.com> Date: Mon, 22 May 2023 14:14:09 +0800 Subject: add hpgptapi --- gpt4free/hpgptai/README.md | 39 +++++++++++++++++++++ gpt4free/hpgptai/__init__.py | 83 ++++++++++++++++++++++++++++++++++++++++++++ testing/hpgptai_test.py | 41 ++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 gpt4free/hpgptai/README.md create mode 100644 gpt4free/hpgptai/__init__.py create mode 100644 testing/hpgptai_test.py diff --git a/gpt4free/hpgptai/README.md b/gpt4free/hpgptai/README.md new file mode 100644 index 00000000..2735902f --- /dev/null +++ b/gpt4free/hpgptai/README.md @@ -0,0 +1,39 @@ +# HpgptAI +Written by [hp_mzx](https://github.com/hpsj). + +## Examples: +### Completion: +```python +res = hpgptai.Completion.create("你是谁","127.0.0.1:7890") +print(res["reply"]) +``` + +### Chat Completion: +Support context +```python +messages = [ + { + "content": "你是谁", + "html": "你是谁", + "id": hpgptai.ChatCompletion.randomStr(), + "role": "user", + "who": "User: ", + }, + { + "content": "我是一位AI助手,专门为您提供各种服务和支持。我可以回答您的问题,帮助您解决问题,提供相关信息,并执行一些任务。请随时告诉我您需要什么帮助。", + "html": "我是一位AI助手,专门为您提供各种服务和支持。我可以回答您的问题,帮助您解决问题,提供相关信息,并执行一些任务。请随时告诉我您需要什么帮助。", + "id": hpgptai.ChatCompletion.randomStr(), + "role": "assistant", + "who": "AI: ", + }, + { + "content": "我上一句问的是什么?", + "html": "我上一句问的是什么?", + "id": hpgptai.ChatCompletion.randomStr(), + "role": "user", + "who": "User: ", + }, +] +res = hpgptai.ChatCompletion.create(messages,proxy="127.0.0.1:7890") +print(res["reply"]) +``` \ No newline at end of file diff --git a/gpt4free/hpgptai/__init__.py b/gpt4free/hpgptai/__init__.py new file mode 100644 index 00000000..66841a87 --- /dev/null +++ b/gpt4free/hpgptai/__init__.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- +""" +@Time : 2023/5/22 14:04 +@Auth : Hp_mzx +@File :__init__.py.py +@IDE :PyCharm +""" +import json +import requests +import random +import string + +class ChatCompletion: + @staticmethod + def create( + messages: list, + context: str="Converse as if you were an AI assistant. Be friendly, creative.", + proxy:str=None + ): + url = "https://chatgptlogin.ac/wp-json/ai-chatbot/v1/chat" + headers = { + "Content-Type": "application/json", + "X-Wp-Nonce": "02244d73c2" + } + proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None + data = { + "env": "chatbot", + "session": "N/A", + "prompt": ChatCompletion.__build_prompt(context,messages), + "context": context, + "messages": messages, + "newMessage": messages[-1]["content"], + "userName": "
User:
", + "aiName": "
AI:
", + "model": "gpt-3.5-turbo", + "temperature": 0.8, + "maxTokens": 1024, + "maxResults": 1, + "apiKey": "", + "service": "openai", + "embeddingsIndex": "", + "stop": "", + "clientId": ChatCompletion.randomStr(), + } + res = requests.post(url=url, data=json.dumps(data), headers=headers, proxies=proxies) + if res.status_code == 200: + return res.json() + return res.text + + + @staticmethod + def randomStr(): + return ''.join(random.choices(string.ascii_lowercase + string.digits, k=34))[:11] + + @classmethod + def __build_prompt(cls, context: str, message: list, isCasuallyFineTuned=False, last=15): + prompt = context + '\n\n' if context else '' + message = message[-last:] + if isCasuallyFineTuned: + lastLine = message[-1] + prompt = lastLine.content + "" + return prompt + conversation = [x["who"] + x["content"] for x in message] + prompt += '\n'.join(conversation) + prompt += '\n' + "AI: " + return prompt + + + + +class Completion: + @staticmethod + def create(prompt: str,proxy:str): + messages = [ + { + "content": prompt, + "html": prompt, + "id": ChatCompletion.randomStr(), + "role": "user", + "who": "User: ", + }, + ] + return ChatCompletion.create(messages=messages,proxy=proxy) \ No newline at end of file diff --git a/testing/hpgptai_test.py b/testing/hpgptai_test.py new file mode 100644 index 00000000..cdd146dd --- /dev/null +++ b/testing/hpgptai_test.py @@ -0,0 +1,41 @@ +import hpgptai + +#single completion +res = hpgptai.Completion.create("你是谁","127.0.0.1:7890") +print(res["reply"]) + + +#chat completion +messages = [ + { + "content": "你是谁", + "html": "你是谁", + "id": hpgptai.ChatCompletion.randomStr(), + "role": "user", + "who": "User: ", + }, + { + "content": "我是一位AI助手,专门为您提供各种服务和支持。我可以回答您的问题,帮助您解决问题,提供相关信息,并执行一些任务。请随时告诉我您需要什么帮助。", + "html": "我是一位AI助手,专门为您提供各种服务和支持。我可以回答您的问题,帮助您解决问题,提供相关信息,并执行一些任务。请随时告诉我您需要什么帮助。", + "id": hpgptai.ChatCompletion.randomStr(), + "role": "assistant", + "who": "AI: ", + }, + { + "content": "我上一句问的是什么?", + "html": "我上一句问的是什么?", + "id": hpgptai.ChatCompletion.randomStr(), + "role": "user", + "who": "User: ", + }, +] +res = hpgptai.ChatCompletion.create(messages,proxy="127.0.0.1:7890") +print(res["reply"]) + + + + + + + + -- cgit v1.2.3