国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Elasticsearch:使用 Elasticsearch 矢量搜索和 FastAPI 構(gòu)建文本搜索應用程序

這篇具有很好參考價值的文章主要介紹了Elasticsearch:使用 Elasticsearch 矢量搜索和 FastAPI 構(gòu)建文本搜索應用程序。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Elasticsearch:使用 Elasticsearch 矢量搜索和 FastAPI 構(gòu)建文本搜索應用程序

在我的文章 “Elastic:開發(fā)者上手指南” 的 “NLP - 自然語言處理及矢量搜索”,我對 Elastic Stack 所提供的矢量搜索有大量的描述。其中很多的方法需要使用到 huggingface.co 及 Elastic 的機器學習。這個對于許多的開發(fā)者來說,意味著付費使用。在那些方案里,帶有機器學習的 inference processor 是收費的。還有那個上傳的 eland 也是收費的。

在今天的文章中,我們來介紹另外一種方法來進行矢量搜素。我們繞過使用 eland 來進行上傳模型。取而代之的是使用 Python 應用來上傳我們已經(jīng)生成好的 dense_vector 字段值。?我們將首先使用數(shù)據(jù)攝取腳本將數(shù)據(jù)攝取到 Elasticsearch 中。 該腳本將使用本地托管的 Elasticsearch 和 SentenceTransformer 庫連接到 Elasticsearch 并執(zhí)行文本嵌入。

在下面的展示中,我使用最新的 Elastic Stack 8.8.1 來進行展示,盡管它適用于其他版本的 Elastic Stack 8.x 版本。

ingest.py

from elasticsearch import Elasticsearch
from sentence_transformers import SentenceTransformer

USERNAME = "elastic"
PASSWORD = "z5nxTriCD4fi7jSS=GFM"
ELATICSEARCH_ENDPOINT = "https://localhost:9200"
CERT_FINGERPRINT = "783663875df7ae1daf3541ab293d8cd48c068b3dbc2d9dd6fa8a668289986ac2"

# Connect to Elasticsearch
es = Elasticsearch(ELATICSEARCH_ENDPOINT, 
                   ssl_assert_fingerprint = (CERT_FINGERPRINT),
                   basic_auth=(USERNAME, PASSWORD),
                   verify_certs = False)
resp = es.info()
print(resp)

# Index name
index_name = "test1"

# Example data
data = [
    {"id": 1, "text": "The sun slowly set behind the mountains, casting a golden glow across the landscape. The air was crisp and cool, a gentle breeze rustling through the leaves of the trees. Birds chirped in the distance, their melodic songs filling the air. As I walked along the winding path, I couldn't help but marvel at the beauty of nature surrounding me. The scent of wildflowers wafted through the air, intoxicating and refreshing. It was a moment of tranquility, a moment to escape from the chaos of everyday life and immerse myself in the serenity of the natural world."},
    {"id": 2, "text": "The bustling city streets were filled with the sound of car horns and chatter. People hurried past, their faces lost in a sea of anonymity. Skyscrapers towered above, their reflective glass windows shimmering in the sunlight. The aroma of street food filled the air, mingling with the scent of exhaust fumes. Neon signs flashed with vibrant colors, advertising the latest products and services. It was a city that never slept, a constant whirlwind of activity and excitement. Amidst the chaos, I navigated through the crowds, searching for moments of connection and inspiration."},
    {"id": 3, "text": "The waves crashed against the shore, each one a powerful force of nature. The sand beneath my feet shifted with every step, as if it was alive. Seagulls soared overhead, their calls echoing through the salty air. The ocean stretched out before me, its vastness both awe-inspiring and humbling. I closed my eyes and listened to the symphony of the sea, the rhythm of the waves lulling me into a state of tranquility. It was a place of solace, a place where the worries of the world melted away and all that remained was the beauty of the natural world."},
    {"id": 4, "text": "The old bookstore was a treasure trove of knowledge and stories. Rows upon rows of bookshelves lined the walls, each one filled with books of every genre and era. The scent of aged paper and ink filled the air, creating an atmosphere of nostalgia and adventure. As I perused the shelves, my fingers lightly grazing the spines of the books, I felt a sense of wonder and curiosity. Each book held the potential to transport me to another world, to introduce me to new ideas and perspectives. It was a sanctuary for the avid reader, a place where imagination flourished and stories came to life."}
]

# Create Elasticsearch index and mapping
if not es.indices.exists(index=index_name):
    es_index = {
        "mappings": {
            "properties": {
                "text": {"type": "text"},
                "embedding": {"type": "dense_vector", "dims": 768}
            }
        }
    }
    es.indices.create(index=index_name, body=es_index, ignore=[400])

# Upload documents to Elasticsearch with text embeddings
model = SentenceTransformer('quora-distilbert-multilingual')

for doc in data:
    # Calculate text embeddings using the SentenceTransformer model
    embedding = model.encode(doc["text"], show_progress_bar=False)

    # Create document with text and embedding
    document = {
        "text": doc["text"],
        "embedding": embedding.tolist()
    }

    # Index the document in Elasticsearch
    es.index(index=index_name, id=doc["id"], document=document)

為了運行上面的應用,我們需要安裝 elasticsearch 及?sentence_transformers 包:

pip install sentence_transformers elasticsearch

如果我們對上面的 Python 連接到 Elasticsearch 還比較不清楚的話,請詳細閱讀我之前的文章 “Elasticsearch:關(guān)于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x”。

我們首先在數(shù)據(jù)攝取腳本中導入必要的庫,包括 Elasticsearch 和 SentenceTransformer。 我們使用 Elasticsearch URL 建立與 Elasticsearch 的連接。 我們定義 index_name 變量來保存 Elasticsearch 索引的名稱。

接下來,我們將示例數(shù)據(jù)定義為字典列表,其中每個字典代表一個具有 ID 和文本的文檔。 這些文檔模擬了我們想要搜索的數(shù)據(jù)。 你可以根據(jù)您的特定數(shù)據(jù)源和元數(shù)據(jù)提取要求自定義腳本。

我們檢查 Elasticsearch 索引是否存在,如果不存在,則使用適當?shù)挠成鋭?chuàng)建它。 該映射定義了我們文檔的字段類型,包括作為文本的文本字段和作為維度為 768 的密集向量的嵌入(embedding)字段。

我們使用 quora-distilbert-multilingual?預訓練文本嵌入模型來初始化 SentenceTransformer 模型。 該模型可以將文本編碼為長度為 768 的密集向量。

對于示例數(shù)據(jù)中的每個文檔,我們使用 model.encode() 函數(shù)計算文本嵌入并將其存儲在嵌入變量中。 我們使用文本和嵌入字段創(chuàng)建一個文檔字典。 最后,我們使用 es.index() 函數(shù)在 Elasticsearch 中索引文檔。

現(xiàn)在我們已經(jīng)將數(shù)據(jù)提取到 Elasticsearch 中,讓我們繼續(xù)使用 FastAPI 創(chuàng)建搜索 API。

main.py

from fastapi import FastAPI
from elasticsearch import Elasticsearch
from sentence_transformers import SentenceTransformer

USERNAME = "elastic"
PASSWORD = "z5nxTriCD4fi7jSS=GFM"
ELATICSEARCH_ENDPOINT = "https://localhost:9200"
CERT_FINGERPRINT = "783663875df7ae1daf3541ab293d8cd48c068b3dbc2d9dd6fa8a668289986ac2"

# Connect to Elasticsearch
es = Elasticsearch(ELATICSEARCH_ENDPOINT, 
                   ssl_assert_fingerprint = (CERT_FINGERPRINT),
                   basic_auth=(USERNAME, PASSWORD),
                   verify_certs = False)

app = FastAPI()

@app.get("/search/")
async def search(query: str):
    print("query string is: ", query)
    model = SentenceTransformer('quora-distilbert-multilingual')
    embedding = model.encode(query, show_progress_bar=False)

    # Build the Elasticsearch script query
    script_query = {
        "script_score": {
            "query": {"match_all": {}},
            "script": {
                "source": "cosineSimilarity(params.query_vector, 'embedding') + 1.0",
                "params": {"query_vector": embedding.tolist()}
            }
        }
    }

    # Execute the search query
    search_results = es.search(index="test1", body={"query": script_query})

    # Process and return the search results
    results = search_results["hits"]["hits"]
    return {"results": results}

@app.get("/")
async def root():
    return {"message": "Hello World"}

要運行 FastAPI 應用程序,請將代碼保存在文件中(例如 main.py)并在終端中執(zhí)行以下命令:

uvicorn main:app --reload
$ pwd
/Users/liuxg/python/fastapi_vector
$ uvicorn main:app --reload
INFO:     Will watch for changes in these directories: ['/Users/liuxg/python/fastapi_vector']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [95339] using WatchFiles
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/elasticsearch/_sync/client/__init__.py:395: SecurityWarning: Connecting to 'https://localhost:9200' using TLS with verify_certs=False is insecure
  _transport = transport_class(
INFO:     Started server process [95341]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:59811 - "GET / HTTP/1.1" 200 OK

這將啟動 FastAPI 開發(fā)服務器。 然后,您可以訪問 http://localhost:8000/search/ 的搜索端點并提供查詢參數(shù)來執(zhí)行搜索。 結(jié)果將作為 JSON 響應返回。

確保根據(jù)你的要求自定義代碼,例如添加錯誤處理、身份驗證和修改響應結(jié)構(gòu)。我們做如下的搜索:

Elasticsearch:使用 Elasticsearch 矢量搜索和 FastAPI 構(gòu)建文本搜索應用程序

Elasticsearch:使用 Elasticsearch 矢量搜索和 FastAPI 構(gòu)建文本搜索應用程序

很顯然,當我們搜索語句 “The sun slowly set behind the mountains” 的時候,第一個文檔是最相近的。其它的文檔沒有那么相近,但是他們也會作為候選結(jié)果返回給用戶。文章來源地址http://www.zghlxwxcb.cn/news/detail-507351.html

到了這里,關(guān)于Elasticsearch:使用 Elasticsearch 矢量搜索和 FastAPI 構(gòu)建文本搜索應用程序的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • Elasticsearch 中的矢量搜索:設(shè)計背后的基本原理

    Elasticsearch 中的矢量搜索:設(shè)計背后的基本原理

    作者:Adrien Grand 你是否有興趣了解 Elasticsearch 用于向量搜索(vector search)的特性以及設(shè)計是什么樣子? 一如既往,設(shè)計決策有利有弊。 本博客旨在詳細介紹我們?nèi)绾芜x擇在 Elasticsearch 中構(gòu)建向量搜索。 首先是有關(guān) Lucene 的一些背景知識:Lucene 將數(shù)據(jù)組織成定期合并的不可

    2024年02月16日
    瀏覽(21)
  • fastapi結(jié)合Manticore Search、elasticsearch、mysql實現(xiàn)全文搜索

    fastapi結(jié)合Manticore Search、elasticsearch、mysql實現(xiàn)全文搜索

    創(chuàng)建測試表 測試表插入數(shù)據(jù) 表字段描述 字段意義 mysql數(shù)據(jù)同步到es es查看數(shù)據(jù)(Elasticvue插件) ? mysql數(shù)據(jù)同步到Manticore 注:Manticore 和 Mysql 使用pymysql即mysql客戶端 Manticore 數(shù)據(jù)查詢(工具Webyog SQLyog) ? es安全認證連接(參考官網(wǎng)) 按fields查詢方法封裝,輸入?yún)?shù)fields 篩選器,

    2024年02月12日
    瀏覽(19)
  • Elasticsearch:使用在本地計算機上運行的 LLM 以及 Ollama 和 Langchain 構(gòu)建 RAG 應用程序

    Elasticsearch:使用在本地計算機上運行的 LLM 以及 Ollama 和 Langchain 構(gòu)建 RAG 應用程序

    無需 GPU 的隱私保護 LLM。在本博客中,我將演示使用不同的工具 Ollama 構(gòu)建的 RAG 應用程序。 與本文相關(guān)的所有源代碼均已發(fā)布在 github上。 請克隆存儲庫以跟隨文章操作。我們可以通過如下的方式來克?。?Ollama 是一個輕量級且靈活的框架,專為在個人計算機上本地部署 LL

    2024年04月16日
    瀏覽(29)
  • 使用Elasticsearch構(gòu)建強大的搜索和分析引擎

    Elasticsearch是一個基于Lucene的分布式搜索和分析引擎,被廣泛用于處理大規(guī)模的文本數(shù)據(jù)。無論是構(gòu)建全文搜索引擎、進行日志分析還是實現(xiàn)實時數(shù)據(jù)可視化,Elasticsearch都是一個強大而靈活的工具。本文將帶您逐步了解如何使用Elasticsearch,并構(gòu)建您自己的搜索和分析應用。

    2024年02月04日
    瀏覽(23)
  • Elasticsearch:使用 Elasticsearch 和 BERT 構(gòu)建搜索引擎 - TensorFlow

    Elasticsearch:使用 Elasticsearch 和 BERT 構(gòu)建搜索引擎 - TensorFlow

    在本文中,我們使用預訓練的 BERT 模型和 Elasticsearch 來構(gòu)建搜索引擎。 Elasticsearch 最近發(fā)布了帶有向量場的文本相似性(text similarity search with vector field)搜索。 另一方面,你可以使用 BERT 將文本轉(zhuǎn)換為固定長度的向量。 因此,一旦我們將文檔通過 BERT 轉(zhuǎn)換為向量并存儲到

    2024年02月07日
    瀏覽(21)
  • 如何在 Elasticsearch 中將矢量搜索與過濾結(jié)合起來 - Python 8.x

    如何在 Elasticsearch 中將矢量搜索與過濾結(jié)合起來 - Python 8.x

    大型語言模型(LLM)每天都在發(fā)展,這種情況有助于語義搜索的擴展。 LLM 擅長分析文本和揭示語義相似性。 這種情況也反映在搜索引擎上,因為語義搜索引擎可以為用戶提供更滿意的結(jié)果。 盡管大型語言模型可以捕獲語義上接近的結(jié)果,但在搜索結(jié)果中實施過濾器對于增強

    2024年02月12日
    瀏覽(15)
  • Whisper、React 和 Node 構(gòu)建語音轉(zhuǎn)文本 Web 應用程序

    在本文中,我們將使用 OpenAI 的 Whisper 以及 React、Node.js 和 FFmpeg 構(gòu)建一個語音轉(zhuǎn)文本應用程序。該應用程序?qū)@取用戶輸入,使用 OpenAI 的 Whisper API 將其合成為語音,并輸出結(jié)果文本。Whisper 提供了我用過的最準確的語音到文本轉(zhuǎn)錄,即使對于非英語母語人士也是如此。 Ope

    2024年02月13日
    瀏覽(17)
  • Elasticsearch之文本搜索(十三)

    Elasticsearch之文本搜索(十三)

    ????????ES作為一款搜索引擎框架,文本搜索是其核心功能。ES在文本索引的建立和搜索過程中依賴兩大組件,即Lucene和分析器。其中,Lucene負責進行倒排索引的物理構(gòu)建,分析器負責在建立倒排索引前和搜索前對文本進行分詞和語法處理。 ????????為了完成對文本的快

    2024年02月07日
    瀏覽(23)
  • Elasticsearch:為現(xiàn)代搜索工作流程和生成式人工智能應用程序鋪平道路

    Elasticsearch:為現(xiàn)代搜索工作流程和生成式人工智能應用程序鋪平道路

    作者:Matt Riley Elastic 的創(chuàng)新投資支持開放的生態(tài)系統(tǒng)和更簡單的開發(fā)者體驗。 在本博客中,我們希望分享 Elastic? 為簡化你構(gòu)建 AI 應用程序的體驗而進行的投資。 我們知道,開發(fā)人員必須在當今快速發(fā)展的人工智能環(huán)境中保持靈活性。 然而,常見的挑戰(zhàn)使得構(gòu)建生成式人工

    2024年02月04日
    瀏覽(27)
  • 使用矢量數(shù)據(jù)庫打造全新的搜索引擎

    在技術(shù)層面上,矢量數(shù)據(jù)庫采用了一種名為“矢量索引”的技術(shù),這是一種組織和搜索矢量數(shù)據(jù)的方法,可以快速找到相似矢量。其中關(guān)鍵的一環(huán)是“距離函數(shù)”的概念,它可以衡量兩個矢量的相似程度。 矢量數(shù)據(jù)庫是專門設(shè)計用來高效處理矢量數(shù)據(jù)的數(shù)據(jù)庫。什么是矢量數(shù)

    2024年02月14日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包