สำคัญ
ชั้นแคชสำหรับ LLM ที่ใช้ประโยชน์จาก Elasticsearch เข้ากันได้อย่างเต็มที่กับการแคช Langchain ทั้งสำหรับการแชทและแบบฝังตัว
pip install llm-elasticsearch-cacheแคช Langchain สามารถใช้งานได้คล้ายกับการรวมแคชอื่น ๆ
from langchain . globals import set_llm_cache
from llmescache . langchain import ElasticsearchCache
from elasticsearch import Elasticsearch
es_client = Elasticsearch ( hosts = "http://localhost:9200" )
set_llm_cache (
ElasticsearchCache (
es_client = es_client ,
es_index = "llm-chat-cache" ,
metadata = { "project" : "my_chatgpt_project" }
)
) พารามิเตอร์ es_index ยังสามารถใช้นามแฝงได้ สิ่งนี้ช่วยให้ใช้ ILM: จัดการวงจรชีวิตดัชนีที่เราแนะนำให้พิจารณาสำหรับการจัดการการเก็บรักษาและควบคุมการเติบโตของแคช
ดูที่คลาส DocString สำหรับพารามิเตอร์ทั้งหมด
ข้อมูลแคชจะไม่สามารถค้นหาได้ตามค่าเริ่มต้น นักพัฒนาสามารถปรับแต่งการสร้างเอกสาร Elasticsearch เพื่อเพิ่มฟิลด์ข้อความที่จัดทำดัชนีซึ่งจะวางตัวอย่างเช่นข้อความที่สร้างโดย LLM
สิ่งนี้สามารถทำได้โดยวิธีการแทนที่การเอาชนะ คลาสแคชใหม่สามารถนำไปใช้กับดัชนีแคชที่มีอยู่แล้ว:
from llmescache . langchain import ElasticsearchCache
from elasticsearch import Elasticsearch
from langchain_core . caches import RETURN_VAL_TYPE
from typing import Any , Dict , List
from langchain . globals import set_llm_cache
import json
class SearchableElasticsearchCache ( ElasticsearchCache ):
@ property
def mapping ( self ) -> Dict [ str , Any ]:
mapping = super (). mapping
mapping [ "mappings" ][ "properties" ][ "parsed_llm_output" ] = { "type" : "text" , "analyzer" : "english" }
return mapping
def build_document ( self , prompt : str , llm_string : str , return_val : RETURN_VAL_TYPE ) -> Dict [ str , Any ]:
body = super (). build_document ( prompt , llm_string , return_val )
body [ "parsed_llm_output" ] = self . _parse_output ( body [ "llm_output" ])
return body
@ staticmethod
def _parse_output ( data : List [ str ]) -> List [ str ]:
return [ json . loads ( output )[ "kwargs" ][ "message" ][ "kwargs" ][ "content" ] for output in data ]
es_client = Elasticsearch ( hosts = "http://localhost:9200" )
set_llm_cache ( SearchableElasticsearchCache ( es_client = es_client , es_index = "llm-chat-cache" ))การฝังแคชนั้นได้มาจากการใช้ CachebackedEmbeddings ในวิธีที่แตกต่างจากเอกสารอย่างเป็นทางการเล็กน้อย
from llmescache . langchain import ElasticsearchStore
from elasticsearch import Elasticsearch
from langchain . embeddings import CacheBackedEmbeddings
from langchain_openai import OpenAIEmbeddings
es_client = Elasticsearch ( hosts = "http://localhost:9200" )
underlying_embeddings = OpenAIEmbeddings ( model = "text-embedding-3-small" )
store = ElasticsearchStore (
es_client = es_client ,
es_index = "llm-embeddings-cache" ,
namespace = underlying_embeddings . model ,
metadata = { "project" : "my_llm_project" }
)
cached_embeddings = CacheBackedEmbeddings (
underlying_embeddings ,
store
) ในทำนองเดียวกันกับแคชแชทหนึ่งสามารถ subclass ElasticsearchStore เพื่อจัดทำดัชนีเวกเตอร์สำหรับการค้นหา
from llmescache . langchain import ElasticsearchStore
from typing import Any , Dict , List
class SearchableElasticsearchStore ( ElasticsearchStore ):
@ property
def mapping ( self ) -> Dict [ str , Any ]:
mapping = super (). mapping
mapping [ "mappings" ][ "properties" ][ "vector" ] = { "type" : "dense_vector" , "dims" : 1536 , "index" : True , "similarity" : "dot_product" }
return mapping
def build_document ( self , llm_input : str , vector : List [ float ]) -> Dict [ str , Any ]:
body = super (). build_document ( llm_input , vector )
body [ "vector" ] = vector
return body โปรดทราบว่า CacheBackedEmbeddings ไม่สนับสนุนการสอบถามแคชซึ่งหมายความว่าข้อความค้นหาข้อความสำหรับการค้นหาเวกเตอร์จะไม่ถูกแคช อย่างไรก็ตามโดยการเอาชนะวิธี embed_query หนึ่งควรจะสามารถใช้งานได้อย่างง่ายดาย