Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

fix/implicit_memory

thomashebrard2604e7a8474a
diff --git a/pipelex/client/implicit_memory.py b/pipelex/client/implicit_memory.py
new file mode 100644
index 000000000..3e592db5b
--- /dev/null
+++ b/pipelex/client/implicit_memory.py
@@ -0,0 +1,187 @@
+from unittest import suite
+from pipelex.core.stuffs.stuff import Stuff
+from pipelex.core.concepts.concept_factory import ConceptFactory
+from pipelex.core.concepts.concept_native import NativeConceptCode
+from pipelex.core.stuffs.text_content import TextContent
+from pipelex.core.stuffs.list_content import ListContent
+from pipelex.core.stuffs.structured_content import StructuredContent
+from pipelex.core.stuffs.json_content import JSONContent
+
+
+class MySubClass(StructuredContent):
+ arg4: str
+
+
+class MyConcept(StructuredContent):
+ arg1: str
+ arg2: int
+ arg3: MySubClass
+
+
+####################################################################################################
+## CASE 1: I have directly the content.
+# Possible solutions:
+# 1.1: stuff_data is a string.
+# 1.2: stuff_data is a list of strings.
+# 1.3: stuff_data is a StuffContent object.
+# 1.4: stuff_data is a list of StuffContent objects.
+####################################################################################################
+
+# 1.1: Content is a string.
+stuff_data = "my text"
+
+# Stuff should be
+stuff = Stuff(
+ stuff_code="stuff_code",
+ stuff_name="stuff_name",
+ concept=ConceptFactory.make_native_concept(native_concept_code=NativeConceptCode.TEXT),
+ content=TextContent(text="my_text"),
+)
+
+# 1.2: Content is a list of strings
+stuff_data = {"concept": "concept_string", "content": "my text"}
+
+# Concept string needs to be compatible strictly with the concept native.Text. Otherwise it should raise an error.
+
+stuff = Stuff(
+ stuff_code="stuff_code",
+ stuff_name="stuff_name",
+ concept=ConceptFactory.make_native_concept(native_concept_code=NativeConceptCode.TEXT),
+ content=TextContent(text="my_text"),
+)
+
+# 1.3: Content is a StuffContent object.
+stuff_data = MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4"))
+
+# It should look in all the library for the concept with the same name.
+# If no search domains are provided and we find 2 or more concepts with the same name, it should raise an error.
+
+stuff = Stuff(
+ stuff_code="stuff_code",
+ stuff_name="stuff_name",
+ concept=ConceptFactory.make(concept_code="MyConcept", domain="domain", description="description", structure_class_name=MyConcept.__name__),
+ content=MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")),
+)
+
+# 1.4:
+stuff_data = [MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")), MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4"))]
+
+# It should look in all the library for the concept with the same name.
+# If no search domains are provided and we find 2 or more concepts with the same name, it should raise an error.
+# EVERY item of the list should be of the same type. Otherwise it should raise an error.
+
+stuff = Stuff(
+ stuff_code="stuff_code",
+ stuff_name="stuff_name",
+ concept=ConceptFactory.make(concept_code="MyConcept", domain="domain", description="description", structure_class_name=MyConcept.__name__),
+ content=ListContent(
+ items=[MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")), MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4"))]
+ ),
+)
+
+####################################################################################################
+## CASE 2: Here, we HAVE the key "concept_string" AND the key "content".
+# The value of the "content" key can Either be a:
+# 2.1: a string.
+# 2.2: a list of strings.
+# 2.3: a StuffContent object.
+# 2.4: a list of StuffContent objects.
+# 2.5: a dict (corresponding to the concept right above).
+# 2.6: a list of dict (corresponding to the concept right above).
+####################################################################################################
+
+# 2.1: Content is a string
+
+stuff_data = {"concept": "concept_string", "content": "my text"}
+
+# Here, the concept can either be Text, native.Text or any other concept. But if its any other concept, it should be stricly compatible with the concept native.Text.
+
+stuff = Stuff(
+ stuff_code="stuff_code",
+ stuff_name="stuff_name",
+ concept=ConceptFactory.make_native_concept(native_concept_code=NativeConceptCode.TEXT), # Or else
+ content=TextContent(text="my text"),
+)
+
+# 2.2: Content is a list of strings
+stuff_data = {"concept": "concept_string", "content": ["text1", "text2", "text3"]}
+
+# Here, the concept can either be Text, native.Text or any other concept. But if its any other concept, it should be stricly compatible with the concept native.Text.
+
+stuff = Stuff(
+ stuff_code="stuff_code",
+ stuff_name="stuff_name",
+ concept=ConceptFactory.make_native_concept(native_concept_code=NativeConceptCode.TEXT), # Or else
+ content=ListContent(items=[TextContent(text="text1"), TextContent(text="text2"), TextContent(text="text3")]),
+)
+
+# 2.3: Content is a StuffContent object
+stuff_data = {"concept": "concept_string", "content": MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4"))}
+
+# For the concept mentioned: If no domain is specified, it should look in all the library for the concept with the same name.
+# If no search domains are provided and we find 2 or more concepts with the same name, it should raise an error and ask the user to specify the domain.
+# If the domain is specified or found, it should check that the structure corresponds with the given content.
+# Otherwise it should raise an error.
+
+stuff = Stuff(
+ stuff_code="stuff_code",
+ stuff_name="stuff_name",
+ concept=ConceptFactory.make(concept_code="MyConcept", domain="domain", description="description", structure_class_name=MyConcept.__name__),
+ content=MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")),
+)
+
+# 2.4: Content is a list of StuffContent objects
+stuff_data = {
+ "concept": "concept_string",
+ "content": [MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")), MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4"))],
+}
+
+stuff = Stuff(
+ stuff_code="stuff_code",
+ stuff_name="stuff_name",
+ concept=ConceptFactory.make(concept_code="MyConcept", domain="domain", description="description", structure_class_name=MyConcept.__name__),
+ content=ListContent(
+ items=[MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")), MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4"))]
+ ),
+)
+
+# 2.5: Content is a dict
+stuff_data = {"concept": "concept_string", "content": {"arg1": "something", "arg2": 1, "arg3": {"arg4": "something else else"}}}
+
+# For the concept mentioned: If no domain is specified, it should look in all the library for the concept with the same name.
+# If no search domains are provided and we find 2 or more concepts with the same name, it should raise an error and ask the user to specify the domain.
+# If the domain is specified or found, it should check that the structure corresponds with the given content by validating the model.
+# Otherwise it should raise an error.
+
+stuff = Stuff(
+ stuff_code="stuff_code",
+ stuff_name="stuff_name",
+ concept=ConceptFactory.make(concept_code="MyConcept", domain="domain", description="description", structure_class_name=MyConcept.__name__),
+ content=MyConcept(arg1="something", arg2=1, arg3=MySubClass(arg4="something else else")),
+)
+
+# 2.6: Content is a list of dict
+stuff_data = {
+ "concept": "concept_string",
+ "content": [
+ {"arg1": "something", "arg2": 1, "arg3": {"arg4": "something else else"}},
+ {"arg1": "something else", "arg2": 2, "arg3": {"arg4": "something else else else"}},
+ ],
+}
+
+# For the concept mentioned: If no domain is specified, it should look in all the library for the concept with the same name.
+# If no search domains are provided and we find 2 or more concepts with the same name, it should raise an error and ask the user to specify the domain.
+# If the domain is specified or found, it should check that the structure corresponds with the given content by validating the model.
+# Otherwise it should raise an error.
+
+stuff = Stuff(
+ stuff_code="stuff_code",
+ stuff_name="stuff_name",
+ concept=ConceptFactory.make(concept_code="MyConcept", domain="domain", description="description", structure_class_name=MyConcept.__name__),
+ content=ListContent(
+ items=[
+ MyConcept(arg1="something", arg2=1, arg3=MySubClass(arg4="something else else")),
+ MyConcept(arg1="something else", arg2=2, arg3=MySubClass(arg4="something else else else")),
+ ]
+ ),
+)
diff --git a/pipelex/core/concepts/structure_generator.py b/pipelex/core/concepts/structure_generator.py
index 39277b842..14493d1a6 100644
--- a/pipelex/core/concepts/structure_generator.py
+++ b/pipelex/core/concepts/structure_generator.py
@@ -188,23 +188,20 @@ class StructureGenerator:
# Recursively handle item types if they're FieldType enums
try:
item_type_enum = ConceptStructureBlueprintFieldType(item_type)
- # Create a temporary blueprint for the item type
- temp_blueprint = ConceptStructureBlueprint(description="temp", type=item_type_enum)
- item_type = self._get_python_type_from_blueprint(temp_blueprint)
+ if item_type_enum == ConceptStructureBlueprintFieldType.DICT:
+ temp_blueprint = ConceptStructureBlueprint(description="temp", type=item_type_enum, key_type="str", value_type="Any")
+ item_type = self._get_python_type_from_blueprint(temp_blueprint)
+ else:
+ # Create a temporary blueprint for the item type
+ temp_blueprint = ConceptStructureBlueprint(description="temp", type=item_type_enum)
+ item_type = self._get_python_type_from_blueprint(temp_blueprint)
except ValueError:
# Keep as string if not a known FieldType
pass
return f"List[{item_type}]"
case ConceptStructureBlueprintFieldType.DICT:
- key_type = field_blueprint.key_type or "str"
+ key_type = "str"
value_type = field_blueprint.value_type or "Any"
- # Recursively handle key and value types
- try:
- key_type_enum = ConceptStructureBlueprintFieldType(key_type)
- temp_blueprint = ConceptStructureBlueprint(description="temp", type=key_type_enum)
- key_type = self._get_python_type_from_blueprint(temp_blueprint)
- except ValueError:
- pass
try:
value_type_enum = ConceptStructureBlueprintFieldType(value_type)
temp_blueprint = ConceptStructureBlueprint(description="temp", type=value_type_enum)
diff --git a/pipelex/core/memory/working_memory_factory.py b/pipelex/core/memory/working_memory_factory.py
index bafe3ceb4..68ada701e 100644
--- a/pipelex/core/memory/working_memory_factory.py
+++ b/pipelex/core/memory/working_memory_factory.py
@@ -5,7 +5,7 @@ from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel
from pipelex import log
-from pipelex.client.protocol import CompactMemory, ImplicitMemory
+from pipelex.client.protocol import ImplicitMemory
from pipelex.core.concepts.concept import ConceptBlueprint, SpecialDomain
from pipelex.core.concepts.concept_native import NativeConceptCode
from pipelex.core.memory.working_memory import MAIN_STUFF_NAME, StuffDict, WorkingMemory
@@ -122,18 +122,6 @@ class WorkingMemoryFactory(BaseModel):
def make_empty(cls) -> WorkingMemory:
return WorkingMemory(root={})
- @classmethod
- def make_from_compact_memory(
- cls,
- compact_memory: CompactMemory,
- search_domains: list[str] | None = None,
- ) -> WorkingMemory:
- implicit_memory = cast("ImplicitMemory", compact_memory)
- return cls.make_from_implicit_memory(
- implicit_memory=implicit_memory,
- search_domains=search_domains,
- )
-
@classmethod
def make_from_implicit_memory(
cls,
@@ -152,7 +140,7 @@ class WorkingMemoryFactory(BaseModel):
"""
working_memory = cls.make_empty()
- for stuff_key, stuff_content_or_data in implicit_memory.items():
+ for stuff_key, compact_stuff_content in implicit_memory.items():
stuff = StuffFactory.make_stuff_from_stuff_content_using_search_domains(
name=stuff_key,
stuff_content_or_data=stuff_content_or_data,
diff --git a/pipelex/core/stuffs/json_content.py b/pipelex/core/stuffs/json_content.py
new file mode 100644
index 000000000..e2cb175f7
--- /dev/null
+++ b/pipelex/core/stuffs/json_content.py
@@ -0,0 +1,20 @@
+from typing import Any
+
+from json2html import json2html
+from kajson import kajson
+from typing_extensions import override
+
+from pipelex.core.stuffs.stuff_content import StuffContent
+from pipelex.tools.misc.markdown_utils import convert_to_markdown
+
+
+class JSONContent(StuffContent):
+ json_obj: dict[str, Any]
+
+ @override
+ def rendered_html(self) -> str:
+ return str(json2html.convert(json=kajson.dumps(self.json_obj, indent=4), clubbing=True)) # pyright: ignore[reportUnknownArgumentType]
+
+ @override
+ def rendered_markdown(self, level: int = 1, is_pretty: bool = False) -> str:
+ return convert_to_markdown(self.json_obj)