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 <lizheng@ssc-hn.com>
This commit is contained in:
Roccat 2025-03-24 15:14:36 +08:00 committed by GitHub
parent b77ce4e846
commit 200b6f55c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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("</think>")
@ -336,6 +336,9 @@ 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()}
# 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()}