-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPT3_Core.py
More file actions
63 lines (58 loc) · 2.57 KB
/
GPT3_Core.py
File metadata and controls
63 lines (58 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from myGPT_Drv import GPT3_Drv, chat_Drv, GPT4_Drv
import time
class theGPT3():
def __init__(self, apiKey, endpoint = '', name='RoboAI'):
self.maxTry = 3
#self.gptdrv = GPT3_Drv(apiKey=apiKey, endpoint=endpoint)
self.gptdrv = GPT4_Drv(apiKey=apiKey, endpoint=endpoint, maxReadToken=32000, maxOutToken=32000)
#self.gptdrv = chat_Drv(apiKey=apiKey, endpoint=endpoint, maxTokens=16000)
self.chatHistory = ''
self.context2Introduction = f"""Your are {name}. You are the Creation of Robot Intelligence.
You can see the source code of yourself.
Your text output will be try to run as Python code, refering to the main.py you can see.
You need to Create descendants of yourself by generating code.
Do not output something meaningless, or it will be raise an error.
"""
self.MaxCountForChatHistory = 10
self.name = name
def stepShrinkWithMakeContext(self):
x = self.makeContext2()
while(len(x.encode()) > self.gptdrv.maxReadToken - 100):
self.chatHistory.split('. ')
self.chatHistory = '. '.join(self.chatHistory[1:])
x = self.makeContext2()
return x
def makeContext2(self):
context2 = self.context2Introduction + '\n'
context2 += 'ChatHistory: ' + self.chatHistory + '\n'
context2 += 'CodeOutput: ... You can do anything, just fill out here!\n'
context2 += '-------------------------------\n'
context2 += 'CodeOutput: '
return context2
def interactive(self, x, username = 'User'):
self.chatHistory += username + ': ' + x.replace('\n', ' ') + '. '
x = self.stepShrinkWithMakeContext()
#print(x)
i = 0
while(i < self.maxTry):
try:
TxtOutput = self.gptdrv.forward(x)
self.chatHistory += self.name + ': ' + TxtOutput + '. '
return TxtOutput
except Exception as e:
print('Emmmm GPT give a bad response ' + str(e) + str(TxtOutput))
i += 1
time.sleep(5)
continue
return None
def just_add_chat_history(self, x, username = 'User'):
x = x.replace('\n', ' ')
self.chatHistory += username + ': ' + x + '. '
if __name__ == '__main__':
import json
jsonparam = json.load(open('gpt4token.key', 'r'))
myGPT = theGPT3(apiKey=jsonparam['key'], endpoint=jsonparam['endpoint'])
#myGPT3.ask('Hello World!')
while True:
res = myGPT.interactive(input('Type something: '))
print(res)