arre99 commited on
Commit
b0c3be9
·
verified ·
1 Parent(s): 8c5c24b

added custom Levenschtein distance and already defined tools

Browse files
Files changed (1) hide show
  1. app.py +40 -1
app.py CHANGED
@@ -17,6 +17,45 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
17
  arg2: the second argument
18
  """
19
  return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,7 +94,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
17
  arg2: the second argument
18
  """
19
  return "What magic will you build ?"
20
+
21
+ @tool
22
+ def levenshtein_distance(str1:str, str2:str) -> str:
23
+ """
24
+ Calculate the Levenshtein distance (edit distance) between two strings.
25
+
26
+ The Levenshtein distance is the minimum number of single-character operations
27
+ (insertions, deletions, or substitutions) required to change one string into another.
28
+
29
+ Args:
30
+ str1 (str): First string
31
+ str2 (str): Second string
32
+ """
33
+ # Create a matrix of size (len(str1)+1) x (len(str2)+1)
34
+ m, n = len(str1), len(str2)
35
+ dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
36
+
37
+ # Initialize the first row and column
38
+ for i in range(m + 1):
39
+ dp[i][0] = i
40
+ for j in range(n + 1):
41
+ dp[0][j] = j
42
+
43
+ # Fill the matrix using dynamic programming
44
+ for i in range(1, m + 1):
45
+ for j in range(1, n + 1):
46
+ # If characters match, no operation needed
47
+ if str1[i-1] == str2[j-1]:
48
+ dp[i][j] = dp[i-1][j-1]
49
+ else:
50
+ # Take the minimum of three operations:
51
+ # 1. Insertion: dp[i][j-1] + 1
52
+ # 2. Deletion: dp[i-1][j] + 1
53
+ # 3. Substitution: dp[i-1][j-1] + 1
54
+ dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])
55
+
56
+ # The bottom-right cell contains the Levenshtein distance
57
+ return f"The edit distance between {str1} and {str1} is {dp[m][n]} operations"
58
+
59
 
60
  @tool
61
  def get_current_time_in_timezone(timezone: str) -> str:
 
94
 
95
  agent = CodeAgent(
96
  model=model,
97
+ tools=[final_answer, image_generation_tool, levenshtein_distance, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
98
  max_steps=6,
99
  verbosity_level=1,
100
  grammar=None,