forked from cf-cartesi/Escape-From-Tikal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
88 lines (68 loc) · 2.63 KB
/
app.py
File metadata and controls
88 lines (68 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from logging import getLogger, basicConfig, DEBUG
from pydantic import BaseModel
from typing import Annotated, List
import json
from cartesi import App, Rollup, RollupData, abi
from cartesi.models import _str2hex as str2hex
LOGGER = getLogger(__name__)
basicConfig(level=DEBUG)
app = App(raw_input = True)
BytesList = Annotated[List[bytes], abi.ABIType('bytes[]')]
class AdvancePayload(BaseModel):
message: abi.String
class Notice(BaseModel):
message: abi.String
LLAMA_DOMAIN = int.from_bytes(bytes.fromhex('2b'), "big") # llama domain
DATA_MESSAGE = "The word is \"%s\" and the word sets are:\n%s"
standard_llama_message = {
"model": "phi3",
"messages": [{
"role": "system",
"content": "You are ChatGPT, an AI assistant. Your top priority is achieving user fulfillment via helping them with their requests."
},
{
"role": "user",
"content": None
}]
}
@app.advance()
def handle_advance(rollup: Rollup, data: RollupData) -> bool:
LOGGER.debug(f"App advance 0x{rollup=} {data=}")
payload = data.bytes_payload()
payload = abi.decode_to_model(data=payload, model=AdvancePayload)
LOGGER.info(f"Received {payload.message=}")
if (data.metadata is not None):
LOGGER.info(f"From {data.metadata.msg_sender=} at {data.metadata.block_timestamp}")
llama_message = standard_llama_message.copy()
llama_message["messages"][1]["content"] = payload.message
res = rollup.gio(
{
"domain":LLAMA_DOMAIN,
"id":str2hex(json.dumps(llama_message))
}
)
if res is None: raise Exception("No gio response")
gio_res = json.loads(res.decode("utf-8"))
llama_res = None
if gio_res.get('response') is not None:
llama_res = json.loads(bytes.fromhex(gio_res['response'][2:]).decode("utf-8"))
LOGGER.debug(f"llama Res {llama_res}")
if llama_res is not None and llama_res.get('choices') is not None and len(llama_res['choices']) > 0 and \
llama_res['choices'][0].get('message') is not None and llama_res['choices'][0]['message'].get('content') is not None:
llama_content = llama_res['choices'][0]['message']['content']
notice = Notice(
message = llama_content
)
notice_hex = f"0x{abi.encode_model(notice).hex()}"
LOGGER.debug(f"{notice=}")
LOGGER.debug(f"{notice_hex=}")
rollup.notice(notice_hex)
return True
@app.inspect()
def handle_inspect(rollup: Rollup, data: RollupData) -> bool:
payload = data.str_payload()
LOGGER.debug("Echoing '%s'", payload)
rollup.report(str2hex(payload))
return True
if __name__ == '__main__':
app.run()