跳到主要內容
Open In ColabOpen on GitHub

Dappier

Dappier 將任何 LLM 或您的 Agentic AI 連接到來自可信任來源的即時、權利已清除的專有數據,使您的 AI 成為任何方面的專家。我們的專業模型包括即時網路搜尋、新聞、體育、金融股市數據、加密貨幣數據以及來自優質發行商的獨家內容。在我們的市場 marketplace.dappier.com 探索各種數據模型。

Dappier 提供豐富、可立即提示且在上下文相關的數據字串,並針對與 LangChain 的無縫整合進行了最佳化。無論您是要建構對話式 AI、推薦引擎還是智慧搜尋,Dappier 與 LLM 無關的 RAG 模型都能確保您的 AI 可以存取經過驗證的最新數據,而無需建構和管理自己的檢索管線的複雜性。

DappierRetriever

這將幫助您開始使用 Dappier 檢索器。如需所有 DappierRetriever 功能和配置的詳細文件,請前往 API 參考文件

設定

安裝 langchain-dappier 並設定環境變數 DAPPIER_API_KEY

pip install -U langchain-dappier
export DAPPIER_API_KEY="your-api-key"

我們還需要設定您的 Dappier API 憑證,這可以在 Dappier 網站上產生。

您可以前往 Dappier 市場找到支援的資料模型。

如果您想要從個別查詢取得自動追蹤,您也可以取消註解下方內容來設定您的 LangSmith API 金鑰

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

安裝

此檢索器位於 langchain-dappier 套件中

%pip install -qU langchain-dappier

實例化

  • data_model_id: str 資料模型 ID,以 dm_ 開頭。您可以在 Dappier 市場找到可用的資料模型 ID。
  • k: int 要傳回的文件數量。
  • ref: Optional[str] 顯示 AI 建議的網站網域。
  • num_articles_ref: int 來自指定 ref 網域的最小文章數量。其餘文章將來自 RAG 模型中的其他網站。
  • search_algorithm: Literal[ "most_recent", "most_recent_semantic", "semantic", "trending" ] 用於檢索文章的搜尋演算法。
  • api_key: Optional[str] 用於與 Dappier API 互動的 API 金鑰。
from langchain_dappier import DappierRetriever

retriever = DappierRetriever(data_model_id="dm_01jagy9nqaeer9hxx8z1sk1jx6")

使用方式

query = "latest tech news"

retriever.invoke(query)
[Document(metadata={'title': 'Man shot and killed on Wells Street near downtown Fort Wayne', 'author': 'Gregg Montgomery', 'source_url': 'https://www.wishtv.com/news/indiana-news/man-shot-dies-fort-wayne-december-25-2024/', 'image_url': 'https://images.dappier.com/dm_01jagy9nqaeer9hxx8z1sk1jx6/fort-wayne-police-department-vehicle-via-Flickr_.jpg?width=428&height=321', 'pubdata': 'Thu, 26 Dec 2024 01:00:33 +0000'}, page_content='A man was shot and killed on December 25, 2024, in Fort Wayne, Indiana, near West Fourth and Wells streets. Police arrived shortly after 6:30 p.m. following reports of gunfire and found the victim in the 1600 block of Wells Street, where he was pronounced dead. The area features a mix of businesses, including a daycare and restaurants.\n\nAs of the latest updates, police have not provided details on the safety of the area, potential suspects, or the motive for the shooting. Authorities are encouraging anyone with information to reach out to the Fort Wayne Police Department or Crime Stoppers.'),
Document(metadata={'title': 'House cat dies from bird flu in pet food, prompting recall', 'author': 'Associated Press', 'source_url': 'https://www.wishtv.com/news/business/house-cat-bird-flu-pet-food-recall/', 'image_url': 'https://images.dappier.com/dm_01jagy9nqaeer9hxx8z1sk1jx6/BACKGROUND-Northwest-Naturals-cat-food_.jpg?width=428&height=321', 'pubdata': 'Wed, 25 Dec 2024 23:12:41 +0000'}, page_content='An Oregon house cat has died after eating pet food contaminated with the H5N1 bird flu virus, prompting a nationwide recall of Northwest Naturals\' 2-pound Feline Turkey Recipe raw frozen pet food. The Oregon Department of Agriculture confirmed that the strictly indoor cat contracted the virus solely from the food, which has "best if used by" dates of May 21, 2026, and June 23, 2026. \n\nThe affected product was distributed across several states, including Arizona, California, and Florida, as well as British Columbia, Canada. Consumers are urged to dispose of the recalled food and seek refunds. This incident raises concerns about the spread of bird flu and its potential impact on domestic animals, particularly as California has declared a state of emergency due to the outbreak affecting various bird species.'),
Document(metadata={'title': '20 big cats die from bird flu at Washington sanctuary', 'author': 'Nic F. Anderson, CNN', 'source_url': 'https://www.wishtv.com/news/national/bird-flu-outbreak-wild-felid-center-2024/', 'image_url': 'https://images.dappier.com/dm_01jagy9nqaeer9hxx8z1sk1jx6/BACKGROUND-Amur-Bengal-tiger-at-Wild-Felid-Advocacy-Center-of-Washington-FB-post_.jpg?width=428&height=321', 'pubdata': 'Wed, 25 Dec 2024 23:04:34 +0000'}, page_content='The Wild Felid Advocacy Center in Washington state has experienced a devastating bird flu outbreak, resulting in the deaths of 20 big cats, over half of its population. The first death was reported around Thanksgiving, affecting various species, including cougars and a tiger mix. The sanctuary is currently under quarantine, closed to the public, and working with animal health officials to disinfect enclosures and implement prevention strategies.\n\nAs the situation unfolds, the Washington Department of Fish and Wildlife has noted an increase in bird flu cases statewide, including infections in cougars. While human infections from bird flu through contact with mammals are rare, the CDC acknowledges the potential risk. The sanctuary hopes to reopen in the new year, focusing on the recovery of the remaining animals and taking measures to prevent further outbreaks, marking an unprecedented challenge in its 20-year history.')]

在鏈中使用

與其他檢索器一樣,DappierRetriever 可以透過整合到 LLM 應用程式中。

我們將需要 LLM 或聊天模型

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
API 參考:ChatOpenAI
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(
"What are the key highlights and outcomes from the latest events covered in the article?"
)
"The key highlights and outcomes from the latest events covered in the article include:\n\n1. An Israeli airstrike in Gaza killed five journalists from Al-Quds Today Television, leading to condemnation from their outlet and raising concerns about violence against media professionals in the region.\n2. The Committee to Protect Journalists reported that since October 7, 2023, at least 141 journalists have been killed in the region, marking the deadliest period for journalists since 1992, with the majority being Palestinians in Gaza.\n3. A man was shot and killed in Fort Wayne, Indiana, with police not providing details on suspects, motive, or the safety of the area.\n4. An Oregon house cat died after eating pet food contaminated with the H5N1 bird flu virus, leading to a nationwide recall of Northwest Naturals' Feline Turkey Recipe raw frozen pet food and raising concerns about the spread of bird flu among domestic animals."

API 參考

如需所有 DappierRetriever 功能和配置的詳細文件,請前往 API 參考文件


此頁面是否對您有幫助?