跳到内容

Valyu

ValyuToolSpec #

基类: BaseToolSpec

Valyu工具规范。

源代码位于 llama-index-integrations/tools/llama-index-tools-valyu/llama_index/tools/valyu/base.py
 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
class ValyuToolSpec(BaseToolSpec):
    """Valyu tool spec."""

    spec_functions = [
        "context",
    ]

    def __init__(
        self,
        api_key: str,
        verbose: bool = False,
        max_price: Optional[float] = 100,
    ) -> None:
        """Initialize with parameters."""
        from valyu import Valyu

        self.client = Valyu(api_key=api_key)
        self._verbose = verbose
        self._max_price = max_price

    def context(
        self,
        query: str,
        search_type: str = "both",
        data_sources: Optional[List[str]] = None,
        max_num_results: int = 10,
        max_price: Optional[float] = None,
        query_rewrite: bool = True,
        similarity_threshold: float = 0.5,
    ) -> List[Document]:
        """
        Find relevant programmaticly licensed proprietary content and the web to answer your query.

        Args:
            query (str): The question or topic to search for
            search_type (str): Type of sources to search - "proprietary", "web", or "both"
            data_sources (Optional[List[str]]): Specific indexes to query from
            max_num_results (int): Maximum number of results to return. Defaults to 10
            max_price (Optional[float]): Maximum price per content in PCM. Defaults to 100
            query_rewrite (bool): Whether to rewrite the query to improve results. Defaults to True
            similarity_threshold (float): Minimum similarity score for results. Defaults to 0.5

        Returns:
            List[Document]: List of Document objects containing the search results

        """
        if max_price is None:
            max_price = self._max_price

        response = self.client.context(
            query=query,
            search_type=search_type,
            data_sources=data_sources,
            max_num_results=max_num_results,
            max_price=max_price,
            query_rewrite=query_rewrite,
            similarity_threshold=similarity_threshold,
        )

        if self._verbose:
            print(f"[Valyu Tool] Response: {response}")

        return [
            Document(
                text=result.content,
                metadata={
                    "title": result.title,
                    "url": result.url,
                    "source": result.source,
                    "price": result.price,
                },
            )
            for result in response.results
        ]

context #

context(query: str, search_type: str = 'both', data_sources: Optional[List[str]] = None, max_num_results: int = 10, max_price: Optional[float] = None, query_rewrite: bool = True, similarity_threshold: float = 0.5) -> List[Document]

查找相关的程序化许可专有内容和网络内容来回答您的查询。

参数

名称 类型 描述 默认值
query str

要搜索的问题或主题

必填
search_type str

要搜索的来源类型 - "proprietary"(专有)、"web"(网络)或 "both"(两者)

'both'
data_sources 可选[列表[str]]

要查询的特定索引

max_num_results int

要返回的最大结果数。默认为 10

10
max_price 可选[浮点数]

每条内容的最大价格(单位PCM)。默认为 100

query_rewrite bool

是否重写查询以改进结果。默认为 True

True
similarity_threshold float

结果所需的最小相似度得分。默认为 0.5

0.5

返回值

类型 描述
列表[Document]

List[Document]: 包含搜索结果的Document对象列表

源代码位于 llama-index-integrations/tools/llama-index-tools-valyu/llama_index/tools/valyu/base.py
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
def context(
    self,
    query: str,
    search_type: str = "both",
    data_sources: Optional[List[str]] = None,
    max_num_results: int = 10,
    max_price: Optional[float] = None,
    query_rewrite: bool = True,
    similarity_threshold: float = 0.5,
) -> List[Document]:
    """
    Find relevant programmaticly licensed proprietary content and the web to answer your query.

    Args:
        query (str): The question or topic to search for
        search_type (str): Type of sources to search - "proprietary", "web", or "both"
        data_sources (Optional[List[str]]): Specific indexes to query from
        max_num_results (int): Maximum number of results to return. Defaults to 10
        max_price (Optional[float]): Maximum price per content in PCM. Defaults to 100
        query_rewrite (bool): Whether to rewrite the query to improve results. Defaults to True
        similarity_threshold (float): Minimum similarity score for results. Defaults to 0.5

    Returns:
        List[Document]: List of Document objects containing the search results

    """
    if max_price is None:
        max_price = self._max_price

    response = self.client.context(
        query=query,
        search_type=search_type,
        data_sources=data_sources,
        max_num_results=max_num_results,
        max_price=max_price,
        query_rewrite=query_rewrite,
        similarity_threshold=similarity_threshold,
    )

    if self._verbose:
        print(f"[Valyu Tool] Response: {response}")

    return [
        Document(
            text=result.content,
            metadata={
                "title": result.title,
                "url": result.url,
                "source": result.source,
                "price": result.price,
            },
        )
        for result in response.results
    ]