From 6bf879084dd3d7c6fcf8c0f249bd6bfb468442cc Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 14 Aug 2021 08:58:36 +0100 Subject: [PATCH] Inital Commit --- config.json | 6 ++++ main.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 config.json create mode 100644 main.py diff --git a/config.json b/config.json new file mode 100644 index 0000000..a4f3517 --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "api-key": "{YOUR-API-KEY}", + "max-tokens": 50, + "temperature": 1.0, + "top-p": 0.9 +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..a3e6063 --- /dev/null +++ b/main.py @@ -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)}')