SarahXia0405 commited on
Commit
4ddc6e7
·
verified ·
1 Parent(s): 95d3cb3

Update api/clare_core.py

Browse files
Files changed (1) hide show
  1. api/clare_core.py +35 -2
api/clare_core.py CHANGED
@@ -24,6 +24,12 @@ ENABLE_TRACING = os.getenv("CLARE_ENABLE_TRACING", "0").strip() == "1"
24
  if ENABLE_TRACING:
25
  from langsmith import traceable # type: ignore
26
  from langsmith.run_helpers import set_run_metadata # type: ignore
 
 
 
 
 
 
27
  else:
28
  # no-op decorators / funcs
29
  def traceable(*args, **kwargs): # type: ignore
@@ -34,6 +40,8 @@ else:
34
  def set_run_metadata(**kwargs): # type: ignore
35
  return None
36
 
 
 
37
 
38
  # ----------------------------
39
  # Speed knobs (simple + stable)
@@ -478,6 +486,28 @@ def model_name_or_default(x: str) -> str:
478
  return (x or "").strip() or DEFAULT_MODEL
479
 
480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
481
  @traceable(run_type="chain", name="chat_with_clare")
482
  def chat_with_clare(
483
  message: str,
@@ -490,7 +520,7 @@ def chat_with_clare(
490
  weaknesses: Optional[List[str]],
491
  cognitive_state: Optional[Dict[str, int]],
492
  rag_context: Optional[str] = None,
493
- ) -> Tuple[str, List[Tuple[str, str]]]:
494
  # avoid any tracing overhead when disabled (set_run_metadata is no-op in that case)
495
  try:
496
  set_run_metadata(
@@ -523,8 +553,11 @@ def chat_with_clare(
523
  max_tokens=DEFAULT_MAX_OUTPUT_TOKENS,
524
  )
525
 
 
 
 
526
  history = history + [(message, answer)]
527
- return answer, history
528
 
529
 
530
  def export_conversation(
 
24
  if ENABLE_TRACING:
25
  from langsmith import traceable # type: ignore
26
  from langsmith.run_helpers import set_run_metadata # type: ignore
27
+
28
+ try:
29
+ # Available in newer langsmith versions
30
+ from langsmith.run_helpers import get_current_run_tree # type: ignore
31
+ except Exception:
32
+ get_current_run_tree = None # type: ignore
33
  else:
34
  # no-op decorators / funcs
35
  def traceable(*args, **kwargs): # type: ignore
 
40
  def set_run_metadata(**kwargs): # type: ignore
41
  return None
42
 
43
+ get_current_run_tree = None # type: ignore
44
+
45
 
46
  # ----------------------------
47
  # Speed knobs (simple + stable)
 
486
  return (x or "").strip() or DEFAULT_MODEL
487
 
488
 
489
+ def get_langsmith_run_id() -> Optional[str]:
490
+ """
491
+ 从 traceable 上下文里获取当前 run_id(用于把 UI feedback 挂到同一个 run 上)
492
+ 若 tracing 关闭或环境不支持,则返回 None
493
+ """
494
+ if not ENABLE_TRACING:
495
+ return None
496
+ try:
497
+ if get_current_run_tree is None:
498
+ return None
499
+ rt = get_current_run_tree()
500
+ if not rt:
501
+ return None
502
+ rid = getattr(rt, "id", None)
503
+ if not rid:
504
+ return None
505
+ return str(rid)
506
+ except Exception as e:
507
+ print(f"[LangSmith get run id error] {repr(e)}")
508
+ return None
509
+
510
+
511
  @traceable(run_type="chain", name="chat_with_clare")
512
  def chat_with_clare(
513
  message: str,
 
520
  weaknesses: Optional[List[str]],
521
  cognitive_state: Optional[Dict[str, int]],
522
  rag_context: Optional[str] = None,
523
+ ) -> Tuple[str, List[Tuple[str, str]], Optional[str]]:
524
  # avoid any tracing overhead when disabled (set_run_metadata is no-op in that case)
525
  try:
526
  set_run_metadata(
 
553
  max_tokens=DEFAULT_MAX_OUTPUT_TOKENS,
554
  )
555
 
556
+ # Get run_id AFTER the run exists (works when tracing enabled; otherwise None)
557
+ run_id = get_langsmith_run_id()
558
+
559
  history = history + [(message, answer)]
560
+ return answer, history, run_id
561
 
562
 
563
  def export_conversation(