-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from stoerr/feature/runchatfromcommandline
Add pmcodevgpt : run a chat with the CoDeveloper engine on the command line
- Loading branch information
Showing
22 changed files
with
714 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env bash | ||
# "Poor mans" co developer engine: run it with the chatgpt script from https://github.com/stoerr/chatGPTtools | ||
|
||
# start the co-developer-gpt-engine.jar in the directory this script is placed, following links | ||
progfile=$(realpath $0) | ||
progdir=$(dirname "$progfile") | ||
|
||
JAVA=java | ||
# if jenv is in the path, we use the version that is set for the directory this script is in, | ||
# since the current dir could use some ridiculously low version. | ||
if which jenv >/dev/null 2>&1; then | ||
JAVA=$(cd $progdir; jenv which java) | ||
fi | ||
|
||
$JAVA -jar "$progdir/co-developer-gpt-engine.jar" -w -q & | ||
pid=$! | ||
trap "kill $pid" EXIT | ||
|
||
sleep 2 | ||
|
||
ARGS="" | ||
# if $* doesn't contain -cr or -ca we add -cr to ARGS | ||
if [[ ! "$*" =~ -cr ]] && [[ ! "$*" =~ -ca ]]; then | ||
ARGS="-cr" | ||
fi | ||
|
||
chatgpt -tf <($JAVA -jar "$progdir/co-developer-gpt-engine.jar" --aitoolsdef) $ARGS "$@" |
14 changes: 14 additions & 0 deletions
14
project-bin/generate_chatgpt_script_toolsdefinition.prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Transform the retrieved OpenAI tools definition file into the following format: | ||
|
||
[ | ||
{ | ||
"function": { | ||
// here comes the function definition of the tool | ||
}, | ||
"commandline: [ | ||
"curl", "-X", "POST", "-d", "@-", "http://localhost:3002/executetool" | ||
], | ||
"stdin": "$toolcall" | ||
}, | ||
... // more tools | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Print an OpenAI tools definition file that describes the requests specified in the retrieved OpenAPI YAML. | ||
Set "strict" to false. Include the descriptions unmodified. | ||
Take care that the "required" attribute contains exactly the required attributes. | ||
If the request is a POST with requestBody, then the body content should be contained in a parameter "requestBody". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env bash | ||
# generates a json schema for using the engine within other tools | ||
scriptdir=$(dirname "$(realpath $0)") | ||
cd "$(dirname $0)/../" | ||
aigenpipeline -wvf -o src/main/resources/static/codeveloperengine-toolsdefinition.json \ | ||
-p $scriptdir/generate_openai_toolsdefinition.prompt src/test/resources/test-expected/codeveloperengine.yaml | ||
|
||
aigenpipeline -wvf -o src/main/resources/static/codeveloperengine-chatgptscript-toolsdefinition.json \ | ||
-p $scriptdir/generate_chatgpt_script_toolsdefinition.prompt src/main/resources/static/codeveloperengine-toolsdefinition.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/net/stoerr/chatgpt/codevengine/ExecuteOpenAIToolCallAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package net.stoerr.chatgpt.codevengine; | ||
|
||
import static net.stoerr.chatgpt.codevengine.TbUtils.logInfo; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletRequestWrapper; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Executes an OpenAI tool call coming in as JSON - for usage outside ChatGPT. | ||
*/ | ||
public class ExecuteOpenAIToolCallAction extends AbstractPluginAction { | ||
|
||
private final Map<String, AbstractPluginAction> handlers; | ||
|
||
public ExecuteOpenAIToolCallAction(Map<String, AbstractPluginAction> handlers) { | ||
this.handlers = handlers; | ||
} | ||
|
||
@Override | ||
public String getUrl() { | ||
return "/executetool"; | ||
} | ||
|
||
/** | ||
* This is not registered in the yaml description since it's not a normal action, but rather | ||
* distributes to actions. | ||
*/ | ||
@Override | ||
public String openApiDescription() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
BufferedReader reader = req.getReader(); | ||
String json = reader.lines().collect(Collectors.joining(System.lineSeparator())); | ||
logInfo("Received tool call:\n" + json); | ||
String name = getBodyParameter(resp, json, "name", true); | ||
String arguments = getBodyParameter(resp, json, "arguments", true); | ||
Map<String, Object> parsedArguments = gson.fromJson(arguments, Map.class); | ||
logInfo("Executing tool call: " + name + " " + parsedArguments); | ||
Object requestBody = parsedArguments.get("requestBody"); | ||
String body = requestBody != null ? gson.toJson(requestBody) : null; | ||
if (StringUtils.isNotBlank(body)) logInfo("Body: " + body); | ||
AbstractPluginAction handler = handlers.get("/" + name); | ||
if (null == handler) { | ||
sendError(resp, HttpServletResponse.SC_BAD_REQUEST, "No handler for tool call: " + name); | ||
return; | ||
} | ||
// call handler with a request that has parsedArguments as parameters and body as request body (JSON request) | ||
HttpServletRequest requestWrapper = new HttpServletRequestWrapper(req) { | ||
@Override | ||
public String getParameter(String name) { | ||
Object value = parsedArguments.get(name); | ||
if (value == null) return null; | ||
if (value instanceof String) return (String) value; | ||
if (value instanceof Double) { | ||
// check whether it's an integer | ||
double d = (Double) value; | ||
if (Math.abs(d - Math.round(d)) < 0.001) return "" + Math.round(d); | ||
} | ||
return String.valueOf(value); | ||
} | ||
|
||
@Override | ||
public BufferedReader getReader() throws IOException { | ||
return body != null ? new BufferedReader(new StringReader(body)) : null; | ||
} | ||
|
||
@Override | ||
public String getMethod() { | ||
return requestBody != null ? "POST" : "GET"; | ||
} | ||
}; | ||
try { | ||
handler.service(requestWrapper, resp); | ||
} catch (ExecutionAbortedException e) { | ||
// is already sufficiently handled. Just ignore. | ||
} catch (ServletException | IOException | RuntimeException e) { | ||
TbUtils.logError("Error executing tool call: " + name + "\n" + arguments); | ||
TbUtils.logStacktrace(e); | ||
throw e; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.