Closed AkashBais closed 3 weeks ago
@AkashBais in the Prompt do not use query
but use question
and it should work:
CYPHER_GENERATION_TEMPLATE = '''Task:Generate Cypher statement to query a graph database.
Instructions:
Use only the provided relationship types and properties in the schema.
Do not use any other relationship types or properties that are not provided.
Schema:
{schema}
Note: Do not include any explanations or apologies in your responses.
Do not respond to any questions that might ask anything else than for you to construct a Cypher statement.
Do not include any text except the generated Cypher statement.
Examples: Here are a few examples of generated Cypher statements for particular questions:
Which sections talk about medication?
MATCH (n2:ner_entity)-[new_present_in:PRESENT_IN]->(n1:Chunk)
WHERE n2.name = 'medication'
RETURN DISTINCT n1.section as section_list
The question is:
{question} '''
CYPHER_GENERATION_PROMPT = PromptTemplate(
input_variables=['schema', 'question'],
template=CYPHER_GENERATION_TEMPLATE
)
# print(CYPHER_GENERATION_PROMPT.input_variables)
QA_CHAIN = GraphCypherQAChain.from_llm(
graph=return_graph(), # This is an internal method that returns a graph object
llm=llm,
verbose=True,
cypher_prompt=CYPHER_GENERATION_PROMPT,
)
QA_CHAIN.invoke(
input = {'question': query},
return_only_outputs=True,
)
@keenborder786 I did try that but i get the same error. I noticed that the implimentation of GraphCypherQAChain has query hardcoded in it as input key. "input_key: str = "query" #: :meta private:" This key is used to populate question variable in _call method that gets passed to the cypher_generation_chain question = inputs[self.input_key] '''some other code''' generated_cypher = self.cypher_generation_chain.run( {"question": question, "schema": self.graph_schema}, callbacks=callbacks )
PFA the stacktrace P.S: I made the change in Prompt and PromptTemplate too
ValueError Traceback (most recent call last)
[<ipython-input-7-21a65eed48c5>](https://localhost:8080/#) in <cell line: 1>()
----> 1 responce = rag.query(
2 query = 'Which sections talk about medication in the document?'
3 )
3 frames
[/content/RAG/KG_for_RAG/src/execute_rag.py](https://localhost:8080/#) in query(self, query)
337
338 if (self.query_type is not None) and (self.query_type.lower() in ['self_genrate','prompt']):
--> 339 answer = self.QA_CHAIN.invoke(
340 {"question": query,},
341 return_only_outputs=True,
[/usr/local/lib/python3.10/dist-packages/langchain/chains/base.py](https://localhost:8080/#) in invoke(self, input, config, **kwargs)
164 except BaseException as e:
165 run_manager.on_chain_error(e)
--> 166 raise e
167 run_manager.on_chain_end(outputs)
168
[/usr/local/lib/python3.10/dist-packages/langchain/chains/base.py](https://localhost:8080/#) in invoke(self, input, config, **kwargs)
152 )
153 try:
--> 154 self._validate_inputs(inputs)
155 outputs = (
156 self._call(inputs, run_manager=run_manager)
[/usr/local/lib/python3.10/dist-packages/langchain/chains/base.py](https://localhost:8080/#) in _validate_inputs(self, inputs)
282 missing_keys = set(self.input_keys).difference(inputs)
283 if missing_keys:
--> 284 raise ValueError(f"Missing some input keys: {missing_keys}")
285
286 def _validate_outputs(self, outputs: Dict[str, Any]) -> None:
ValueError: Missing some input keys: {'query'}
@keenborder786 Would appriciate any help or direction on this. Or just a list of plausible causes for this error or something to start looking into . Let me know .Thanks
For anyone looking for a solution. Step 1: Set the key for question in QA_CHAIN.invoke as query because thats the key that is hard coded in the GraphCypherQAChain and it checks for it's existance. Hence the error [Check the comment above]
Step 2: Set the input variable for the prompt and PromptTemplae as question because when QA_CHAIN.invoke calls it's internal _call method it dose so using
generated_cypher = self.cypher_generation_chain.run(
{"question": question, "schema": self.graph_schema}, callbacks=callbacks
)
And so the question passed along with the key query is passed down further as question
Checked other resources
Example Code
""" python CYPHER_GENERATION_TEMPLATE = '''Task:Generate Cypher statement to query a graph database. Instructions: Use only the provided relationship types and properties in the schema. Do not use any other relationship types or properties that are not provided. Schema: {schema} Note: Do not include any explanations or apologies in your responses. Do not respond to any questions that might ask anything else than for you to construct a Cypher statement. Do not include any text except the generated Cypher statement. Examples: Here are a few examples of generated Cypher statements for particular questions:
Which sections talk about medication? MATCH (n2:ner_entity)-[new_present_in:PRESENT_IN]->(n1:Chunk) WHERE n2.name = 'medication' RETURN DISTINCT n1.section as section_list
The question is: {query} '''
CYPHER_GENERATION_PROMPT = PromptTemplate( input_variables=['schema', 'query'], template=CYPHER_GENERATION_TEMPLATE )
QA_CHAIN = GraphCypherQAChain.from_llm( graph=return_graph(), # This is an internal method that returns a graph object llm=READER_LLM, verbose=True, cypher_prompt=CYPHER_GENERATION_PROMPT, ) QA_CHAIN.invoke( input = {'query': query}, return_only_outputs=True, ) """
query = 'Sample question?'
Error Message and Stack Trace (if applicable)
10 frames /content/RAG/KG_for_RAG/src/execute_rag.py in query(self, query) 336 337 if (self.query_type is not None) and (self.query_type.lower() in ['self_genrate','prompt']): --> 338 answer = self.QA_CHAIN.invoke( 339 input = {'query': query}, 340 return_only_outputs=True,
/usr/local/lib/python3.10/dist-packages/langchain/chains/base.py in invoke(self, input, config, **kwargs) 164 except BaseException as e: 165 run_manager.on_chain_error(e) --> 166 raise e 167 run_manager.on_chain_end(outputs) 168
/usr/local/lib/python3.10/dist-packages/langchain/chains/base.py in invoke(self, input, config, **kwargs) 154 self._validate_inputs(inputs) 155 outputs = ( --> 156 self._call(inputs, run_manager=run_manager) 157 if new_arg_supported 158 else self._call(inputs)
/usr/local/lib/python3.10/dist-packages/langchain_community/chains/graph_qa/cypher.py in _call(self, inputs, run_manager) 251 intermediate_steps: List = [] 252 --> 253 generated_cypher = self.cypher_generation_chain.run( 254 {"question": question, "schema": self.graph_schema}, callbacks=callbacks 255 )
/usr/local/lib/python3.10/dist-packages/langchain_core/_api/deprecation.py in warning_emitting_wrapper(*args, kwargs) 168 warned = True 169 emit_warning() --> 170 return wrapped(*args, *kwargs) 171 172 async def awarning_emitting_wrapper(args: Any, kwargs: Any) -> Any:
/usr/local/lib/python3.10/dist-packages/langchain/chains/base.py in run(self, callbacks, tags, metadata, *args, **kwargs) 598 if len(args) != 1: 599 raise ValueError("
run
supports only one positional argument.") --> 600 return self(args[0], callbacks=callbacks, tags=tags, metadata=metadata)[ 601 _output_key 602 ]/usr/local/lib/python3.10/dist-packages/langchain_core/_api/deprecation.py in warning_emitting_wrapper(*args, kwargs) 168 warned = True 169 emit_warning() --> 170 return wrapped(*args, *kwargs) 171 172 async def awarning_emitting_wrapper(args: Any, kwargs: Any) -> Any:
/usr/local/lib/python3.10/dist-packages/langchain/chains/base.py in call(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info) 381 } 382 --> 383 return self.invoke( 384 inputs, 385 cast(RunnableConfig, {k: v for k, v in config.items() if v is not None}),
/usr/local/lib/python3.10/dist-packages/langchain/chains/base.py in invoke(self, input, config, **kwargs) 164 except BaseException as e: 165 run_manager.on_chain_error(e) --> 166 raise e 167 run_manager.on_chain_end(outputs) 168
/usr/local/lib/python3.10/dist-packages/langchain/chains/base.py in invoke(self, input, config, **kwargs) 152 ) 153 try: --> 154 self._validate_inputs(inputs) 155 outputs = ( 156 self._call(inputs, run_manager=run_manager)
/usr/local/lib/python3.10/dist-packages/langchain/chains/base.py in _validate_inputs(self, inputs) 282 missing_keys = set(self.input_keys).difference(inputs) 283 if missing_keys: --> 284 raise ValueError(f"Missing some input keys: {missing_keys}") 285 286 def _validate_outputs(self, outputs: Dict[str, Any]) -> None:
ValueError: Missing some input keys: {'query'}
Description
As seen I am passing my input question with the dictionary key 'query' to the GraphCypherQAChain. It still keeps poping the error "Missing some input keys: {'query'} "
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies