diff --git a/README.md b/README.md index 08ab94fa..7775bfb4 100644 --- a/README.md +++ b/README.md @@ -32,78 +32,18 @@ pip install zep-cloud > [!NOTE] > Zep Cloud [overview](https://help.getzep.com/concepts) and [cloud sdk guide](https://help.getzep.com/sdks). -### Community Installation -```bash -pip install zep-python -``` -> [!NOTE] -> Zep Community Edition [quick start](https://help.getzep.com/ce/quickstart) and [sdk guide](https://help.getzep.com/ce/sdks). - -### Zep v0.x Compatible SDK -You can install Zep v0.x compatible sdk by running: -```bash -pip install "zep-python>=1.5.0,<2.0.0" -``` -> [!NOTE] -> Zep v0.x [quick start](https://help.getzep.com/ce/legacy/deployment/quickstart) and [sdk guide](https://help.getzep.com/ce/legacy/sdk). - ### How Zep works Zep persists and recalls chat histories, and automatically generates summaries and other artifacts from these chat histories. It also embeds messages and summaries, enabling you to search Zep for relevant context from past conversations. Zep does all of this asynchronously, ensuring these operations don't impact your user's chat experience. Data is persisted to database, allowing you to scale out when growth demands. -Zep also provides a simple, easy to use abstraction for document vector search called Document Collections. This is designed to complement Zep's core context features, but is not designed to be a general purpose vector database. - Zep allows you to be more intentional about constructing your prompt: 1. automatically adding a few recent messages, with the number customized for your app; 2. a summary of recent conversations prior to the messages above; 3. and/or contextually relevant summaries or messages surfaced from the entire chat session. -4. and/or relevant Business data from Zep Document Collections. - -Zep Cloud offers: -- **Fact Extraction:** Automatically build fact tables from conversations, without having to define a data schema upfront. -- **Dialog Classification:** Instantly and accurately classify chat dialog. Understand user intent and emotion, segment users, and more. Route chains based on semantic context, and trigger events. -- **Structured Data Extraction:** Quickly extract business data from chat conversations using a schema you define. Understand what your Assistant should ask for next in order to complete its task. You will also need to provide a Zep Project API key to your zep client. You can find out about zep projects in our [cloud docs](https://help.getzep.com/projects.html) -### Using LangChain Zep Classes with `zep-python` - -(Currently only available on release candidate versions) - -In the pre-release version `zep-python` sdk comes with `ZepChatMessageHistory` and `ZepVectorStore` -classes that are compatible with [LangChain's Python expression language](https://python.langchain.com/docs/expression_language/) - -In order to use these classes in your application, you need to make sure that you have -`langchain_core` package installed, please refer to [Langchain's docs installation section](https://python.langchain.com/docs/get_started/installation#langchain-core). - -We support `langchain_core@>=0.1.3<0.2.0` - -You can import these classes in the following way: - -```python -from zep_cloud.langchain import ZepChatMessageHistory, ZepVectorStore -``` - -### Running Examples -You will need to set the following environment variables to run examples in the `examples` directory: - -```dotenv -# Please use examples/.env.example as a template for .env file - -# Required -ZEP_API_KEY=# Your Zep Project API Key -ZEP_COLLECTION=# used in ingestion script and in vector store examples -OPENAI_API_KEY=# Your OpenAI API Key - -# Optional (If you want to use langsmith with LangServe Sample App) -LANGCHAIN_TRACING_V2=true -LANGCHAIN_API_KEY= -LANGCHAIN_PROJECT=# If not specified, defaults to "default" -``` - - - ## Installation ```sh diff --git a/src/zep_cloud/ontology.py b/src/zep_cloud/ontology.py index ee07e737..5f5ca1d4 100644 --- a/src/zep_cloud/ontology.py +++ b/src/zep_cloud/ontology.py @@ -62,6 +62,7 @@ class TraveledTo(EdgeModel): "EntityFloat", "EntityBoolean", "Identity", + "Excluded", "PropertyType", "build_ontology", ] @@ -88,6 +89,18 @@ class _Identity: Identity = _Identity() +class _Excluded: + """Marks a field as left out of the ontology entirely.""" + + +# Annotate a field with this to leave it out of the ontology, regardless of +# whether it also carries a property type marker. This is what lets a model +# reused for other purposes, such as one already shaped by another schema, +# keep a field that is not an ontology property instead of having to be split +# into a separate class just for that field. +Excluded = _Excluded() + + # The four property types the API accepts. Declared once: a change to the wire # spelling is a change here and nowhere else. EntityText = Annotated[typing.Optional[str], PropertyType("text")] @@ -132,6 +145,8 @@ def _properties( properties: typing.List[EntityProperty] = [] identity_properties: typing.List[str] = [] for name, field in model.model_fields.items(): + if any(isinstance(m, _Excluded) for m in field.metadata): + continue marker = next( (m for m in field.metadata if isinstance(m, PropertyType)), None, @@ -161,6 +176,10 @@ def build_ontology( ) -> typing.Tuple[typing.List[EntityType], typing.List[EdgeType]]: """Derive the entity and edge type lists from the given model classes. + Every field needs a property type marker (``EntityText``, ``EntityInt``, + ``EntityFloat``, or ``EntityBoolean``), unless it is annotated with + ``Excluded``, which leaves it out of the ontology entirely. + Pass the result to ``graph.set_ontology`` for one graph, or to ``project.set_ontology`` for the project default. v3 addressed many graphs in one call; v4 has one ontology endpoint per scope, so a caller targeting diff --git a/tests/ontology/test_build_ontology.py b/tests/ontology/test_build_ontology.py index f543a36b..c2f534b3 100644 --- a/tests/ontology/test_build_ontology.py +++ b/tests/ontology/test_build_ontology.py @@ -9,6 +9,7 @@ EntityInt, EntityModel, EntityText, + Excluded, Identity, build_ontology, ) @@ -145,6 +146,46 @@ class Bad(EntityModel): build_ontology(entities={"Bad": Bad}) +def test_an_excluded_field_is_left_out_of_the_ontology(): + # A model reused for something other than the ontology can carry a field + # that is not a property, the same way Go's `zep:"-"` struct tag lets a + # struct field opt out. + class Place(EntityModel): + """A place.""" + + country: EntityText = Field(default=None, description="Its country") + internal_id: Annotated[str, Excluded] = "unused" + + entity_types, _ = build_ontology(entities={"Place": Place}) + assert [p.name for p in entity_types[0].properties] == ["country"] + + +def test_an_excluded_field_with_no_description_still_builds(): + # Excluded means the field is never read as a property at all, so it + # cannot be rejected for missing a description either. + class Place(EntityModel): + """A place.""" + + country: EntityText = Field(default=None, description="Its country") + internal_id: Annotated[str, Excluded] = "unused" + + entity_types, _ = build_ontology(entities={"Place": Place}) + assert len(entity_types[0].properties) == 1 + + +def test_an_unmarked_field_alongside_an_excluded_one_is_still_rejected(): + # Excluded opts a specific field out; it does not relax the requirement + # for every other field to carry a property type marker. + class Bad(EntityModel): + """Has one excluded field and one that is still unmarked.""" + + internal_id: Annotated[str, Excluded] = "unused" + oops: str = "x" + + with pytest.raises(ValueError, match="Bad.oops is not an ontology property"): + build_ontology(entities={"Bad": Bad}) + + def test_a_property_with_no_description_is_rejected_by_name(): # The description goes into the extraction prompt; an empty one is accepted # by the write path and quietly degrades extraction.