Adding hyperlinks to the used sources in response
# 🤝help
s
Hey! I want bot to provide links to the sources it got the response from. So for example if it generates response from knowledge base (which is the website) I want it to give links to the blog posts or product pages he used to generate this response. What I did, is I created the AI response with the command "Respond to queries as if you were an employee and customer advisor at *, a construction wholesale store. Provide the customer with a link to a specific page on the website www.**.pl from which you gather the information." Additionaly, I've added "Execute code" (as seen on the screen shot). Few times it worked, but it is not woring anymore and it gives only text answers. How I can go further with this?
a
You're on the right track using code to generate the list of citations. I've gotten good results with this code, try it out and modidy it to your bot if you need to.
Copy code
js
let citations = []
workflow.category = ''
turn.KnowledgeAgent.citations.map((citation) => {
  try {
    if (/Page Title:/.test(citation)) {
      var page_title = /Page Title: (.*?)\(https/.exec(citation)[1].trim()
      var page_url = /(?:\((http[^\)]*)\))/.exec(citation)[1]
      citations.push(`\n* [${page_title}](${page_url})`)
    } 
    citations = _.uniq(citations)
    if (citations.length > 5) {
      citations = citations.slice(0, 4)
    }
    workflow.citationMessage = 'Learn more at the links below:\n' + citations.join()
  } catch (e) {
    console.error(e)
  }
})
After running the code just send
@citationMessage
in a text card
s
great! thank you @acceptable-kangaroo-64719 , works like a charm. How did you come up with this code? I would love to know how to get those skills for future.
a
Glad to know it worked! These skills come from spending too much time doing data processing. The more projects you do that involve large datasets, the better your skills at handling text data will become. Good luck!
q
Hi! I would love to use this (and understand haha) but I'm getting this error: "unexpected identifier 'citations' " any ideas?
Any ideas @acceptable-kangaroo-64719 ?
a
Where in the code is your error?
f
Do you have knowledge answering enabled? The code Gordy sent works perfectly to parse through the knowledge base answer that is generated, as it contains a section of json labeled citations that contains the links. If you have knowledge answering not enabled in that node, or if you have it set to manual responses only, it will give you issues
q
I have the knowledge agent enabled and it's not set as manual
This is the error message from the logs: errorError executing action "inline-ins-714e335c1f.js" in flow:Main:node:Test [SyntaxError, Unexpected identifier 'citations']
f
Did you change the code or add anything to it
q
The error happens on the execute code command
Oh, I found the error, apologies.
I must have formatted wrong or mistyped when I copied the text, went for copy paste and it works now, Thank you!
730 Views