Skip to content

Commit

Permalink
Inital Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Aug 14, 2021
0 parents commit 6bf8790
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config.json
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
}
84 changes: 84 additions & 0 deletions main.py
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)}')

0 comments on commit 6bf8790

Please sign in to comment.