OracleKeywordRetriever

OracleKeywordRetriever

Oracle Document Store에서 검색어와 일치하는 문서를 가져오는 키워드 기반 Retriever예요.

출처: 문서

본문

OracleKeywordRetriever는 OracleDocumentStore와 호환되는 키워드 기반 Retriever예요. Document Store를 초기화할 때 자동으로 만들어지는 Oracle의 DBMS_SEARCH 전문(full-text) 인덱스를 사용해서 키워드 관련성으로 문서를 검색해요.

이 Retriever는 임베딩 없이 동작해서, 키워드 전용 파이프라인이나 하이브리드 검색 파이프라인의 키워드 분기로 적합해요.

query 외에도 Retriever는 top_k(반환할 최대 문서 수)와 검색 공간을 좁히는 filters를 받아요.

설치

Oracle Database 23ai를 Docker로 로컬에서 실행하려면:

docker run -d --name oracle23ai \
  -p 1521:1521 \
  -e ORACLE_PASSWORD=oracle \
  -e ORACLE_INIT_PARAMS=vector_memory_size=512M \
  gvenzl/oracle-free:23-slim

Haystack용 Oracle 통합을 설치해요.

pip install oracle-haystack

더 알아보기 (Learn more)

단독으로 쓰기

이 Retriever는 OracleDocumentStore와 인덱싱된 문서가 필요해요.

from haystack.utils import Secret
from haystack_integrations.document_stores.oracle import (
    OracleDocumentStore,
    OracleConnectionConfig,
)
from haystack_integrations.components.retrievers.oracle import OracleKeywordRetriever


document_store = OracleDocumentStore(
    connection_config=OracleConnectionConfig(
        user=Secret.from_env_var("ORACLE_USER"),
        password=Secret.from_env_var("ORACLE_PASSWORD"),
        dsn=Secret.from_env_var("ORACLE_DSN"),
    ),
    embedding_dim=768,
)

retriever = OracleKeywordRetriever(document_store=document_store)
retriever.run(query="my keyword query")

RAG 파이프라인에서 쓰기

from haystack import Document, Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.document_stores.types import DuplicatePolicy
from haystack.utils import Secret

from haystack_integrations.document_stores.oracle import (
    OracleDocumentStore,
    OracleConnectionConfig,
)
from haystack_integrations.components.retrievers.oracle import OracleKeywordRetriever


prompt_template = [
    ChatMessage.from_user(
        """
    Given these documents, answer the question.\nDocuments:
    {% for doc in documents %}
        {{ doc.content }}
    {% endfor %}

    \nQuestion: {{question}}
    \nAnswer:
        """,
    ),
]

document_store = OracleDocumentStore(
    connection_config=OracleConnectionConfig(
        user=Secret.from_env_var("ORACLE_USER"),
        password=Secret.from_env_var("ORACLE_PASSWORD"),
        dsn=Secret.from_env_var("ORACLE_DSN"),
    ),
    embedding_dim=768,
)

documents = [
    Document(content="There are over 7,000 languages spoken around the world today."),
    Document(
        content="Elephants have been observed to behave in a way that indicates a high level of self-awareness, such as recognizing themselves in mirrors.",
    ),
    Document(
        content="In certain parts of the world, like the Maldives, Puerto Rico, and San Diego, you can witness the phenomenon of bioluminescent waves.",
    ),
]

document_store.write_documents(documents=documents, policy=DuplicatePolicy.SKIP)

retriever = OracleKeywordRetriever(document_store=document_store)

rag_pipeline = Pipeline()
rag_pipeline.add_component(name="retriever", instance=retriever)
rag_pipeline.add_component(
    instance=ChatPromptBuilder(template=prompt_template, required_variables="*"),
    name="prompt_builder",
)
rag_pipeline.add_component(instance=OpenAIChatGenerator(), name="llm")

rag_pipeline.connect("retriever", "prompt_builder.documents")
rag_pipeline.connect("prompt_builder.prompt", "llm.messages")

question = "How many languages are there?"
result = rag_pipeline.run(
    {
        "retriever": {"query": question},
        "prompt_builder": {"question": question},
    },
)

print(result["llm"]["replies"][0].text)