diff --git a/server/continuedev/plugins/steps/test.py b/server/continuedev/plugins/steps/test.py new file mode 100644 index 000000000..c054c634c --- /dev/null +++ b/server/continuedev/plugins/steps/test.py @@ -0,0 +1,44 @@ +import asyncio +import json +import requests +from bs4 import BeautifulSoup +from html2text import html2text +from dotenv import load_dotenv +import os + +load_dotenv() +SERPER_API_KEY = os.getenv("SERPER_API_KEY") + + +async def get_results(q: str): + url = "https://google.serper.dev/search" + + payload = json.dumps({"q": q}) + headers = { + "X-API-KEY": SERPER_API_KEY, + "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()) diff --git a/server/continuedev/plugins/steps/web.py b/server/continuedev/plugins/steps/web.py new file mode 100644 index 000000000..a4ac4f567 --- /dev/null +++ b/server/continuedev/plugins/steps/web.py @@ -0,0 +1,67 @@ +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 +from dotenv import load_dotenv +import os + +load_dotenv() +SERPER_API_KEY = os.getenv("SERPER_API_KEY") + +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": SERPER_API_KEY, + "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), + ) + ) diff --git a/server/poetry.lock b/server/poetry.lock index c070827cc..88e090644 100644 --- a/server/poetry.lock +++ b/server/poetry.lock @@ -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" diff --git a/server/pyproject.toml b/server/pyproject.toml index 85a2a8690..5b40b17eb 100644 --- a/server/pyproject.toml +++ b/server/pyproject.toml @@ -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"