prompt-caller 0.0.5__py3-none-any.whl → 0.1.1__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.
@@ -5,7 +5,8 @@ import requests
5
5
  import yaml
6
6
  from dotenv import load_dotenv
7
7
  from jinja2 import Template
8
- from langchain_core.messages import HumanMessage, SystemMessage
8
+ from langchain_core.tools import tool
9
+ from langchain_core.messages import HumanMessage, SystemMessage, ToolMessage
9
10
  from langchain_openai import ChatOpenAI
10
11
  from PIL import Image
11
12
  from pydantic import BaseModel, Field, create_model
@@ -18,6 +19,9 @@ load_dotenv()
18
19
 
19
20
  class PromptCaller:
20
21
 
22
+ def __init__(self, promptPath="prompts"):
23
+ self.promptPath = promptPath
24
+
21
25
  def _loadPrompt(self, file_path):
22
26
  with open(file_path, "r", encoding="utf-8") as file:
23
27
  content = file.read()
@@ -61,7 +65,7 @@ class PromptCaller:
61
65
  context = {}
62
66
 
63
67
  configuration, template = self._loadPrompt(
64
- os.path.join("prompts", f"{promptName}.prompt")
68
+ os.path.join(self.promptPath, f"{promptName}.prompt")
65
69
  )
66
70
 
67
71
  template = self._renderTemplate(template, context)
@@ -124,3 +128,79 @@ class PromptCaller:
124
128
  response = chat.invoke(messages)
125
129
 
126
130
  return response
131
+
132
+ def agent(self, promptName, context=None, tools=None, allowed_steps=3):
133
+
134
+ configuration, messages = self.loadPrompt(promptName, context)
135
+
136
+ output = None
137
+
138
+ if "output" in configuration:
139
+ output = configuration.get("output")
140
+ configuration.pop("output")
141
+
142
+ for message in messages:
143
+ if isinstance(message, SystemMessage):
144
+ message.content += "\nOnly use the tool DynamicModel when providing an output call."
145
+ break
146
+
147
+ chat = ChatOpenAI(**configuration)
148
+
149
+ # Register the tools
150
+ if tools is None:
151
+ tools = []
152
+
153
+ # Transform functions in tools
154
+ tools = [tool(t) for t in tools]
155
+
156
+ tools_dict = {t.name.lower(): t for t in tools}
157
+
158
+ if output:
159
+ dynamicModel = self.createPydanticModel(output)
160
+
161
+ tools.extend([dynamicModel])
162
+ tools_dict["dynamicmodel"] = dynamicModel
163
+
164
+ chat = chat.bind_tools(tools)
165
+
166
+ try:
167
+ # First LLM invocation
168
+ response = chat.invoke(messages)
169
+ messages.append(response)
170
+
171
+ steps = 0
172
+ while response.tool_calls and steps < allowed_steps:
173
+ for tool_call in response.tool_calls:
174
+ tool_name = tool_call["name"].lower()
175
+
176
+ # If it's the final formatting tool, validate and return
177
+ if tool_name == "dynamicmodel":
178
+ return dynamicModel.model_validate(tool_call["args"])
179
+
180
+ selected_tool = tools_dict.get(tool_name)
181
+ if not selected_tool:
182
+ raise ValueError(f"Unknown tool: {tool_name}")
183
+
184
+ # Invoke the selected tool with provided arguments
185
+ tool_response = selected_tool.invoke(tool_call)
186
+ messages.append(tool_response)
187
+
188
+ # If the latest message is a ToolMessage, re-invoke the LLM
189
+ if isinstance(messages[-1], ToolMessage):
190
+ response = chat.invoke(messages)
191
+ messages.append(response)
192
+ else:
193
+ break
194
+
195
+ steps += 1
196
+
197
+ # Final LLM call if the last message is still a ToolMessage
198
+ if isinstance(messages[-1], ToolMessage):
199
+ response = chat.invoke(messages)
200
+ messages.append(response)
201
+
202
+ return response
203
+
204
+ except Exception as e:
205
+ # Replace with appropriate logging in production
206
+ raise RuntimeError("Error during agent process") from e
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: prompt_caller
3
- Version: 0.0.5
3
+ Version: 0.1.1
4
4
  Summary: This package is responsible for calling prompts in a specific format. It uses LangChain and OpenAI API
5
5
  Home-page: https://github.com/ThiNepo/prompt-caller
6
6
  Author: Thiago Nepomuceno
@@ -87,6 +87,36 @@ In this example:
87
87
  - The `expression` value `3+8/9` is injected into the user message.
88
88
  - The model will respond with both the result of the expression and an explanation, as specified in the `output` section of the prompt.
89
89
 
90
+ 3. **Using the agent feature:**
91
+
92
+ The `agent` method allows you to enhance the prompt's functionality by integrating external tools. Here’s an example where we evaluate a mathematical expression using Python’s `eval` in a safe execution environment:
93
+
94
+ ```python
95
+ from prompt_caller import PromptCaller
96
+
97
+ ai = PromptCaller()
98
+
99
+ def evaluate_expression(expression: str):
100
+ """
101
+ Evaluate a math expression using eval.
102
+ """
103
+ safe_globals = {"__builtins__": None}
104
+ return eval(expression, safe_globals, {})
105
+
106
+ response = ai.agent(
107
+ "sample-agent", {"expression": "3+8/9"}, tools=[evaluate_expression]
108
+ )
109
+
110
+ print(response)
111
+ ```
112
+
113
+ In this example:
114
+
115
+ - The `agent` method is used to process the prompt while integrating external tools.
116
+ - The `evaluate_expression` function evaluates the mathematical expression securely.
117
+ - The response includes the processed result based on the prompt and tool execution.
118
+
119
+
90
120
  ## How It Works
91
121
 
92
122
  1. **\_loadPrompt:** Loads the prompt file, splits the YAML header from the body, and parses them.
@@ -0,0 +1,8 @@
1
+ prompt_caller/__init__.py,sha256=4EGdeAJ_Ig7A-b-e17-nYbiXjckT7uL3to5lchMsoW4,41
2
+ prompt_caller/__main__.py,sha256=dJ0dYtVmnhZuoV79R6YiAIta1ZkUKb-TEX4VEuYbgk0,139
3
+ prompt_caller/prompt_caller.py,sha256=fy-pLXmYD2j5fnAxgBvxCNBrkQvDPGX0nWyqnaWeqSo,6737
4
+ prompt_caller-0.1.1.dist-info/LICENSE,sha256=HrhfyXIkWY2tGFK11kg7vPCqhgh5DcxleloqdhrpyMY,11558
5
+ prompt_caller-0.1.1.dist-info/METADATA,sha256=j1xmg_Y_NAhh7CmlCgAcfSMuHChZ9C4DPcszLADg7dk,4909
6
+ prompt_caller-0.1.1.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
7
+ prompt_caller-0.1.1.dist-info/top_level.txt,sha256=iihiDRq-0VrKB8IKjxf7Lrtv-fLMq4tvgM4fH3x0I94,14
8
+ prompt_caller-0.1.1.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- prompt_caller/__init__.py,sha256=4EGdeAJ_Ig7A-b-e17-nYbiXjckT7uL3to5lchMsoW4,41
2
- prompt_caller/__main__.py,sha256=dJ0dYtVmnhZuoV79R6YiAIta1ZkUKb-TEX4VEuYbgk0,139
3
- prompt_caller/prompt_caller.py,sha256=k5xiY_COGxvuRVOgaVuBzO9vbrUMFrzjG4EXarMj_jk,3899
4
- prompt_caller-0.0.5.dist-info/LICENSE,sha256=HrhfyXIkWY2tGFK11kg7vPCqhgh5DcxleloqdhrpyMY,11558
5
- prompt_caller-0.0.5.dist-info/METADATA,sha256=Xk8vrEi0fyyQtcXlPT4QhDtc4TPW6cps0cWHW2tYJI8,3947
6
- prompt_caller-0.0.5.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
7
- prompt_caller-0.0.5.dist-info/top_level.txt,sha256=iihiDRq-0VrKB8IKjxf7Lrtv-fLMq4tvgM4fH3x0I94,14
8
- prompt_caller-0.0.5.dist-info/RECORD,,