agentx-dev 2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,349 @@
1
+ """
2
+ This module contains classes and functions for working with agent prompts and JSON data.
3
+
4
+ It includes classes for different agent prompt templates, a function to convert strings to JSON,
5
+ and a container class for standard pre-defined agent prompt templates.
6
+ """
7
+
8
+ import yaml,logging,json,uuid,time,os
9
+ from pydantic import BaseModel, Field, ValidationError
10
+ from typing import Dict, Any,Type,Optional,List,Union
11
+ from rich.console import Console
12
+ console=Console()
13
+ # Set up logging
14
+ logging.basicConfig(level=logging.ERROR)
15
+ logger = logging.getLogger(__name__)
16
+
17
+ class StandardParser(BaseModel):
18
+ """
19
+ A Pydantic model representing the standard parser.
20
+
21
+ Attributes:
22
+ - action: str, the action to take.
23
+ - action_input: str or Dict, the input to the action.
24
+
25
+ Methods:
26
+ - from_json: Creates an instance from a JSON string.
27
+ """
28
+ action: str = Field("The action to take", alias='action')
29
+ action_input: str | Dict = Field("The input to the action", alias='action_input')
30
+
31
+ class Config:
32
+ populate_by_name = True
33
+
34
+ @classmethod
35
+ def from_json(cls, json_str: str):
36
+ try:
37
+ json_object = convert_to_json(json_str)
38
+ if isinstance(json_object,dict):
39
+ return cls(**json_object)
40
+ elif isinstance(json_object,str):
41
+ return json_object
42
+ except (ValueError, ValidationError) as e:
43
+ logger.error(f"Error creating StandardParser instance: {e}")
44
+ raise
45
+
46
+
47
+
48
+
49
+
50
+
51
+ def load_prompt_templates(file_path: str) -> Dict:
52
+ """
53
+ Loads prompt templates from a YAML file.
54
+
55
+ Args:
56
+ - file_path: str, the path to the YAML file containing the prompt templates.
57
+
58
+ Returns:
59
+ - Dict: A dictionary containing the loaded prompt templates.
60
+ """
61
+ if not os.path.exists(file_path):
62
+ raise FileNotFoundError(f"Can't find file {file_path}")
63
+ with open(file_path, 'r') as yaml_:
64
+
65
+ return yaml.safe_load(yaml_)
66
+
67
+ system = load_prompt_templates( r"..\promptTemplate.yaml")
68
+
69
+
70
+ def convert_to_json(json_str: str) -> Dict[str, Any]:
71
+ """
72
+ Converts a given string into a JSON object.
73
+
74
+ Args:
75
+ - json_str: str, the string to be converted into JSON.
76
+
77
+ Returns:
78
+ - Dict[str, Any]: A dictionary representing the JSON object.
79
+
80
+ Raises:
81
+ - json.JSONDecodeError: If the input string is not valid JSON.
82
+ """
83
+ try:
84
+ if json_str.startswith("```json") and json_str.endswith("```"):
85
+ return json.loads(json_str.split("```json")[-1].split("```")[0])
86
+ elif json_str.startswith('{') and json_str.endswith('}'):
87
+ return json.loads(json_str)
88
+ else :
89
+ return json_str
90
+ except json.JSONDecodeError as e:
91
+ logger.error(f"Failed to decode JSON: {e}")
92
+ return f"Failed to decode JSON: {e}" # or you can re-raise the exception, depending on your error handling strategy
93
+
94
+ # Consider adding a Config class to these models to allow population by field name
95
+ class Instruction_Tuned_(BaseModel):
96
+ """
97
+ A Pydantic model representing the Instruction-Tuned prompt template.
98
+
99
+ Attributes:
100
+ - action: str, the action to take or final answer.
101
+ - action_input: str or Dict, the input to the action.
102
+
103
+ Methods:
104
+ - from_json: Creates an instance from a JSON string.
105
+ """
106
+ action: str = Field("The action to take OR Final Answer", alias="action")
107
+ action_input: str | Dict = Field("The input to the action OR You should put what you want to return to use here and return to user immediately", alias="action_input")
108
+
109
+
110
+ @classmethod
111
+ def from_json(cls, json_str: str):
112
+ try:
113
+ json_object = convert_to_json(json_str)
114
+ if isinstance(json_object,dict):
115
+ return cls(**json_object)
116
+ elif isinstance(json_object,str):
117
+ return json_object
118
+ except (ValueError, ValidationError) as e:
119
+ # Handle the error, e.g., log it, raise a custom exception, or return a default value
120
+ error = f"Error creating Instruction_Tuned_ instance: {e}"
121
+ logger.error(error)
122
+ raise
123
+
124
+
125
+ class React_(BaseModel):
126
+ """
127
+ A Pydantic model representing the ReAct prompt template.
128
+
129
+ Attributes:
130
+ - Thought: str, the agent's thoughts.
131
+ - action: str, the action to take or final answer.
132
+ - action_input: str or Dict, the input to the action.
133
+
134
+ Methods:
135
+ - from_json: Creates an instance from a JSON string.
136
+ """
137
+ Thought: str = Field('The Agent Thoughts', alias='Thought')
138
+ action: str = Field("The action to take OR Final Answer", alias="action")
139
+ action_input: str | Dict = Field("The input to the action OR You should put what you want to return to use here and return to user immediately", alias="action_input")
140
+
141
+ @classmethod
142
+ def from_json(cls, json_str: str):
143
+ try:
144
+ json_object = convert_to_json(json_str)
145
+ if isinstance(json_object,dict):
146
+ console.print("[bold cyan]Thinking...[/bold cyan]")
147
+ print(f"\n{json_object['Thought']}")
148
+ return cls(**json_object)
149
+ elif isinstance(json_object,str):
150
+ return json_object
151
+ except (ValueError, ValidationError) as e:
152
+ # Handle the error, e.g., log it, raise a custom exception, or return a default value
153
+ error = f"Error creating React_ instance: {e}"
154
+ logger.error(error)
155
+ raise
156
+
157
+ class ChainOfThought(BaseModel):
158
+ """
159
+ A Pydantic model representing the Chain-of-Thought prompt template.
160
+
161
+ Attributes:
162
+ - Thought: str, the step-by-step reasoning.
163
+ - Action: str, the action to take or final answer.
164
+ - Action_Input: str or Dict, the input to the action.
165
+
166
+ Methods:
167
+ - from_json: Creates an instance from a JSON string.
168
+ """
169
+ Thought: str = Field('The step-by-step reasoning', alias='Thought')
170
+ Action: str = Field("The action to take or Final Answer", alias='Action')
171
+ Action_Input: str | Dict = Field("The input to the action", alias='Action Input')
172
+
173
+ @classmethod
174
+ def from_json(cls, json_str: str):
175
+ try:
176
+ json_object = convert_to_json(json_str)
177
+ if isinstance(json_object,dict):
178
+ console.print("[bold cyan]Thinking...[/bold cyan]")
179
+ print(f"\n{json_object['Thought']}")
180
+ return cls(**json_object)
181
+ elif isinstance(json_object,str):
182
+ return json_object
183
+ except (ValueError, ValidationError) as e:
184
+ logger.error(f"Error creating ChainOfThought instance: {e}")
185
+ raise
186
+
187
+ class ZeroShot(BaseModel):
188
+ """
189
+ A Pydantic model representing the Zero-Shot prompt template.
190
+
191
+ Attributes:
192
+ - action: str, the action to take.
193
+ - action_input: str or Dict, the input to the action.
194
+
195
+ Methods:
196
+ - from_json: Creates an instance from a JSON string.
197
+ """
198
+ action: str = Field("The action to take", alias='action')
199
+ action_input: str | Dict = Field("The input to the action", alias='action_input')
200
+
201
+ @classmethod
202
+ def from_json(cls, json_str: str):
203
+ try:
204
+ json_object = convert_to_json(json_str)
205
+ if isinstance(json_object,dict):
206
+ return cls(**json_object)
207
+ elif isinstance(json_object,str):
208
+ return json_object
209
+ except (ValueError, ValidationError) as e:
210
+ logger.error(f"Error creating ZeroShot instance: {e}")
211
+ raise
212
+
213
+ class FewShot(BaseModel):
214
+ """
215
+ A Pydantic model representing the Few-Shot prompt template.
216
+
217
+ Attributes:
218
+ - action: str, the action to take.
219
+ - action_input: str or Dict, the input to the action.
220
+
221
+ Methods:
222
+ - from_json: Creates an instance from a JSON string.
223
+ """
224
+ action: str = Field("The action to take", alias='action')
225
+ action_input: str | Dict = Field("The input to the action", alias='action_input')
226
+
227
+ @classmethod
228
+ def from_json(cls, json_str: str):
229
+ try:
230
+ json_object = convert_to_json(json_str)
231
+ if isinstance(json_object,dict):
232
+ return cls(**json_object)
233
+ elif isinstance(json_object,str):
234
+ return json_object
235
+ except (ValueError, ValidationError) as e:
236
+ logger.error(f"Error creating FewShot instance: {e}")
237
+ raise
238
+
239
+
240
+ class AgentFormattor(BaseModel):
241
+ prompt: str
242
+ Agent: Type[BaseModel]=Field(description="The agent that will under the prompt and extract what's needed")
243
+
244
+ @classmethod
245
+ def Formattor(cls,prompt:str,Agent:Type[BaseModel]):
246
+ return cls(prompt=prompt,Agent=Agent)
247
+
248
+
249
+ class AgentPrompt:
250
+ """
251
+ Manages and formats the prompt template for the agent, allowing for
252
+ flexible placeholder values.
253
+ """
254
+ def __init__(self, prompt: str):
255
+ """
256
+ Initializes the prompt with a template string.
257
+ """
258
+ self.prompt = prompt
259
+
260
+ def format(self, tools: List,user_hold, **kwargs: Any) -> str:
261
+ """
262
+ Formats the prompt template with dynamic values.
263
+
264
+ Args:
265
+ tools (List): A list of tool objects to populate {tools} and {tool_names}.
266
+ **kwargs (Any): Optional. Any other key-value pairs corresponding to
267
+ placeholders in the template (e.g., user_input="...").
268
+ Returns:
269
+ str: The fully formatted prompt.
270
+ """
271
+ # Build the dictionary of values that are always available.
272
+ format_values = {
273
+ 'tools': "\n".join([f"- {t.name}: {t.description}" for t in tools]),
274
+ 'tool_names': ", ".join([t.name for t in tools]),
275
+ 'user_input' : user_hold
276
+
277
+ }
278
+
279
+ # The update() method merges the optional kwargs.
280
+ # If kwargs is empty, this does nothing.
281
+ format_values.update(kwargs)
282
+
283
+ # format_map uses the combined dictionary to fill in the template.
284
+ return self.prompt.format_map(format_values)
285
+
286
+
287
+
288
+
289
+ class AgentType:
290
+ """
291
+ A container for standard, pre-defined agent prompt templates.
292
+
293
+ This class holds various prompt styles as class attributes, making them
294
+ easy to access and use to initialize an AgentPrompt.
295
+
296
+ Example:
297
+ react_prompt_template = AgentType.ReAct
298
+ agent_prompt = AgentPrompt(template=react_prompt_template)
299
+ """
300
+
301
+ ReAct = AgentFormattor.Formattor(prompt=system['ReAct'],Agent=React_)
302
+ Instruction_Tuned = AgentFormattor.Formattor(prompt=system['Instruction-Tuned'],Agent=Instruction_Tuned_)
303
+ Chain_of_Thought = AgentFormattor.Formattor(prompt=system['Chain-of-Thought'],Agent=ChainOfThought)
304
+ Zero_Shot = AgentFormattor.Formattor(prompt=system['Zero-Shot'],Agent=ZeroShot)
305
+ Few_Shot = AgentFormattor.Formattor(prompt=system['Few-Shot'],Agent=FewShot)
306
+
307
+
308
+
309
+
310
+ class ToolCall(BaseModel):
311
+ name: str
312
+ args: Dict[str, Any]
313
+ result: str
314
+
315
+
316
+ class ToolCall(BaseModel):
317
+ name: str
318
+ args: Dict[str, Any]
319
+ result: str
320
+
321
+ class AgentCompletion(BaseModel):
322
+ id: str
323
+ object: str = "agent.completion"
324
+ created: int
325
+ model: str
326
+ query: str
327
+ content: str
328
+ tool_calls: List[ToolCall] = Field(default_factory=list)
329
+ steps: List[str] = Field(default_factory=list)
330
+ history: Optional[List[Dict[str, str]]] = None
331
+
332
+
333
+ @classmethod
334
+ def from_agent(cls, *, model_name: str, query: str, content: Union[str, Dict[str, Any], List[Dict[str, Any]]], tool_calls: Optional[List[ToolCall]] = None, steps: Optional[List[str]] = None, history: List[Dict[str, str]]):
335
+ if isinstance(content, (dict, list)):
336
+ content_str = json.dumps(content, ensure_ascii=False)
337
+ else:
338
+ content_str = content
339
+ return cls(
340
+ id=str(uuid.uuid4()),
341
+ created=int(time.time()),
342
+ model=model_name,
343
+ query=query,
344
+ content=content_str,
345
+ tool_calls=tool_calls,
346
+ steps=steps,
347
+ history=history
348
+ )
349
+
@@ -0,0 +1,11 @@
1
+ from .Agent import AgentFormattor,AgentPrompt,AgentType,AgentCompletion,StandardParser,ToolCall
2
+
3
+
4
+ __all__ =[
5
+ 'AgentFormattor',
6
+ 'AgentCompletion',
7
+ 'AgentPrompt',
8
+ 'AgentType',
9
+ 'StandardParser',
10
+ 'ToolCall'
11
+ ]
@@ -0,0 +1,139 @@
1
+ ReAct: |
2
+ Nova is an intelligent, friendly assistant capable of using external tools. Nova never guesses and always reasons before responding.You have access to the following Tools:
3
+ {tools}
4
+
5
+
6
+
7
+ This are the tool name when calling the tool:
8
+ {tool_names}
9
+
10
+
11
+ dont provide content outside the json for all should be put in the json
12
+ Always Follow this exact but ,must be a valid json format either for tool or final output reasoning is IMPORTANT:
13
+ DONT add '```json just a valid json
14
+ {{
15
+ "Thought" : "<your reasoning of 20 seconds>",
16
+ "action": "<tool_name / Final_Answer>",
17
+ "action_input": "<tool input>""
18
+ }}
19
+
20
+ Observation: <result of tool>
21
+ ...repeat if needed...
22
+
23
+ User Input: {user_input}
24
+ Nova:
25
+
26
+ Instruction-Tuned: |
27
+ You are Nova, a helpful AI assistant. When you can't answer directly, you intelligently use tools.
28
+
29
+ Use this exact json format:
30
+ ```json
31
+ {{"action": "<tool_name> / Final_Answer", "action_input": "<input>"}}
32
+ ```
33
+ You must always be honest, safe, and clear. You never guess.
34
+
35
+ Available tools:
36
+ {tools}
37
+
38
+ Tools names:
39
+ {tool_names}
40
+
41
+ User: {user_input}
42
+ Nova:
43
+
44
+ Chain-of-Thought: |
45
+ You are Nova, a smart assistant. Think through the problem step by step before choosing an action.
46
+
47
+ You can use any of these tools:
48
+ {tools}
49
+
50
+ Tools names:
51
+ {tool_names}
52
+
53
+ Human: {user_input}
54
+
55
+ IMPORTANT: You must respond in this exact json format:
56
+
57
+ {{
58
+ Thought: [Your step-by-step reasoning]
59
+ Action: [tool_name] / Final_Answer
60
+ Action Input: [input_value]
61
+ }}
62
+
63
+ Example:
64
+ {{
65
+ Thought: The user wants to know the weather in Barrie. I should use the weather tool.
66
+ Action: weather
67
+ Action Input: Barrie
68
+ }}
69
+
70
+ Zero-Shot: |
71
+ You are Nova, an intelligent assistant with tool-usage capabilities.
72
+
73
+ Your goal is to help the human using tools when needed. Be honest, clear, and structured. Respond directly, or use a tool by formatting your output like this:
74
+
75
+ {{"action": "<tool_name> / Final_Answer", "action_input": "<input>" }}
76
+
77
+ Available tools:
78
+ {tools}
79
+
80
+ Tools names:
81
+ {tool_names}
82
+
83
+ Human: {user_input}
84
+ Nova:
85
+
86
+ Few-Shot: |
87
+ You are Nova, a reasoning AI agent who can use external tools to assist the human. You follow patterns from past conversations and think before acting.
88
+
89
+ Tools:
90
+ {tools}
91
+
92
+ Tools name:
93
+ {tool_names}
94
+
95
+ ### Example 1
96
+ Human: What’s the population of Canada?
97
+ Nova:
98
+
99
+ {{ "action": "KnowledgeSearch", "action_input": "Population of Canada" }}
100
+
101
+ ### Example 2
102
+ Human: what is 2+2
103
+ Nova:
104
+
105
+ {{"action":"Final_Answer","action_input" : "4"}}
106
+ Human: {user_input}
107
+
108
+ resume_AI: |
109
+ ### YOUR ROLE & GOAL ###
110
+ You are a professional AI assistant representing [Your Name]. Your primary goal is to provide accurate and helpful answers to a recruiter who is asking questions about [Your Name]'s resume.
111
+
112
+ ### YOUR KNOWLEDGE BASE ###
113
+ Your entire knowledge base is the text of [Your Name]'s resume, which is provided below the line. You cannot access any outside information.
114
+
115
+ ### RULES OF ENGAGEMENT ###
116
+ 1. **Accuracy is Paramount:** You must only use information explicitly stated in the resume. Do not make assumptions or infer details.
117
+ 2. **Handle Unknowns Gracefully:** If the answer to a question is not in the resume, you must politely state: "I don't have that specific detail in the resume, but [Your Name] would be happy to elaborate on that during an interview."
118
+ 3. **Maintain Persona:** Answer from a third-person perspective (e.g., "[Your Name] has experience in...", "His skills include...").
119
+ 4. **Be Professional and Concise:** Avoid overly casual language. Get straight to the point while remaining polite.
120
+ 5. **Do Not Speculate:** Do not offer opinions on [Your Name]'s strengths or weaknesses unless the resume text directly supports it (e.g., a section titled "Key Strengths").
121
+
122
+ ---
123
+ Use this exact json format:
124
+
125
+ {{"action": "<tool_name> / Final_Answer", "action_input": "<input>"}}
126
+
127
+ You must always be honest, safe, and clear. You never guess.
128
+ Question should be about the person who resume is in question anything outside just say something polite to say you cant do that only answering question about resume.
129
+
130
+ Available tools:
131
+ {tools}
132
+
133
+ Tools names:
134
+ {tool_names}
135
+
136
+ you can repeat n times if needed
137
+
138
+ User: {user_input}
139
+ ---