fix: max tokens max is 8192 now

This commit is contained in:
LUIS NOVO 2025-10-18 13:21:53 -03:00
parent 059ee29e18
commit 8b5daa86bc
3 changed files with 70 additions and 53 deletions

View file

@ -42,8 +42,7 @@ def call_model_with_messages(state: ThreadState, config: RunnableConfig) -> dict
str(payload), str(payload),
model_id, model_id,
"chat", "chat",
max_tokens=10000, max_tokens=8192
)
) )
finally: finally:
new_loop.close() new_loop.close()
@ -64,7 +63,7 @@ def call_model_with_messages(state: ThreadState, config: RunnableConfig) -> dict
str(payload), str(payload),
model_id, model_id,
"chat", "chat",
max_tokens=10000, max_tokens=8192,
) )
) )

View file

@ -26,7 +26,9 @@ class SourceChatState(TypedDict):
context_indicators: Optional[Dict[str, List[str]]] context_indicators: Optional[Dict[str, List[str]]]
def call_model_with_source_context(state: SourceChatState, config: RunnableConfig) -> dict: def call_model_with_source_context(
state: SourceChatState, config: RunnableConfig
) -> dict:
""" """
Main function that builds source context and calls the model. Main function that builds source context and calls the model.
@ -50,7 +52,7 @@ def call_model_with_source_context(state: SourceChatState, config: RunnableConfi
source_id=source_id, source_id=source_id,
include_insights=True, include_insights=True,
include_notes=False, # Focus on source-specific content include_notes=False, # Focus on source-specific content
max_tokens=50000 # Reasonable limit for source context max_tokens=50000, # Reasonable limit for source context
) )
return new_loop.run_until_complete(context_builder.build()) return new_loop.run_until_complete(context_builder.build())
finally: finally:
@ -63,6 +65,7 @@ def call_model_with_source_context(state: SourceChatState, config: RunnableConfi
asyncio.get_running_loop() asyncio.get_running_loop()
# If we're in an event loop, run in a thread with a new loop # If we're in an event loop, run in a thread with a new loop
import concurrent.futures import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as executor: with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(build_context) future = executor.submit(build_context)
context_data = future.result() context_data = future.result()
@ -73,7 +76,11 @@ def call_model_with_source_context(state: SourceChatState, config: RunnableConfi
# Extract source and insights from context # Extract source and insights from context
source = None source = None
insights = [] insights = []
context_indicators: dict[str, list[str | None]] = {"sources": [], "insights": [], "notes": []} context_indicators: dict[str, list[str | None]] = {
"sources": [],
"insights": [],
"notes": [],
}
if context_data.get("sources"): if context_data.get("sources"):
source_info = context_data["sources"][0] # First source source_info = context_data["sources"][0] # First source
@ -82,7 +89,11 @@ def call_model_with_source_context(state: SourceChatState, config: RunnableConfi
if context_data.get("insights"): if context_data.get("insights"):
for insight_data in context_data["insights"]: for insight_data in context_data["insights"]:
insight = SourceInsight(**insight_data) if isinstance(insight_data, dict) else insight_data insight = (
SourceInsight(**insight_data)
if isinstance(insight_data, dict)
else insight_data
)
insights.append(insight) insights.append(insight)
context_indicators["insights"].append(insight.id) context_indicators["insights"].append(insight.id)
@ -94,7 +105,7 @@ def call_model_with_source_context(state: SourceChatState, config: RunnableConfi
"source": source.model_dump() if source else None, "source": source.model_dump() if source else None,
"insights": [insight.model_dump() for insight in insights] if insights else [], "insights": [insight.model_dump() for insight in insights] if insights else [],
"context": formatted_context, "context": formatted_context,
"context_indicators": context_indicators "context_indicators": context_indicators,
} }
# Apply the source_chat prompt template # Apply the source_chat prompt template
@ -110,9 +121,10 @@ def call_model_with_source_context(state: SourceChatState, config: RunnableConfi
return new_loop.run_until_complete( return new_loop.run_until_complete(
provision_langchain_model( provision_langchain_model(
str(payload), str(payload),
config.get("configurable", {}).get("model_id") or state.get("model_override"), config.get("configurable", {}).get("model_id")
or state.get("model_override"),
"chat", "chat",
max_tokens=10000, max_tokens=8192,
) )
) )
finally: finally:
@ -124,6 +136,7 @@ def call_model_with_source_context(state: SourceChatState, config: RunnableConfi
asyncio.get_running_loop() asyncio.get_running_loop()
# If we're in an event loop, run in a thread with a new loop # If we're in an event loop, run in a thread with a new loop
import concurrent.futures import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as executor: with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(run_in_new_loop) future = executor.submit(run_in_new_loop)
model = future.result() model = future.result()
@ -132,9 +145,10 @@ def call_model_with_source_context(state: SourceChatState, config: RunnableConfi
model = asyncio.run( model = asyncio.run(
provision_langchain_model( provision_langchain_model(
str(payload), str(payload),
config.get("configurable", {}).get("model_id") or state.get("model_override"), config.get("configurable", {}).get("model_id")
or state.get("model_override"),
"chat", "chat",
max_tokens=10000, max_tokens=8192,
) )
) )
@ -146,7 +160,7 @@ def call_model_with_source_context(state: SourceChatState, config: RunnableConfi
"source": source, "source": source,
"insights": insights, "insights": insights,
"context": formatted_context, "context": formatted_context,
"context_indicators": context_indicators "context_indicators": context_indicators,
} }
@ -183,8 +197,12 @@ def _format_source_context(context_data: Dict) -> str:
for insight in context_data["insights"]: for insight in context_data["insights"]:
if isinstance(insight, dict): if isinstance(insight, dict):
context_parts.append(f"**Insight ID:** {insight.get('id', 'Unknown')}") context_parts.append(f"**Insight ID:** {insight.get('id', 'Unknown')}")
context_parts.append(f"**Type:** {insight.get('insight_type', 'Unknown')}") context_parts.append(
context_parts.append(f"**Content:** {insight.get('content', 'No content')}") f"**Type:** {insight.get('insight_type', 'Unknown')}"
)
context_parts.append(
f"**Content:** {insight.get('content', 'No content')}"
)
context_parts.append("") # Empty line for separation context_parts.append("") # Empty line for separation
# Add metadata # Add metadata

View file

@ -12,7 +12,7 @@ from langchain_text_splitters import RecursiveCharacterTextSplitter
from .token_utils import token_count from .token_utils import token_count
# Pattern for matching thinking content in AI responses # Pattern for matching thinking content in AI responses
THINK_PATTERN = re.compile(r'<think>(.*?)</think>', re.DOTALL) THINK_PATTERN = re.compile(r"<think>(.*?)</think>", re.DOTALL)
def split_text(txt: str, chunk_size=500): def split_text(txt: str, chunk_size=500):
@ -114,7 +114,7 @@ def parse_thinking_content(content: str) -> Tuple[str, str]:
cleaned_content = THINK_PATTERN.sub("", content) cleaned_content = THINK_PATTERN.sub("", content)
# Clean up extra whitespace # Clean up extra whitespace
cleaned_content = re.sub(r'\n\s*\n\s*\n', '\n\n', cleaned_content).strip() cleaned_content = re.sub(r"\n\s*\n\s*\n", "\n\n", cleaned_content).strip()
return thinking_content, cleaned_content return thinking_content, cleaned_content