🚧 WIP on web retrieval

This commit is contained in:
Nate Sesti 2023-11-06 15:29:54 -08:00
parent d63ae2ece3
commit 8e8939577b
5 changed files with 128 additions and 14 deletions

View file

@ -0,0 +1,39 @@
import asyncio
import json
import requests
from bs4 import BeautifulSoup
from html2text import html2text
async def get_results(q: str):
url = "https://google.serper.dev/search"
payload = json.dumps({"q": q})
headers = {
"X-API-KEY": "2a419454db38b1267362ca5bb084f94d9b99a131",
"Content-Type": "application/json",
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json()["organic"]
async def get_link_contents(url: str):
response = requests.get(url)
return html2text(response.text)
# soup = BeautifulSoup(response.text, "html.parser")
# return soup.prettify()
async def main():
results = await get_results(
"socket.io not connecting to python server from kotlin client site:stackoverflow.com"
)
for result in results:
contents = await get_link_contents(result["link"])
print(f"{result['title']}\n\n{contents}\n\n---\n\n")
if __name__ == "__main__":
asyncio.run(main())

View file

@ -0,0 +1,63 @@
import requests
import json
from bs4 import BeautifulSoup
from ...libs.llm.base import CompletionOptions
from .chat import SimpleChatStep
from ...core.main import Step
from ...core.sdk import ContinueSDK
PROMPT = """
{sources}
Use the above sources to answer this question. Provide links to the sources in markdown whenever possible:
{user_input}
"""
async def get_results(q: str):
url = "https://google.serper.dev/search"
payload = json.dumps({"q": q})
headers = {
"X-API-KEY": "2a419454db38b1267362ca5bb084f94d9b99a131",
"Content-Type": "application/json",
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json()
async def get_link_contents(url: str):
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
return soup.get_text()
class WebSearchChatStep(Step):
user_input: str
name: str = "Chat using Web Search for reference"
async def run(self, sdk: ContinueSDK):
model = sdk.models.chat.model
# if model == "gpt-4":
# model = "gpt-4-32k" # Not publicly available yet?
if model == "gpt-3.5-turbo":
model = "gpt-3.5-turbo-16k"
sources = []
results = await get_results(self.user_input)
for result in results:
contents = await get_link_contents(result["link"])
sources.append(f"{result['title']}\n\n{contents}\n\n---\n\n")
await sdk.run_step(
SimpleChatStep(
name="Answer Question",
completion_options=CompletionOptions(model=model),
prompt=PROMPT.format(user_input=self.user_input, sources=sources),
)
)

View file

@ -93,11 +93,11 @@ class Window:
if self._error_loading_config is not None:
await self.ide.setFileOpen(getConfigFilePath())
async def index():
async for progress in build_index(self.ide, self.config):
await self.gui.send_indexing_progress(progress)
# async def index():
# async for progress in build_index(self.ide, self.config):
# await self.gui.send_indexing_progress(progress)
create_async_task(index(), on_error=self.ide.on_error)
# create_async_task(index(), on_error=self.ide.on_error)
# Start models
await self.config.models.start(
@ -115,15 +115,15 @@ class Window:
dev_data_logger.setup(self.config.user_token, self.config.data_server_url)
# Load documents into the search index
await self.context_manager.start(
self.config.context_providers
+ [
HighlightedCodeContextProvider(ide=self.ide),
FileContextProvider(workspace_dir=self.ide.workspace_directory),
],
self.ide,
only_reloading=only_reloading,
)
# await self.context_manager.start(
# self.config.context_providers
# + [
# HighlightedCodeContextProvider(ide=self.ide),
# FileContextProvider(workspace_dir=self.ide.workspace_directory),
# ],
# self.ide,
# only_reloading=only_reloading,
# )
async def onFileSavedCallback(filepath: str, contents: str):
if filepath.endswith(".continue/config.py") or filepath.endswith(

13
server/poetry.lock generated
View file

@ -1160,6 +1160,17 @@ files = [
{file = "h11-0.14.0.tar.gz", hash = "sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d"},
]
[[package]]
name = "html2text"
version = "2020.1.16"
description = "Turn HTML into equivalent Markdown-structured text."
optional = false
python-versions = ">=3.5"
files = [
{file = "html2text-2020.1.16-py3-none-any.whl", hash = "sha256:c7c629882da0cf377d66f073329ccf34a12ed2adf0169b9285ae4e63ef54c82b"},
{file = "html2text-2020.1.16.tar.gz", hash = "sha256:e296318e16b059ddb97f7a8a1d6a5c1d7af4544049a01e261731d2d5cc277bbb"},
]
[[package]]
name = "httpcore"
version = "0.18.0"
@ -4194,4 +4205,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p
[metadata]
lock-version = "2.0"
python-versions = "^3.8.1"
content-hash = "5471373c2174145df18c185131db4afedd948236c0176196ea5ae9ccaefe2719"
content-hash = "db5551a0cc4e046dc56b65742db55623f86e0adbb826905a750c5b8caafd98b4"

View file

@ -41,6 +41,7 @@ rich = "^13.6.0"
python-socketio = "^5.10.0"
pathspec = "^0.11.2"
tqdm = "^4.66.1"
html2text = "^2020.1.16"
[tool.poetry.scripts]
typegen = "continuedev.models.generate_json_schema:main"