From f8cda802cd19945598870a0a7f543f36816a8d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=8B=87?= Date: Fri, 19 Apr 2024 14:51:42 +0800 Subject: [PATCH] Official and Stable ORM Framework for MongoDB close(#829) (#830) * update * The vertices of the graph support the map data structure clsoe(#810) * ai support Use local LLM(claude3) close #827 * Official and Stable ORM Framework for MongoDB close(#829) --- .../aws/src/main/java/run/mone/AwsClient.java | 51 +++++++++++++++++++ .../aws/src/main/java/run/mone/ModelEnum.java | 4 +- .../docean-plugin-mongodb/pom.xml | 11 ++++ .../youpin/docean/plugin/mongodb/MongoDb.java | 37 +++++++------- .../docean/plugin/mongodb/MongodbPlugin.java | 21 +++++++- jcommon/docean/pom.xml | 1 - .../java/com/xiaomi/youpin/docean/Mvc.java | 45 ++++++++++++++-- .../youpin/docean/mvc/HttpRequestMethod.java | 3 ++ .../youpin/docean/common/MethodInvoker.java | 4 ++ .../com/xiaomi/data/push/mongodb/MongoDb.java | 10 ++++ 10 files changed, 162 insertions(+), 25 deletions(-) create mode 100644 jcommon/ai/aws/src/main/java/run/mone/AwsClient.java diff --git a/jcommon/ai/aws/src/main/java/run/mone/AwsClient.java b/jcommon/ai/aws/src/main/java/run/mone/AwsClient.java new file mode 100644 index 000000000..9b68db744 --- /dev/null +++ b/jcommon/ai/aws/src/main/java/run/mone/AwsClient.java @@ -0,0 +1,51 @@ +package run.mone; + +import com.google.gson.Gson; +import org.json.JSONObject; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; +import software.amazon.awssdk.identity.spi.IdentityProvider; +import software.amazon.awssdk.identity.spi.ResolveIdentityRequest; +import software.amazon.awssdk.identity.spi.internal.DefaultAwsCredentialsIdentity; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; +import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelRequest; +import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelResponse; + +import java.util.concurrent.CompletableFuture; + +/** + * @author goodjava@qq.com + * @date 2024/4/12 13:35 + */ +public class AwsClient { + + private static final Gson gson = new Gson(); + + + public static ResponsePayload call(JSONObject payload, Region region, String modelId, Key key) { + BedrockRuntimeClient client = BedrockRuntimeClient.builder() + .credentialsProvider(new IdentityProvider<>() { + @Override + public Class identityType() { + return AwsCredentialsIdentity.class; + } + + public CompletableFuture resolveIdentity(ResolveIdentityRequest request) { + return CompletableFuture.completedFuture(DefaultAwsCredentialsIdentity.builder().accessKeyId(key.getKeyId()).secretAccessKey(key.getKey()).build()); + } + }) + .region(region) + .build(); + + InvokeModelRequest request = InvokeModelRequest.builder() + .contentType("application/json") + .body(SdkBytes.fromUtf8String(payload.toString())) + .modelId(modelId) + .build(); + + InvokeModelResponse resp = client.invokeModel(request); + String str = new String(resp.body().asByteArray()); + return gson.fromJson(str, ResponsePayload.class); + } +} \ No newline at end of file diff --git a/jcommon/ai/aws/src/main/java/run/mone/ModelEnum.java b/jcommon/ai/aws/src/main/java/run/mone/ModelEnum.java index 056788248..4ef479798 100644 --- a/jcommon/ai/aws/src/main/java/run/mone/ModelEnum.java +++ b/jcommon/ai/aws/src/main/java/run/mone/ModelEnum.java @@ -8,7 +8,9 @@ public enum ModelEnum { Sonnet("anthropic.claude-3-sonnet-20240229-v1:0"), - Haiku("anthropic.claude-3-haiku-20240307-v1:0"); + Haiku("anthropic.claude-3-haiku-20240307-v1:0"), + Opus("anthropic.claude-3-opus-20240229-v1:0"); + public String modelName; diff --git a/jcommon/docean-plugin/docean-plugin-mongodb/pom.xml b/jcommon/docean-plugin/docean-plugin-mongodb/pom.xml index a5a6ce68a..95876db84 100644 --- a/jcommon/docean-plugin/docean-plugin-mongodb/pom.xml +++ b/jcommon/docean-plugin/docean-plugin-mongodb/pom.xml @@ -10,19 +10,30 @@ docean-plugin-mongodb 1.5.0-jdk21-SNAPSHOT + run.mone docean-plugin-config + org.mongodb mongo-java-driver 3.8.0 + run.mone catPlugin 1.6.0-jdk21-SNAPSHOT + + + dev.morphia.morphia + morphia-core + 2.4.13 + + + diff --git a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/com/xiaomi/youpin/docean/plugin/mongodb/MongoDb.java b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/com/xiaomi/youpin/docean/plugin/mongodb/MongoDb.java index 858616b92..9efde4904 100644 --- a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/com/xiaomi/youpin/docean/plugin/mongodb/MongoDb.java +++ b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/com/xiaomi/youpin/docean/plugin/mongodb/MongoDb.java @@ -25,6 +25,7 @@ import com.mongodb.client.MongoDatabase; import com.xiaomi.youpin.cat.CatPlugin; import org.bson.Document; +import org.bson.conversions.Bson; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -73,6 +74,10 @@ public MongoCollection getCollection(String collectionName) { return db.getCollection(collectionName); } + public MongoCollection getCollection(String collectionName, Class clazz) { + return db.getCollection(collectionName, clazz); + } + public void insert(String collectionName, Document doc) { CatPlugin cat = new CatPlugin("insert", catEnabled, CAT_TYPE); boolean success = true; @@ -111,29 +116,25 @@ public void insertMany(String collectionName, List docList) { } public Document findFirst(String collectionName) { - CatPlugin cat = new CatPlugin("findFirst", catEnabled, CAT_TYPE); - boolean success = true; - cat.before(null); - try { - MongoCollection collection = this.getCollection(collectionName); - return collection.find().first(); - } catch (MongoException e) { - success = false; - logger.error(e.getMessage(), e); - } catch (Exception e) { - success = false; - logger.error(e.getMessage(), e); - } finally { - cat.after(success); - } - return null; + MongoCollection collection = this.getCollection(collectionName); + return collection.find().first(); + } + + public Document findFirst(String collectionName, Bson filter) { + MongoCollection collection = this.getCollection(collectionName); + return collection.find(filter).first(); + } + + public T findFirst(String collectionName, Bson filter, Class clazz) { + MongoCollection collection = this.getCollection(collectionName, clazz); + return collection.find(filter).first(); } - public List findAll(String collectionName, Document doc) { + public List findAll(String collectionName, Bson filter) { try { MongoCollection collection = this.getCollection(collectionName); List res = new ArrayList<>(); - for (Document cur : collection.find(doc)) { + for (Document cur : collection.find(filter)) { res.add(cur); } return res; diff --git a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/com/xiaomi/youpin/docean/plugin/mongodb/MongodbPlugin.java b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/com/xiaomi/youpin/docean/plugin/mongodb/MongodbPlugin.java index 41cf013ea..23983b8f8 100644 --- a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/com/xiaomi/youpin/docean/plugin/mongodb/MongodbPlugin.java +++ b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/com/xiaomi/youpin/docean/plugin/mongodb/MongodbPlugin.java @@ -16,10 +16,16 @@ package com.xiaomi.youpin.docean.plugin.mongodb; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import com.xiaomi.youpin.docean.Ioc; import com.xiaomi.youpin.docean.anno.DOceanPlugin; import com.xiaomi.youpin.docean.plugin.IPlugin; import com.xiaomi.youpin.docean.plugin.config.Config; +import dev.morphia.Datastore; +import dev.morphia.Morphia; +import dev.morphia.mapping.Mapper; +import dev.morphia.mapping.MapperOptions; import lombok.extern.slf4j.Slf4j; import java.util.Set; @@ -42,10 +48,23 @@ public void init(Set> classSet, Ioc ioc) { mongoDb.setCatEnabled(config.get("mongodb.cat.enabled", "false").equals("true")); mongoDb.init(); ioc.putBean(mongoDb); + + + MongoClient mongoClient = MongoClients.create(mongoDb.getMongoDbClient()); + Datastore datastore = Morphia.createDatastore(mongoClient, mongoDb.getMongoDatabase()); + + + String packagePath = config.get("mongodb.package", "run.mone.bo"); + datastore.getMapper().mapPackage(packagePath); + datastore.ensureIndexes(); + + + ioc.putBean(Datastore.class.getName(), datastore); } @Override public String version() { - return "0.0.1:2020-07-04:zheng.xucn@outlook.com"; + return "0.0.1:2020-07-04:goodjava@qq.com"; } + } diff --git a/jcommon/docean/pom.xml b/jcommon/docean/pom.xml index a69a6d573..0574d9b39 100644 --- a/jcommon/docean/pom.xml +++ b/jcommon/docean/pom.xml @@ -17,7 +17,6 @@ run.mone easy 1.6.0-jdk21-SNAPSHOT - cglib diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java index 68f697e41..6df887eaf 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java @@ -16,6 +16,7 @@ package com.xiaomi.youpin.docean; +import com.google.common.collect.Maps; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.xiaomi.youpin.docean.anno.RequestMapping; @@ -41,10 +42,12 @@ import lombok.Data; import lombok.extern.slf4j.Slf4j; -import java.util.Arrays; -import java.util.Locale; -import java.util.Optional; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.*; import java.util.concurrent.*; +import java.util.stream.Collectors; /** * @author goodjava@qq.com @@ -138,11 +141,32 @@ private void registerControllerMethods(Bean bean) { hrm.setObj(bean.getObj()); hrm.setMethod(m); hrm.setHttpMethod(rm.method()); + hrm.setGenericSuperclassTypeArguments(getGenericSuperclassTypeArguments(bean.getClazz())); ioc.publishEvent(new Event(EventType.initController, path)); requestMethodMap.put(path, hrm); })); } + public static Map getGenericSuperclassTypeArguments(Class clazz) { + List list = Arrays.stream(clazz.getSuperclass().getTypeParameters()).map(it -> it.getName()).collect(Collectors.toList()); + if (list.size() == 0) { + return Maps.newHashMap(); + } + Map map = new HashMap<>(); + Type genericSuperclass = clazz.getGenericSuperclass(); + if (genericSuperclass instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass; + Type[] typeArguments = parameterizedType.getActualTypeArguments(); + for (int i = 0; i < typeArguments.length; i++) { + Type argument = typeArguments[i]; + if (argument instanceof Class) { + map.put(list.get(i), (Class) argument); + } + } + } + return map; + } + private static final class LazyHolder { private static final Mvc ins = new Mvc(Ioc.ins()); @@ -187,6 +211,16 @@ public void callService(MvcContext context, MvcRequest request, MvcResponse resp response.writeAndFlush(context, new Gson().toJson(mr)); } + public List mapMethodParametersToClasses(Method method, Map map) { + return Arrays.stream(method.getParameters()).map(it -> { + String name = it.getParameterizedType().getTypeName(); + if ((it.getType() instanceof Object) && map.containsKey(name)) { + return map.get(name); + } + return it.getType(); + }).collect(Collectors.toList()); + } + public void callMethod(MvcContext context, MvcRequest request, MvcResponse response, MvcResult result, HttpRequestMethod method) { Safe.run(() -> { Object[] params = new Object[]{null}; @@ -199,7 +233,10 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo params[0] = context; } else { try { - params = methodInvoker.getMethodParams(method.getMethod(), args); + //可能方法中有泛型,这里给fix调,用实际的Class + List list = mapMethodParametersToClasses(method.getMethod(), method.getGenericSuperclassTypeArguments()); + Class[] types = list.toArray(new Class[]{}); + params = methodInvoker.getMethodParams(args, types); } catch (Exception e) { log.error("getMethodParams error,path:{},params:{},method:{}", context.getPath(), GsonUtils.gson.toJson(context.getParams()), request.getMethod().toLowerCase(Locale.ROOT), e); diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java index 3a540b3fd..70756ec32 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java @@ -19,6 +19,7 @@ import lombok.Data; import java.lang.reflect.Method; +import java.util.Map; /** * @author goodjava@qq.com @@ -36,5 +37,7 @@ public class HttpRequestMethod { private long timeout; + private Map genericSuperclassTypeArguments; + } diff --git a/jcommon/easy/src/main/java/com/xiaomi/youpin/docean/common/MethodInvoker.java b/jcommon/easy/src/main/java/com/xiaomi/youpin/docean/common/MethodInvoker.java index 804a4d6c6..31b6d5ec4 100644 --- a/jcommon/easy/src/main/java/com/xiaomi/youpin/docean/common/MethodInvoker.java +++ b/jcommon/easy/src/main/java/com/xiaomi/youpin/docean/common/MethodInvoker.java @@ -166,6 +166,10 @@ public Object[] getMethodParams(Object obj, String methodName, JsonElement param public Object[] getMethodParams(Method method, JsonElement params) { Class[] types = method.getParameterTypes(); + return getMethodParams(params, types); + } + + public Object[] getMethodParams(JsonElement params, Class[] types) { if (types.length == 0) { return new Object[]{}; } diff --git a/jcommon/mongodb/src/main/java/com/xiaomi/data/push/mongodb/MongoDb.java b/jcommon/mongodb/src/main/java/com/xiaomi/data/push/mongodb/MongoDb.java index 60bf99257..ff4cfe682 100644 --- a/jcommon/mongodb/src/main/java/com/xiaomi/data/push/mongodb/MongoDb.java +++ b/jcommon/mongodb/src/main/java/com/xiaomi/data/push/mongodb/MongoDb.java @@ -80,8 +80,13 @@ public MongoCollection getCollection(String collectionName) { return db.getCollection(collectionName); } + public MongoCollection getCollection(String collectionName, Class clazz) { + return db.getCollection(collectionName, clazz); + } + /** * Inserts a document under the specified collection + * * @param collectionName * @param doc */ @@ -106,6 +111,7 @@ public void insert(String collectionName, Document doc) { /** * Inserts a list of documents under the specified collection + * * @param collectionName * @param docList */ @@ -129,6 +135,7 @@ public void insertMany(String collectionName, List docList) { /** * Finds the first document under the specified collection + * * @param collectionName * @return */ @@ -153,6 +160,7 @@ public Document findFirst(String collectionName) { /** * Finds all documents under the specified collection according to the criteria + * * @param collectionName * @param doc * @return @@ -184,6 +192,7 @@ public List findAll(String collectionName, Document doc) { /** * Deletes a document under a specified collection under specified conditions + * * @param collectionName * @param doc */ @@ -207,6 +216,7 @@ public void delete(String collectionName, Document doc) { /** * Returns the number of documents under the specified collection + * * @param collectionName * @return */