English 中文(简体)
利用Langchain RetrivalQA精简数据
原标题:Streaming data using Langchain RetrivalQA

I am using Django, and Langchain with OpenAI to generate responses to my prompts. I was trying to enable streaming using Server-Sent-Events (SSE) in my API function. When I run my code It does stream the OpenAI output in the terminal, but it returns the output as a whole to the client once the streaming has ended.

我尝试了精简HttpResponse,但却没有取得成功。 如果任何人能够提出该法典中的错误,那将是真的麻烦。

页: 1

@api_view([ GET , POST ])
def sse_view(request):
if request.method !=  POST :
    return Response({"message": "Use the POST method for the response"})

url = request.data.get("url")
questions = request.data.get("questions")
prompt = request.data.get("promptName")

if not url or not questions:
    return Response({"message": "Please provide valid URL and questions"})

# Process the documents received from the user
try:
    doc_store = process_url(url)
    if not doc_store:
        return Response({"message": "PDF document not loaded"})
except Exception as e:
    return Response({"message": "Error loading PDF document"})


custom_prompt_template = set_custom_prompt(url,prompt)
# Load and process documents
loader = DirectoryLoader(DATA_PATH, glob= *.pdf , loader_cls=PyPDFLoader)
documents = loader.load()   

text_splitter = CustomRecursiveCharacterTextSplitter(chunk_size=1500, chunk_overlap=30)
texts = text_splitter.split_documents(documents)

#Creating Embeddings using OpenAI
embeddings = OpenAIEmbeddings(chunk_size= 16, openai_api_key= openai_gpt_key,)

db = FAISS.from_documents(texts, embeddings)
db.save_local(DB_FAISS_PATH)
search_kwargs = {
     k : 30,
     fetch_k :100,
     maximal_marginal_relevance : True,
     distance_metric :  cos ,
}
retriever=db.as_retriever(search_kwargs=search_kwargs)

# get the list of questions from the body
questionList =request.data[ questions ]

#This function is to generate responses from OpenAi
def openai_response_generator():
        
        # Create an instance of ChatOpenAI
        llm = ChatOpenAI(
            model_name="gpt-3.5-turbo-16k",
            streaming=True,
            callbacks=[StreamingStdOutCallbackHandler()],
            temperature=0,
            openai_api_key= openai_gpt_key,
        )

        # Iterating through the questions list
        for question in (questionList):
            qa = RetrievalQA.from_chain_type(
                llm=llm,
                chain_type="stuff",
                retriever=retriever,
                return_source_documents=True,
                chain_type_kwargs={"prompt": custom_prompt_template},
            )

        res = qa({ query : question})
        yield f"data: {res[ result ]}

"

response = StreamingHttpResponse(openai_response_generator(), content_type="text/event-stream")
return response

Here s what I see in PostMan when I run the function. enter image description here

问题回答

如果你使用<代码>Callback Handler<>/code>处理来自LM的新流,则会有所帮助。

langchain>/code>提供许多内联网接听器,但我们能够使用定制的手稿

class CustomStreamingCallbackHandler(BaseCallbackHandler):
"""Callback Handler that Stream LLM response."""

    def __init__(self, queue):
        self.queue = queue

    
    def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
    """Run on new LLM token. Only available when streaming is enabled."""
        self.queue.put(token)

we are doing here that whenever OpenAI API streams a new token, we put it in a Queue. Our Custom Handler Accept the queue and put tokens in that queue

我们现在需要这样做,

def generate_stream(q: Queue):
    #Any Custom Condition To stop while loop
    #like while the queue is not empty
    while (...):
        stream = q.get()
        yield stream

queue = Queue()
response = StreamingHttpResponse(generate_stream(queue), content_type="text/event-stream")

除此以外,您需要开始履行关于校对的主要<条码>。

queue = Queue()
task = threading.Thread(
    target=openai_response_generator,
    args=(queue,)
)
task.start()
response = StreamingHttpResponse(generate_stream(queue), content_type="text/event-stream")

露天-反应功能将发生变化。

def openai_response_generator(queue):
    
    # Create an instance of ChatOpenAI
    llm = ChatOpenAI(
        model_name="gpt-3.5-turbo-16k",
        streaming=True,
        temperature=0,
        openai_api_key= openai_gpt_key,
    )
    llm.callback_manager = CustomStreamingCallbackHandler(queue)
    # Iterating through the questions list
    for question in (questionList):
        qa = RetrievalQA.from_chain_type(
            llm=llm,
            chain_type="stuff",
            retriever=retriever,
            return_source_documents=True,
            chain_type_kwargs={"prompt": custom_prompt_template},
        )

    res = qa({ query : question})

www.un.org/Depts/DGACM/index_spanish.htm 摘要:

在界定“LM”时,我们制定了“<条码>cus背书/代码>,接受<条码>queue,并将新的条码输入到此处,而我们认为,我们回去了<条码>。 简化HttpResponse,使用这一格言

......

这可能是现在的一点点,我可能误解了一点,但像你一样,对问题进行思考,从而打造了卡片物体,但只是在电梯完成之后通过。 如果你想单独回答问题,你就不想逐一回答问题(在主持人内部)。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签