Nico8800 commited on
Commit
e051030
·
1 Parent(s): a755c90

add curl agent with elbow and shoulder tools

Browse files
Files changed (1) hide show
  1. Modules/PoseEstimation/curl_agent.py +147 -0
Modules/PoseEstimation/curl_agent.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from Modules.PoseEstimation.pose_estimator import calculate_angle, joints_id_dict, model
2
+ from langchain.tools import tool
3
+ from langchain.agents import AgentExecutor, create_tool_calling_agent
4
+ from langchain_core.prompts import ChatPromptTemplate
5
+ from langchain_core.messages import HumanMessage
6
+ from langchain_mistralai.chat_models import ChatMistralAI
7
+
8
+ from operator import itemgetter
9
+ from typing import Dict, List, Union
10
+
11
+ from langchain_core.messages import AIMessage
12
+ from langchain_core.runnables import (
13
+ Runnable,
14
+ RunnableLambda,
15
+ RunnableMap,
16
+ RunnablePassthrough,
17
+ )
18
+
19
+ import numpy as np
20
+
21
+ # If api_key is not passed, default behavior is to use the `MISTRAL_API_KEY` environment variable.
22
+ llm = ChatMistralAI(model='mistral-large-latest', api_key="i5jSJkCFNGKfgIztloxTMjfckiFbYBj4")
23
+
24
+ @tool
25
+ def shoulder_angle(pose: list) -> float:
26
+
27
+ """
28
+ Computes the shoulder angle.
29
+
30
+ Args:
31
+ pose (list): list of keypoints
32
+
33
+ Returns:
34
+ arm_angle (float): arm angle with chest
35
+ """
36
+ right_elbow = pose[joints_id_dict['right_elbow']]
37
+ right_shoulder = pose[joints_id_dict['right_shoulder']]
38
+ right_hip = pose[joints_id_dict['right_hip']]
39
+
40
+ left_elbow = pose[joints_id_dict['left_elbow']]
41
+ left_shoulder = pose[joints_id_dict['left_shoulder']]
42
+ left_hip = pose[joints_id_dict['left_hip']]
43
+
44
+ right_arm_angle = calculate_angle(right_elbow, right_shoulder, right_hip)
45
+ left_arm_angle = calculate_angle(left_elbow, left_shoulder, left_hip)
46
+
47
+ return right_arm_angle
48
+
49
+
50
+ @tool
51
+ def elbow_angle(pose):
52
+ """
53
+ Computes the elbow angle.
54
+
55
+ Args:
56
+ pose (list): list of keypoints
57
+
58
+ Returns:
59
+ elbow_angle (float): elbow angle with chest
60
+ """
61
+ right_elbow = pose[joints_id_dict['right_elbow']]
62
+ right_shoulder = pose[joints_id_dict['right_shoulder']]
63
+ right_wrist = pose[joints_id_dict['right_wrist']]
64
+
65
+ left_elbow = pose[joints_id_dict['left_elbow']]
66
+ left_shoulder = pose[joints_id_dict['left_shoulder']]
67
+ left_wrist = pose[joints_id_dict['left_wrist']]
68
+
69
+ right_elbow_angle = calculate_angle(right_shoulder, right_elbow, right_wrist)
70
+ left_elbow_angle = calculate_angle(left_shoulder, left_elbow, left_wrist)
71
+
72
+ return right_elbow_angle
73
+
74
+
75
+ tools = [shoulder_angle, elbow_angle]
76
+
77
+ llm_with_tools = llm.bind_tools(tools)
78
+ tool_map = {tool.name: tool for tool in tools}
79
+
80
+ # prompt = ChatPromptTemplate.from_messages(
81
+ # [
82
+ # (
83
+ # "system",
84
+ # "You are a helpful assistant. Make sure to use the compute_right_knee_angle tool for information.",
85
+ # ),
86
+ # ("placeholder", "{chat_history}"),
87
+ # ("human", "{input}"),
88
+ # ("placeholder", "{agent_scratchpad}"),
89
+ # ]
90
+ # )
91
+
92
+ # Construct the Tools agent
93
+ # curl_agent = create_tool_calling_agent(llm, tools, prompt)
94
+
95
+
96
+ pose_sequence = [
97
+ # Pose 1
98
+ [
99
+ # Head
100
+ [50, 50],
101
+ # Shoulders
102
+ [40, 80], [60, 80],
103
+ # Elbows
104
+ [30, 110], [70, 110],
105
+ # Wrists
106
+ [25, 140], [75, 140],
107
+ # Hips
108
+ [45, 180], [55, 180],
109
+ # Knees
110
+ [40, 220], [60, 220],
111
+ # Ankles
112
+ [35, 250], [65, 250],
113
+ ],
114
+ # Pose 2
115
+ [
116
+ # Head
117
+ [60, 60],
118
+ # Shoulders
119
+ [50, 90], [70, 90],
120
+ # Elbows
121
+ [40, 120], [80, 120],
122
+ # Wrists
123
+ [35, 150], [85, 150],
124
+ # Hips
125
+ [55, 180], [65, 180],
126
+ # Knees
127
+ [50, 220], [70, 220],
128
+ # Ankles
129
+ [45, 250], [75, 250],
130
+ ]]
131
+
132
+ # Create an agent executor by passing in the agent and tools
133
+ # agent_executor = AgentExecutor(agent=curl_agent, tools=tools, verbose=True)
134
+ # agent_executor.invoke({"input": f"Compute shoulder and elbow angle and display them given the following pose estimation: {pose_sequence[0]}"})
135
+
136
+ def call_tools(msg: AIMessage) -> Runnable:
137
+ """Simple sequential tool calling helper."""
138
+ tool_map = {tool.name: tool for tool in tools}
139
+ tool_calls = msg.tool_calls.copy()
140
+ for tool_call in tool_calls:
141
+ tool_call["output"] = tool_map[tool_call["name"]].invoke(tool_call["args"])
142
+ return tool_calls
143
+
144
+
145
+ chain = llm_with_tools | call_tools
146
+
147
+ print(chain.invoke(f"What is the shoulder angle and elbow angle given the following pose estimation: {pose_sequence[0]}"))