Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Aug 8, 2024
1 parent a0b3fca commit 49898b0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
24 changes: 21 additions & 3 deletions core/src/main/java/lucee/runtime/ai/AIUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lucee.runtime.ai;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lucee.runtime.exp.ApplicationException;
Expand All @@ -14,9 +15,16 @@

public class AIUtil {

public static Exception toException(String msg, String type, String code) {
// CFMLEngine eng = CFMLEngineFactory.getInstance();
PageException ae = new ApplicationException(msg, "type:" + type + ";code:" + code);
public static Exception toException(AIEngine engine, String msg, String type, String code) {
String appendix = "";
if ("model_not_found".equals(code)) {
try {
appendix = " Available model names are [" + AIUtil.getModelNamesAsStringList(engine) + "]";
}
catch (PageException e) {
}
}
PageException ae = new ApplicationException(msg + appendix, "type:" + type + ";code:" + code);
ae.setErrorCode(code);
return ae;
}
Expand All @@ -27,9 +35,19 @@ public static List<String> getModelNames(AIEngine aie) throws PageException {
for (AIModel m: models) {
names.add(m.getName());
}
Collections.sort(names);
return names;
}

public static String getModelNamesAsStringList(AIEngine aie) throws PageException {
StringBuilder sb = new StringBuilder();
for (String name: getModelNames(aie)) {
if (sb.length() > 0) sb.append(", ");
sb.append(name);
}
return sb.toString();
}

public static Struct getMetaData(AIEngine aie) throws PageException {

Struct meta = new StructImpl();
Expand Down
24 changes: 16 additions & 8 deletions core/src/main/java/lucee/runtime/ai/openai/OpenAIEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@

public class OpenAIEngine extends AIEngineSupport {
private static final long DEFAULT_TIMEOUT = 3000L;
private static final String DEFAULT_CHARSET = null;
private static final String DEFAULT_CHARSET = "UTF-8";
private static final String DEFAULT_MIMETYPE = null;
// private static final String DEFAULT_MODEL = "gpt-4";
// private static final String DEFAULT_MODEL = "gpt-3.5-turbo";
private static final String DEFAULT_MODEL = "gpt-4o-mini"; // Change to your preferred model
private static final URL DEFAULT_URL_OPENAI;
private static final URL DEFAULT_URL_OLLAMA;

Expand Down Expand Up @@ -119,15 +116,26 @@ else throw new ApplicationException(
timeout = Caster.toLongValue(properties.get(KeyConstants._timeout, null), DEFAULT_TIMEOUT);
// charset
charset = Caster.toString(properties.get(KeyConstants._charset, null), DEFAULT_CHARSET);
if (Util.isEmpty(charset, true)) charset = null;
if (Util.isEmpty(charset, true)) charset = DEFAULT_CHARSET;
// mimetype
mimetype = Caster.toString(properties.get(KeyConstants._mimetype, null), DEFAULT_MIMETYPE);
if (Util.isEmpty(mimetype, true)) mimetype = null;
// model
model = Caster.toString(properties.get(KeyConstants._model, DEFAULT_MODEL), DEFAULT_MODEL);
model = Caster.toString(properties.get(KeyConstants._model, null), null);
if (Util.isEmpty(model, true)) {
// nice to have
String appendix = "";
try {
appendix = " Available models for this engine are [" + AIUtil.getModelNamesAsStringList(this) + "]";
}
catch (PageException pe) {
}

throw new ApplicationException("the property [model] is required for a OpenAI Engine!." + appendix);
}

// message
systemMessage = Caster.toString(properties.get(KeyConstants._message, null), null);
getModels();
return this;
}

Expand Down Expand Up @@ -166,7 +174,7 @@ public List<AIModel> getModels() throws PageException {
Struct raw = Caster.toStruct(new JSONExpressionInterpreter().interpret(null, rsp.getContentAsString(cs)));
Struct err = Caster.toStruct(raw.get(KeyConstants._error, null), null);
if (err != null) {
throw AIUtil.toException(Caster.toString(err.get(KeyConstants._message)), Caster.toString(err.get(KeyConstants._type, null), null),
throw AIUtil.toException(this, Caster.toString(err.get(KeyConstants._message)), Caster.toString(err.get(KeyConstants._type, null), null),
Caster.toString(err.get(KeyConstants._code, null), null));
}

Expand Down
23 changes: 12 additions & 11 deletions core/src/main/java/lucee/runtime/ai/openai/OpenAISession.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

public class OpenAISession extends AISessionSupport {

private OpenAIEngine chatGPTEngine;
private OpenAIEngine openaiEngine;
private String initalMessage;

public OpenAISession(OpenAIEngine engine, String initalMessage, long timeout) {
super(engine, timeout);
this.chatGPTEngine = engine;
this.openaiEngine = engine;
this.initalMessage = initalMessage;
}

Expand Down Expand Up @@ -77,26 +77,27 @@ public Response inquiry(String message) throws PageException {
arr.append(msg);

Struct sct = new StructImpl();
sct.set(KeyConstants._model, chatGPTEngine.model);
sct.set(KeyConstants._model, openaiEngine.model);
sct.set(KeyConstants._messages, arr);

JSONConverter json = new JSONConverter(true, CharsetUtil.UTF8, JSONDateFormat.PATTERN_CF, false);
String str = json.serialize(null, sct, SerializationSettings.SERIALIZE_AS_COLUMN, null);

URL url = new URL(chatGPTEngine.getBaseURL(), "chat/completions");
HTTPResponse rsp = HTTPEngine4Impl.post(url, null, null, getTimeout(), false, chatGPTEngine.mimetype, chatGPTEngine.charset, AIEngineSupport.DEFAULT_USERAGENT,
chatGPTEngine.proxy, new Header[] { new HeaderImpl("Authorization", "Bearer " + chatGPTEngine.secretKey), new HeaderImpl("Content-Type", "application/json") },
chatGPTEngine.formfields, str);
URL url = new URL(openaiEngine.getBaseURL(), "chat/completions");
HTTPResponse rsp = HTTPEngine4Impl.post(url, null, null, getTimeout(), false, openaiEngine.mimetype, openaiEngine.charset, AIEngineSupport.DEFAULT_USERAGENT,
openaiEngine.proxy, new Header[] { new HeaderImpl("Authorization", "Bearer " + openaiEngine.secretKey), new HeaderImpl("Content-Type", "application/json") },
openaiEngine.formfields, str);

ContentType ct = rsp.getContentType();
if ("application/json".equals(ct.getMimeType())) {
String cs = ct.getCharset();
if (Util.isEmpty(cs, true)) cs = chatGPTEngine.charset;

if (Util.isEmpty(cs, true)) cs = openaiEngine.charset;
// stream=rsp.getContentAsStream();
Struct raw = Caster.toStruct(new JSONExpressionInterpreter().interpret(null, rsp.getContentAsString(cs)));

Struct err = Caster.toStruct(raw.get(KeyConstants._error, null), null);
if (err != null) {
throw AIUtil.toException(Caster.toString(err.get(KeyConstants._message)), Caster.toString(err.get(KeyConstants._type, null), null),
throw AIUtil.toException(this.getEngine(), Caster.toString(err.get(KeyConstants._message)), Caster.toString(err.get(KeyConstants._type, null), null),
Caster.toString(err.get(KeyConstants._code, null), null));
}

Expand All @@ -105,7 +106,7 @@ public Response inquiry(String message) throws PageException {
return response;
}
else {
throw new ApplicationException("Chat GPT did answer with the mime type [" + ct.getMimeType() + "] that is not supported, only [application/json] is supported");
throw new ApplicationException("The AI did answer with the mime type [" + ct.getMimeType() + "] that is not supported, only [application/json] is supported");
}
}
catch (Exception e) {
Expand Down

0 comments on commit 49898b0

Please sign in to comment.