Skip to content

The agent engineering platform.

experimental: LLMGraphTransformer add missing conditional adding restrictions to prompts for LLM that do not support function calling (#22793)

Jordy Jackson Antunes da Rochaa50eabbd4866
diff --git a/libs/experimental/langchain_experimental/graph_transformers/llm.py b/libs/experimental/langchain_experimental/graph_transformers/llm.py
index 16023ab452..8886a55068 100644
--- a/libs/experimental/langchain_experimental/graph_transformers/llm.py
+++ b/libs/experimental/langchain_experimental/graph_transformers/llm.py
@@ -242,22 +242,31 @@ def create_unstructured_prompt(
system_message = SystemMessage(content=system_prompt)
parser = JsonOutputParser(pydantic_object=UnstructuredRelation)
+ human_string_parts = [
+ "Based on the following example, extract entities and "
+ "relations from the provided text.\n\n",
+ "Use the following entity types, don't use other entity "
+ "that is not defined below:"
+ "# ENTITY TYPES:"
+ "{node_labels}"
+ if node_labels
+ else "",
+ "Use the following relation types, don't use other relation "
+ "that is not defined below:"
+ "# RELATION TYPES:"
+ "{rel_types}"
+ if rel_types
+ else "",
+ "Below are a number of examples of text and their extracted "
+ "entities and relationships."
+ "{examples}\n"
+ "For the following text, extract entities and relations as "
+ "in the provided example."
+ "{format_instructions}\nText: {input}",
+ ]
+ human_prompt_string = "\n".join(filter(None, human_string_parts))
human_prompt = PromptTemplate(
- template="""Based on the following example, extract entities and
-relations from the provided text.\n\n
-Use the following entity types, don't use other entity that is not defined below:
-# ENTITY TYPES:
-{node_labels}
-
-Use the following relation types, don't use other relation that is not defined below:
-# RELATION TYPES:
-{rel_types}
-
-Below are a number of examples of text and their extracted entities and relationships.
-{examples}
-
-For the following text, extract entities and relations as in the provided example.
-{format_instructions}\nText: {input}""",
+ template=human_prompt_string,
input_variables=["input"],
partial_variables={
"format_instructions": parser.get_format_instructions(),