-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
BuildTools
committed
Aug 14, 2021
0 parents
commit 6bf8790
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"api-key": "{YOUR-API-KEY}", | ||
"max-tokens": 50, | ||
"temperature": 1.0, | ||
"top-p": 0.9 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import requests | ||
import pickle | ||
import json | ||
|
||
with open("config.json", "r") as file: | ||
config_data = json.load(file) | ||
API_KEY = config_data["api-key"] | ||
MAX_TOKENS = config_data["max-tokens"] | ||
TEMPERATURE = config_data["temperature"] | ||
TOP_P = config_data["top-p"] | ||
|
||
|
||
class Agent: | ||
def __init__(self, name, userName): | ||
self.name = name | ||
self.context = "" | ||
self.conversation = "" | ||
self.userName = userName | ||
|
||
def save(self): # Saves the agent object to a file | ||
file = open(f"{self.name}.mtrx", "wb") | ||
pickle.dump(self, file) | ||
file.close() | ||
|
||
def getLog(self, prompt): | ||
out = self.context + "\n" + self.conversation + "\n" | ||
out += f'{self.userName}: {prompt}\n {self.name}: ' | ||
return out | ||
|
||
def getResponse(self, prompt): | ||
response = requests.post("https://api.ai21.com/studio/v1/j1-jumbo/complete", | ||
headers={"Authorization": f"Bearer {API_KEY}"}, | ||
json={ | ||
"prompt": self.getLog(prompt), | ||
"numResults": 1, | ||
"maxTokens": MAX_TOKENS, | ||
"stopSequences": ["\n"], | ||
"temperature": TEMPERATURE, | ||
"topP": TOP_P | ||
}) | ||
responseData = json.loads(response.text)["completions"][0]["data"]["text"] | ||
return responseData | ||
|
||
def generate(self, prompt, doTraining): | ||
response = self.getResponse(prompt) | ||
if doTraining: | ||
self.conversation += f'{self.userName}: {prompt}\n{agentName}: {response}' | ||
self.save() | ||
return response | ||
|
||
|
||
def loadAgent(name): # Loads an agent object from a file | ||
file = open(f"{name}.mtrx", "rb") | ||
agent = pickle.load(file) | ||
file.close() | ||
return agent | ||
|
||
|
||
def doesFileExist(fileName): | ||
try: | ||
open(fileName, "r").close() | ||
exists = True | ||
except FileNotFoundError: | ||
exists = False | ||
return exists | ||
|
||
|
||
userName = input("Enter your name: ") | ||
agentName = input("Enter the agent's name: ") | ||
doTraining = input("Do you want the agent to remember this conversation? (Y/N): ") | ||
doTraining = False | ||
if doTraining.lower() == "y": | ||
doTraining = True | ||
if doesFileExist(f"{agentName}.mtrx"): | ||
agent = loadAgent(agentName) | ||
else: | ||
context = input("Enter the context: ") | ||
agent = Agent(agentName, userName) | ||
prompt = None | ||
while True: | ||
prompt = input(f"{userName}: ") | ||
if prompt == "": | ||
break | ||
print(f'{agentName}: {agent.generate(prompt, doTraining)}') |