Skip to content

Commit

Permalink
Fix chatbot answers presentation (#345)
Browse files Browse the repository at this point in the history
* remove versions from pillow and make psycopg install the binary version

* fix prompt client edge case bug

* lint

* fixed how the chatbot presents answers, tickets #331 and #342

* lint

* fix smartlink bug

* lint
  • Loading branch information
northdpole authored Aug 29, 2023
1 parent 929c5a0 commit 789a895
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
31 changes: 27 additions & 4 deletions application/frontend/src/pages/chatbot/chatbot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,25 @@ export const Chatbot = () => {
}

function displayDocument(d: Document) {
var link = '/node/' + d.doctype.toLowerCase() + '/' + d.name;
if (d.section) {
link = link + '/section/' + d.section;
} else {
link = link + '/sectionid/' + d.sectionID;
}
return (
<a href={d.hyperlink} target="_blank">
*Reference: The above answer was based on the {d.name} section of{' '}
{d.section ? d.section : d.sectionID};
</a>
<p>
<p>
*Reference: The above answer was based on:
<a href={d.hyperlink} target="_blank">
{' '}
{d.name} section: {d.section ? d.section : d.sectionID};
</a>
</p>
<p>
You can find more information about {d.name} <a href={link}> on its OpenCRE page</a>
</p>
</p>
);
}

Expand Down Expand Up @@ -372,6 +386,15 @@ export const Chatbot = () => {
<div className="table-container mt-5 ms-5 d-none">
<div className="table-content bg-light shadow p-3" id="table-content"></div>
</div>
<div className="chatbot">
<i>
ChatCRE uses Google's PALM2 LLM, you can find the code for OpenCRE in
https://github.com/owaps/OpenCRE. Your question travels to Heroku (OpenCRE hosting provider)
and then to GCP over a protected connection. Your data is never stored in the OpenCRE
servers, you can start a new session by refreshing your page. The OpenCRE team has taken all
reasonable precautions we could think off to protect your privacy and security.
</i>
</div>
</Grid>
</Container>
</Grid.Column>
Expand Down
9 changes: 4 additions & 5 deletions application/prompt_client/prompt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,9 @@ def generate_text(self, prompt: str) -> Dict[str, str]:
timestamp = datetime.now().strftime("%I:%M:%S %p")
if not prompt:
return {"response": "", "table": "", "timestamp": timestamp}
logger.info(f"getting embeddings for {prompt}")
logger.debug(f"getting embeddings for {prompt}")
question_embedding = self.ai_client.get_text_embeddings(prompt)
logger.info(f"retrieved embeddings for {prompt}")
logger.debug(f"retrieved embeddings for {prompt}")

# Find the closest area in the existing embeddings
closest_id, similarity = self.get_id_of_most_similar_node_paginated(
Expand All @@ -426,8 +426,7 @@ def generate_text(self, prompt: str) -> Dict[str, str]:
)
closest_object = self.database.get_node_by_db_id(closest_id)
answer = ""

logger.info(
logger.debug(
f"The prompt {prompt}, was most similar to object \n{closest_object}\n, with similarity:{similarity}"
)
closest_content = ""
Expand All @@ -449,7 +448,7 @@ def generate_text(self, prompt: str) -> Dict[str, str]:
else:
return {"response": "An adequate answer could not be found", "table": [""]}

logger.info(f"retrieved completion for {prompt}")
logger.debug(f"retrieved completion for {prompt}")
table = [closest_object]
result = f"Answer: {answer}"
return {"response": result, "table": table}
11 changes: 5 additions & 6 deletions application/tests/web_main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def test_smartlink(self) -> None:
for head in response.headers:
if head[0] == "Location":
location = head[1]
self.assertEqual(location, "/node/Standard/CWE/sectionid/456")
self.assertEqual(location, "/node/standard/CWE/sectionid/456")
self.assertEqual(302, response.status_code)

response = client.get(
Expand All @@ -554,9 +554,10 @@ def test_smartlink(self) -> None:
for head in response.headers:
if head[0] == "Location":
location = head[1]
self.assertEqual(location, "/node/Standard/ASVS/section/v0.1.2")
self.assertEqual(location, "/node/standard/ASVS/section/v0.1.2")
self.assertEqual(302, response.status_code)

# negative test, this cwe does not exist, therefore there is nowhere to redirect to
response = client.get(
"/smartlink/standard/CWE/999",
headers={"Content-Type": "application/json"},
Expand All @@ -565,7 +566,5 @@ def test_smartlink(self) -> None:
for head in response.headers:
if head[0] == "Location":
location = head[1]
self.assertEqual(
location, "https://cwe.mitre.org/data/definitions/999.html"
)
self.assertEqual(302, response.status_code)
self.assertEqual(location, "")
self.assertEqual(404, response.status_code)
7 changes: 4 additions & 3 deletions application/web/web_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,9 @@ def smartlink(
opt_version = request.args.get("version")
# match ntype to the credoctypes case-insensitive
typ = [t for t in defs.Credoctypes if t.value.lower() == ntype.lower()]
doctype = None
if typ:
ntype = typ[0]
doctype = typ[0]

page = 1
items_per_page = 1
Expand All @@ -312,7 +313,7 @@ def smartlink(
page=int(page),
items_per_page=int(items_per_page),
version=opt_version,
ntype=ntype,
ntype=doctype,
)

if not nodes or len(nodes) == 0:
Expand All @@ -322,7 +323,7 @@ def smartlink(
page=int(page),
items_per_page=int(items_per_page),
version=opt_version,
ntype=ntype,
ntype=doctype,
)
found_section_id = True
if nodes and len(nodes[0].links):
Expand Down

0 comments on commit 789a895

Please sign in to comment.