mirror of
https://github.com/slhaf/Partner.git
synced 2026-10-25 19:03:48 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a929b3e0e6 | ||
|
|
fe6895d10b |
@@ -3,6 +3,7 @@ package work.slhaf.partner.core.cognition.impression
|
|||||||
import com.alibaba.fastjson2.JSONObject
|
import com.alibaba.fastjson2.JSONObject
|
||||||
import work.slhaf.partner.framework.agent.state.State
|
import work.slhaf.partner.framework.agent.state.State
|
||||||
import work.slhaf.partner.framework.agent.state.StateSerializable
|
import work.slhaf.partner.framework.agent.state.StateSerializable
|
||||||
|
import work.slhaf.partner.framework.agent.state.StateValue
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
@@ -122,15 +123,83 @@ class Entity @JvmOverloads constructor(
|
|||||||
override fun statePath(): Path = Path.of("core", "impression", "entity-$uuid.json")
|
override fun statePath(): Path = Path.of("core", "impression", "entity-$uuid.json")
|
||||||
|
|
||||||
override fun load(state: JSONObject) {
|
override fun load(state: JSONObject) {
|
||||||
TODO("Not yet implemented")
|
state.getJSONObject("relations")?.let { loadedRelations ->
|
||||||
|
relationLock.withLock {
|
||||||
|
relations.clear()
|
||||||
|
loadedRelations.forEach { (target, relationValue) ->
|
||||||
|
val relationObject = relationValue as? JSONObject ?: return@forEach
|
||||||
|
val relationMap = mutableMapOf<String, Double>()
|
||||||
|
relationObject.forEach { (relation, strengthValue) ->
|
||||||
|
numberValue(strengthValue)?.let { relationMap[relation] = it }
|
||||||
|
}
|
||||||
|
if (relationMap.isNotEmpty()) {
|
||||||
|
relations[target] = relationMap
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state.getJSONObject("impressions")?.let { loadedImpressions ->
|
||||||
|
impressionLock.withLock {
|
||||||
|
impressions.clear()
|
||||||
|
impressions.putAll(loadIndexableDataMap(loadedImpressions))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state.getJSONObject("features")?.let { loadedFeatures ->
|
||||||
|
featureLock.withLock {
|
||||||
|
features.clear()
|
||||||
|
features.putAll(loadIndexableDataMap(loadedFeatures))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun convert(): State {
|
override fun convert(): State {
|
||||||
TODO("Not yet implemented")
|
val state = State()
|
||||||
|
state.append("uuid", StateValue.str(uuid))
|
||||||
|
state.append("subject", StateValue.str(subject))
|
||||||
|
|
||||||
|
val relationState = relationLock.withLock {
|
||||||
|
relations.mapValues { (_, relationMap) -> relationMap.toMap() }
|
||||||
|
}
|
||||||
|
state.append("relations", StateValue.obj(relationState))
|
||||||
|
|
||||||
|
val impressionState = impressionLock.withLock {
|
||||||
|
indexableDataState(impressions)
|
||||||
|
}
|
||||||
|
state.append("impressions", StateValue.obj(impressionState))
|
||||||
|
|
||||||
|
val featureState = featureLock.withLock {
|
||||||
|
indexableDataState(features)
|
||||||
|
}
|
||||||
|
state.append("features", StateValue.obj(featureState))
|
||||||
|
|
||||||
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun autoLoadOnRegister(): Boolean = false
|
override fun autoLoadOnRegister(): Boolean = false
|
||||||
|
|
||||||
|
private fun loadIndexableDataMap(state: JSONObject): Map<String, IndexableData> {
|
||||||
|
val loaded = mutableMapOf<String, IndexableData>()
|
||||||
|
state.forEach { (key, value) ->
|
||||||
|
val confidence = when (value) {
|
||||||
|
is JSONObject -> value.getDouble("confidence")?.toDouble() ?: 1.0
|
||||||
|
else -> numberValue(value) ?: return@forEach
|
||||||
|
}
|
||||||
|
loaded[key] = IndexableData(confidence)
|
||||||
|
}
|
||||||
|
return loaded
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun indexableDataState(source: Map<String, IndexableData>): Map<String, Map<String, Double>> =
|
||||||
|
source.mapValues { (_, data) -> mapOf("confidence" to data.confidence) }
|
||||||
|
|
||||||
|
private fun numberValue(value: Any?): Double? = when (value) {
|
||||||
|
is Number -> value.toDouble()
|
||||||
|
is String -> value.toDoubleOrNull()
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
data class IndexableData(
|
data class IndexableData(
|
||||||
var confidence: Double
|
var confidence: Double
|
||||||
) {
|
) {
|
||||||
@@ -164,4 +233,4 @@ class Entity @JvmOverloads constructor(
|
|||||||
val confidence: Double,
|
val confidence: Double,
|
||||||
val vector: DoubleArray?
|
val vector: DoubleArray?
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
@@ -22,6 +22,7 @@ public class ImpressionCore implements StateSerializable {
|
|||||||
* Keyed by entity uuid. Subject can be revised or merged later, so it should not be used as the stable key.
|
* Keyed by entity uuid. Subject can be revised or merged later, so it should not be used as the stable key.
|
||||||
*/
|
*/
|
||||||
private final ConcurrentHashMap<String, Entity> knownEntitiesByUuid = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<String, Entity> knownEntitiesByUuid = new ConcurrentHashMap<>();
|
||||||
|
private final ImpressionVectorIndex vectorIndex = new ImpressionVectorIndex();
|
||||||
|
|
||||||
@CapabilityMethod
|
@CapabilityMethod
|
||||||
public void updateRelation() {
|
public void updateRelation() {
|
||||||
@@ -66,6 +67,7 @@ public class ImpressionCore implements StateSerializable {
|
|||||||
|
|
||||||
Entity entity = new Entity(uuid, subject);
|
Entity entity = new Entity(uuid, subject);
|
||||||
entity.load();
|
entity.load();
|
||||||
|
vectorIndex.sync(entity);
|
||||||
knownEntitiesByUuid.put(uuid, entity);
|
knownEntitiesByUuid.put(uuid, entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package work.slhaf.partner.core.cognition.impression;
|
||||||
|
|
||||||
|
public class ImpressionVectorIndex {
|
||||||
|
|
||||||
|
public void sync(Entity entity){
|
||||||
|
// TODO sync entity impressions/features with vector index.
|
||||||
|
}
|
||||||
|
|
||||||
|
public void upsert(String content, Entity.IndexableData indexableData){
|
||||||
|
// TODO update vector for content when embedding/vector client boundary is finalized.
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user