Kuzu Agent - Issue Capturing Steps #24215
Replies: 2 comments
-
Hey there, @harryaultdev! I'm Dosu, a bot here to help you solve bugs, answer questions, and become a contributor. I'm here to assist you while waiting for a human maintainer. To capture the Cypher generated by
Step 1: Create a Custom Callback Handlerfrom langchain_core.callbacks import BaseCallbackHandler
class CustomCallbackHandler(BaseCallbackHandler):
def __init__(self):
self.generated_cypher = None
def on_text(self, text: str, **kwargs: Any) -> None:
if "Generated Cypher:" in text:
self.generated_cypher = text.split("Generated Cypher:")[1].strip() Step 2: Modify the
|
Beta Was this translation helpful? Give feedback.
-
This is probably a hacky workaround ... but if you inspect the ...
"""Generate Cypher statement, use it to look up in db and answer question."""
_run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager()
callbacks = _run_manager.get_child()
question = inputs[self.input_key]
self.graph.refresh_schema()
generated_cypher = self.cypher_generation_chain.run(
{"question": question, "schema": self.graph.get_schema}, callbacks=callbacks
)
# Extract Cypher code if it is wrapped in triple backticks
# with the language marker "cypher"
generated_cypher = remove_prefix(extract_cypher(generated_cypher), "cypher")
_run_manager.on_text("Generated Cypher:", end="\n", verbose=self.verbose)
_run_manager.on_text(generated_cypher, color="green", end="\n", verbose=self.verbose)
context = self.graph.query(generated_cypher)
_run_manager.on_text("Full Context:", end="\n", verbose=self.verbose)
_run_manager.on_text(str(context), color="green", end="\n", verbose=self.verbose)
result = self.qa_chain(
{"question": question, "context": context},
callbacks=callbacks,
)
return {self.output_key: result[self.qa_chain.output_key]} You can kinda get the generated cypher by doing: # note: the run method was deprecated in langchain 0.1.0, just illustrating with an example
generated_cypher = self.cypher_generation_chain.run(
{"question": question, "schema": self.graph.get_schema}, callbacks=callbacks
) So if you have something like: # Create the KuzuQAChain with verbosity enabled to see the generated Cypher queries
chain = KuzuQAChain.from_llm(
llm=ChatOpenAI(model="gpt-4o-mini", temperature=0.3, api_key=os.getenv("OPENAI_API_KEY")),
graph=graph,
verbose=True,
allow_dangerous_requests=True,
)
# with the user's question and the KuzuGraph instance, then you can do
generated_cypher_result = chain.cypher_generation_chain.invoke(
{"question": question, "schema": graph.get_schema}, callbacks=callbacks
)
# Clean up the cypher result by removing markdown formatting
# note this might not handle multiline queries well
cypher_query = generated_cypher_result['text'].strip("```").strip("cypher").strip()
print(cypher_query) I would prefer if the API just provided an optional result with the Cypher query though |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
Expecting to see the intermediate steps but they're not being returned? Alternatively is there a callback handler I can use? Or am I just being an idiot...
I basically just want to capture the Cypher generated which I can see in the logs but I need to store it in a variable to return in an API response.
Verbose Logging below.
System Info
System Information
Package Information
Packages not installed (Not Necessarily a Problem)
The following packages were not found:
Beta Was this translation helpful? Give feedback.
All reactions