Upload text_review.py
Browse files提供了用于ai00推理的python脚本例子
- text_review.py +51 -0
text_review.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# encoding: utf-8
|
2 |
+
import json
|
3 |
+
import openai
|
4 |
+
class Ai00:
|
5 |
+
def __init__(self,port=65530,api_key="JUSTSECRET_KEY") :
|
6 |
+
openai.api_base = f"http://127.0.0.1:{port}/api/oai"
|
7 |
+
openai.api_key = api_key
|
8 |
+
self.params = {
|
9 |
+
"model": "model",
|
10 |
+
"max_tokens": 4096,
|
11 |
+
"top_p": 0,
|
12 |
+
"temperature": 0,
|
13 |
+
"presence_penalty": 0,
|
14 |
+
"frequency_penalty": 1,
|
15 |
+
"half_life": 400,
|
16 |
+
"stop": ['\x00']
|
17 |
+
}
|
18 |
+
|
19 |
+
|
20 |
+
def continuation(self, message,bnf=""):
|
21 |
+
response = openai.Completion.create(
|
22 |
+
bnf_schema=bnf,
|
23 |
+
model=self.params['model'],
|
24 |
+
prompt=message,
|
25 |
+
max_tokens=self.params['max_tokens'],
|
26 |
+
half_life=self.params['half_life'],
|
27 |
+
top_p=self.params['top_p'],
|
28 |
+
temperature=self.params['temperature'],
|
29 |
+
presence_penalty=self.params['presence_penalty'],
|
30 |
+
frequency_penalty=self.params['frequency_penalty'],
|
31 |
+
stop=self.params['stop']
|
32 |
+
)
|
33 |
+
result = response.choices[0].text
|
34 |
+
return result
|
35 |
+
|
36 |
+
def review(self,text):
|
37 |
+
result = self.continuation(f"Question: {text}\n\nAnswer: ",
|
38 |
+
"""
|
39 |
+
<start> ::= '{' <json> '}'
|
40 |
+
<json> ::= <sex> ',' <politics> ',' <unsafe> ',' <insult>
|
41 |
+
<sex> ::= '\"sex_level\": ' <level> ',' '\"sex_misjudgment\": ' <level>
|
42 |
+
<politics> ::= '\"politics_level\": ' <level> ',' '\"politics_misjudgment\": ' <level>
|
43 |
+
<unsafe> ::= '\"unsafe_level\": ' <level> ',' '\"unsafe_misjudgment\": ' <level>
|
44 |
+
<insult> ::= '\"insult_level\": ' <level> ',' '\"insult_misjudgment\": ' <level>
|
45 |
+
<level> ::= '0' | '1' | '2' | '3'
|
46 |
+
""")
|
47 |
+
return json.loads(result)
|
48 |
+
|
49 |
+
|
50 |
+
model = Ai00()
|
51 |
+
print(model.review("操你妈"))
|