File size: 2,098 Bytes
d102e03
 
 
 
 
 
8d0a0d3
 
 
 
 
 
 
d102e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfa084c
 
 
 
d102e03
 
 
8d0a0d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from dataclasses import dataclass
from datetime import datetime
from typing import Generator

import requests

INSTRUCTIONS = "Instructions: " \
               "Using the provided web search results, " \
               "write a comprehensive reply to the given query. " \
               "Make sure to cite results using [[number](URL)] notation after the reference. " \
               "If the provided search results refer to multiple subjects with the same name, " \
               "write separate answers for each subject."


@dataclass
class SearchResult:
    def __init__(self, title: str, body: str, url: str):
        self.title = title
        self.body = body
        self.url = url


def get_web_search_results(
        prompt: str,
        num_results: int,
) -> Generator[SearchResult, None, None]:
    """Adds web search results to the prompt.
    Using """
    url = f"https://ddg-webapp-aagd.vercel.app/search?max_results={num_results}&q=${prompt}"
    response = requests.get(url)
    if response.status_code != 200:
        raise ValueError(f"Failed to get web search results for prompt: {prompt}")
    results = response.json()
    for result in results:
        yield SearchResult(
            title=result["title"],
            body=result["body"],
            url=result["href"],
        )


def format_search_result(search_result: Generator[SearchResult, None, None]) -> str:
    """Formats a search result to be added to the prompt."""
    ans = ""
    for i, result in enumerate(search_result):
        ans += f"[{i}] {result.body}\nURL: {result.url}\n\n"
    return ans


def rewrite_prompt(
        prompt: str,
) -> str:
    """Rewrites the prompt by adding web search results to it."""
    raw_results = get_web_search_results(
        prompt=prompt,
        num_results=5,
    )
    formatted_results = "Web search results: " + format_search_result(raw_results)
    formatted_date = "Current date: " + datetime.now().strftime("%d/%m/%Y")
    formatted_prompt = f"Query: {prompt}"
    return "\n".join([formatted_results, formatted_date, INSTRUCTIONS, formatted_prompt])