open-notebook/open_notebook/graphs/transformation.py
LUIS NOVO ab5560c9a2 refactor: reorganize folder structure for better maintainability
Changes:
- Move migrations/ under open_notebook/database/migrations/
- Extract AI models to open_notebook/ai/ (Model, ModelManager, provision)
- Extract podcasts to open_notebook/podcasts/ (EpisodeProfile, SpeakerProfile, PodcastEpisode)
- Reorganize prompts to mirror graphs structure (chat/, source_chat/)

This improves code organization by:
- Consolidating database concerns (migrations now with database code)
- Separating AI infrastructure from domain entities
- Isolating podcast feature into its own module
- Creating consistent prompt/graph naming conventions

All 52 tests pass.
2026-01-03 14:04:27 -03:00

65 lines
2.4 KiB
Python

from ai_prompter import Prompter
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.runnables import RunnableConfig
from langgraph.graph import END, START, StateGraph
from typing_extensions import TypedDict
from open_notebook.ai.provision import provision_langchain_model
from open_notebook.domain.notebook import Source
from open_notebook.domain.transformation import DefaultPrompts, Transformation
from open_notebook.utils import clean_thinking_content
class TransformationState(TypedDict):
input_text: str
source: Source
transformation: Transformation
output: str
async def run_transformation(state: dict, config: RunnableConfig) -> dict:
source_obj = state.get("source")
source: Source = source_obj if isinstance(source_obj, Source) else None # type: ignore[assignment]
content = state.get("input_text")
assert source or content, "No content to transform"
transformation: Transformation = state["transformation"]
if not content:
content = source.full_text
transformation_template_text = transformation.prompt
default_prompts: DefaultPrompts = DefaultPrompts(transformation_instructions=None)
if default_prompts.transformation_instructions:
transformation_template_text = f"{default_prompts.transformation_instructions}\n\n{transformation_template_text}"
transformation_template_text = f"{transformation_template_text}\n\n# INPUT"
system_prompt = Prompter(template_text=transformation_template_text).render(
data=state
)
content_str = str(content) if content else ""
payload = [SystemMessage(content=system_prompt), HumanMessage(content=content_str)]
chain = await provision_langchain_model(
str(payload),
config.get("configurable", {}).get("model_id"),
"transformation",
max_tokens=5055,
)
response = await chain.ainvoke(payload)
# Clean thinking content from the response
response_content = response.content if isinstance(response.content, str) else str(response.content)
cleaned_content = clean_thinking_content(response_content)
if source:
await source.add_insight(transformation.title, cleaned_content)
return {
"output": cleaned_content,
}
agent_state = StateGraph(TransformationState)
agent_state.add_node("agent", run_transformation) # type: ignore[type-var]
agent_state.add_edge(START, "agent")
agent_state.add_edge("agent", END)
graph = agent_state.compile()