Skip to content

Commit

Permalink
Geometric RAG from index (#6034)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 5f24018f17b197aa7e9091a0ac1d22a14198363c
  • Loading branch information
szymondudycz authored and Manul from Pathway committed Mar 27, 2024
1 parent 4dfc030 commit b4f70c6
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions examples/pipelines/contextful_geometric/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
import os

import pathway as pw
from pathway.stdlib.ml.index import KNNIndex
from pathway.stdlib.indexing import VectorDocumentIndex
from pathway.xpacks.llm.embedders import OpenAIEmbedder
from pathway.xpacks.llm.llms import OpenAIChat
from pathway.xpacks.llm.question_answering import answer_with_geometric_rag_strategy
from pathway.xpacks.llm.question_answering import (
answer_with_geometric_rag_strategy_from_index,
)


class DocumentInputSchema(pw.Schema):
Expand Down Expand Up @@ -78,10 +80,11 @@ def run(
autocommit_duration_ms=50,
)

enriched_documents = documents + documents.select(vector=embedder(pw.this.doc))

index = KNNIndex(
enriched_documents.vector, enriched_documents, n_dimensions=embedding_dimension
index = VectorDocumentIndex(
documents.doc,
documents,
embedder,
n_dimensions=embedding_dimension,
)

query, response_writer = pw.io.http.rest_connector(
Expand All @@ -92,14 +95,6 @@ def run(
delete_completed_queries=True,
)

query += query.select(vector=embedder(pw.this.query))

max_documents = n_starting_documents * (factor ** (max_iterations - 1))

query_context = query + index.get_nearest_items(
query.vector, k=max_documents, collapse_rows=True
).select(documents_list=pw.this.doc)

model = OpenAIChat(
api_key=api_key,
model=model_locator,
Expand All @@ -109,10 +104,11 @@ def run(
cache_strategy=pw.asynchronous.DefaultCache(),
)

responses = query_context.select(
result=answer_with_geometric_rag_strategy(
query_context.query,
query_context.documents_list,
responses = query.select(
result=answer_with_geometric_rag_strategy_from_index(
query.query,
index,
documents.doc,
model,
n_starting_documents,
factor,
Expand Down

0 comments on commit b4f70c6

Please sign in to comment.