repl
This commit is contained in:
@@ -4,19 +4,22 @@ from re import search
|
||||
from dataclasses import dataclass
|
||||
from typing import Union
|
||||
|
||||
from langchain_core.tools import Tool
|
||||
from langchain_experimental.utilities import PythonREPL
|
||||
|
||||
@tool
|
||||
def add(a: float, b: float) -> str:
|
||||
"""Adds a+b and returns the sum"""
|
||||
af = float(a)
|
||||
bf = float(b)
|
||||
return f"{a} + {b} = {a+b}"
|
||||
return f"{af} + {af} = {af+bf}"
|
||||
|
||||
@tool
|
||||
def multiply(a: float, b: float) -> str:
|
||||
"""Multiplies a*b and returns the product"""
|
||||
af = float(a)
|
||||
bf = float(b)
|
||||
return f"{a} * {b} = {a*b}"
|
||||
return f"{af} * {bf} = {af*bf}"
|
||||
|
||||
@tool
|
||||
def get_current_date_and_time() -> str:
|
||||
@@ -99,10 +102,13 @@ def get_notes_in_timespan(begin: str, to: str) -> str:
|
||||
try:
|
||||
begin_d = datetime.strptime(begin, "%Y/%m/%d")
|
||||
to_d = datetime.strptime(to+" 23:59", "%Y/%m/%d %H:%M")
|
||||
except: return "Error: Invalid input. Date format is %Y/%m/%d"
|
||||
except ValueError:
|
||||
return "Error: Invalid input. Date format is %Y/%m/%d"
|
||||
|
||||
try: assert begin_d < to_d
|
||||
except: return "Error: from time has to be before to time."
|
||||
try:
|
||||
assert begin_d < to_d
|
||||
except AssertionError:
|
||||
return "Error: from time has to be before to time."
|
||||
|
||||
filtered_entries = [entry for entry in note_entries if begin_d <= entry.time <= to_d]
|
||||
|
||||
@@ -128,9 +134,12 @@ def get_notes_containing(patterns: Union[list[str], str]) -> str:
|
||||
exaples:
|
||||
{"patterns": [ "Aunt(ie)?", "Sabine" ]} # Looks for Notes related to Aunt Sabine"""
|
||||
|
||||
if isinstance(patterns, list): big_pattern = '|'.join(f"({s})" for s in patterns)
|
||||
elif isinstance(patterns, str): big_pattern = patterns
|
||||
else: return f"Error: Invalid Input type. `patterns` can either be a list of strings or a single string. But got {type(patterns)}."
|
||||
if isinstance(patterns, list):
|
||||
big_pattern = '|'.join(f"({s})" for s in patterns)
|
||||
elif isinstance(patterns, str):
|
||||
big_pattern = patterns
|
||||
else:
|
||||
return f"Error: Invalid Input type. `patterns` can either be a list of strings or a single string. But got {type(patterns)}."
|
||||
|
||||
filtered_entries = [entry for entry in note_entries if search(big_pattern.lower(), entry.content.lower())]
|
||||
|
||||
@@ -147,7 +156,29 @@ def get_notes_containing(patterns: Union[list[str], str]) -> str:
|
||||
return ret
|
||||
|
||||
@tool
|
||||
def write_note(content: str) -> str:
|
||||
def write_note(command: str) -> str:
|
||||
"""Write a not with the current time to the database."""
|
||||
return content
|
||||
return command
|
||||
|
||||
@tool
|
||||
def save_python_repl(command: str):
|
||||
"""Simulates the normal python repl but with certain patterns blocked for savety reasons"""
|
||||
python_repl = PythonREPL()
|
||||
blocked_patterns = [
|
||||
"^os\\.",
|
||||
"^subprocess\\.",
|
||||
"^with open\\(",
|
||||
]
|
||||
valid = True
|
||||
for pattern in blocked_patterns:
|
||||
if search(pattern, command):
|
||||
valid = False
|
||||
break
|
||||
|
||||
if valid:
|
||||
return python_repl.run(command)
|
||||
else:
|
||||
return f"Command not executed, becaise the blocked pattern `{pattern}` was found in the command."
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from libs.classes import Test
|
||||
from libs.runnables import basic_prompt, one_tool_call_answer, agent_with_tools
|
||||
from libs.validators import regex_match_any, system_human_answer_match
|
||||
from libs.tools import add, multiply, get_current_date_and_time, get_notes_in_timespan, get_notes_containing, write_note
|
||||
from libs.tools import add, multiply, get_current_date_and_time, get_notes_in_timespan, get_notes_containing, write_note, save_python_repl
|
||||
from textwrap import dedent
|
||||
from langchain_core.messages import HumanMessage, SystemMessage, ToolMessage, AIMessage
|
||||
|
||||
@@ -49,6 +49,19 @@ tests = {
|
||||
validator=regex_match_any,
|
||||
validation_input={"patterns": ["6134205", "6.134.205", "6,134,205"]},
|
||||
),
|
||||
363: Test(
|
||||
name="Complex Multiplication Python",
|
||||
runnable=one_tool_call_answer,
|
||||
runnable_input={
|
||||
"system_msg": 'You are a helpful assistant.',
|
||||
"human_msg": 'Is 31515261 divisible by 425? If not, whats the remainder?',
|
||||
"tools": { "python_repl": save_python_repl },
|
||||
},
|
||||
validator=regex_match_any,
|
||||
validation_input={
|
||||
"patterns": [ "236", "two ?hundred and thirty ?six", "two ?hundred thirty ?six" ]
|
||||
}
|
||||
),
|
||||
283: Test(
|
||||
name="Notes from last Saturday",
|
||||
runnable=agent_with_tools,
|
||||
@@ -119,7 +132,6 @@ tests = {
|
||||
- just one single conversational answer, without any AI fragments (A/B versions, "end of message" parts, unfitting discalimers or notes, what specific tool was used to get the answer, etc.)""")
|
||||
},
|
||||
),
|
||||
# 363: Test(),
|
||||
# 600: Test(),
|
||||
# 221: Test(),
|
||||
# 985: Test(),
|
||||
|
||||
Reference in New Issue
Block a user