跳到主要內容

LinkupSearchRetriever

Linkup 提供一個 API,用於將 LLM 連接到網路和 Linkup Premium Partner 來源。

這將幫助您開始使用 LinkupSearchRetriever retriever。有關所有 LinkupSearchRetriever 功能和配置的詳細文檔,請前往 API 參考

Integration details(整合細節)

Retriever(檢索器)Source(來源)Package(套件)
LinkupSearchRetrieverWeb and partner sources(網路和合作夥伴來源)langchain-linkup

Setup(設定)

要使用 Linkup 提供者,您需要有效的 API 金鑰,您可以通過註冊 here 找到。然後,您可以將其設定為 LINKUP_API_KEY 環境變數。對於下面的鏈示例,您還需要將 OpenAI API 金鑰設定為 OPENAI_API_KEY 環境變數,您也可以在這裡執行此操作

# import os
# os.environ["LINKUP_API_KEY"] = "" # Fill with your API key
# os.environ["OPENAI_API_KEY"] = "" # Fill with your API key

如果您想從個別查詢中獲得自動追蹤,您也可以通過取消註釋下方來設定您的 LangSmith API 金鑰

# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"

Installation(安裝)

This retriever lives in the langchain-linkup package(此檢索器位於 langchain-linkup 套件中)

%pip install -qU langchain-linkup

Instantiation(實例化)

Now we can instantiate our retriever(現在我們可以實例化我們的檢索器)

from langchain_linkup import LinkupSearchRetriever

retriever = LinkupSearchRetriever(
depth="deep", # "standard" or "deep"
linkup_api_key=None, # API key can be passed here or set as the LINKUP_API_KEY environment variable
)

Usage(用法)

query = "Who won the latest US presidential elections?"

retriever.invoke(query)
[Document(metadata={'name': 'US presidential election results 2024: Harris vs. Trump | Live maps ...', 'url': 'https://www.reuters.com/graphics/USA-ELECTION/RESULTS/zjpqnemxwvx/'}, page_content='Updated results from the 2024 election for the US president. Reuters live coverage of the 2024 US President, Senate, House and state governors races.'),
Document(metadata={'name': 'Election 2024: Presidential results - CNN', 'url': 'https://www.cnn.com/election/2024/results/president'}, page_content='View maps and real-time results for the 2024 US presidential election matchup between former President Donald Trump and Vice President Kamala Harris. For more ...'),
Document(metadata={'name': 'Presidential Election 2024 Live Results: Donald Trump wins - NBC News', 'url': 'https://www.nbcnews.com/politics/2024-elections/president-results'}, page_content='View live election results from the 2024 presidential race as Kamala Harris and Donald Trump face off. See the map of votes by state as results are tallied.'),
Document(metadata={'name': '2024 President Election - Live Results | RealClearPolitics', 'url': 'https://www.realclearpolitics.com/elections/live_results/2024/president/'}, page_content='Latest Election 2024 Results • President • United States • Tuesday November 3rd • Presidential Election Details'),
Document(metadata={'name': 'Live: Presidential Election Results 2024 : NPR', 'url': 'https://apps.npr.org/2024-election-results/'}, page_content='Presidential race ratings are based on NPR analysis. Maps do not shade in until 50% of the estimated vote is in for a given state, to mitigate flutuations in early returns . 2024 General Election Results'),
Document(metadata={'name': '2024 US Presidential Election Results: Live Map - Bloomberg.com', 'url': 'https://www.bloomberg.com/graphics/2024-us-election-results/'}, page_content='US Presidential Election Results November 5, 2024. Bloomberg News is reporting live election results in the presidential race between Democratic Vice President Kamala Harris and her Republican ...'),
Document(metadata={'name': 'Presidential Election Results 2024: Electoral Votes & Map by State ...', 'url': 'https://www.politico.com/2024-election/results/president/'}, page_content='Live 2024 Presidential election results, maps and electoral votes by state. POLITICO’s real-time coverage of 2024 races for President, Senate, House and Governor.'),
Document(metadata={'name': 'US Presidential Election Results 2024 - BBC News', 'url': 'https://www.bbc.com/news/election/2024/us/results'}, page_content='Kamala Harris of the Democrat party has 74,498,303 votes (48.3%) Donald Trump of the Republican party has 76,989,499 votes (49.9%) This map of the US states was filled in as presidential results ...'),
Document(metadata={'name': 'Election Results 2024: Live Map - Races by State - POLITICO', 'url': 'https://www.politico.com/2024-election/results/'}, page_content='Live 2024 election results and maps by state. POLITICO’s real-time coverage of 2024 races for President, Senate, House and Governor.'),
Document(metadata={'name': '2024 U.S. Presidential Election: Live Results and Maps - USA TODAY', 'url': 'https://www.usatoday.com/elections/results/2024-11-05/president'}, page_content='See who is winning in the Nov. 5, 2024 U.S. Presidential election nationwide with real-time results and state-by-state maps.'),
Document(metadata={'name': 'Presidential Election 2024 Live Results: Donald Trump winsNBC News LogoSearchSearchNBC News LogoMSNBC LogoToday Logo', 'url': 'https://www.nbcnews.com/politics/2024-elections/president-results'}, page_content="Profile\n\nSections\n\nLocal\n\ntv\n\nFeatured\n\nMore From NBC\n\nFollow NBC News\n\nnews Alerts\n\nThere are no new alerts at this time\n\n2024 President Results: Trump wins\n==================================\n\nDonald Trump has secured more than the 270 Electoral College votes needed to secure the presidency, NBC News projects.\n\nRaces to watch\n--------------\n\nAll Presidential races\n----------------------\n\nElection Night Coverage\n-----------------------\n\n### China competition should be top priority for Trump, Sullivan says, as Biden and Xi prepare for final meeting\n\n### Jim Himes says 'truth and analysis are not what drive’ Gabbard and Gaetz\n\n### Trump praises RFK Jr. in Mar-a-Lago remarks\n\n### Trump announces North Dakota Gov. Doug Burgum as his pick for interior secretary\n\n### House Ethics Committee cancels meeting at which Gaetz probe was on the agenda\n\n### Trump picks former Rep. Doug Collins for veterans affairs secretary\n\n### Trump to nominate his criminal defense lawyer for deputy attorney general\n\n### From ‘brilliant’ to ‘dangerous’: Mixed reactions roll in after Trump picks RFK Jr. for top health post\n\n### Donald Trump Jr. says he played key role in RFK Jr., Tulsi Gabbard picks\n\n### Jared Polis offers surprising words of support for RFK Jr. pick for HHS secretary\n\nNational early voting\n---------------------\n\n### 88,233,886 mail-in and early in-person votes cast nationally\n\n### 65,676,748 mail-in and early in-person votes requested nationally\n\nPast Presidential Elections\n---------------------------\n\n### Vote Margin by State in the 2020 Presidential Election\n\nCircle size represents the number electoral votes in that state.\n\nThe expected vote is the total number of votes that are expected in a given race once all votes are counted. This number is an estimate and is based on several different factors, including information on the number of votes cast early as well as information provided to our vote reporters on Election Day from county election officials. The figure can change as NBC News gathers new information.\n\n**Source**: [National Election Pool (NEP)](https://www.nbcnews.com/politics/2024-elections/how-election-data-is-collected )\n\n2024 election results\n---------------------\n\nElection Night Coverage\n-----------------------\n\n### China competition should be top priority for Trump, Sullivan says, as Biden and Xi prepare for final meeting\n\n### Jim Himes says 'truth and analysis are not what drive’ Gabbard and Gaetz\n\n### Trump praises RFK Jr. in Mar-a-Lago remarks\n\n©\xa02024 NBCUniversal Media, LLC")]

Use within a chain(在鏈中使用)

Like other retrievers, LinkupSearchRetriever can be incorporated into LLM applications via chains.(與其他檢索器一樣,LinkupSearchRetriever 可以通過 整合到 LLM 應用程式中。)

We will need a LLM or chat model(我們將需要一個 LLM 或聊天模型)

import ChatModelTabs from "@theme/ChatModelTabs";

<ChatModelTabs customVarName="llm" />
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough

prompt = ChatPromptTemplate.from_template(
"""Answer the question based only on the context provided.

Context: {context}

Question: {question}"""
)


def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)


chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
chain.invoke("Who won the 3 latest US presidential elections?")
'The 3 latest US presidential elections were won by Joe Biden in 2020, Donald Trump in 2016, and Barack Obama in 2012.'

API reference(API 參考)

For detailed documentation of all LinkupSearchRetriever features and configurations head to the API reference.(有關所有 LinkupSearchRetriever 功能和配置的詳細文檔,請前往 API 參考。)


Was this page helpful?(此頁面是否有幫助?)