-
Notifications
You must be signed in to change notification settings - Fork 0
Fix/workflow structure #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,43 +1,57 @@ | ||||||||||||||||||||||||||
| """Nodo 4: Parafraseo - Paraphrases user input.""" | ||||||||||||||||||||||||||
| """Nodo 4: Parafraseo - Saves message, retrieves chat history, and paraphrases user input.""" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| from app.agents.state import AgentState | ||||||||||||||||||||||||||
| from langchain_core.messages import SystemMessage | ||||||||||||||||||||||||||
| from langchain_openai import ChatOpenAI | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| llm = ChatOpenAI(model="gpt-5-nano") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def parafraseo(state: AgentState) -> AgentState: | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| Parafraseo node - Paraphrases the user input. | ||||||||||||||||||||||||||
| Parafraseo node - Saves message to DB, retrieves chat history, and paraphrases user input. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| This node: | ||||||||||||||||||||||||||
| 1. Takes the adjusted text from Fallback Inicial | ||||||||||||||||||||||||||
| 2. Paraphrases it to improve clarity or adjust format | ||||||||||||||||||||||||||
| 3. Prepares text for retrieval step | ||||||||||||||||||||||||||
| 1. Saves the user's message to the chat session in PostgreSQL | ||||||||||||||||||||||||||
| 2. Retrieves all chat messages for the session (including the newly saved message) | ||||||||||||||||||||||||||
| 3. Paraphrases the user input using chat history to improve clarity | ||||||||||||||||||||||||||
| 4. Prepares text for retrieval step | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||
| state: Agent state containing adjusted_text | ||||||||||||||||||||||||||
| state: Agent state containing prompt, chat_session_id, and user_id | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||
| Updated state with paraphrased_text set | ||||||||||||||||||||||||||
| Updated state with chat_messages, paraphrased_text set | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| # TODO: Implement paraphrasing logic | ||||||||||||||||||||||||||
| updated_state = state.copy() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # TODO: Implement endpoint call to save message and retrieve chat history | ||||||||||||||||||||||||||
| # This should: | ||||||||||||||||||||||||||
| # 1. Use an LLM or paraphrasing model to rephrase the text | ||||||||||||||||||||||||||
| # 2. Improve clarity, adjust tone, or format as needed | ||||||||||||||||||||||||||
| # 3. Set paraphrased_text with the result | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Paraphrase the last message using history | ||||||||||||||||||||||||||
| # 1. Call an endpoint (not yet developed) that: | ||||||||||||||||||||||||||
| # - Saves the current user message to the chat session | ||||||||||||||||||||||||||
| # - Retrieves all chat messages for the session (including the newly saved message) | ||||||||||||||||||||||||||
| # - Returns the updated chat_messages list | ||||||||||||||||||||||||||
| # 2. Update state with chat_messages from the endpoint response | ||||||||||||||||||||||||||
| # 3. Handle errors appropriately (session not found, permission denied, etc.) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Placeholder: For now, we'll use empty chat history | ||||||||||||||||||||||||||
| # Once the endpoint is implemented, replace this with the actual endpoint call | ||||||||||||||||||||||||||
| updated_state["chat_messages"] = None | ||||||||||||||||||||||||||
| logger.warning("Chat history retrieval endpoint not yet implemented - using empty history") | ||||||||||||||||||||||||||
|
Comment on lines
+32
to
+44
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial LGTM! Clear TODO with implementation plan. The TODO block provides clear guidance on what needs to be implemented, including the endpoint responsibilities and error handling considerations. The placeholder implementation with a warning log is appropriate for the current state. Once the endpoint is developed, this integration will complete the security fix described in the PR objectives. Would you like me to help draft the endpoint integration code when ready? |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Paraphrase the last message using history | ||||||||||||||||||||||||||
| system_instruction = """You are an expert at paraphrasing user questions to be standalone and clear, given the conversation history. | ||||||||||||||||||||||||||
| Reformulate the last user message to be a self-contained query that includes necessary context from previous messages. | ||||||||||||||||||||||||||
| Do not answer the question, just rewrite it.""" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| messages = [SystemMessage(content=system_instruction)] + state["messages"] | ||||||||||||||||||||||||||
| # Use messages from state (will include chat history once endpoint is implemented) | ||||||||||||||||||||||||||
| messages = [SystemMessage(content=system_instruction)] + state.get("messages", []) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| response = llm.invoke(messages) | ||||||||||||||||||||||||||
| updated_state = state.copy() # Create a copy of the state to update | ||||||||||||||||||||||||||
| updated_state["paraphrased_text"] = response.content | ||||||||||||||||||||||||||
|
Comment on lines
+51
to
55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent state access - should use Line 52 uses Consider updating the code to use - # Use messages from state (will include chat history once endpoint is implemented)
- messages = [SystemMessage(content=system_instruction)] + state.get("messages", [])
+ # Use chat_messages from state (will include chat history once endpoint is implemented)
+ chat_msgs = state.get("chat_messages") or state.get("messages", [])
+ messages = [SystemMessage(content=system_instruction)] + chat_msgsThis ensures the paraphrasing will automatically use the retrieved chat history once 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return updated_state | ||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation claims functionality not yet implemented.
The docstring states that this node "Saves the user's message to the chat session in PostgreSQL" and "Retrieves all chat messages for the session" (lines 19-20), but the implementation shows this is not yet developed (see TODO at lines 32-44). The function currently uses empty chat history with a warning log.
Update the docstring to reflect the current implementation status:
📝 Committable suggestion
🤖 Prompt for AI Agents