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-22 12:26:13 -07:00
|
|
|
import logging
|
2024-08-15 12:03:41 -04:00
|
|
|
from datetime import datetime
|
2024-08-21 12:03:32 -04:00
|
|
|
from time import time
|
2024-08-15 12:03:41 -04:00
|
|
|
|
|
|
|
from core.llm_client import LLMClient
|
2024-08-22 12:26:13 -07:00
|
|
|
from core.nodes import EntityNode, EpisodicNode
|
2024-08-15 12:03:41 -04:00
|
|
|
from core.prompts import prompt_library
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-08-18 13:22:31 -04:00
|
|
|
async def extract_nodes(
|
2024-08-23 14:18:45 -04:00
|
|
|
llm_client: LLMClient,
|
|
|
|
episode: EpisodicNode,
|
|
|
|
previous_episodes: list[EpisodicNode],
|
2024-08-18 13:22:31 -04:00
|
|
|
) -> list[EntityNode]:
|
2024-08-23 14:18:45 -04:00
|
|
|
start = time()
|
|
|
|
|
|
|
|
# Prepare context for LLM
|
|
|
|
context = {
|
|
|
|
'episode_content': episode.content,
|
|
|
|
'episode_timestamp': (episode.valid_at.isoformat() if episode.valid_at else None),
|
|
|
|
'previous_episodes': [
|
|
|
|
{
|
|
|
|
'content': ep.content,
|
|
|
|
'timestamp': ep.valid_at.isoformat() if ep.valid_at else None,
|
|
|
|
}
|
|
|
|
for ep in previous_episodes
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
llm_response = await llm_client.generate_response(prompt_library.extract_nodes.v3(context))
|
|
|
|
new_nodes_data = llm_response.get('new_nodes', [])
|
|
|
|
|
|
|
|
end = time()
|
|
|
|
logger.info(f'Extracted new nodes: {new_nodes_data} in {(end - start) * 1000} ms')
|
|
|
|
# Convert the extracted data into EntityNode objects
|
|
|
|
new_nodes = []
|
|
|
|
for node_data in new_nodes_data:
|
|
|
|
new_node = EntityNode(
|
|
|
|
name=node_data['name'],
|
|
|
|
labels=node_data['labels'],
|
|
|
|
summary=node_data['summary'],
|
|
|
|
created_at=datetime.now(),
|
|
|
|
)
|
|
|
|
new_nodes.append(new_node)
|
|
|
|
logger.info(f'Created new node: {new_node.name} (UUID: {new_node.uuid})')
|
|
|
|
|
|
|
|
return new_nodes
|
2024-08-18 13:22:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def dedupe_extracted_nodes(
|
2024-08-23 14:18:45 -04:00
|
|
|
llm_client: LLMClient,
|
|
|
|
extracted_nodes: list[EntityNode],
|
|
|
|
existing_nodes: list[EntityNode],
|
2024-08-23 08:15:44 -07:00
|
|
|
) -> tuple[list[EntityNode], dict[str, str], list[EntityNode]]:
|
2024-08-23 14:18:45 -04:00
|
|
|
start = time()
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
# build existing node map
|
|
|
|
node_map: dict[str, EntityNode] = {}
|
|
|
|
for node in existing_nodes:
|
|
|
|
node_map[node.name] = node
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
# Temp hack
|
|
|
|
new_nodes_map: dict[str, EntityNode] = {}
|
|
|
|
for node in extracted_nodes:
|
|
|
|
new_nodes_map[node.name] = node
|
2024-08-22 18:09:44 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
# Prepare context for LLM
|
|
|
|
existing_nodes_context = [
|
|
|
|
{'name': node.name, 'summary': node.summary} for node in existing_nodes
|
|
|
|
]
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
extracted_nodes_context = [
|
|
|
|
{'name': node.name, 'summary': node.summary} for node in extracted_nodes
|
|
|
|
]
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
context = {
|
|
|
|
'existing_nodes': existing_nodes_context,
|
|
|
|
'extracted_nodes': extracted_nodes_context,
|
|
|
|
}
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
llm_response = await llm_client.generate_response(prompt_library.dedupe_nodes.v2(context))
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
duplicate_data = llm_response.get('duplicates', [])
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
end = time()
|
|
|
|
logger.info(f'Deduplicated nodes: {duplicate_data} in {(end - start) * 1000} ms')
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
uuid_map: dict[str, str] = {}
|
|
|
|
for duplicate in duplicate_data:
|
|
|
|
uuid = new_nodes_map[duplicate['name']].uuid
|
|
|
|
uuid_value = node_map[duplicate['duplicate_of']].uuid
|
|
|
|
uuid_map[uuid] = uuid_value
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
nodes: list[EntityNode] = []
|
|
|
|
brand_new_nodes: list[EntityNode] = []
|
|
|
|
for node in extracted_nodes:
|
|
|
|
if node.uuid in uuid_map:
|
|
|
|
existing_uuid = uuid_map[node.uuid]
|
|
|
|
# TODO(Preston): This is a bit of a hack I implemented because we were getting incorrect uuids for existing nodes,
|
|
|
|
# can you revisit the node dedup function and make it somewhat cleaner and add more comments/tests please?
|
|
|
|
# find an existing node by the uuid from the nodes_map (each key is name, so we need to iterate by uuid value)
|
|
|
|
existing_node = next((v for k, v in node_map.items() if v.uuid == existing_uuid), None)
|
|
|
|
if existing_node:
|
|
|
|
nodes.append(existing_node)
|
2024-08-23 08:15:44 -07:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
continue
|
|
|
|
brand_new_nodes.append(node)
|
|
|
|
nodes.append(node)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
return nodes, uuid_map, brand_new_nodes
|
2024-08-21 12:03:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def dedupe_node_list(
|
2024-08-23 14:18:45 -04:00
|
|
|
llm_client: LLMClient,
|
|
|
|
nodes: list[EntityNode],
|
2024-08-21 12:03:32 -04:00
|
|
|
) -> tuple[list[EntityNode], dict[str, str]]:
|
2024-08-23 14:18:45 -04:00
|
|
|
start = time()
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
# build node map
|
|
|
|
node_map = {}
|
|
|
|
for node in nodes:
|
|
|
|
node_map[node.name] = node
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
# Prepare context for LLM
|
|
|
|
nodes_context = [{'name': node.name, 'summary': node.summary} for node in nodes]
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
context = {
|
|
|
|
'nodes': nodes_context,
|
|
|
|
}
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
llm_response = await llm_client.generate_response(
|
|
|
|
prompt_library.dedupe_nodes.node_list(context)
|
|
|
|
)
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
nodes_data = llm_response.get('nodes', [])
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
end = time()
|
|
|
|
logger.info(f'Deduplicated nodes: {nodes_data} in {(end - start) * 1000} ms')
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
# Get full node data
|
|
|
|
unique_nodes = []
|
|
|
|
uuid_map: dict[str, str] = {}
|
|
|
|
for node_data in nodes_data:
|
|
|
|
node = node_map[node_data['names'][0]]
|
|
|
|
unique_nodes.append(node)
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
for name in node_data['names'][1:]:
|
|
|
|
uuid = node_map[name].uuid
|
|
|
|
uuid_value = node_map[node_data['names'][0]].uuid
|
|
|
|
uuid_map[uuid] = uuid_value
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
return unique_nodes, uuid_map
|