From 200b6f55c6539b93c023d8906a216bacaae720b9 Mon Sep 17 00:00:00 2001 From: Roccat <153699410+lizheng419@users.noreply.github.com> Date: Mon, 24 Mar 2025 15:14:36 +0800 Subject: [PATCH] Fix: NameError: free variable 'langfuse_generation' referenced before assignment in enclosing scope (#6451) ### What problem does this PR solve? ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --------- Co-authored-by: lizheng@ssc-hn.com --- api/db/services/dialog_service.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/api/db/services/dialog_service.py b/api/db/services/dialog_service.py index 2474475e6..ad9a386e4 100644 --- a/api/db/services/dialog_service.py +++ b/api/db/services/dialog_service.py @@ -260,7 +260,7 @@ def chat(dialog, messages, stream=True, **kwargs): gen_conf["max_tokens"] = min(gen_conf["max_tokens"], max_tokens - used_token_count) def decorate_answer(answer): - nonlocal prompt_config, knowledges, kwargs, kbinfos, prompt, retrieval_ts, questions + nonlocal prompt_config, knowledges, kwargs, kbinfos, prompt, retrieval_ts, questions, langfuse_tracer refs = [] ans = answer.split("") @@ -336,7 +336,10 @@ def chat(dialog, messages, stream=True, **kwargs): langfuse_output = "\n" + re.sub(r"^.*?(### Query:.*)", r"\1", prompt, flags=re.DOTALL) langfuse_output = {"time_elapsed:": re.sub(r"\n", " \n", langfuse_output), "created_at": time.time()} - langfuse_generation.end(output=langfuse_output) + + # Add a condition check to call the end method only if langfuse_tracer exists + if langfuse_tracer and 'langfuse_generation' in locals(): + langfuse_generation.end(output=langfuse_output) return {"answer": think + answer, "reference": refs, "prompt": re.sub(r"\n", " \n", prompt), "created_at": time.time()}