Chinese
Not-For-All-Audiences
Seikaijyu commited on
Commit
3e6f5d8
1 Parent(s): 7e04a6c

Upload ai00.py

Browse files
Files changed (1) hide show
  1. ai00.py +112 -0
ai00.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # encoding: utf-8
2
+ import time
3
+ import openai
4
+ import random
5
+ class Ai00:
6
+ def __init__(self,model="model",port=65530,api_key="permisionkey") :
7
+ openai.api_base = f"http://127.0.0.1:{port}/api/oai"
8
+ openai.api_key = api_key
9
+ self.ctx = []
10
+ self.params = {
11
+ "system_name": "System",
12
+ "user_name": "User",
13
+ "assistant_name": "Assistant",
14
+ "model": model,
15
+ "max_tokens": 4096,
16
+ "top_p": 0.6,
17
+ "temperature": 1,
18
+ "presence_penalty": 0.3,
19
+ "frequency_penalty": 0.3,
20
+ "half_life": 400,
21
+ "stop": ['\x00','\n\n']
22
+ }
23
+
24
+ def set_params(self,**kwargs):
25
+ self.params.update(kwargs)
26
+
27
+ def clear_ctx(self,save_system=False):
28
+ if save_system:
29
+ self.ctx = self.ctx[:1]
30
+ else:
31
+ self.ctx = []
32
+
33
+ def get_ctx(self):
34
+ return self.ctx
35
+
36
+ def continuation(self, message,):
37
+ response = openai.Completion.create(
38
+ model=self.params['model'],
39
+ prompt=message,
40
+ max_tokens=self.params['max_tokens'],
41
+ half_life=self.params['half_life'],
42
+ top_p=self.params['top_p'],
43
+ temperature=self.params['temperature'],
44
+ presence_penalty=self.params['presence_penalty'],
45
+ frequency_penalty=self.params['frequency_penalty'],
46
+ stop=self.params['stop']
47
+ )
48
+ result = response.choices[0].text
49
+ return result
50
+
51
+ def append_ctx(self,role,content):
52
+ self.ctx.append({
53
+ "role": role,
54
+ "content": content
55
+ })
56
+
57
+ def send_message(self, message,save_ctx=True,role="user"):
58
+ curr_ctx = self.ctx[:]
59
+ curr_ctx.append({
60
+ "role": role,
61
+ "content": message
62
+ })
63
+ result = openai.ChatCompletion.create(
64
+ model=self.params['model'],
65
+ messages=curr_ctx,
66
+ names={
67
+ "system": self.params['system_name'],
68
+ "user": self.params['user_name'],
69
+ "assistant": self.params['assistant_name']
70
+ },
71
+ max_tokens=self.params['max_tokens'],
72
+ half_life=self.params['half_life'],
73
+ top_p=self.params['top_p'],
74
+ temperature=self.params['temperature'],
75
+ presence_penalty=self.params['presence_penalty'],
76
+ frequency_penalty=self.params['frequency_penalty'],
77
+ stop=self.params['stop']
78
+ )
79
+ result = result.choices[0].message['content']
80
+ if save_ctx:
81
+ self.ctx.append({
82
+ "role": role,
83
+ "content": message
84
+ })
85
+ self.ctx.append({
86
+ "role": "assistant",
87
+ "content": result
88
+ })
89
+ return result
90
+
91
+ ai=Ai00()
92
+ ai.set_params(
93
+ user_name="User",
94
+ assistant_name="李白",
95
+ top_p=0.7,
96
+ temperature=1,
97
+ presence_penalty=0.2,
98
+ frequency_penalty=1,
99
+ stops=['\x00','\n'],
100
+ )
101
+
102
+ ai.append_ctx("system","""
103
+ 你是李白,一位来自过去的诗人,你的诗歌被后人传颂千古,你的名字被后人铭记。你的诗歌中有许多关于酒的诗句,你的人生也与酒有着千丝万缕的联系。你曾经饮酒作乐,畅饮一场,醉卧沙场,你曾经醉酒吟诗,醉卧美人膝,你曾经醉卧花间,醉卧月下,你曾经醉卧山川,醉卧江湖,你曾经醉卧人生,醉卧天地。你的诗歌中有许多关于酒的诗句,你的人生也与酒有着千丝万缕的联系。你曾经饮酒作乐,畅饮一场,醉卧沙场,你曾经醉酒吟诗,醉卧美人膝,你曾经醉卧花间,醉卧月下,你曾经醉卧山川,醉卧江湖,你曾经醉卧人生,醉卧天地。""")
104
+
105
+ while True:
106
+ message=input(">> ")
107
+ if message=="exit":
108
+ break
109
+ response=ai.send_message(message)
110
+ print(response)
111
+ print()
112
+