返回提交历史
Added
gpt4free/hpgptai/README.md
+39
-0
Added
gpt4free/hpgptai/__init__.py
+83
-0
Added
testing/hpgptai_test.py
+41
-0
XFEstudio/gpt4free
add hpgptapi
3faf3630
代码差异
3 个文件
+163
-0
@@ -0,0 +1,39 @@
1
# HpgptAI
2
Written by [hp_mzx](https://github.com/hpsj).
3
4
## Examples:
5
### Completion:
6
```python
7
res = hpgptai.Completion.create("你是谁","127.0.0.1:7890")
8
print(res["reply"])
9
```
10
11
### Chat Completion:
12
Support context
13
```python
14
messages = [
15
{
16
"content": "你是谁",
17
"html": "你是谁",
18
"id": hpgptai.ChatCompletion.randomStr(),
19
"role": "user",
20
"who": "User: ",
21
},
22
{
23
"content": "我是一位AI助手,专门为您提供各种服务和支持。我可以回答您的问题,帮助您解决问题,提供相关信息,并执行一些任务。请随时告诉我您需要什么帮助。",
24
"html": "我是一位AI助手,专门为您提供各种服务和支持。我可以回答您的问题,帮助您解决问题,提供相关信息,并执行一些任务。请随时告诉我您需要什么帮助。",
25
"id": hpgptai.ChatCompletion.randomStr(),
26
"role": "assistant",
27
"who": "AI: ",
28
},
29
{
30
"content": "我上一句问的是什么?",
31
"html": "我上一句问的是什么?",
32
"id": hpgptai.ChatCompletion.randomStr(),
33
"role": "user",
34
"who": "User: ",
35
},
36
]
37
res = hpgptai.ChatCompletion.create(messages,proxy="127.0.0.1:7890")
38
print(res["reply"])
39
```
@@ -0,0 +1,83 @@
1
# -*- coding: utf-8 -*-
2
"""
3
@Time : 2023/5/22 14:04
4
@Auth : Hp_mzx
5
@File :__init__.py.py
6
@IDE :PyCharm
7
"""
8
import json
9
import requests
10
import random
11
import string
12
13
class ChatCompletion:
14
@staticmethod
15
def create(
16
messages: list,
17
context: str="Converse as if you were an AI assistant. Be friendly, creative.",
18
proxy:str=None
19
):
20
url = "https://chatgptlogin.ac/wp-json/ai-chatbot/v1/chat"
21
headers = {
22
"Content-Type": "application/json",
23
"X-Wp-Nonce": "02244d73c2"
24
}
25
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None
26
data = {
27
"env": "chatbot",
28
"session": "N/A",
29
"prompt": ChatCompletion.__build_prompt(context,messages),
30
"context": context,
31
"messages": messages,
32
"newMessage": messages[-1]["content"],
33
"userName": "<div class=\"mwai-name-text\">User:</div>",
34
"aiName": "<div class=\"mwai-name-text\">AI:</div>",
35
"model": "gpt-3.5-turbo",
36
"temperature": 0.8,
37
"maxTokens": 1024,
38
"maxResults": 1,
39
"apiKey": "",
40
"service": "openai",
41
"embeddingsIndex": "",
42
"stop": "",
43
"clientId": ChatCompletion.randomStr(),
44
}
45
res = requests.post(url=url, data=json.dumps(data), headers=headers, proxies=proxies)
46
if res.status_code == 200:
47
return res.json()
48
return res.text
49
50
51
@staticmethod
52
def randomStr():
53
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=34))[:11]
54
55
@classmethod
56
def __build_prompt(cls, context: str, message: list, isCasuallyFineTuned=False, last=15):
57
prompt = context + '\n\n' if context else ''
58
message = message[-last:]
59
if isCasuallyFineTuned:
60
lastLine = message[-1]
61
prompt = lastLine.content + ""
62
return prompt
63
conversation = [x["who"] + x["content"] for x in message]
64
prompt += '\n'.join(conversation)
65
prompt += '\n' + "AI: "
66
return prompt
67
68
69
70
71
class Completion:
72
@staticmethod
73
def create(prompt: str,proxy:str):
74
messages = [
75
{
76
"content": prompt,
77
"html": prompt,
78
"id": ChatCompletion.randomStr(),
79
"role": "user",
80
"who": "User: ",
81
},
82
]
83
return ChatCompletion.create(messages=messages,proxy=proxy)
@@ -0,0 +1,41 @@
1
import hpgptai
2
3
#single completion
4
res = hpgptai.Completion.create("你是谁","127.0.0.1:7890")
5
print(res["reply"])
6
7
8
#chat completion
9
messages = [
10
{
11
"content": "你是谁",
12
"html": "你是谁",
13
"id": hpgptai.ChatCompletion.randomStr(),
14
"role": "user",
15
"who": "User: ",
16
},
17
{
18
"content": "我是一位AI助手,专门为您提供各种服务和支持。我可以回答您的问题,帮助您解决问题,提供相关信息,并执行一些任务。请随时告诉我您需要什么帮助。",
19
"html": "我是一位AI助手,专门为您提供各种服务和支持。我可以回答您的问题,帮助您解决问题,提供相关信息,并执行一些任务。请随时告诉我您需要什么帮助。",
20
"id": hpgptai.ChatCompletion.randomStr(),
21
"role": "assistant",
22
"who": "AI: ",
23
},
24
{
25
"content": "我上一句问的是什么?",
26
"html": "我上一句问的是什么?",
27
"id": hpgptai.ChatCompletion.randomStr(),
28
"role": "user",
29
"who": "User: ",
30
},
31
]
32
res = hpgptai.ChatCompletion.create(messages,proxy="127.0.0.1:7890")
33
print(res["reply"])
34
35
36
37
38
39
40
41