Merge branch 'nate/web' into preview

This commit is contained in:
Nate Sesti 2023-11-09 17:46:35 -08:00
commit 92cf9892ed
4 changed files with 124 additions and 1 deletions

View file

@ -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())

View file

@ -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),
)
)

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"