Skip to content

Commit

Permalink
Clean up ticket processing on genai server
Browse files Browse the repository at this point in the history
  • Loading branch information
danopato committed Mar 5, 2024
1 parent 9ae688b commit 9640c35
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
17 changes: 12 additions & 5 deletions gai-backend/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import websockets

class jobs:
def __init__(self, model, url):
def __init__(self, model, url, llmkey, llmparams):
self.queue = asyncio.PriorityQueue()
self.sessions = {}
self.model = model
self.url = url
self.llmkey = llmkey
self.llmparams = llmparams

def get_queues(self, id):
squeue = asyncio.Queue(maxsize=10)
Expand All @@ -31,10 +33,15 @@ async def process_jobs(self):
id, bid, job = job_params
await self.sessions[id]['send'].put(json.dumps({'type': 'started'}))
result = ""
data = {'model': self.model, "temperature": 0.7, "top_p": 1,
"max_tokens": 4000, "stream": False, "safe_prompt": False, "random_seed": None,
'messages': [{'role': 'user', 'content': job['prompt']}]}
r = requests.post(self.url, data=json.dumps(data))
data = {'model': self.model, 'messages': [{'role': 'user', 'content': job['prompt']}]}
print(f'data: {data}')
print(f'llmparams: {self.llmparams}')
data = {**data, **self.llmparams}
headers = {"Content-Type":"application/json"}
print(f'llmkey: {self.llmkey}')
if not self.llmkey is None:
headers["Authorization"] = f"Bearer {self.llmkey}"
r = requests.post(self.url, data=json.dumps(data), headers=headers)
result = r.json()
print(result)
if result['object'] != 'chat.completion':
Expand Down
28 changes: 20 additions & 8 deletions gai-backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
import traceback
import sys

uint256 = pow(2,256) - 1
uint64 = pow(2,64) - 1
wei = pow(10, 18)

prices = {
'invoice': 0.0001,
'payment': 0.0001,
Expand All @@ -32,7 +36,7 @@
disconnect_threshold = -25

def invoice(amt, commit, recipient):
return json.dumps({'type': 'invoice', 'amount': amt, 'commit': '0x' + str(commit), 'recipient': recipient})
return json.dumps({'type': 'invoice', 'amount': int(pow(10,18) * amt), 'commit': '0x' + str(commit), 'recipient': recipient})

def process_tickets(tix, recip, reveal, commit, lotto, key):
try:
Expand All @@ -43,7 +47,8 @@ def process_tickets(tix, recip, reveal, commit, lotto, key):
hash = lotto.claim_ticket(tk, recip, key, reveal)
print(f"Claim tx: {hash}")
reveal, commit = new_reveal()
return tk.face_value() / pow(10,18), reveal, commit
tk.print_ticket()
return tk.value() / wei, reveal, commit
except Exception:
print('process_ticket() failed')
exc_type, exc_value, exc_traceback = sys.exc_info()
Expand Down Expand Up @@ -95,6 +100,7 @@ async def session(websocket, bills=None, job=None, recipient='0x0', key=''):
if message['type'] == 'payment':
try:
amt, reveal, commit = process_tickets(message['tickets'], recipient, reveal, commit, lotto, key)
print(f'Got ticket worth {amt}')
bills.credit(id, amount=amt)
except:
print('outer failure in processing payment')
Expand Down Expand Up @@ -131,23 +137,29 @@ async def session(websocket, bills=None, job=None, recipient='0x0', key=''):
break


async def main(model, url, bind_addr, bind_port, recipient_key):
async def main(model, url, bind_addr, bind_port, recipient_key, llmkey, llmparams):
recipient_addr = web3.Account.from_key(recipient_key).address
bills = billing.Billing(prices)
job = jobs.jobs(model, url)
job = jobs.jobs(model, url, llmkey, llmparams)
print("\n*****")
print(f"* Server starting up at {bind_addr} {bind_port}")
print(f"* Connecting to back end at {url}")
print(f"* With model {model}")
print(f"* Using wallet at {recipient_addr}")
print("******\n\n")
async with websockets.serve(functools.partial(session, bills=bills, job=job, recipient=recipient_addr, key=recipient_key), bind_addr, bind_port):
async with websockets.serve(functools.partial(session, bills=bills, job=job,
recipient=recipient_addr, key=recipient_key),
bind_addr, bind_port):
await asyncio.wait([asyncio.create_task(job.process_jobs())])

if __name__ == "__main__":
bind_addr = os.environ['ORCHID_GENAI_ADDR']
bind_port = os.environ['ORCHID_GENAI_PORT']
recipient_key = os.environ['ORCHID_GENAI_RECIPIENT_KEY']
url = sys.argv[1]
model = sys.argv[2]
asyncio.run(main(model, url, bind_addr, bind_port, recipient_key))
url = os.environ['ORCHID_GENAI_LLM_URL']
model = os.environ['ORCHID_GENAI_LLM_MODEL']
llmkey = None if 'ORCHID_GENAI_LLM_AUTH_KEY' not in os.environ else os.environ['ORCHID_GENAI_LLM_AUTH_KEY']
llmparams = {}
if 'ORCHID_GENAI_LLM_PARAMS' in os.environ:
llmparams = json.loads(os.environ['ORCHID_GENAI_LLM_PARAMS'])
asyncio.run(main(model, url, bind_addr, bind_port, recipient_key, llmkey, llmparams))
6 changes: 5 additions & 1 deletion gai-backend/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,16 @@ def is_winner(self, reveal):
return False
return True

def face_value(self):
def value(self):
return self.amount * self.ratio / uint64

def print_ticket(self):
amount = self.packed0 & uint128
nonce = (self.packed0 >> 128) & uint64
funder = addrtype & (self.packed1 >> 1)
ratio = uint64 & (self.packed1 >> 161)
print('Print_ticket():')
print(f'Face Value: {amount}')
print(f'Funder: {funder}')
print(f'Ratio: {ratio}')

0 comments on commit 9640c35

Please sign in to comment.