Implement OpenAI Structured Output (#225)
* implement so * bug fixes and typing * inject schema for non-openai clients * correct datetime format * remove List keyword * Refactor node_operations.py to use updated prompt_library functions * update example
This commit is contained in:
parent
427c73d2a8
commit
567a8ab74a
19 changed files with 249 additions and 181 deletions
File diff suppressed because one or more lines are too long
|
|
@ -34,7 +34,7 @@ class CrossEncoderClient(ABC):
|
|||
passages (list[str]): A list of passages to rank.
|
||||
|
||||
Returns:
|
||||
List[tuple[str, float]]: A list of tuples containing the passage and its score,
|
||||
list[tuple[str, float]]: A list of tuples containing the passage and its score,
|
||||
sorted in descending order of relevance.
|
||||
"""
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import typing
|
|||
|
||||
import anthropic
|
||||
from anthropic import AsyncAnthropic
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..prompts.models import Message
|
||||
from .client import LLMClient
|
||||
|
|
@ -46,7 +47,9 @@ class AnthropicClient(LLMClient):
|
|||
max_retries=1,
|
||||
)
|
||||
|
||||
async def _generate_response(self, messages: list[Message]) -> dict[str, typing.Any]:
|
||||
async def _generate_response(
|
||||
self, messages: list[Message], response_model: type[BaseModel] | None = None
|
||||
) -> dict[str, typing.Any]:
|
||||
system_message = messages[0]
|
||||
user_messages = [{'role': m.role, 'content': m.content} for m in messages[1:]] + [
|
||||
{'role': 'assistant', 'content': '{'}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ from abc import ABC, abstractmethod
|
|||
|
||||
import httpx
|
||||
from diskcache import Cache
|
||||
from pydantic import BaseModel
|
||||
from tenacity import retry, retry_if_exception, stop_after_attempt, wait_random_exponential
|
||||
|
||||
from ..prompts.models import Message
|
||||
|
|
@ -66,14 +67,18 @@ class LLMClient(ABC):
|
|||
else None,
|
||||
reraise=True,
|
||||
)
|
||||
async def _generate_response_with_retry(self, messages: list[Message]) -> dict[str, typing.Any]:
|
||||
async def _generate_response_with_retry(
|
||||
self, messages: list[Message], response_model: type[BaseModel] | None = None
|
||||
) -> dict[str, typing.Any]:
|
||||
try:
|
||||
return await self._generate_response(messages)
|
||||
return await self._generate_response(messages, response_model)
|
||||
except (httpx.HTTPStatusError, RateLimitError) as e:
|
||||
raise e
|
||||
|
||||
@abstractmethod
|
||||
async def _generate_response(self, messages: list[Message]) -> dict[str, typing.Any]:
|
||||
async def _generate_response(
|
||||
self, messages: list[Message], response_model: type[BaseModel] | None = None
|
||||
) -> dict[str, typing.Any]:
|
||||
pass
|
||||
|
||||
def _get_cache_key(self, messages: list[Message]) -> str:
|
||||
|
|
@ -82,7 +87,17 @@ class LLMClient(ABC):
|
|||
key_str = f'{self.model}:{message_str}'
|
||||
return hashlib.md5(key_str.encode()).hexdigest()
|
||||
|
||||
async def generate_response(self, messages: list[Message]) -> dict[str, typing.Any]:
|
||||
async def generate_response(
|
||||
self, messages: list[Message], response_model: type[BaseModel] | None = None
|
||||
) -> dict[str, typing.Any]:
|
||||
if response_model is not None:
|
||||
serialized_model = json.dumps(response_model.model_json_schema())
|
||||
messages[
|
||||
-1
|
||||
].content += (
|
||||
f'\n\nRespond with a JSON object in the following format:\n\n{serialized_model}'
|
||||
)
|
||||
|
||||
if self.cache_enabled:
|
||||
cache_key = self._get_cache_key(messages)
|
||||
|
||||
|
|
@ -91,7 +106,7 @@ class LLMClient(ABC):
|
|||
logger.debug(f'Cache hit for {cache_key}')
|
||||
return cached_response
|
||||
|
||||
response = await self._generate_response_with_retry(messages)
|
||||
response = await self._generate_response_with_retry(messages, response_model)
|
||||
|
||||
if self.cache_enabled:
|
||||
self.cache_dir.set(cache_key, response)
|
||||
|
|
|
|||
|
|
@ -21,3 +21,11 @@ class RateLimitError(Exception):
|
|||
def __init__(self, message='Rate limit exceeded. Please try again later.'):
|
||||
self.message = message
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class RefusalError(Exception):
|
||||
"""Exception raised when the LLM refuses to generate a response."""
|
||||
|
||||
def __init__(self, message: str):
|
||||
self.message = message
|
||||
super().__init__(self.message)
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import typing
|
|||
import groq
|
||||
from groq import AsyncGroq
|
||||
from groq.types.chat import ChatCompletionMessageParam
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..prompts.models import Message
|
||||
from .client import LLMClient
|
||||
|
|
@ -43,7 +44,9 @@ class GroqClient(LLMClient):
|
|||
|
||||
self.client = AsyncGroq(api_key=config.api_key)
|
||||
|
||||
async def _generate_response(self, messages: list[Message]) -> dict[str, typing.Any]:
|
||||
async def _generate_response(
|
||||
self, messages: list[Message], response_model: type[BaseModel] | None = None
|
||||
) -> dict[str, typing.Any]:
|
||||
msgs: list[ChatCompletionMessageParam] = []
|
||||
for m in messages:
|
||||
if m.role == 'user':
|
||||
|
|
|
|||
|
|
@ -14,18 +14,18 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import typing
|
||||
|
||||
import openai
|
||||
from openai import AsyncOpenAI
|
||||
from openai.types.chat import ChatCompletionMessageParam
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..prompts.models import Message
|
||||
from .client import LLMClient
|
||||
from .config import LLMConfig
|
||||
from .errors import RateLimitError
|
||||
from .errors import RateLimitError, RefusalError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -65,6 +65,10 @@ class OpenAIClient(LLMClient):
|
|||
client (Any | None): An optional async client instance to use. If not provided, a new AsyncOpenAI client is created.
|
||||
|
||||
"""
|
||||
# removed caching to simplify the `generate_response` override
|
||||
if cache:
|
||||
raise NotImplementedError('Caching is not implemented for OpenAI')
|
||||
|
||||
if config is None:
|
||||
config = LLMConfig()
|
||||
|
||||
|
|
@ -75,7 +79,9 @@ class OpenAIClient(LLMClient):
|
|||
else:
|
||||
self.client = client
|
||||
|
||||
async def _generate_response(self, messages: list[Message]) -> dict[str, typing.Any]:
|
||||
async def _generate_response(
|
||||
self, messages: list[Message], response_model: type[BaseModel] | None = None
|
||||
) -> dict[str, typing.Any]:
|
||||
openai_messages: list[ChatCompletionMessageParam] = []
|
||||
for m in messages:
|
||||
if m.role == 'user':
|
||||
|
|
@ -83,17 +89,33 @@ class OpenAIClient(LLMClient):
|
|||
elif m.role == 'system':
|
||||
openai_messages.append({'role': 'system', 'content': m.content})
|
||||
try:
|
||||
response = await self.client.chat.completions.create(
|
||||
response = await self.client.beta.chat.completions.parse(
|
||||
model=self.model or DEFAULT_MODEL,
|
||||
messages=openai_messages,
|
||||
temperature=self.temperature,
|
||||
max_tokens=self.max_tokens,
|
||||
response_format={'type': 'json_object'},
|
||||
response_format=response_model, # type: ignore
|
||||
)
|
||||
result = response.choices[0].message.content or ''
|
||||
return json.loads(result)
|
||||
|
||||
response_object = response.choices[0].message
|
||||
|
||||
if response_object.parsed:
|
||||
return response_object.parsed.model_dump()
|
||||
elif response_object.refusal:
|
||||
raise RefusalError(response_object.refusal)
|
||||
else:
|
||||
raise Exception('No response from LLM')
|
||||
except openai.LengthFinishReasonError as e:
|
||||
raise Exception(f'Output length exceeded max tokens {self.max_tokens}: {e}') from e
|
||||
except openai.RateLimitError as e:
|
||||
raise RateLimitError from e
|
||||
except Exception as e:
|
||||
logger.error(f'Error in generating LLM response: {e}')
|
||||
raise
|
||||
|
||||
async def generate_response(
|
||||
self, messages: list[Message], response_model: type[BaseModel] | None = None
|
||||
) -> dict[str, typing.Any]:
|
||||
response = await self._generate_response(messages, response_model)
|
||||
|
||||
return response
|
||||
|
|
|
|||
|
|
@ -15,11 +15,30 @@ limitations under the License.
|
|||
"""
|
||||
|
||||
import json
|
||||
from typing import Any, Protocol, TypedDict
|
||||
from typing import Any, Optional, Protocol, TypedDict
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .models import Message, PromptFunction, PromptVersion
|
||||
|
||||
|
||||
class EdgeDuplicate(BaseModel):
|
||||
is_duplicate: bool = Field(..., description='true or false')
|
||||
uuid: Optional[str] = Field(
|
||||
None,
|
||||
description="uuid of the existing edge like '5d643020624c42fa9de13f97b1b3fa39' or null",
|
||||
)
|
||||
|
||||
|
||||
class UniqueFact(BaseModel):
|
||||
uuid: str = Field(..., description='unique identifier of the fact')
|
||||
fact: str = Field(..., description='fact of a unique edge')
|
||||
|
||||
|
||||
class UniqueFacts(BaseModel):
|
||||
unique_facts: list[UniqueFact]
|
||||
|
||||
|
||||
class Prompt(Protocol):
|
||||
edge: PromptVersion
|
||||
edge_list: PromptVersion
|
||||
|
|
@ -56,12 +75,6 @@ def edge(context: dict[str, Any]) -> list[Message]:
|
|||
|
||||
Guidelines:
|
||||
1. The facts do not need to be completely identical to be duplicates, they just need to express the same information.
|
||||
|
||||
Respond with a JSON object in the following format:
|
||||
{{
|
||||
"is_duplicate": true or false,
|
||||
"uuid": uuid of the existing edge like "5d643020624c42fa9de13f97b1b3fa39" or null,
|
||||
}}
|
||||
""",
|
||||
),
|
||||
]
|
||||
|
|
@ -90,16 +103,6 @@ def edge_list(context: dict[str, Any]) -> list[Message]:
|
|||
3. Facts will often discuss the same or similar relation between identical entities
|
||||
4. The final list should have only unique facts. If 3 facts are all duplicates of each other, only one of their
|
||||
facts should be in the response
|
||||
|
||||
Respond with a JSON object in the following format:
|
||||
{{
|
||||
"unique_facts": [
|
||||
{{
|
||||
"uuid": "unique identifier of the fact",
|
||||
"fact": "fact of a unique edge"
|
||||
}}
|
||||
]
|
||||
}}
|
||||
""",
|
||||
),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -15,11 +15,25 @@ limitations under the License.
|
|||
"""
|
||||
|
||||
import json
|
||||
from typing import Any, Protocol, TypedDict
|
||||
from typing import Any, Optional, Protocol, TypedDict
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .models import Message, PromptFunction, PromptVersion
|
||||
|
||||
|
||||
class NodeDuplicate(BaseModel):
|
||||
is_duplicate: bool = Field(..., description='true or false')
|
||||
uuid: Optional[str] = Field(
|
||||
None,
|
||||
description="uuid of the existing node like '5d643020624c42fa9de13f97b1b3fa39' or null",
|
||||
)
|
||||
name: str = Field(
|
||||
...,
|
||||
description="Updated name of the new node (use the best name between the new node's name, an existing duplicate name, or a combination of both)",
|
||||
)
|
||||
|
||||
|
||||
class Prompt(Protocol):
|
||||
node: PromptVersion
|
||||
node_list: PromptVersion
|
||||
|
|
|
|||
|
|
@ -17,9 +17,26 @@ limitations under the License.
|
|||
import json
|
||||
from typing import Any, Protocol, TypedDict
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .models import Message, PromptFunction, PromptVersion
|
||||
|
||||
|
||||
class QueryExpansion(BaseModel):
|
||||
query: str = Field(..., description='query optimized for database search')
|
||||
|
||||
|
||||
class QAResponse(BaseModel):
|
||||
ANSWER: str = Field(..., description='how Alice would answer the question')
|
||||
|
||||
|
||||
class EvalResponse(BaseModel):
|
||||
is_correct: bool = Field(..., description='boolean if the answer is correct or incorrect')
|
||||
reasoning: str = Field(
|
||||
..., description='why you determined the response was correct or incorrect'
|
||||
)
|
||||
|
||||
|
||||
class Prompt(Protocol):
|
||||
qa_prompt: PromptVersion
|
||||
eval_prompt: PromptVersion
|
||||
|
|
@ -41,10 +58,6 @@ def query_expansion(context: dict[str, Any]) -> list[Message]:
|
|||
<QUESTION>
|
||||
{json.dumps(context['query'])}
|
||||
</QUESTION>
|
||||
respond with a JSON object in the following format:
|
||||
{{
|
||||
"query": "query optimized for database search"
|
||||
}}
|
||||
"""
|
||||
return [
|
||||
Message(role='system', content=sys_prompt),
|
||||
|
|
@ -67,10 +80,6 @@ def qa_prompt(context: dict[str, Any]) -> list[Message]:
|
|||
<QUESTION>
|
||||
{context['query']}
|
||||
</QUESTION>
|
||||
respond with a JSON object in the following format:
|
||||
{{
|
||||
"ANSWER": "how Alice would answer the question"
|
||||
}}
|
||||
"""
|
||||
return [
|
||||
Message(role='system', content=sys_prompt),
|
||||
|
|
@ -96,12 +105,6 @@ def eval_prompt(context: dict[str, Any]) -> list[Message]:
|
|||
<RESPONSE>
|
||||
{context['response']}
|
||||
</RESPONSE>
|
||||
|
||||
respond with a JSON object in the following format:
|
||||
{{
|
||||
"is_correct": "boolean if the answer is correct or incorrect"
|
||||
"reasoning": "why you determined the response was correct or incorrect"
|
||||
}}
|
||||
"""
|
||||
return [
|
||||
Message(role='system', content=sys_prompt),
|
||||
|
|
|
|||
|
|
@ -14,11 +14,24 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
"""
|
||||
|
||||
from typing import Any, Protocol, TypedDict
|
||||
from typing import Any, Optional, Protocol, TypedDict
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .models import Message, PromptFunction, PromptVersion
|
||||
|
||||
|
||||
class EdgeDates(BaseModel):
|
||||
valid_at: Optional[str] = Field(
|
||||
None,
|
||||
description='The date and time when the relationship described by the edge fact became true or was established. YYYY-MM-DDTHH:MM:SS.SSSSSSZ or null.',
|
||||
)
|
||||
invalid_at: Optional[str] = Field(
|
||||
None,
|
||||
description='The date and time when the relationship described by the edge fact stopped being true or ended. YYYY-MM-DDTHH:MM:SS.SSSSSSZ or null.',
|
||||
)
|
||||
|
||||
|
||||
class Prompt(Protocol):
|
||||
v1: PromptVersion
|
||||
|
||||
|
|
@ -60,7 +73,7 @@ def v1(context: dict[str, Any]) -> list[Message]:
|
|||
Analyze the conversation and determine if there are dates that are part of the edge fact. Only set dates if they explicitly relate to the formation or alteration of the relationship itself.
|
||||
|
||||
Guidelines:
|
||||
1. Use ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ) for datetimes.
|
||||
1. Use ISO 8601 format (YYYY-MM-DDTHH:MM:SS.SSSSSSZ) for datetimes.
|
||||
2. Use the reference timestamp as the current time when determining the valid_at and invalid_at dates.
|
||||
3. If the fact is written in the present tense, use the Reference Timestamp for the valid_at date
|
||||
4. If no temporal information is found that establishes or changes the relationship, leave the fields as null.
|
||||
|
|
@ -69,11 +82,6 @@ def v1(context: dict[str, Any]) -> list[Message]:
|
|||
7. If only a date is mentioned without a specific time, use 00:00:00 (midnight) for that date.
|
||||
8. If only year is mentioned, use January 1st of that year at 00:00:00.
|
||||
9. Always include the time zone offset (use Z for UTC if no specific time zone is mentioned).
|
||||
Respond with a JSON object:
|
||||
{{
|
||||
"valid_at": "YYYY-MM-DDTHH:MM:SS.SSSSSSZ or null",
|
||||
"invalid_at": "YYYY-MM-DDTHH:MM:SS.SSSSSSZ or null",
|
||||
}}
|
||||
""",
|
||||
),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -17,9 +17,26 @@ limitations under the License.
|
|||
import json
|
||||
from typing import Any, Protocol, TypedDict
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .models import Message, PromptFunction, PromptVersion
|
||||
|
||||
|
||||
class Edge(BaseModel):
|
||||
relation_type: str = Field(..., description='RELATION_TYPE_IN_CAPS')
|
||||
source_entity_name: str = Field(..., description='name of the source entity')
|
||||
target_entity_name: str = Field(..., description='name of the target entity')
|
||||
fact: str = Field(..., description='extracted factual information')
|
||||
|
||||
|
||||
class ExtractedEdges(BaseModel):
|
||||
edges: list[Edge]
|
||||
|
||||
|
||||
class MissingFacts(BaseModel):
|
||||
missing_facts: list[str] = Field(..., description="facts that weren't extracted")
|
||||
|
||||
|
||||
class Prompt(Protocol):
|
||||
edge: PromptVersion
|
||||
reflexion: PromptVersion
|
||||
|
|
@ -54,25 +71,12 @@ def edge(context: dict[str, Any]) -> list[Message]:
|
|||
|
||||
Given the above MESSAGES and ENTITIES, extract all facts pertaining to the listed ENTITIES from the CURRENT MESSAGE.
|
||||
|
||||
|
||||
Guidelines:
|
||||
1. Extract facts only between the provided entities.
|
||||
2. Each fact should represent a clear relationship between two DISTINCT nodes.
|
||||
3. The relation_type should be a concise, all-caps description of the fact (e.g., LOVES, IS_FRIENDS_WITH, WORKS_FOR).
|
||||
4. Provide a more detailed fact containing all relevant information.
|
||||
5. Consider temporal aspects of relationships when relevant.
|
||||
|
||||
Respond with a JSON object in the following format:
|
||||
{{
|
||||
"edges": [
|
||||
{{
|
||||
"relation_type": "RELATION_TYPE_IN_CAPS",
|
||||
"source_entity_name": "name of the source entity",
|
||||
"target_entity_name": "name of the target entity",
|
||||
"fact": "extracted factual information",
|
||||
}}
|
||||
]
|
||||
}}
|
||||
""",
|
||||
),
|
||||
]
|
||||
|
|
@ -98,12 +102,7 @@ def reflexion(context: dict[str, Any]) -> list[Message]:
|
|||
</EXTRACTED FACTS>
|
||||
|
||||
Given the above MESSAGES, list of EXTRACTED ENTITIES entities, and list of EXTRACTED FACTS;
|
||||
determine if any facts haven't been extracted:
|
||||
|
||||
Respond with a JSON object in the following format:
|
||||
{{
|
||||
"missing_facts": [ "facts that weren't extracted", ...]
|
||||
}}
|
||||
determine if any facts haven't been extracted.
|
||||
"""
|
||||
return [
|
||||
Message(role='system', content=sys_prompt),
|
||||
|
|
|
|||
|
|
@ -17,9 +17,19 @@ limitations under the License.
|
|||
import json
|
||||
from typing import Any, Protocol, TypedDict
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .models import Message, PromptFunction, PromptVersion
|
||||
|
||||
|
||||
class ExtractedNodes(BaseModel):
|
||||
extracted_node_names: list[str] = Field(..., description='Name of the extracted entity')
|
||||
|
||||
|
||||
class MissedEntities(BaseModel):
|
||||
missed_entities: list[str] = Field(..., description="Names of entities that weren't extracted")
|
||||
|
||||
|
||||
class Prompt(Protocol):
|
||||
extract_message: PromptVersion
|
||||
extract_json: PromptVersion
|
||||
|
|
@ -56,11 +66,6 @@ Guidelines:
|
|||
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.
|
||||
|
||||
Respond with a JSON object in the following format:
|
||||
{{
|
||||
"extracted_node_names": ["Name of the extracted entity", ...],
|
||||
}}
|
||||
"""
|
||||
return [
|
||||
Message(role='system', content=sys_prompt),
|
||||
|
|
@ -87,11 +92,6 @@ Given the above source description and JSON, extract relevant entity nodes from
|
|||
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:
|
||||
{{
|
||||
"extracted_node_names": ["Name of the extracted entity", ...],
|
||||
}}
|
||||
"""
|
||||
return [
|
||||
Message(role='system', content=sys_prompt),
|
||||
|
|
@ -116,11 +116,6 @@ Guidelines:
|
|||
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.
|
||||
|
||||
Respond with a JSON object in the following format:
|
||||
{{
|
||||
"extracted_node_names": ["Name of the extracted entity", ...],
|
||||
}}
|
||||
"""
|
||||
return [
|
||||
Message(role='system', content=sys_prompt),
|
||||
|
|
@ -144,12 +139,7 @@ def reflexion(context: dict[str, Any]) -> list[Message]:
|
|||
</EXTRACTED ENTITIES>
|
||||
|
||||
Given the above previous messages, current message, and list of extracted entities; determine if any entities haven't been
|
||||
extracted:
|
||||
|
||||
Respond with a JSON object in the following format:
|
||||
{{
|
||||
"missed_entities": [ "name of entity that wasn't extracted", ...]
|
||||
}}
|
||||
extracted.
|
||||
"""
|
||||
return [
|
||||
Message(role='system', content=sys_prompt),
|
||||
|
|
|
|||
|
|
@ -16,9 +16,22 @@ limitations under the License.
|
|||
|
||||
from typing import Any, Protocol, TypedDict
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .models import Message, PromptFunction, PromptVersion
|
||||
|
||||
|
||||
class InvalidatedEdge(BaseModel):
|
||||
uuid: str = Field(..., description='The UUID of the edge to be invalidated')
|
||||
fact: str = Field(..., description='Updated fact of the edge')
|
||||
|
||||
|
||||
class InvalidatedEdges(BaseModel):
|
||||
invalidated_edges: list[InvalidatedEdge] = Field(
|
||||
..., description='List of edges that should be invalidated'
|
||||
)
|
||||
|
||||
|
||||
class Prompt(Protocol):
|
||||
v1: PromptVersion
|
||||
v2: PromptVersion
|
||||
|
|
@ -56,18 +69,6 @@ def v1(context: dict[str, Any]) -> list[Message]:
|
|||
{context['new_edges']}
|
||||
|
||||
Each edge is formatted as: "UUID | SOURCE_NODE - EDGE_NAME - TARGET_NODE (fact: EDGE_FACT), START_DATE (END_DATE, optional))"
|
||||
|
||||
For each existing edge that should be invalidated, respond with a JSON object in the following format:
|
||||
{{
|
||||
"invalidated_edges": [
|
||||
{{
|
||||
"edge_uuid": "The UUID of the edge to be invalidated (the part before the | character)",
|
||||
"fact": "Updated fact of the edge"
|
||||
}}
|
||||
]
|
||||
}}
|
||||
|
||||
If no relationships need to be invalidated based on these strict criteria, return an empty list for "invalidated_edges".
|
||||
""",
|
||||
),
|
||||
]
|
||||
|
|
@ -89,19 +90,6 @@ def v2(context: dict[str, Any]) -> list[Message]:
|
|||
|
||||
New Edge:
|
||||
{context['new_edge']}
|
||||
|
||||
|
||||
For each existing edge that should be invalidated, respond with a JSON object in the following format:
|
||||
{{
|
||||
"invalidated_edges": [
|
||||
{{
|
||||
"uuid": "The UUID of the edge to be invalidated",
|
||||
"fact": "Updated fact of the edge"
|
||||
}}
|
||||
]
|
||||
}}
|
||||
|
||||
If no relationships need to be invalidated based on these strict criteria, return an empty list for "invalidated_edges".
|
||||
""",
|
||||
),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -17,9 +17,21 @@ limitations under the License.
|
|||
import json
|
||||
from typing import Any, Protocol, TypedDict
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .models import Message, PromptFunction, PromptVersion
|
||||
|
||||
|
||||
class Summary(BaseModel):
|
||||
summary: str = Field(
|
||||
..., description='Summary containing the important information from both summaries'
|
||||
)
|
||||
|
||||
|
||||
class SummaryDescription(BaseModel):
|
||||
description: str = Field(..., description='One sentence description of the provided summary')
|
||||
|
||||
|
||||
class Prompt(Protocol):
|
||||
summarize_pair: PromptVersion
|
||||
summarize_context: PromptVersion
|
||||
|
|
@ -45,11 +57,6 @@ def summarize_pair(context: dict[str, Any]) -> list[Message]:
|
|||
|
||||
Summaries:
|
||||
{json.dumps(context['node_summaries'], indent=2)}
|
||||
|
||||
Respond with a JSON object in the following format:
|
||||
{{
|
||||
"summary": "Summary containing the important information from both summaries"
|
||||
}}
|
||||
""",
|
||||
),
|
||||
]
|
||||
|
|
@ -77,12 +84,6 @@ def summarize_context(context: dict[str, Any]) -> list[Message]:
|
|||
<ENTITY>
|
||||
{context['node_name']}
|
||||
</ENTITY>
|
||||
|
||||
|
||||
Respond with a JSON object in the following format:
|
||||
{{
|
||||
"summary": "Entity summary"
|
||||
}}
|
||||
""",
|
||||
),
|
||||
]
|
||||
|
|
@ -101,11 +102,6 @@ def summary_description(context: dict[str, Any]) -> list[Message]:
|
|||
|
||||
Summary:
|
||||
{json.dumps(context['summary'], indent=2)}
|
||||
|
||||
Respond with a JSON object in the following format:
|
||||
{{
|
||||
"description": "One sentence description of the provided summary"
|
||||
}}
|
||||
""",
|
||||
),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ from graphiti_core.nodes import (
|
|||
get_community_node_from_record,
|
||||
)
|
||||
from graphiti_core.prompts import prompt_library
|
||||
from graphiti_core.prompts.summarize_nodes import Summary, SummaryDescription
|
||||
from graphiti_core.utils.maintenance.edge_operations import build_community_edges
|
||||
|
||||
MAX_COMMUNITY_BUILD_CONCURRENCY = 10
|
||||
|
|
@ -131,7 +132,7 @@ async def summarize_pair(llm_client: LLMClient, summary_pair: tuple[str, str]) -
|
|||
context = {'node_summaries': [{'summary': summary} for summary in summary_pair]}
|
||||
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.summarize_nodes.summarize_pair(context)
|
||||
prompt_library.summarize_nodes.summarize_pair(context), response_model=Summary
|
||||
)
|
||||
|
||||
pair_summary = llm_response.get('summary', '')
|
||||
|
|
@ -143,7 +144,8 @@ async def generate_summary_description(llm_client: LLMClient, summary: str) -> s
|
|||
context = {'summary': summary}
|
||||
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.summarize_nodes.summary_description(context)
|
||||
prompt_library.summarize_nodes.summary_description(context),
|
||||
response_model=SummaryDescription,
|
||||
)
|
||||
|
||||
description = llm_response.get('description', '')
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@ from graphiti_core.helpers import MAX_REFLEXION_ITERATIONS
|
|||
from graphiti_core.llm_client import LLMClient
|
||||
from graphiti_core.nodes import CommunityNode, EntityNode, EpisodicNode
|
||||
from graphiti_core.prompts import prompt_library
|
||||
from graphiti_core.prompts.dedupe_edges import EdgeDuplicate, UniqueFacts
|
||||
from graphiti_core.prompts.extract_edges import ExtractedEdges, MissingFacts
|
||||
from graphiti_core.utils.maintenance.temporal_operations import (
|
||||
extract_edge_dates,
|
||||
get_edge_contradictions,
|
||||
|
|
@ -91,7 +93,7 @@ async def extract_edges(
|
|||
reflexion_iterations = 0
|
||||
while facts_missed and reflexion_iterations < MAX_REFLEXION_ITERATIONS:
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.extract_edges.edge(context)
|
||||
prompt_library.extract_edges.edge(context), response_model=ExtractedEdges
|
||||
)
|
||||
edges_data = llm_response.get('edges', [])
|
||||
|
||||
|
|
@ -100,7 +102,7 @@ async def extract_edges(
|
|||
reflexion_iterations += 1
|
||||
if reflexion_iterations < MAX_REFLEXION_ITERATIONS:
|
||||
reflexion_response = await llm_client.generate_response(
|
||||
prompt_library.extract_edges.reflexion(context)
|
||||
prompt_library.extract_edges.reflexion(context), response_model=MissingFacts
|
||||
)
|
||||
|
||||
missing_facts = reflexion_response.get('missing_facts', [])
|
||||
|
|
@ -317,7 +319,9 @@ async def dedupe_extracted_edge(
|
|||
'extracted_edges': extracted_edge_context,
|
||||
}
|
||||
|
||||
llm_response = await llm_client.generate_response(prompt_library.dedupe_edges.edge(context))
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.dedupe_edges.edge(context), response_model=EdgeDuplicate
|
||||
)
|
||||
|
||||
is_duplicate: bool = llm_response.get('is_duplicate', False)
|
||||
uuid: str | None = llm_response.get('uuid', None)
|
||||
|
|
@ -352,7 +356,7 @@ async def dedupe_edge_list(
|
|||
context = {'edges': [{'uuid': edge.uuid, 'fact': edge.fact} for edge in edges]}
|
||||
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.dedupe_edges.edge_list(context)
|
||||
prompt_library.dedupe_edges.edge_list(context), response_model=UniqueFacts
|
||||
)
|
||||
unique_edges_data = llm_response.get('unique_facts', [])
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,9 @@ from graphiti_core.helpers import MAX_REFLEXION_ITERATIONS
|
|||
from graphiti_core.llm_client import LLMClient
|
||||
from graphiti_core.nodes import EntityNode, EpisodeType, EpisodicNode
|
||||
from graphiti_core.prompts import prompt_library
|
||||
from graphiti_core.prompts.dedupe_nodes import NodeDuplicate
|
||||
from graphiti_core.prompts.extract_nodes import ExtractedNodes, MissedEntities
|
||||
from graphiti_core.prompts.summarize_nodes import Summary
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -42,7 +45,7 @@ async def extract_message_nodes(
|
|||
}
|
||||
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.extract_nodes.extract_message(context)
|
||||
prompt_library.extract_nodes.extract_message(context), response_model=ExtractedNodes
|
||||
)
|
||||
extracted_node_names = llm_response.get('extracted_node_names', [])
|
||||
return extracted_node_names
|
||||
|
|
@ -63,7 +66,7 @@ async def extract_text_nodes(
|
|||
}
|
||||
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.extract_nodes.extract_text(context)
|
||||
prompt_library.extract_nodes.extract_text(context), ExtractedNodes
|
||||
)
|
||||
extracted_node_names = llm_response.get('extracted_node_names', [])
|
||||
return extracted_node_names
|
||||
|
|
@ -81,7 +84,7 @@ async def extract_json_nodes(
|
|||
}
|
||||
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.extract_nodes.extract_json(context)
|
||||
prompt_library.extract_nodes.extract_json(context), ExtractedNodes
|
||||
)
|
||||
extracted_node_names = llm_response.get('extracted_node_names', [])
|
||||
return extracted_node_names
|
||||
|
|
@ -101,7 +104,7 @@ async def extract_nodes_reflexion(
|
|||
}
|
||||
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.extract_nodes.reflexion(context)
|
||||
prompt_library.extract_nodes.reflexion(context), MissedEntities
|
||||
)
|
||||
missed_entities = llm_response.get('missed_entities', [])
|
||||
|
||||
|
|
@ -273,9 +276,12 @@ async def resolve_extracted_node(
|
|||
}
|
||||
|
||||
llm_response, node_summary_response = await asyncio.gather(
|
||||
llm_client.generate_response(prompt_library.dedupe_nodes.node(context)),
|
||||
llm_client.generate_response(
|
||||
prompt_library.summarize_nodes.summarize_context(summary_context)
|
||||
prompt_library.dedupe_nodes.node(context), response_model=NodeDuplicate
|
||||
),
|
||||
llm_client.generate_response(
|
||||
prompt_library.summarize_nodes.summarize_context(summary_context),
|
||||
response_model=Summary,
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -294,7 +300,8 @@ async def resolve_extracted_node(
|
|||
summary_response = await llm_client.generate_response(
|
||||
prompt_library.summarize_nodes.summarize_pair(
|
||||
{'node_summaries': [extracted_node.summary, existing_node.summary]}
|
||||
)
|
||||
),
|
||||
response_model=Summary,
|
||||
)
|
||||
node = existing_node
|
||||
node.name = name
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@ from graphiti_core.edges import EntityEdge
|
|||
from graphiti_core.llm_client import LLMClient
|
||||
from graphiti_core.nodes import EpisodicNode
|
||||
from graphiti_core.prompts import prompt_library
|
||||
from graphiti_core.prompts.extract_edge_dates import EdgeDates
|
||||
from graphiti_core.prompts.invalidate_edges import InvalidatedEdges
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -38,7 +40,9 @@ async def extract_edge_dates(
|
|||
'previous_episodes': [ep.content for ep in previous_episodes],
|
||||
'reference_timestamp': current_episode.valid_at.isoformat(),
|
||||
}
|
||||
llm_response = await llm_client.generate_response(prompt_library.extract_edge_dates.v1(context))
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.extract_edge_dates.v1(context), response_model=EdgeDates
|
||||
)
|
||||
|
||||
valid_at = llm_response.get('valid_at')
|
||||
invalid_at = llm_response.get('invalid_at')
|
||||
|
|
@ -75,7 +79,9 @@ async def get_edge_contradictions(
|
|||
|
||||
context = {'new_edge': new_edge_context, 'existing_edges': existing_edge_context}
|
||||
|
||||
llm_response = await llm_client.generate_response(prompt_library.invalidate_edges.v2(context))
|
||||
llm_response = await llm_client.generate_response(
|
||||
prompt_library.invalidate_edges.v2(context), response_model=InvalidatedEdges
|
||||
)
|
||||
|
||||
contradicted_edge_data = llm_response.get('invalidated_edges', [])
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue