from indexify_extractor_sdk import Extractor, Content, Feature
from indexify_extractor_sdk.base_extractor import Content
import json
class InputParams(BaseModel):
a: int = 0
b: str = ""
class MyExtractor(Extractor):
name = "your-docker-hub-username/MyExtractor"
description = "Description of the extractor goes here."
# Any python dependencies included in the extractor must be listed here.
python_dependencies = ["torch", "transformers"]
# Any system dependencies that the python code here depends on needs to be listed here. We use Ubuntu base images, so any ubuntu package can be installed here.
"You are an expert in paper interpretation,You must help me interpret the core content of the paper. Answer the question, based on the context. Answer "Information not found" if there is no context. Do not hallucinate. \nquestion: {question} \ncontext: {context}"
`#query_doc.pyfrom indexify import IndexifyClientfrom groq import Groqclient = IndexifyClient()groq_client = Groq( api_key="API_KEY",)def get_context(question: str, index: str, top_k=3): results = client.search_index(name=index, query=question, top_k=3) context = "" for result in results: context = context + f"content id: {result['content_id']} \n \npassage: {result['text']}\n" return contextdef create_prompt(question, context): return f"You are an expert in paper interpretation,You must help me interpret the core content of the paper. Answer the question, based on the context. Answer "Information not found" if there is no context. Do not hallucinate. \nquestion: {question} \ncontext: {context}"
def generate_response(prompt): chat_completion = groq_client.chat.completions.create( messages=[ { "role": "user", "content": prompt, } ], model="gemma-7b-it", ) return chat_completion.choices[0].message.contentquestion = "How does the Transformer model use self-attention mechanisms to improve the efficiency and effectiveness of sequence-to-sequence tasks compared to traditional RNN-based models?"context = get_context(question, "propertyQA.pdfembedding.embedding")prompt = create_prompt(question, context)response = generate_response(prompt)print(response)`
复制代码
运行此文件以获取响应。 终端 3
python3 ./query_doc.py
复制代码
The Transformer model replaces recurrent layers with self-attention mechanisms, allowing for parallelization and capturing dependencies between words regardless of their distance in the sequence. This improves efficiency by reducing the training time and enhances effectiveness by better handling long-range dependencies.
# langchain_query_doc.pyimport requestsimport dotenv# Setup retrieverfrom indexify import IndexifyClientfrom indexify_langchain import IndexifyRetrieverclient = IndexifyClient()params = {"name": "propertyQA.pdfembedding.embedding", "top_k": 2}retriever = IndexifyRetriever(client=client, params=params)from langchain_groq import ChatGroqllm = ChatGroq( model="gemma-7b-it", temperature=0, max_tokens=None, timeout=None, max_retries=2, api_key="API_KEY",)# Setup Chat Prompt Templatefrom langchain.prompts import ChatPromptTemplatetemplate = """You are an expert in paper interpretation,You must help me interpret the core content of the paper. Answer the question, based on the context. Answer "Information not found" if there is no context. Do not hallucinate. \nquestion: {question} \ncontext: {context}"
""prompt = ChatPromptTemplate.from_template(template)from langchain.schema.runnable import RunnablePassthroughfrom langchain.schema.output_parser import StrOutputParserrag_chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser())# Queryquery = "How does the Transformer model use self-attention mechanisms to improve the efficiency and effectiveness of sequence-to-sequence tasks compared to traditional RNN-based models?"print(rag_chain.invoke(query))
question = "How does the Transformer model use self-attention mechanisms to improve the efficiency and effectiveness of sequence-to-sequence tasks compared to traditional RNN-based models?"