From 47742ee1998f9961d671313d011fb0d133c07a5b Mon Sep 17 00:00:00 2001 From: Santi Adavani Date: Wed, 20 Dec 2023 09:33:22 -0800 Subject: [PATCH 1/2] clock conflict --- pgml-extension/Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pgml-extension/Cargo.lock b/pgml-extension/Cargo.lock index b31db4833..2a414d079 100644 --- a/pgml-extension/Cargo.lock +++ b/pgml-extension/Cargo.lock @@ -1723,7 +1723,7 @@ dependencies = [ [[package]] name = "pgml" -version = "2.8.0" +version = "2.8.1" dependencies = [ "anyhow", "blas", From 3684dac449861d2d6b5b1d6f57dd1fe4eed9cbe4 Mon Sep 17 00:00:00 2001 From: Santi Adavani Date: Wed, 20 Dec 2023 10:29:25 -0800 Subject: [PATCH 2/2] Added RAG q/a example --- .../python/examples/rag_question_answering.py | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 pgml-sdks/pgml/python/examples/rag_question_answering.py diff --git a/pgml-sdks/pgml/python/examples/rag_question_answering.py b/pgml-sdks/pgml/python/examples/rag_question_answering.py new file mode 100644 index 000000000..94db6846c --- /dev/null +++ b/pgml-sdks/pgml/python/examples/rag_question_answering.py @@ -0,0 +1,92 @@ +from pgml import Collection, Model, Splitter, Pipeline, Builtins, OpenSourceAI +import json +from datasets import load_dataset +from time import time +from dotenv import load_dotenv +from rich.console import Console +import asyncio + + +async def main(): + load_dotenv() + console = Console() + + # Initialize collection + collection = Collection("squad_collection") + + # Create a pipeline using the default model and splitter + model = Model() + splitter = Splitter() + pipeline = Pipeline("squadv1", model, splitter) + await collection.add_pipeline(pipeline) + + # Prep documents for upserting + data = load_dataset("squad", split="train") + data = data.to_pandas() + data = data.drop_duplicates(subset=["context"]) + documents = [ + {"id": r["id"], "text": r["context"], "title": r["title"]} + for r in data.to_dict(orient="records") + ] + + # Upsert documents + await collection.upsert_documents(documents[:200]) + + # Query for context + query = "Who won more than 20 grammy awards?" + + console.print("Question: %s"%query) + console.print("Querying for context ...") + + start = time() + results = ( + await collection.query().vector_recall(query, pipeline).limit(5).fetch_all() + ) + end = time() + + #console.print("Query time = %0.3f" % (end - start)) + + # Construct context from results + context = " ".join(results[0][1].strip().split()) + context = context.replace('"', '\\"').replace("'", "''") + console.print("Context is ready...") + + # Query for answer + system_prompt = """Use the following pieces of context to answer the question at the end. + If you don't know the answer, just say that you don't know, don't try to make up an answer. + Use three sentences maximum and keep the answer as concise as possible. + Always say "thanks for asking!" at the end of the answer.""" + user_prompt_template = """ + #### + Documents + #### + {context} + ### + User: {question} + ### + """ + + user_prompt = user_prompt_template.format(context=context, question=query) + messages = [ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": user_prompt}, + ] + + # Using OpenSource LLMs for Chat Completion + client = OpenSourceAI() + chat_completion_model = "HuggingFaceH4/zephyr-7b-beta" + console.print("Generating response using %s LLM..."%chat_completion_model) + response = client.chat_completions_create( + model=chat_completion_model, + messages=messages, + temperature=0.3, + max_tokens=256, + ) + output = response["choices"][0]["message"]["content"] + console.print("Answer: %s"%output) + # Archive collection + await collection.archive() + + +if __name__ == "__main__": + asyncio.run(main()) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy