Skip to content

Commit

Permalink
feat: add discussurl example
Browse files Browse the repository at this point in the history
  • Loading branch information
av committed Sep 25, 2024
1 parent fbe9ff4 commit 650cda1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions boost/src/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def plain(self):
def history(self):
return self.tail.history()

def text(self):
# __str__ already does exactly this
return f"{self}"

def __create_node(self, **kwargs):
NodeType = self.chat_node_type
return NodeType(**kwargs)
Expand Down
40 changes: 40 additions & 0 deletions boost/src/custom_modules/discussurl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import re
import requests

url_regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
prompt = """
<instruction>
Your task is to fulfill the user's request by discussing provided content.
</instruction>
<content>
{content}
</content>
<request>
{request}
</request>
""".strip()

ID_PREFIX = "discussurl"


async def apply(chat, llm):
text = chat.text()
urls = re.findall(url_regex, text)

# No - URLs - proceed as usual
if len(urls) == 0:
return await llm.stream_final_completion()

# Yes - URLs - read them
content = ""
for url in urls:
await llm.emit_status(f"Reading {url[0]}...")
content += requests.get(url[0]).text

await llm.stream_final_completion(
prompt=prompt,
content=content,
request=chat.tail.content,
)

0 comments on commit 650cda1

Please sign in to comment.