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-15 12:03:41 -04:00
import json
2024-08-23 08:15:44 -07:00
from typing import Any , Protocol , TypedDict
2024-08-15 12:03:41 -04:00
2024-08-22 12:26:13 -07:00
from . models import Message , PromptFunction , PromptVersion
2024-08-15 12:03:41 -04:00
class Prompt ( Protocol ) :
2024-11-13 20:13:06 -05:00
extract_message : PromptVersion
2024-08-26 10:30:22 -04:00
extract_json : PromptVersion
2024-09-11 12:06:08 -04:00
extract_text : PromptVersion
2024-11-13 11:58:56 -05:00
reflexion : PromptVersion
2024-08-15 12:03:41 -04:00
class Versions ( TypedDict ) :
2024-11-13 20:13:06 -05:00
extract_message : PromptFunction
2024-08-26 10:30:22 -04:00
extract_json : PromptFunction
2024-09-11 12:06:08 -04:00
extract_text : PromptFunction
2024-11-13 11:58:56 -05:00
reflexion : PromptFunction
2024-08-15 12:03:41 -04:00
2024-11-13 20:13:06 -05:00
def extract_message ( context : dict [ str , Any ] ) - > list [ Message ] :
sys_prompt = """ You are an AI assistant that extracts entity nodes from conversational messages. Your primary task is to identify and extract the speaker and other significant entities mentioned in the conversation. """
2024-08-19 09:37:56 -04:00
2024-08-23 14:18:45 -04:00
user_prompt = f """
2024-11-13 20:13:06 -05:00
< PREVIOUS MESSAGES >
{ json . dumps ( [ ep for ep in context [ ' previous_episodes ' ] ] , indent = 2 ) }
< / PREVIOUS MESSAGES >
2024-08-23 18:06:42 -04:00
< CURRENT MESSAGE >
2024-08-19 09:37:56 -04:00
{ context [ " episode_content " ] }
2024-11-13 20:13:06 -05:00
< / CURRENT MESSAGE >
2024-08-19 09:37:56 -04:00
2024-11-13 11:58:56 -05:00
{ context [ ' custom_prompt ' ] }
2024-11-13 20:13:06 -05:00
Given the above conversation , extract entity nodes from the CURRENT MESSAGE that are explicitly or implicitly mentioned :
2024-08-19 09:37:56 -04:00
Guidelines :
1. ALWAYS extract the speaker / actor as the first node . The speaker is the part before the colon in each line of dialogue .
2024-11-13 20:13:06 -05:00
2. Extract other significant entities , concepts , or actors mentioned in the CURRENT MESSAGE .
3. DO NOT create nodes for relationships or actions .
4. DO NOT create nodes for temporal information like dates , times or years ( these will be added to edges later ) .
5. Be as explicit as possible in your node names , using full names .
6. DO NOT extract entities mentioned only in PREVIOUS MESSAGES , those messages are only to provide context .
2024-08-19 09:37:56 -04:00
Respond with a JSON object in the following format :
{ {
2024-11-13 20:13:06 -05:00
" extracted_node_names " : [ " Name of the extracted entity " , . . . ] ,
2024-08-26 10:30:22 -04:00
} }
"""
return [
Message ( role = ' system ' , content = sys_prompt ) ,
Message ( role = ' user ' , content = user_prompt ) ,
]
def extract_json ( context : dict [ str , Any ] ) - > list [ Message ] :
2024-11-13 20:13:06 -05:00
sys_prompt = """ You are an AI assistant that extracts entity nodes from JSON.
2024-08-26 10:30:22 -04:00
Your primary task is to identify and extract relevant entities from JSON files """
user_prompt = f """
2024-11-13 20:13:06 -05:00
< SOURCE DESCRIPTION > :
2024-08-26 10:30:22 -04:00
{ context [ " source_description " ] }
2024-11-13 20:13:06 -05:00
< / SOURCE DESCRIPTION >
< JSON >
2024-08-26 10:30:22 -04:00
{ context [ " episode_content " ] }
2024-11-13 20:13:06 -05:00
< / JSON >
2024-08-26 10:30:22 -04:00
2024-11-13 11:58:56 -05:00
{ context [ ' custom_prompt ' ] }
2024-11-13 20:13:06 -05:00
Given the above source description and JSON , extract relevant entity nodes from the provided JSON :
2024-08-26 10:30:22 -04:00
Guidelines :
1. Always try to extract an entities that the JSON represents . This will often be something like a " name " or " user field
2. Do NOT extract any properties that contain dates
Respond with a JSON object in the following format :
{ {
2024-11-13 20:13:06 -05:00
" extracted_node_names " : [ " Name of the extracted entity " , . . . ] ,
2024-08-19 09:37:56 -04:00
} }
"""
2024-08-23 14:18:45 -04:00
return [
Message ( role = ' system ' , content = sys_prompt ) ,
Message ( role = ' user ' , content = user_prompt ) ,
]
2024-08-19 09:37:56 -04:00
2024-09-11 12:06:08 -04:00
def extract_text ( context : dict [ str , Any ] ) - > list [ Message ] :
2024-11-13 20:13:06 -05:00
sys_prompt = """ You are an AI assistant that extracts entity nodes from text. Your primary task is to identify and extract the speaker and other significant entities mentioned in the provided text. """
2024-09-11 12:06:08 -04:00
user_prompt = f """
2024-11-13 20:13:06 -05:00
< TEXT >
2024-09-11 12:06:08 -04:00
{ context [ " episode_content " ] }
2024-11-13 20:13:06 -05:00
< / TEXT >
2024-11-13 11:58:56 -05:00
{ context [ ' custom_prompt ' ] }
2024-09-11 12:06:08 -04:00
2024-11-13 20:13:06 -05:00
Given the following text , extract entity nodes from the TEXT that are explicitly or implicitly mentioned :
2024-09-11 12:06:08 -04:00
Guidelines :
2024-11-13 11:58:56 -05:00
1. Extract significant entities , concepts , or actors mentioned in the conversation .
2024-11-13 20:13:06 -05:00
2. Avoid creating nodes for relationships or actions .
3. Avoid creating nodes for temporal information like dates , times or years ( these will be added to edges later ) .
4. Be as explicit as possible in your node names , using full names and avoiding abbreviations .
2024-09-11 12:06:08 -04:00
Respond with a JSON object in the following format :
{ {
2024-11-13 20:13:06 -05:00
" extracted_node_names " : [ " Name of the extracted entity " , . . . ] ,
2024-09-11 12:06:08 -04:00
} }
"""
return [
Message ( role = ' system ' , content = sys_prompt ) ,
Message ( role = ' user ' , content = user_prompt ) ,
]
2024-11-13 11:58:56 -05:00
def reflexion ( context : dict [ str , Any ] ) - > list [ Message ] :
sys_prompt = """ You are an AI assistant that determines which entities have not been extracted from the given context """
user_prompt = f """
2024-11-13 20:13:06 -05:00
< PREVIOUS MESSAGES >
{ json . dumps ( [ ep for ep in context [ ' previous_episodes ' ] ] , indent = 2 ) }
< / PREVIOUS MESSAGES >
2024-11-13 11:58:56 -05:00
< CURRENT MESSAGE >
{ context [ " episode_content " ] }
< / CURRENT MESSAGE >
2024-11-13 20:13:06 -05:00
2024-11-13 11:58:56 -05:00
< EXTRACTED ENTITIES >
{ context [ " extracted_entities " ] }
< / EXTRACTED ENTITIES >
2024-11-13 20:13:06 -05:00
Given the above previous messages , current message , and list of extracted entities ; determine if any entities haven ' t been
extracted :
2024-11-13 11:58:56 -05:00
Respond with a JSON object in the following format :
{ {
" missed_entities " : [ " name of entity that wasn ' t extracted " , . . . ]
} }
"""
return [
Message ( role = ' system ' , content = sys_prompt ) ,
Message ( role = ' user ' , content = user_prompt ) ,
]
2024-09-11 12:06:08 -04:00
versions : Versions = {
2024-11-13 20:13:06 -05:00
' extract_message ' : extract_message ,
2024-09-11 12:06:08 -04:00
' extract_json ' : extract_json ,
' extract_text ' : extract_text ,
2024-11-13 11:58:56 -05:00
' reflexion ' : reflexion ,
2024-09-11 12:06:08 -04:00
}