1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Yield not change local variable in streaming

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024 às 11:52.

  1. Stack

    Stack Membro Participativo

    full_answer = ""
    total_tokens = 0
    conversation_id = None
    def generate_stream():
    nonlocal total_tokens, full_answer
    global conversation_id
    current_response = ""
    for chunk in answer_stream:
    if (
    hasattr(chunk, "usage_metadata")
    and chunk.usage_metadata is not None
    ):
    total_tokens = chunk.usage_metadata.get("total_tokens", 0)
    if hasattr(chunk, "content") and chunk.content:
    current_response += chunk.content
    yield chunk.content
    current_response = current_response.strip()
    full_answer = current_response
    conversation_id = self.store_message(
    {
    "message": full_answer,
    "chat_conversation_id": chat_conversation_id,
    "tokens_used": total_tokens,
    }
    )
    print(conversation_id)
    return generate_stream, conversation_id

    // Route

    @search_bp.route("/search", methods=["POST"])
    def searchQuery(id):
    data = request.get_json()

    try:
    result_stream, id = search_service.search(data)

    return app.response_class(
    stream_with_context(result_stream()), content_type="application/json", headers={"x-conversation-id": id}
    )

    except Exception as e:
    return error_helper(str(e), status_code=500)


    I send conversation_id but it is None inside the function it is proper integer. When I access inside then it works but in headers it was None.

    I want to pass Id into next function.

    Continue reading...

Compartilhe esta Página