2024-08-23 13:01:33 -07:00
"""
Copyright 2024 , Zep Software , Inc .
Licensed under the Apache License , Version 2.0 ( the " License " ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : / / www . apache . org / licenses / LICENSE - 2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an " AS IS " BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
"""
2024-08-23 08:15:44 -07:00
from typing import Any , Protocol , TypedDict
2024-08-22 12:26:13 -07:00
2024-12-05 07:03:18 -08:00
from pydantic import BaseModel , Field
2024-08-22 12:26:13 -07:00
from . models import Message , PromptFunction , PromptVersion
2024-08-20 16:29:19 -04:00
2024-12-05 07:03:18 -08:00
class InvalidatedEdges ( BaseModel ) :
2025-04-30 12:08:52 -04:00
contradicted_facts : list [ int ] = Field (
. . . ,
description = ' List of ids of facts that be should invalidated. If no facts should be invalidated, the list should be empty. ' ,
2024-12-05 07:03:18 -08:00
)
2024-08-20 16:29:19 -04:00
class Prompt ( Protocol ) :
2024-08-23 14:18:45 -04:00
v1 : PromptVersion
2024-09-05 12:05:44 -04:00
v2 : PromptVersion
2024-08-20 16:29:19 -04:00
class Versions ( TypedDict ) :
2024-08-23 14:18:45 -04:00
v1 : PromptFunction
2024-09-05 12:05:44 -04:00
v2 : PromptFunction
2024-08-20 16:29:19 -04:00
2024-08-23 08:15:44 -07:00
def v1 ( context : dict [ str , Any ] ) - > list [ Message ] :
2024-08-23 14:18:45 -04:00
return [
Message (
role = ' system ' ,
content = ' You are an AI assistant that helps determine which relationships in a knowledge graph should be invalidated based solely on explicit contradictions in newer information. ' ,
) ,
Message (
role = ' user ' ,
content = f """
2024-08-26 10:30:22 -04:00
Based on the provided existing edges and new edges with their timestamps , determine which relationships , if any , should be marked as expired due to contradictions or updates in the newer edges .
Use the start and end dates of the edges to determine which edges are to be marked expired .
Only mark a relationship as invalid if there is clear evidence from other edges that the relationship is no longer true .
Do not invalidate relationships merely because they weren ' t mentioned in the episodes. You may use the current episode and previous episodes as well as the facts of each edge to understand the context of the relationships.
2024-08-22 18:09:44 -04:00
Previous Episodes :
{ context [ ' previous_episodes ' ] }
Current Episode :
{ context [ ' current_episode ' ] }
2024-08-20 16:29:19 -04:00
Existing Edges ( sorted by timestamp , newest first ) :
{ context [ ' existing_edges ' ] }
New Edges :
{ context [ ' new_edges ' ] }
2024-08-26 10:30:22 -04:00
Each edge is formatted as : " UUID | SOURCE_NODE - EDGE_NAME - TARGET_NODE (fact: EDGE_FACT), START_DATE (END_DATE, optional)) "
2024-08-20 16:29:19 -04:00
""" ,
2024-08-23 14:18:45 -04:00
) ,
]
2024-08-20 16:29:19 -04:00
2024-09-05 12:05:44 -04:00
def v2 ( context : dict [ str , Any ] ) - > list [ Message ] :
return [
Message (
role = ' system ' ,
2025-04-30 12:08:52 -04:00
content = ' You are an AI assistant that determines which facts contradict each other. ' ,
2024-09-05 12:05:44 -04:00
) ,
Message (
role = ' user ' ,
content = f """
2025-04-30 12:08:52 -04:00
Based on the provided EXISTING FACTS and a NEW FACT , determine which existing facts the new fact contradicts .
Return a list containing all ids of the facts that are contradicted by the NEW FACT .
If there are no contradicted facts , return an empty list .
2024-09-05 12:05:44 -04:00
2025-04-30 12:08:52 -04:00
< EXISTING FACTS >
2024-09-05 12:05:44 -04:00
{ context [ ' existing_edges ' ] }
2025-04-30 12:08:52 -04:00
< / EXISTING FACTS >
2024-09-05 12:05:44 -04:00
2025-04-30 12:08:52 -04:00
< NEW FACT >
2024-09-05 12:05:44 -04:00
{ context [ ' new_edge ' ] }
2025-04-30 12:08:52 -04:00
< / NEW FACT >
2024-09-05 12:05:44 -04:00
""" ,
) ,
]
versions : Versions = { ' v1 ' : v1 , ' v2 ' : v2 }