Extract Knowledge Retrieval's information and Output in Markdown (2/2)
๐ป Dify has a Knowledge Retrieval node, but most people don't know how to make the data of it appear at the end of the conversation, in the middle, or elsewhere. Through this Code Node, you can accurately extract them and output them at the appropriate location according to your needs. โ๏ธ Please note that many people have encountered the "Hallucination" problem, which outputs incorrect information, such as non-existent links. In fact, this is not a problem with Dify, essentially, this is the problem with Large Language Model. If you look carefully at the Knowledge Retrieval node, you will find that it is easy to hit the knowledge base fragment if your configuration is correct (for example, the Enhance / Rewrite Query node in front). ๐จโ๐ป At this time, using the Large Language Model to output is equivalent to outputting the URL word by word, and there is a certain error rate at this time; ๐ Conversely, it is better to use Code Node to extract the URL and insert it into the appropriate position using an error-free program. Not only is it fast, but itโs also unmistakable. ๐ Remember, the documents' name in the example below are the URLs, so if you want to achieve the same effect, you can use url as the document name. This is the second one, for output in markdown. (2/2) Thanks for Claude โค๏ธ
๐ป Dify has a Knowledge Retrieval node, but most people don't know how to make the data of it appear at the end of the conversation, in the middle, or elsewhere. Through this Code Node, you can accurately extract them and output them at the appropriate location according to your needs. โ๏ธ Please note that many people have encountered the "Hallucination" problem, which outputs incorrect information, such as non-existent links. In fact, this is not a problem with Dify, essentially, this is the problem with Large Language Model. If you look carefully at the Knowledge Retrieval node, you will find that it is easy to hit the knowledge base fragment if your configuration is correct (for example, the Enhance / Rewrite Query node in front). ๐จโ๐ป At this time, using the Large Language Model to output is equivalent to outputting the URL word by word, and there is a certain error rate at this time; ๐ Conversely, it is better to use Code Node to extract the URL and insert it into the appropriate position using an error-free program. Not only is it fast, but itโs also unmistakable. ๐ Remember, the documents' name in the example below are the URLs, so if you want to achieve the same effect, you can use url as the document name. This is the second one, for output in markdown. (2/2) Thanks for Claude โค๏ธ
import json
def main(arg1: str) -> dict:
# Parse the input string as JSON
data = json.loads(arg1)
# Sort the data by score in descending order
sorted_data = sorted(data, key=lambda x: float(x.get('score', 0)), reverse=True)
# Format the result with the desired numbering format, removing duplicates
result = ""
seen_links = set()
counter = 1
for item in sorted_data:
document_name = item.get('document_name', '')
if document_name not in seen_links:
result += f"[{counter}] [{document_name}]({document_name})\n"
seen_links.add(document_name)
counter += 1
return {"result": result.strip()}
import json
def main(arg1: str) -> dict:
# Parse the input string as JSON
data = json.loads(arg1)
# Sort the data by score in descending order
sorted_data = sorted(data, key=lambda x: float(x.get('score', 0)), reverse=True)
# Format the result with the desired numbering format, removing duplicates
result = ""
seen_links = set()
counter = 1
for item in sorted_data:
document_name = item.get('document_name', '')
if document_name not in seen_links:
result += f"[{counter}] [{document_name}]({document_name})\n"
seen_links.add(document_name)
counter += 1
return {"result": result.strip()}
Sponsor
Related Tutorial
๐ Nothing Here Now ~
๐ Nothing Here Now ~
๐ Nothing Here Now ~
Subscribe to our newsletter ๐คฉ
We regularly list new indie products & makers. Get them in your inbox!
We regularly list new indie products & makers. Get them in your inbox!
We regularly list new indie products & makers. Get them in your inbox!