Skip to content

Commit

Permalink
feat: Add source URLs to assistant responses
Browse files Browse the repository at this point in the history
  • Loading branch information
StanGirard committed Apr 21, 2024
1 parent 1c7da75 commit 75a53ab
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,31 @@ def ask_question(self, body, brain_id, thread_ts, question=None):

logger.debug(question_response)
if "assistant" in question_response:
if "metadata" in question_response:
if "sources" in question_response["metadata"]:
sources = question_response["metadata"]["sources"][:3]
source_text = "\n".join(
[
f"- <{source['source_url']}|{source['name']}>"
for source in sources
]
)
question_response["assistant"] += f"\n\n*Sources:*\n{source_text}"

blocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": question_response["assistant"],
},
}
]

self.app.client.chat_postMessage(
channel=body["event"]["channel"],
text=question_response["assistant"],
blocks=blocks,
thread_ts=thread_ts,
)
else:
Expand Down Expand Up @@ -418,9 +440,31 @@ def handle_iteractive_request(self, payload):
channel=payload["channel"]["id"],
ts=message[0],
)
if "metadata" in question_response:
if "sources" in question_response["metadata"]:
sources = question_response["metadata"]["sources"]
source_text = "\n".join(
[
f"- <{source['source_url']}|{source['name']}>"
for source in sources
]
)
question_response["assistant"] += f"\n\n*Sources:*\n{source_text}"

blocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": question_response["assistant"],
},
}
]

self.app.client.chat_postMessage(
channel=payload["channel"]["id"],
text=question_response["assistant"],
blocks=blocks,
thread_ts=thread_ts,
)
self.set_brain_id(thread_ts, question_response.get("brain_id"))
Expand Down

0 comments on commit 75a53ab

Please sign in to comment.