成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Auto-Retrieval: RAG的智能進(jìn)化

人工智能
我們借助LlamaCloud來(lái)實(shí)現(xiàn),主要通過(guò)在LlamaCloud檢索器上設(shè)置一個(gè)Auto-Retrieval功能。在高層次上,我們的自動(dòng)檢索函數(shù)使用一個(gè)調(diào)用函數(shù)的LLM來(lái)推斷用戶查詢的元數(shù)據(jù)過(guò)濾器——比僅僅使用原始語(yǔ)義查詢產(chǎn)生更精確和相關(guān)的檢索結(jié)果。

Auto-Retrieval是一種高級(jí)的RAG技術(shù),它在啟動(dòng)向量數(shù)據(jù)庫(kù)檢索之前使用Agent LLM動(dòng)態(tài)推斷元數(shù)據(jù)過(guò)濾器參數(shù)和語(yǔ)義查詢,而不是將用戶查詢直接發(fā)送到向量數(shù)據(jù)庫(kù)檢索接口(例如密集向量搜索)的樸素RAG。您可以將其視為查詢擴(kuò)展/重寫(xiě)的一種形式,也可以將其視為函數(shù)調(diào)用的一種特定形式;后文我們將給出實(shí)現(xiàn)邏輯和代碼。達(dá)到效果如下:

用戶輸入

Give me a summary of the SWE-bench paper

推理結(jié)果

改寫(xiě)查詢: summary of the SWE-bench paper
過(guò)濾參數(shù): {"filters": [{"key": "file_name", "value": "swebench.pdf", "operator": "=="}], "condition": "and"}

實(shí)現(xiàn)步驟

我們借助LlamaCloud來(lái)實(shí)現(xiàn),主要通過(guò)在LlamaCloud檢索器上設(shè)置一個(gè)Auto-Retrieval功能。在高層次上,我們的自動(dòng)檢索函數(shù)使用一個(gè)調(diào)用函數(shù)的LLM來(lái)推斷用戶查詢的元數(shù)據(jù)過(guò)濾器——比僅僅使用原始語(yǔ)義查詢產(chǎn)生更精確和相關(guān)的檢索結(jié)果。
  • 定義一個(gè)自定義prompt來(lái)生成元數(shù)據(jù)過(guò)濾器
  • 給定一個(gè)用戶查詢,首先執(zhí)行塊級(jí)檢索,從檢索到的塊中動(dòng)態(tài)召回元數(shù)據(jù)。
  • 在auto-retrieval prompt中注入元數(shù)據(jù)作為少量示例。目的是向LLM展示現(xiàn)有的、相關(guān)的元數(shù)據(jù)值示例,以便LLM可以推斷出正確的元數(shù)據(jù)過(guò)濾器。

文檔級(jí)檢索器返回整個(gè)文件級(jí)別的文檔,而塊級(jí)檢索器返回特定的塊,實(shí)現(xiàn)如此簡(jiǎn)單。

from llama_index.indices.managed.llama_cloud import LlamaCloudIndex
import os


index = LlamaCloudIndex(
  name="research_papers_page",
  project_name="llamacloud_demo",
  api_key=os.environ["LLAMA_CLOUD_API_KEY"]
)


doc_retriever = index.as_retriever(
    retrieval_mode="files_via_content",
    # retrieval_mode="files_via_metadata",
    files_top_k=1
)


chunk_retriever = index.as_retriever(
    retrieval_mode="chunks",
    rerank_top_n=5
)

代碼實(shí)現(xiàn)

接下來(lái)我們將根據(jù)上面的流程給出實(shí)現(xiàn)代碼:

from llama_index.core.prompts import ChatPromptTemplate
from llama_index.core.vector_stores.types import VectorStoreInfo, VectorStoreQuerySpec, MetadataInfo, MetadataFilters
from llama_index.core.retrievers import BaseRetriever
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.core import Response


import json


SYS_PROMPT = """\
Your goal is to structure the user's query to match the request schema provided below.
You MUST call the tool in order to generate the query spec.


<< Structured Request Schema >>
When responding use a markdown code snippet with a JSON object formatted in the \
following schema:


{schema_str}


The query string should contain only text that is expected to match the contents of \
documents. Any conditions in the filter should not be mentioned in the query as well.


Make sure that filters only refer to attributes that exist in the data source.
Make sure that filters take into account the descriptions of attributes.
Make sure that filters are only used as needed. If there are no filters that should be \
applied return [] for the filter value.\


If the user's query explicitly mentions number of documents to retrieve, set top_k to \
that number, otherwise do not set top_k.


The schema of the metadata filters in the vector db table is listed below, along with some example metadata dictionaries from relevant rows.
The user will send the input query string.


Data Source:
```json
{info_str}
```


Example metadata from relevant chunks:
{example_rows}


"""


example_rows_retriever = index.as_retriever(
    retrieval_mode="chunks",
    rerank_top_n=4
)


def get_example_rows_fn(**kwargs):
    """Retrieve relevant few-shot examples."""
    query_str = kwargs["query_str"]
    nodes = example_rows_retriever.retrieve(query_str)
    # get the metadata, join them
    metadata_list = [n.metadata for n in nodes]


    return "\n".join([json.dumps(m) for m in metadata_list])
        
    


# TODO: define function mapping for `example_rows`.
chat_prompt_tmpl = ChatPromptTemplate.from_messages(
    [
        ("system", SYS_PROMPT),
        ("user", "{query_str}"),
    ],
    function_mappings={
        "example_rows": get_example_rows_fn
    }
)




## NOTE: this is a dataclass that contains information about the metadata
vector_store_info = VectorStoreInfo(
    content_info="contains content from various research papers",
    metadata_info=[
        MetadataInfo(
            name="file_name",
            type="str",
            description="Name of the source paper",
        ),
    ],
)


def auto_retriever_rag(query: str, retriever: BaseRetriever) -> Response:
    """Synthesizes an answer to your question by feeding in an entire relevant document as context."""
    print(f"> User query string: {query}")
    # Use structured predict to infer the metadata filters and query string.
    query_spec = llm.structured_predict(
        VectorStoreQuerySpec,
        chat_prompt_tmpl,
        info_str=vector_store_info.json(indent=4),
        schema_str=VectorStoreQuerySpec.schema_json(indent=4),
        query_str=query
    )
    # build retriever and query engine
    filters = MetadataFilters(filters=query_spec.filters) if len(query_spec.filters) > 0 else None
    print(f"> Inferred query string: {query_spec.query}")
    if filters:
        print(f"> Inferred filters: {filters.json()}")
    query_engine = RetrieverQueryEngine.from_args(
        retriever, 
        llm=llm,
        response_mode="tree_summarize"
    )
    # run query
    return query_engine.query(query_spec.query)

效果展示

auto_doc_rag("Give me a summary of the SWE-bench paper") 
print(str(response))
> User query string: Give me a summary of the SWE-bench paper
> Inferred query string: summary of the SWE-bench paper
> Inferred filters: {"filters": [{"key": "file_name", "value": "swebench.pdf", "operator": "=="}], "condition": "and"}
The construction of SWE-Bench involves a three-stage pipeline:


1. **Repo Selection and Data Scraping**: Pull requests (PRs) are collected from 12 popular open-source Python repositories on GitHub, resulting in approximately 90,000 PRs. These repositories are chosen for their popularity, better maintenance, clear contributor guidelines, and extensive test coverage.


2. **Attribute-Based Filtering**: Candidate tasks are created by selecting merged PRs that resolve a GitHub issue and make changes to the test files of the repository. This indicates that the user likely contributed tests to check whether the issue has been resolved.


3. **Execution-Based Filtering**: For each candidate task, the PR’s test content is applied, and the associated test results are logged before and after the PR’s other content is applied. Tasks are filtered out if they do not have at least one test where its status changes from fail to pass or if they result in installation or runtime errors.


Through these stages, the original 90,000 PRs are filtered down to 2,294 task


責(zé)任編輯:武曉燕 來(lái)源: 哎呀AIYA
相關(guān)推薦

2023-07-07 07:06:47

2023-06-06 10:19:28

2019-03-08 09:54:29

華為

2024-09-03 11:31:04

2021-09-07 10:06:00

人工智能機(jī)器學(xué)習(xí)技術(shù)

2025-06-25 01:00:00

智能體蒸餾AI

2025-03-26 09:30:00

AI人工智能AIOps

2019-05-30 20:54:05

華為

2023-05-26 14:02:29

AI智能

2019-04-28 09:19:33

存儲(chǔ)

2019-05-09 22:10:36

AI

2024-04-08 07:52:24

2021-12-10 18:53:43

百度數(shù)字化轉(zhuǎn)型

2013-01-10 09:58:50

CESCES 2013智能手表

2019-03-21 19:21:33

華為中國(guó)生態(tài)伙伴大會(huì)201智能進(jìn)化

2019-03-21 18:59:18

華為中國(guó)生態(tài)伙伴大會(huì)201智能進(jìn)化

2022-12-22 13:42:14

華為云
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 一级一级一级毛片 | 国产精品影视在线观看 | 国产精品视频导航 | 羞羞视频免费观看入口 | www.久久99| 亚洲高清在线播放 | 国产精品久久久久久久久久久久久 | 一级欧美一级日韩片免费观看 | 中文字幕免费视频 | 91福利电影在线观看 | 色噜噜色综合 | 99中文字幕 | 欧美激情精品久久久久久 | 国产精品久久久久久一级毛片 | 日韩精品在线一区 | 中文字字幕一区二区三区四区五区 | 国产精品揄拍一区二区 | 精品乱码一区二区 | 亚洲三级在线观看 | 一区二区三区四区视频 | 国产三区在线观看视频 | 成人av片在线观看 | 欧美日韩高清一区二区三区 | 91久久精品视频 | 日韩成人在线播放 | 日批的视频 | 久久久高清 | 欧美综合一区二区 | 欧美日韩福利视频 | 黄色av网站免费看 | 精品免费视频 | h视频免费观看 | 91精品国产自产精品男人的天堂 | 精品视频在线一区 | 7777精品伊人久久精品影视 | 中文字幕在线一区二区三区 | 日本字幕在线观看 | 午夜视频网站 | 91精品国产综合久久小仙女图片 | 久久综合伊人 | 欧美视频精品 |