Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional instruction parameter to llm graph transformer #23

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions libs/experimental/langchain_experimental/graph_transformers/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,23 @@
"Adhere to the rules strictly. Non-compliance will result in termination."
)

default_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
system_prompt,
),
(
"human",

def get_default_prompt(
additional_instructions: str = "",
) -> ChatPromptTemplate:
return ChatPromptTemplate.from_messages(
[
("system", system_prompt),
(
"Tip: Make sure to answer in the correct format and do "
"human",
additional_instructions
+ " Tip: Make sure to answer in the correct format and do "
"not include any explanations. "
"Use the given format to extract information from the "
"following input: {input}"
"following input: {input}",
),
),
]
)
]
)


def _get_additional_info(input_type: str) -> str:
Expand Down Expand Up @@ -213,6 +213,7 @@ def create_unstructured_prompt(
node_labels: Optional[List[str]] = None,
rel_types: Optional[Union[List[str], List[Tuple[str, str, str]]]] = None,
relationship_type: Optional[str] = None,
additional_instructions: Optional[str] = "",
) -> ChatPromptTemplate:
node_labels_str = str(node_labels) if node_labels else ""
if rel_types:
Expand Down Expand Up @@ -259,7 +260,8 @@ def create_unstructured_prompt(
"that entity. The knowledge graph should be coherent and easily "
"understandable, so maintaining consistency in entity references is "
"crucial.",
"IMPORTANT NOTES:\n- Don't add any explanation and text.",
"IMPORTANT NOTES:\n- Don't add any explanation and text. ",
additional_instructions,
]
system_prompt = "\n".join(filter(None, base_string_parts))

Expand Down Expand Up @@ -290,7 +292,8 @@ def create_unstructured_prompt(
else "",
"Below are a number of examples of text and their extracted "
"entities and relationships."
"{examples}\n"
"{examples}\n",
additional_instructions,
"For the following text, extract entities and relations as "
"in the provided example."
"{format_instructions}\nText: {input}",
Expand Down Expand Up @@ -739,6 +742,8 @@ class LLMGraphTransformer:
bypass the use of structured output functionality of the language model.
If set to True, the transformer will not use the language model's native
function calling capabilities to handle structured output. Defaults to False.
additional_instructions (str): Allows you to add additional instructions
to the prompt without having to change the whole prompt.

Example:
.. code-block:: python
Expand All @@ -765,6 +770,7 @@ def __init__(
node_properties: Union[bool, List[str]] = False,
relationship_properties: Union[bool, List[str]] = False,
ignore_tool_usage: bool = False,
additional_instructions: str = "",
) -> None:
# Validate and check allowed relationships input
self._relationship_type = validate_and_get_relationship_type(
Expand Down Expand Up @@ -798,7 +804,10 @@ def __init__(
"Please install it with `pip install json-repair`."
)
prompt = prompt or create_unstructured_prompt(
allowed_nodes, allowed_relationships, self._relationship_type
allowed_nodes,
allowed_relationships,
self._relationship_type,
additional_instructions,
)
self.chain = prompt | llm
else:
Expand All @@ -816,7 +825,7 @@ def __init__(
self._relationship_type,
)
structured_llm = llm.with_structured_output(schema, include_raw=True)
prompt = prompt or default_prompt
prompt = prompt or get_default_prompt(additional_instructions)
self.chain = prompt | structured_llm

def process_response(
Expand Down
Loading