-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add assistant with internet access (#13)
* Update dependencies * Add the `tools` module * Add `CONTEXTQA_AGENT_TEMPLATE` * Add extra setting no enable internet access for the assistant * Update `searcher` tool * Added exception handling * It searches 5 different sites sequentially and chooses the one that meets the criteria of containing more than 100 words * Added logger * Update memory configuration based on the `enable_internet_access` flag * Add `CONTEXTQA_AGENT_TEMPLATE` and a custom prefix for the agent * Add `get_llm_assistant` function This function returns an assistant with or without internet access * Update `ChatCard` Now urls in markdown format are rendered properly * Add switch to enable internet access * Update memory chat Now some of the "generated" parameters depend on the `internet_access` flag * Update `qa_service` function * The `qa` endpoint now expects the optional `internet_access` flag * Added chat memory usage depending on that flag * Add `internetEnabled` state * Update Dialog messages * Fix bug Added `v-if` usage so the internet switch is only available in conversations with no context * Update home's welcome text
- Loading branch information
1 parent
8034c8f
commit ffb65d0
Showing
12 changed files
with
397 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import requests | ||
from bs4 import BeautifulSoup | ||
from googlesearch import search | ||
from langchain.agents import Tool | ||
|
||
from contextqa import get_logger | ||
|
||
|
||
LOGGER = get_logger() | ||
|
||
|
||
def _get_content(url: str, timeout: int = 5) -> bytes: | ||
resp = requests.get(url=url, timeout=timeout) | ||
resp.raise_for_status() | ||
return resp.content | ||
|
||
|
||
def _js_disable_message(text: str) -> bool: | ||
return "JavaScript is disabled in this browser" in text | ||
|
||
|
||
def _searcher(search_term: str): | ||
"""Search for the provided search term in Google Search when the assistant could not find information to answer""" | ||
results = search(search_term, num_results=5) | ||
for url in results: | ||
try: | ||
html_content = BeautifulSoup(_get_content(url), "html.parser") | ||
except requests.exceptions.HTTPError as ex: | ||
LOGGER.warning("Got HTTP error when requesting %s. Error %s", url, ex) | ||
continue | ||
else: | ||
html_text = html_content.text | ||
if _js_disable_message(html_text): | ||
LOGGER.warning("%s detected JavaScript not available", url) | ||
continue | ||
words = html_text.replace("\n", "").split() | ||
if len(words) > 100: | ||
LOGGER.info("Chosen url: %s", url) | ||
text = "I got the response:" + " ".join(words[:500]) | ||
break | ||
return text | ||
|
||
|
||
searcher = Tool( | ||
name="Crawl google for external knowledge", | ||
func=_searcher, | ||
description=( | ||
"useful for when the assitant does not know the answer of the human input and it needs external knowledge" | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.