codegood commited on
Commit
63808a2
·
1 Parent(s): f39e265

langfuse and conversiotn memory trimmed

Browse files
Files changed (2) hide show
  1. config.py +5 -0
  2. graph.py +14 -1
config.py CHANGED
@@ -17,6 +17,9 @@ DEFAULT_MAX_TOKENS = 2048
17
  DEFAULT_DATA_DIR = "data"
18
  DEFAULT_LANGFUSE_HOST = "https://cloud.langfuse.com"
19
  DEFAULT_CHECKPOINT_DB = "checkpoints.sqlite"
 
 
 
20
 
21
 
22
  class ConfigError(RuntimeError):
@@ -31,6 +34,7 @@ class Settings:
31
  model: str
32
  temperature: float
33
  max_tokens: int
 
34
  checkpoint_db: Path
35
  langfuse_public_key: str
36
  langfuse_secret_key: str
@@ -95,6 +99,7 @@ def get_settings() -> Settings:
95
  model=os.getenv("GROQ_MODEL", "").strip() or DEFAULT_MODEL,
96
  temperature=_get_float("LLM_TEMPERATURE", DEFAULT_TEMPERATURE),
97
  max_tokens=_get_int("LLM_MAX_TOKENS", DEFAULT_MAX_TOKENS),
 
98
  checkpoint_db=PROJECT_ROOT / (
99
  os.getenv("CHECKPOINT_DB", "").strip() or DEFAULT_CHECKPOINT_DB
100
  ),
 
17
  DEFAULT_DATA_DIR = "data"
18
  DEFAULT_LANGFUSE_HOST = "https://cloud.langfuse.com"
19
  DEFAULT_CHECKPOINT_DB = "checkpoints.sqlite"
20
+ # Turns of conversation replayed to the model. Older turns stay in the
21
+ # checkpoint (nothing is deleted) but are not resent.
22
+ DEFAULT_MAX_HISTORY_TURNS = 10
23
 
24
 
25
  class ConfigError(RuntimeError):
 
34
  model: str
35
  temperature: float
36
  max_tokens: int
37
+ max_history_turns: int
38
  checkpoint_db: Path
39
  langfuse_public_key: str
40
  langfuse_secret_key: str
 
99
  model=os.getenv("GROQ_MODEL", "").strip() or DEFAULT_MODEL,
100
  temperature=_get_float("LLM_TEMPERATURE", DEFAULT_TEMPERATURE),
101
  max_tokens=_get_int("LLM_MAX_TOKENS", DEFAULT_MAX_TOKENS),
102
+ max_history_turns=_get_int("MAX_HISTORY_TURNS", DEFAULT_MAX_HISTORY_TURNS),
103
  checkpoint_db=PROJECT_ROOT / (
104
  os.getenv("CHECKPOINT_DB", "").strip() or DEFAULT_CHECKPOINT_DB
105
  ),
graph.py CHANGED
@@ -9,6 +9,7 @@ rewrite.
9
  import sqlite3
10
  from typing import Annotated, TypedDict
11
 
 
12
  from langchain_groq import ChatGroq
13
  from langgraph.checkpoint.sqlite import SqliteSaver
14
  from langgraph.graph import START, StateGraph
@@ -40,8 +41,20 @@ def build_graph():
40
  profile_data = ProfileLoader(settings.data_dir).load_and_format_markdown()
41
  chain = build_qa_prompt() | build_llm()
42
 
 
 
 
43
  def answer(state: ChatState):
44
- history = state["messages"][:-1]
 
 
 
 
 
 
 
 
 
45
  user_query = state["messages"][-1].content
46
  response = chain.invoke({
47
  "context_text": profile_data,
 
9
  import sqlite3
10
  from typing import Annotated, TypedDict
11
 
12
+ from langchain_core.messages import trim_messages
13
  from langchain_groq import ChatGroq
14
  from langgraph.checkpoint.sqlite import SqliteSaver
15
  from langgraph.graph import START, StateGraph
 
41
  profile_data = ProfileLoader(settings.data_dir).load_and_format_markdown()
42
  chain = build_qa_prompt() | build_llm()
43
 
44
+ # Two messages per turn (human + ai).
45
+ max_history_messages = settings.max_history_turns * 2
46
+
47
  def answer(state: ChatState):
48
+ # Older turns remain in the checkpoint; they are simply not resent,
49
+ # which keeps per-turn cost flat instead of growing with the thread.
50
+ history = trim_messages(
51
+ state["messages"][:-1],
52
+ max_tokens=max_history_messages,
53
+ token_counter=len,
54
+ strategy="last",
55
+ start_on="human",
56
+ include_system=False,
57
+ )
58
  user_query = state["messages"][-1].content
59
  response = chain.invoke({
60
  "context_text": profile_data,