Skip to content

Commit

Permalink
More indentation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed May 14, 2024
1 parent 493c012 commit 7538b43
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 344 deletions.
1 change: 0 additions & 1 deletion examples/funcall.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,3 @@ def get_current_weather( location, unit, source):
# print(res)
print(res["choices"][0]["text"])
# print(llm(prompt=leprompt)["choices"])

4 changes: 2 additions & 2 deletions r2ai/bubble.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
O O |
|| |
. ||_.|
/| | |
/ | `---'
/| | |
/ | `---'
.---------------------------------------------------------------' '----"""

CLIPPY_END = """`-----------------------------------------------------------------------"""
Expand Down
30 changes: 15 additions & 15 deletions r2ai/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,28 @@
r2clippy = False
have_rlang = False
try:
import r2lang
have_rlang = True
print = r2lang.print
r2clippy = True
import r2lang
have_rlang = True
print = r2lang.print
r2clippy = True
except:
pass
pass

Ginterrupted = False
def signal_handler(sig, frame):
global Ginterrupted
if Ginterrupted:
sys.exit(0) # throws exception
Ginterrupted = True
print("^C", file=sys.stderr)
global Ginterrupted
if Ginterrupted:
sys.exit(0) # throws exception
Ginterrupted = True
print("^C", file=sys.stderr)
signal(SIGINT, signal_handler)

def exception_handler(self, sig, frame):
global Ginterrupted
if Ginterrupted:
sys.exit(0) # throws exception
Ginterrupted = True
print("^C", file=sys.stderr)
global Ginterrupted
if Ginterrupted:
sys.exit(0) # throws exception
Ginterrupted = True
print("^C", file=sys.stderr)
sys.excepthook = exception_handler

def incodeblock(msg):
Expand Down
14 changes: 7 additions & 7 deletions r2ai/large.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, ai = None):
else:
self.env = {}
self.env["llm.gpu"] = "true"

def slice_text(self, amsg):
slices = []
pos = self.maxlen
Expand All @@ -24,7 +24,7 @@ def slice_text(self, amsg):
slices.append(s)
slices.append(amsg)
return slices

def compress_text(self, msg):
if self.mistral == None:
self.mistral = new_get_hf_llm(self, self.model, False, self.window)
Expand All @@ -35,7 +35,7 @@ def compress_text(self, msg):
print(response["choices"]) #json.dumps(response))
text0 = response["choices"][0]["text"]
return text0

def summarize_text(self, amsg):
olen = len(amsg)
while len(amsg) > self.maxlen:
Expand All @@ -51,7 +51,7 @@ def summarize_text(self, amsg):
nlen = len(amsg)
print(f"total length {nlen} (original length was {olen})")
return amsg

def keywords_ai(self, text):
# kws = self.keywords_ai("who is the author of radare?") => "author,radare2"
words = []
Expand Down Expand Up @@ -104,7 +104,7 @@ def trimsource(self, msg):
msg = re.sub(r"\s+", " ", msg)
# msg = msg.replace(";", "")
return msg.strip()

def trimsource_ai(self, msg):
words = []
if self.mistral == None:
Expand All @@ -117,7 +117,7 @@ def trimsource_ai(self, msg):
if "```" in text0:
return text0.split("```")[1].strip()
return text0.strip().replace("```", "")

def compress_code_ai(self, code):
piecesize = 1024 * 8 # mistral2 supports 32k vs 4096
codelen = len(code)
Expand All @@ -137,7 +137,7 @@ def compress_code_ai(self, code):
res.append(r)
off += plen
return "\n".join(res)

def compress_messages(self, messages):
# TODO: implement a better logic in here asking the lm to summarize the context
olen = 0
Expand Down
216 changes: 108 additions & 108 deletions r2ai/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ def __main__():
os.environ["TOKENIZERS_PARALLELISM"]="false"

try:
r2aihome = os.path.dirname(os.readlink(__file__))
sys.path.append(r2aihome)
print(r2aihome)
# if available
sys.path.append(f"{r2aihome}/../vectordb")
r2aihome = os.path.dirname(os.readlink(__file__))
sys.path.append(r2aihome)
print(r2aihome)
# if available
sys.path.append(f"{r2aihome}/../vectordb")
except:
pass
pass


# create symlink if it doesnt exist
Expand All @@ -34,13 +34,13 @@ def __main__():

OPENAI_KEY = ""
try:
if "HOME" in os.environ:
from r2ai.utils import slurp
apikey = slurp(os.environ["HOME"] + "/.r2ai.openai-key").strip()
os.environ["OPENAI_API_KEY"] = apikey
print("[R2AI] OpenAI key loaded from ~/.r2ai.openai-key", file=sys.stderr)
if "HOME" in os.environ:
from r2ai.utils import slurp
apikey = slurp(os.environ["HOME"] + "/.r2ai.openai-key").strip()
os.environ["OPENAI_API_KEY"] = apikey
print("[R2AI] OpenAI key loaded from ~/.r2ai.openai-key", file=sys.stderr)
except:
pass
pass

r2 = None
r2_file = None
Expand All @@ -49,117 +49,117 @@ def __main__():
within_r2 = False
print = print
if "R2CORE" in os.environ:
within_r2 = True
within_r2 = True
if os.name != "nt":
try:
import r2lang
have_rlang = True
print = r2lang.print
except:
try:
import r2pipe
have_r2pipe = True
except:
pass
try:
import r2lang
have_rlang = True
print = r2lang.print
except:
try:
import r2pipe
have_r2pipe = True
except:
pass

if not have_rlang and not have_r2pipe and sys.argv[0] != 'main.py' and os.path.exists("venv/bin/python"):
os.system("venv/bin/python main.py")
sys.exit(0)
os.system("venv/bin/python main.py")
sys.exit(0)

if "R2PIPE_IN" in os.environ.keys():
try:
import r2pipe
have_r2pipe = True
except:
pass
try:
import r2pipe
have_r2pipe = True
except:
pass
### MAIN ###
ai = None
if have_r2pipe and not have_rlang:
try:
if "R2PIPE_IN" in os.environ.keys():
r2 = r2pipe.open()
within_r2 = True
else:
file = "/bin/ls"
for arg in sys.argv[1:]:
if arg.startswith("/"):
file = arg
r2_file = file
except:
traceback.print_exc()
try:
if "R2PIPE_IN" in os.environ.keys():
r2 = r2pipe.open()
within_r2 = True
else:
file = "/bin/ls"
for arg in sys.argv[1:]:
if arg.startswith("/"):
file = arg
r2_file = file
except:
traceback.print_exc()

# TODO: see repl.run_script as replacement
def run_rcfile():
global ai
try:
lines = slurp(R2AI_RCFILE)
from r2ai.interpreter import Interpreter
for line in lines.split("\n"):
if line.strip() != "":
if ai is None:
ai = Interpreter()
runline(ai, line)
except:
pass
if ai is None:
from r2ai.interpreter import Interpreter
ai = Interpreter()
global ai
try:
lines = slurp(R2AI_RCFILE)
from r2ai.interpreter import Interpreter
for line in lines.split("\n"):
if line.strip() != "":
if ai is None:
ai = Interpreter()
runline(ai, line)
except:
pass
if ai is None:
from r2ai.interpreter import Interpreter
ai = Interpreter()

rcfile_loaded = False
def run_rcfile_once():
global rcfile_loaded
if rcfile_loaded == False:
run_rcfile()
rcfile_loaded = True
global rcfile_loaded
if rcfile_loaded == False:
run_rcfile()
rcfile_loaded = True

if have_rlang:
from r2ai.repl import runline, r2ai_repl, help_message
if have_r2pipe:
r2ai_repl(ai)
os.exit(0)
def r2ai_rlang_plugin(unused_but_required_argument):
global ai
def _call(s):
if not s.startswith("r2ai"):
return False
try:
run_rcfile_once()
if len(s) == 4:
builtins.print(help_message)
else:
usertext = s[4:].strip()
runline(ai, usertext)
except Exception as e:
builtins.print(e)
traceback.print_exc()
return True
from r2ai.repl import runline, r2ai_repl, help_message
if have_r2pipe:
r2ai_repl(ai)
os.exit(0)
def r2ai_rlang_plugin(unused_but_required_argument):
global ai
def _call(s):
if not s.startswith("r2ai"):
return False
try:
run_rcfile_once()
if len(s) == 4:
builtins.print(help_message)
else:
usertext = s[4:].strip()
runline(ai, usertext)
except Exception as e:
builtins.print(e)
traceback.print_exc()
return True

return {
"name": "r2ai",
"license": "MIT",
"desc": "run llama language models inside r2",
"call": _call,
}
r2lang.plugin("core", r2ai_rlang_plugin)
return {
"name": "r2ai",
"license": "MIT",
"desc": "run llama language models inside r2",
"call": _call,
}
r2lang.plugin("core", r2ai_rlang_plugin)
else:
from r2ai.repl import runline, r2ai_repl
from r2ai.utils import slurp
run_rcfile()
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
if arg.endswith(".py"):
exec(slurp(arg), globals())
sys.stderr.close()
sys.exit(0)
elif not arg.startswith("/"):
runline(ai, arg)
if arg == "-h" or arg == "-v":
sys.exit(0)
elif arg == "-repl":
r2ai_repl(ai)
elif not within_r2:
r2ai_repl(ai)
elif have_r2pipe:
r2ai_repl(ai)
else:
print("r2ai plugin cannot be loaded. Run `r2pm -ci rlang-python`")
from r2ai.repl import runline, r2ai_repl
from r2ai.utils import slurp
run_rcfile()
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
if arg.endswith(".py"):
exec(slurp(arg), globals())
sys.stderr.close()
sys.exit(0)
elif not arg.startswith("/"):
runline(ai, arg)
if arg == "-h" or arg == "-v":
sys.exit(0)
elif arg == "-repl":
r2ai_repl(ai)
elif not within_r2:
r2ai_repl(ai)
elif have_r2pipe:
r2ai_repl(ai)
else:
print("r2ai plugin cannot be loaded. Run `r2pm -ci rlang-python`")
Loading

0 comments on commit 7538b43

Please sign in to comment.