Add JavaShark-Papa Hash Support
This commit is contained in:
parent
ebca2990cd
commit
c6848577cb
6
src/aidgaf/aidgaf-server/hash_calculator.py
Normal file
6
src/aidgaf/aidgaf-server/hash_calculator.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import hashlib
|
||||||
|
|
||||||
|
def calculate_hash(value:bytes, secret:bytes)->str:
|
||||||
|
m = hashlib.sha512()
|
||||||
|
m.update(b"".join([value,secret]))
|
||||||
|
return m.hexdigest()
|
@ -1,89 +1,133 @@
|
|||||||
import json
|
import json
|
||||||
|
import re
|
||||||
import requests
|
import requests
|
||||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||||
import time
|
from time import time
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import openai
|
import openai
|
||||||
import random
|
import random
|
||||||
import settings
|
import settings
|
||||||
|
import hash_calculator
|
||||||
|
|
||||||
openai.organization = "org-hNNV1yHjZp7T3pn5pdZWaKLm"
|
openai.organization = "org-hNNV1yHjZp7T3pn5pdZWaKLm"
|
||||||
#print(openai.Model.list())
|
# print(openai.Model.list())
|
||||||
|
|
||||||
|
|
||||||
|
URL = "https://api.openai.com/v1/completions"
|
||||||
|
|
||||||
URL="https://api.openai.com/v1/completions"
|
DATA = {"model": settings.OPEN_AI_COMPLETION_MODEL,
|
||||||
|
"prompt": settings.PROMPTS[0],
|
||||||
|
"temperature": 0.90,
|
||||||
|
"max_tokens": settings.OPEN_AI_MAX_TOKENS
|
||||||
|
}
|
||||||
|
|
||||||
DATA = {"model": "text-davinci-003",
|
request_headers = {"Authorization": "Bearer " +
|
||||||
"prompt": settings.PROMPTS[0],
|
settings.APIKEY, "Content-Type": "application/json"}
|
||||||
"temperature":1,
|
|
||||||
"max_tokens": 200
|
|
||||||
}
|
|
||||||
|
|
||||||
request_headers={"Authorization": "Bearer "+settings.APIKEY,"Content-Type": "application/json"}
|
|
||||||
|
|
||||||
|
|
||||||
class IDGAFServer(BaseHTTPRequestHandler):
|
class IDGAFServer(BaseHTTPRequestHandler):
|
||||||
|
|
||||||
def do_PATCH(self):
|
def do_PATCH(self):
|
||||||
# print("Request: "+self.request+ " "+webServer.get_request())
|
# print("Request: "+self.request+ " "+webServer.get_request())
|
||||||
request_body=self.rfile.read(int(self.headers.get('Content-Length')))
|
request_body = self.rfile.read(int(self.headers.get('Content-Length')))
|
||||||
command=json.loads(request_body)
|
str_request=str(request_body,'utf-8')
|
||||||
|
print(request_body)
|
||||||
|
if settings.HASHKEY is not None and get_message_hash(str_request) not in str_request:
|
||||||
|
print("Error: hash not match")
|
||||||
|
self.send_response(403)
|
||||||
|
return
|
||||||
|
if settings.HASHKEY is not None and not verify_message_time(str_request):
|
||||||
|
print("Error: timestamp expired")
|
||||||
|
self.send_response(403)
|
||||||
|
return
|
||||||
|
command = json.loads(request_body)
|
||||||
print(command)
|
print(command)
|
||||||
if command['message']['command'] == 'aidgaf':
|
if command['message']['command'] == 'aidgaf':
|
||||||
[responseCode,response_body]=parse_idgaf_request(command)
|
[responseCode, response_body] = parse_idgaf_request(command)
|
||||||
self.handle_response(responseCode,response_body)
|
|
||||||
|
self.handle_response(responseCode, response_body)
|
||||||
print("sending:"+response_body)
|
print("sending:"+response_body)
|
||||||
|
|
||||||
|
|
||||||
|
def handle_response(self, code, body):
|
||||||
def handle_response(self,code,body):
|
|
||||||
self.send_response(code)
|
self.send_response(code)
|
||||||
self.send_header("Content-type", "text/html")
|
self.send_header("Content-type", "text/html")
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(bytes(body,"UTF-8"))
|
self.wfile.write(bytes(body, "UTF-8"))
|
||||||
|
|
||||||
|
|
||||||
def get_response_base_object(text):
|
def get_response_base_object(text):
|
||||||
resultObject={}
|
resultObject = {}
|
||||||
resultObject["message"]={}
|
resultObject["message"] = {}
|
||||||
resultObject["service"]="AIDGAF Server"
|
resultObject["service"] = "AIDGAF Server"
|
||||||
resultObject["message"]["data"]={}
|
resultObject["message"]["data"] = {}
|
||||||
resultObject["message"]["data"]["resultObject"]=text
|
resultObject["message"]["data"]["resultObject"] = text
|
||||||
resultObject["timestamp"]=datetime.utcnow().timestamp()
|
resultObject["timestamp"] = datetime.utcnow().timestamp()
|
||||||
return resultObject
|
return resultObject
|
||||||
|
|
||||||
|
|
||||||
def parse_idgaf_request(command):
|
def parse_idgaf_request(command):
|
||||||
the_data=get_prompt(command)
|
the_data = get_prompt(command)
|
||||||
gpt_response=requests.post(URL, json=the_data, headers=request_headers)
|
gpt_response = requests.post(URL, json=the_data, headers=request_headers)
|
||||||
print(gpt_response)
|
print(gpt_response)
|
||||||
response_text=gpt_response.json()['choices'][0]['text'].strip()
|
response_text = gpt_response.json()['choices'][0]['text'].strip()
|
||||||
obj=get_response_base_object(response_text)
|
obj = get_response_base_object(response_text)
|
||||||
json_result=json.dumps(obj)
|
obj['hash']=get_message_hash(json.dumps(obj))
|
||||||
return [gpt_response.status_code,json_result]
|
json_result = json.dumps(obj)
|
||||||
|
return [gpt_response.status_code, json_result]
|
||||||
|
|
||||||
|
|
||||||
def get_prompt(command):
|
def get_prompt(command):
|
||||||
my_prompt=random.choice(settings.PROMPTS)
|
my_prompt = random.choice(settings.PROMPTS)
|
||||||
my_prompt=my_prompt.replace("USERNAME",command['message']['data']['username'])
|
my_prompt = my_prompt.replace(
|
||||||
|
"USERNAME", command['message']['data']['username'])
|
||||||
|
|
||||||
print("Prompt selected: "+my_prompt)
|
print("Prompt selected: "+my_prompt)
|
||||||
the_data=DATA
|
the_data = DATA
|
||||||
the_data["prompt"]=my_prompt
|
the_data["prompt"] = my_prompt
|
||||||
return the_data
|
return the_data
|
||||||
|
|
||||||
|
|
||||||
value='{"service": "papa", "message": {"command": "aidgaf", "data": {"username": "AdamOutler"}, "timestamp": 1675373229}, "hash": "de08e85b8afc3b7d257fa559fd1dd295838ea1387f1e0fa75ceb11d9da81e59fadfc878d028281dfb739002bae818b89a8363e49d68e923874c969716a90f8e3"}'
|
|
||||||
|
|
||||||
|
def get_message_hash(json_command) -> bytes:
|
||||||
|
"""Get the object named "message", and run a hash over it. """
|
||||||
|
strip1 = re.sub('.*\"message\":', "", json_command, 1)
|
||||||
|
strip2 = re.sub(',\"hash\":.*', '', strip1)
|
||||||
|
json_value = bytes(strip2, "utf-8")
|
||||||
|
hash_value = hash_calculator.calculate_hash(json_value, settings.HASHKEY)
|
||||||
|
return hash_value
|
||||||
|
|
||||||
|
|
||||||
|
def verify_message_time(json_command) -> bool:
|
||||||
|
"""Check message expiration is less than current time + time in settings.
|
||||||
|
Before we accept the JSON as valid, and parse it, we must check the timestamp.
|
||||||
|
The timestamp is a Linux Timestamp. So convert it, add maximum message age, and
|
||||||
|
then verify current time is less than expiration time.
|
||||||
|
"""
|
||||||
|
strip_json = re.sub('.*\"timestamp\":', "", json_command, 1)
|
||||||
|
expiration = int(re.sub('}.*', '', strip_json))+settings.MAX_MESSAGE_AGE
|
||||||
|
current_time = int(time())
|
||||||
|
return not current_time > expiration
|
||||||
|
|
||||||
|
|
||||||
|
value = '{"service":"papa","message":{"command":"aidgaf","data":{"username":"AdamOutler"},"timestamp":1675725191},"hash":"1bc73914478835d03f9ebdfb46328321d2bb656647e2876d6f162cc1860607fcfca8d825c48e390a6a254ee0835c8a4fe5f9a25795a3a0880ae5a23e9c132cf2"}'
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(parse_idgaf_request(command=json.loads(value)))
|
|
||||||
|
if get_message_hash(value) not in value:
|
||||||
|
print("Error: hash not match")
|
||||||
|
if not verify_message_time(value):
|
||||||
|
print("Error: timestamp expired")
|
||||||
|
command = json.loads(value)
|
||||||
|
[code, result] = parse_idgaf_request(command)
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
|
||||||
#curl https://api.openai.com/v1/completions \
|
# curl https://api.openai.com/v1/completions \
|
||||||
# -H "Content-Type: application/json" \
|
# -H "Content-Type: application/json" \
|
||||||
# -H "Authorization: Bearer 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" \
|
# -H "Authorization: Bearer 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" \
|
||||||
# -d '{"model": "text-curie-001",
|
# -d '{"model": "text-curie-001",
|
||||||
# "prompt": "Say \"Adam does not give a fuck\" in a thoughtful and clever prose consisting of one to five paragraphs.",
|
# "prompt": "Say \"Adam does not give a fuck\" in a thoughtful and clever prose consisting of one to five paragraphs.",
|
||||||
# "temperature":1, "max_tokens": 500}'
|
# "temperature":1, "max_tokens": 500}'
|
||||||
# |jq -r .choices[0].text
|
# |jq -r .choices[0].text
|
||||||
# curl -X PATCH 127.0.0.1:8087 -d '{"message":{"command":"aidgaf","data":{"username":"AdamOutler"}}}'
|
# curl -X PATCH 127.0.0.1:8087 -d '{"message":{"command":"aidgaf","data":{"username":"AdamOutler"}}}'
|
||||||
#2,500,000 tokens = $5
|
# 2,500,000 tokens = $5
|
||||||
|
@ -8,8 +8,11 @@ APIKEY:str = os.getenv('APIKEY') #secret key from OpenAPI website
|
|||||||
if APIKEY is None:
|
if APIKEY is None:
|
||||||
raise Exception("APIKEY Environmental Variable must be set")
|
raise Exception("APIKEY Environmental Variable must be set")
|
||||||
#The hash key
|
#The hash key
|
||||||
HASHKEY:str = os.getenv('HASHKEY') #shared secret for hmac of message
|
HASHKEY:str = bytes(os.getenv('HASHKEY'),'utf-8') #shared secret for hmac of message
|
||||||
#The prompts used for OpenAI.
|
#The prompts used for OpenAI.
|
||||||
PROMPTS=["Say \"USERNAME does not give a fuck\" in a thoughtful and clever paragraph of 5 sentences.",
|
PROMPTS=["Say \"USERNAME does not give a fuck\" in a thoughtful and clever paragraph of 5 sentences.",
|
||||||
"Say \"USERNAME does not give a fuck\" in a Dr Suess poem.",
|
"Say \"USERNAME does not give a fuck\" in a Dr Suess poem.",
|
||||||
"Tell me all about how much \"USERNAME does not give a fuck\" using your most colorful words."]
|
"Tell me all about how much \"USERNAME does not give a fuck\" using your most colorful words."]
|
||||||
|
OPEN_AI_MAX_TOKENS=500
|
||||||
|
OPEN_AI_COMPLETION_MODEL="text-davinci-003"
|
||||||
|
MAX_MESSAGE_AGE=600
|
Loading…
x
Reference in New Issue
Block a user