Julia Ostheimer commited on
Commit
d894086
·
1 Parent(s): 6e72fd0

Add function to convert user input prompt into query

Browse files
Files changed (1) hide show
  1. app.py +35 -1
app.py CHANGED
@@ -77,7 +77,41 @@ def query_or_respond(state: MessagesState):
77
  llm_with_tools = llm.bind_tools([retrieve])
78
  response = llm_with_tools.invoke(state["messages"], config={"callbacks": [langfuse_handler]})
79
  # MessagesState appends messages to state instead of overwriting
80
- return {"messages": [response]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
 
83
  graph_builder = StateGraph(MessagesState)
 
77
  llm_with_tools = llm.bind_tools([retrieve])
78
  response = llm_with_tools.invoke(state["messages"], config={"callbacks": [langfuse_handler]})
79
  # MessagesState appends messages to state instead of overwriting
80
+ return {"messages": [response]}
81
+
82
+ def trigger_ai_message_with_tool_call(state: MessagesState) -> AIMessage:
83
+ """
84
+ Takes the last user message from the state and returns an AIMessage
85
+ with example tool_calls populated.
86
+
87
+ Args:
88
+ state (dict): A dictionary with a 'messages' key containing a list of LangChain messages.
89
+
90
+ Returns:
91
+ AIMessage: An AIMessage with tool_calls based on the last user message.
92
+ """
93
+
94
+ # Filter for user messages
95
+ user_messages = [msg for msg in state["messages"] if isinstance(msg, HumanMessage)]
96
+
97
+ if not user_messages:
98
+ raise ValueError("No user messages found in the previous messages.")
99
+
100
+ last_user_msg = user_messages[-1]
101
+
102
+ tool_call = ToolCall(
103
+ name="retrieve",
104
+ args={"query": last_user_msg.content},
105
+ id="tool_call_1"
106
+ )
107
+
108
+ # Construct the AIMessage with tool_calls
109
+ ai_message = AIMessage(
110
+ content="Calling the retrieve function...",
111
+ tool_calls=[tool_call]
112
+ )
113
+
114
+ return {"messages": [ai_message]}
115
 
116
 
117
  graph_builder = StateGraph(MessagesState)