File size: 3,296 Bytes
4a27de3
 
a008b1a
9a1b773
4a27de3
 
 
 
 
 
8534dd6
 
 
a008b1a
4a27de3
 
 
 
 
 
 
 
 
 
 
4583ebb
8534dd6
4a27de3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8534dd6
4a27de3
 
 
4583ebb
8534dd6
4a27de3
 
 
8534dd6
4a27de3
8534dd6
4a27de3
8534dd6
 
 
4a27de3
 
 
 
 
 
 
 
8534dd6
 
 
4a27de3
 
 
 
8534dd6
4a27de3
 
 
8534dd6
4a27de3
 
 
 
 
 
 
 
 
 
 
a008b1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import requests
from dotenv import load_dotenv
from typing import Optional
from brave_ai import BraveAIWrapper
import os

# Load environment variables from .env file
load_dotenv()

# Get API keys from environment variables
YOU_COM_API_KEY = os.getenv("YOU_API_KEY")
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
PERPLEXITY_API_KEY = os.getenv("PPLX_API_KEY")
BRAVE_API_KEY = os.getenv("BRAVE_AI_API_KEY")

def query_you_com(query):
    headers = {"X-API-Key": YOU_COM_API_KEY}
    params = {"query": query}
    try:
        response = requests.get(
            "https://api.ydc-index.io/rag",  # Verify the correctness of the API endpoint
            params=params,
            headers=headers,
        )
        response.raise_for_status()  # Raises an HTTPError if the response code was unsuccessful
        resp = response.json()
        return resp["answer"]
    except requests.exceptions.HTTPError as http_err:
        return f"HTTP error occurred: {http_err}"
    except Exception as err:
        return f"An error occurred: {err}"


def query_tavily(query):
    payload = {
        "api_key": TAVILY_API_KEY,
        "query": query,
        "search_depth": "basic",
        "include_answer": True,
        "include_images": False,
        "include_raw_content": False,
        "max_results": 1,
        "include_domains": [],
        "exclude_domains": [],
    }
    response = requests.post("https://api.tavily.com/search", json=payload)
    if response.status_code == 200:
        resp = response.json()
        return resp["answer"]
    else:
        return f"Request failed with status code: {response.status_code}"


def query_perplexity(query):
    url = "https://api.perplexity.ai/chat/completions"
    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization": f"Bearer {PERPLEXITY_API_KEY}",
    }
    data = {
        "model": "llama-3-sonar-large-32k-online",
        "stream": False,
        "max_tokens": 1024,
        "frequency_penalty": 1,
        "temperature": 0.0,
        "messages": [
            {"role": "system", "content": "Be precise and concise in your responses."},
            {"role": "user", "content": query},
        ],
    }
    response = requests.post(url, headers=headers, json=data)
    if response.status_code == 200:
        result = response.json()
        return result["choices"][0]["message"]["content"]
    else:
        return f"Request failed with status code: {response.status_code}"


def ProcessQuestion(question, model):
    if model == "You.com":
        return query_you_com(question)
    elif model == "Tavily.com":
        return query_tavily(question)
    elif model == "Perplexity.ai":
        return query_perplexity(question)
    elif model == "Brave.com":
        return query_brave(question)
    else:
        return "Model not supported"


def query_brave(query: str) -> Optional[str]:
    """
    Get a summary for the given query using BraveAIWrapper.

    Args:
        query (str): The search query.
        api_key (str): The API key for Brave Search.

    Returns:
        Optional[str]: Summarized result or None if an error occurs.
    """
    brave_ai = BraveAIWrapper(api_key=BRAVE_API_KEY)
    summary = brave_ai.get_and_summarize(query)
    return summary