mirror of
https://github.com/radareorg/r2ai.git
synced 2026-11-09 17:46:36 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1e0b4739a | ||
|
|
ed1269e66d | ||
|
|
b375e89b16 | ||
|
|
8f91fb1452 | ||
|
|
350f4d8f2b | ||
|
|
7632448dc0 | ||
|
|
2a244bbace |
No files matched your search
@@ -2,11 +2,17 @@
|
||||
# pylint: disable=import-outside-toplevel
|
||||
# pylint: disable=unused-import
|
||||
# pylint: disable=missing-function-docstring
|
||||
|
||||
import sys
|
||||
import os
|
||||
import argparse
|
||||
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
sys.path.append(script_dir)
|
||||
|
||||
|
||||
from r2ai.main import main as r2ai_main
|
||||
from r2ai.main import register_r2plugin
|
||||
|
||||
def is_valid_file(parser, arg):
|
||||
if not os.path.isfile(arg):
|
||||
@@ -55,4 +61,8 @@ def main():
|
||||
r2ai_main(args, args.command, runrepl)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
try:
|
||||
import r2lang # pylint: disable=import-error
|
||||
register_r2plugin()
|
||||
except ImportError:
|
||||
main()
|
||||
+19
-8
@@ -3,19 +3,30 @@ import json
|
||||
import sys
|
||||
import re
|
||||
import os
|
||||
import boto3
|
||||
|
||||
have_bedrock = True
|
||||
|
||||
try:
|
||||
import boto3
|
||||
from .backend.bedrock import (
|
||||
BEDROCK_TOOLS_CONFIG, build_messages_for_bedrock, extract_bedrock_tool_calls,
|
||||
process_bedrock_tool_calls, print_bedrock_response
|
||||
)
|
||||
except Exception:
|
||||
have_bedrock = False
|
||||
|
||||
from llama_cpp import Llama
|
||||
from llama_cpp.llama_tokenizer import LlamaHFTokenizer
|
||||
from transformers import AutoTokenizer
|
||||
from anthropic import Anthropic
|
||||
|
||||
have_anthropic = True
|
||||
try:
|
||||
from anthropic import Anthropic
|
||||
from .anthropic import construct_tool_use_system_prompt, extract_claude_tool_calls
|
||||
except Exception:
|
||||
have_anthorpic = False
|
||||
|
||||
from . import index
|
||||
from .anthropic import construct_tool_use_system_prompt, extract_claude_tool_calls
|
||||
from .backend.bedrock import (
|
||||
BEDROCK_TOOLS_CONFIG, build_messages_for_bedrock, extract_bedrock_tool_calls,
|
||||
process_bedrock_tool_calls, print_bedrock_response
|
||||
)
|
||||
from .pipe import have_rlang, r2lang, get_r2_inst
|
||||
|
||||
ANSI_REGEX = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
|
||||
@@ -443,4 +454,4 @@ auto_chat_handlers = {
|
||||
"llama": {
|
||||
"default": auto_chat_llama
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -5,7 +5,11 @@ import builtins
|
||||
import requests
|
||||
import json
|
||||
import traceback
|
||||
import chromadb
|
||||
have_chrome = True
|
||||
try:
|
||||
import chromadb
|
||||
except Exception:
|
||||
have_chrome = False
|
||||
from unidecode import unidecode
|
||||
import sys
|
||||
from r2ai import LOGGER
|
||||
|
||||
+12
-3
@@ -5,8 +5,13 @@ import sys
|
||||
import traceback
|
||||
import json
|
||||
import platform
|
||||
have_local = True
|
||||
import getpass
|
||||
import tokentrim
|
||||
|
||||
try:
|
||||
import tokentrim
|
||||
except Exception:
|
||||
have_local = False
|
||||
|
||||
from rich.rule import Rule
|
||||
from signal import signal, SIGINT
|
||||
@@ -952,7 +957,7 @@ class Interpreter:
|
||||
self.messages.append({"role": "assistant", "content": response})
|
||||
print(response)
|
||||
self.logger.warn("For a better experience install openai python")
|
||||
self.load.warn("pip install -U openai")
|
||||
self.logger.warn("pip install -U openai")
|
||||
self.logger.warn("export OPENAI_API_KEY=...")
|
||||
return
|
||||
|
||||
@@ -991,7 +996,11 @@ class Interpreter:
|
||||
return
|
||||
|
||||
elif self.model.startswith("bedrock:"):
|
||||
import boto3
|
||||
try:
|
||||
import boto3
|
||||
except Exception:
|
||||
self.logger.error("Cannot import boto3. No bedrock for now")
|
||||
return
|
||||
bedrock_model = self.model.split(":")[1] + ":0"
|
||||
self.bedrock_client = boto3.client("bedrock-runtime")
|
||||
request = {
|
||||
|
||||
+10
-6
@@ -23,12 +23,12 @@ if "R2CORE" in os.environ:
|
||||
within_r2 = True
|
||||
|
||||
def r2ai_rlang_plugin(unused_but_required_argument):
|
||||
global ai
|
||||
ai = r2ai_singleton()
|
||||
def _call(s):
|
||||
if not s.startswith("r2ai"):
|
||||
return False
|
||||
try:
|
||||
run_rcfile_once()
|
||||
run_rcfile_once(ai)
|
||||
if len(s) == 4:
|
||||
builtins.print(help_message)
|
||||
else:
|
||||
@@ -47,7 +47,7 @@ def r2ai_rlang_plugin(unused_but_required_argument):
|
||||
}
|
||||
|
||||
# TODO: see repl.run_script as replacement
|
||||
def run_rcfile():
|
||||
def run_rcfile(ai):
|
||||
try:
|
||||
lines = slurp(R2AI_RCFILE)
|
||||
|
||||
@@ -61,13 +61,17 @@ def run_rcfile():
|
||||
if ai is None:
|
||||
ai = r2ai_singleton()
|
||||
|
||||
def run_rcfile_once():
|
||||
def run_rcfile_once(ai):
|
||||
global RCFILE_LOADED
|
||||
if not RCFILE_LOADED:
|
||||
run_rcfile()
|
||||
run_rcfile(ai)
|
||||
RCFILE_LOADED = True
|
||||
|
||||
|
||||
def register_r2plugin():
|
||||
import r2lang
|
||||
r2lang.plugin("core", r2ai_rlang_plugin)
|
||||
|
||||
def main(args, commands, dorepl=True):
|
||||
global within_r2
|
||||
|
||||
@@ -133,7 +137,7 @@ def main(args, commands, dorepl=True):
|
||||
# print("[R2AI] Please: r2pm -ci rlang-python")
|
||||
# sys.exit(0)
|
||||
|
||||
# run_rcfile()
|
||||
# run_rcfile(ai)
|
||||
# if len(sys.argv) > 1:
|
||||
# for arg in sys.argv[1:]:
|
||||
# if arg.endswith(".py"):
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ except Exception:
|
||||
r2lang = FakeLang(r2pipe.open())
|
||||
r2lang.cmd("?V") # r2pipe throws only here
|
||||
else:
|
||||
raise Error("must spawn")
|
||||
raise Exception("must spawn")
|
||||
except Exception:
|
||||
try:
|
||||
have_rlang = False
|
||||
|
||||
+1
-1
@@ -205,7 +205,7 @@ def runline(ai, usertext):
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
if usertext.startswith("-VV"):
|
||||
from ui.app import R2AIApp
|
||||
from ui.app import R2AIApp # pylint: disable=import-error
|
||||
R2AIApp().run()
|
||||
return
|
||||
if usertext.startswith("?V") or usertext.startswith("-v"):
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import sys
|
||||
from .const import R2AI_HISTFILE, R2AI_HOMEDIR, R2AI_RCFILE, R2AI_USERDIR
|
||||
from .models import models
|
||||
|
||||
@@ -67,7 +68,7 @@ class MyCompleter(object): # Custom completer
|
||||
|
||||
def display_matches(self, substitution, matches, longest_match_length):
|
||||
line_buffer = readline.get_line_buffer()
|
||||
columns = environ.get("COLUMNS", 80)
|
||||
columns = os.environ.get("COLUMNS", 80)
|
||||
print()
|
||||
tpl = "{:<" + str(int(max(map(len, matches)) * 1.2)) + "}"
|
||||
buffer = ""
|
||||
|
||||
Reference in new issue
Block a user