Skip to content

Commit

Permalink
Merge pull request #35 from nadheesh/main
Browse files Browse the repository at this point in the history
Replace deprecated regex module with lang-regexp
  • Loading branch information
nadheesh committed Dec 7, 2023
2 parents 3f38d4a + b8331d8 commit d736a04
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 23 deletions.
11 changes: 5 additions & 6 deletions ballerina/agent.bal
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import ballerina/io;
import ballerina/lang.value;
import ballerina/lang.regexp;
import ballerina/log;
import ballerina/regex;

# Parsed response from the LLM.
type NextTool record {|
Expand Down Expand Up @@ -53,7 +53,6 @@ public class AgentIterator {
} {
return self.executor;
}

}

public class AgentExecutor {
Expand Down Expand Up @@ -101,7 +100,7 @@ public class AgentExecutor {
isCompleted: true
};
}
string[] content = regex:split(llmResponse + "<endtoken>", "```");
string[] content = regexp:split(re `${"```"}`, llmResponse + "<endtoken>");
if content.length() < 3 {
log:printWarn("Unexpected LLM response is given", llmResponse = llmResponse);
return error LlmActionParseError("Unable to extract the tool due to invalid generation", llmResponse = llmResponse, instruction = "Tool execution failed due to invalid generation. Regenerate following the given format.");
Expand Down Expand Up @@ -348,8 +347,8 @@ isolated function normalizeLlmResponse(string llmResponse) returns string {
}
}
}
thought = regex:replace(thought, "```json", "```");
thought = regex:replaceAll(thought, "\"\\{\\}\"", "{}");
thought = regex:replaceAll(thought, "\\\\\"", "\"");
thought = regexp:replace(re `${"```"}json`, thought, "```"); // replace ```json
thought = regexp:replaceAll(re `"\{\}"`, thought, "{}"); // replace "{}"
thought = regexp:replaceAll(re `\\"`, thought, "\""); // replace \"
return thought;
}
6 changes: 3 additions & 3 deletions ballerina/http_utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// under the License.

import ballerina/http;
import ballerina/lang.regexp;
import ballerina/mime;
import ballerina/regex;
import ballerina/url;

type QueryParamEncoding record {
Expand Down Expand Up @@ -233,7 +233,7 @@ isolated function getParamEncodedPath(HttpTool tool, map<json>? parameters) retu
}
json parameterValue = parameters.get(paramName);
string value = check getSimpleStyleParams(paramName, parameterValue);
pathWithParams = regex:replaceAll(pathWithParams, string `\{${paramName}\}`, value);
pathWithParams = regexp:replaceAll(re `\{${paramName}\}`, pathWithParams, value);
} else {
if parameters.hasKey(paramName) {
queryParams[paramName] = parameters.get(paramName);
Expand Down Expand Up @@ -265,7 +265,7 @@ isolated function extractResponsePayload(string path, http:Response response) re

json|xml|error body;
string contentType = response.getContentType();
match regex:split(contentType, ";")[0].trim() {
match regexp:split(re `;`, contentType)[0].trim() {
mime:APPLICATION_JSON|mime:APPLICATION_XML|mime:TEXT_PLAIN|mime:TEXT_HTML|mime:TEXT_XML => {
body = response.getTextPayload();
}
Expand Down
6 changes: 3 additions & 3 deletions ballerina/tests/mock-tools.bal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ballerina/regex;
import ballerina/lang.regexp;

type SearchParams record {|
string query;
Expand All @@ -11,10 +11,10 @@ type CalculatorParams record {|
// create two mock tools
isolated function searchToolMock(*SearchParams params) returns string {
string query = params.query.trim().toLowerAscii();
if regex:matches(query, ".*girlfriend.*") {
if regexp:isFullMatch(re `.*girlfriend.*`, query) {
return "Camila Morrone";

} else if regex:matches(query, ".*age.*") {
} else if regexp:isFullMatch(re `.*age.*`, query) {
return "25 years";
}
else {
Expand Down
4 changes: 2 additions & 2 deletions ballerina/tool.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// specific language governing permissions and limitations
// under the License.

import ballerina/regex;
import ballerina/lang.regexp;

type AgentTool record {|
string name;
Expand Down Expand Up @@ -110,7 +110,7 @@ isolated function registerTool(map<AgentTool & readonly> toolMap, Tool[] tools)

AgentTool agentTool = {
name: tool.name,
description: regex:replaceAll(tool.description, "\n", " "),
description: regexp:replaceAll(re `\n`, tool.description, ". "),
variables: variables,
constants: constants,
caller: tool.caller
Expand Down
9 changes: 0 additions & 9 deletions ballerina/toolkit.bal
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ public type ParameterSchema record {|
JsonSubSchema schema;
|};

// # Defines a HTTP parameter schema (can be query parameter or path parameters).
// public type Parameters record {|
// # A list of mandatory parameters
// string[] required?;
// # A map of parameter names and their types
// map<ParameterSchema> schemas;
// |};

# Defines an HTTP tool. This is a special type of tool that can be used to invoke HTTP resources.
public type HttpTool record {|
# Name of the Http resource tool
Expand Down Expand Up @@ -259,4 +251,3 @@ public isolated class HttpServiceToolKit {
return extractResponsePayload(path, optionsResult);
}
}

0 comments on commit d736a04

Please sign in to comment.