quantalogic 0.59.3__py3-none-any.whl → 0.60.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.
Files changed (41) hide show
  1. quantalogic/agent.py +268 -24
  2. quantalogic/create_custom_agent.py +26 -78
  3. quantalogic/prompts/chat_system_prompt.j2 +10 -7
  4. quantalogic/prompts/code_2_system_prompt.j2 +190 -0
  5. quantalogic/prompts/code_system_prompt.j2 +142 -0
  6. quantalogic/prompts/doc_system_prompt.j2 +178 -0
  7. quantalogic/prompts/legal_2_system_prompt.j2 +218 -0
  8. quantalogic/prompts/legal_system_prompt.j2 +140 -0
  9. quantalogic/prompts/system_prompt.j2 +6 -2
  10. quantalogic/prompts/tools_prompt.j2 +2 -4
  11. quantalogic/prompts.py +23 -4
  12. quantalogic/server/agent_server.py +1 -1
  13. quantalogic/tools/__init__.py +2 -0
  14. quantalogic/tools/duckduckgo_search_tool.py +1 -0
  15. quantalogic/tools/execute_bash_command_tool.py +114 -57
  16. quantalogic/tools/file_tracker_tool.py +49 -0
  17. quantalogic/tools/google_packages/google_news_tool.py +3 -0
  18. quantalogic/tools/image_generation/dalle_e.py +89 -137
  19. quantalogic/tools/rag_tool/__init__.py +2 -9
  20. quantalogic/tools/rag_tool/document_rag_sources_.py +728 -0
  21. quantalogic/tools/rag_tool/ocr_pdf_markdown.py +144 -0
  22. quantalogic/tools/replace_in_file_tool.py +1 -1
  23. quantalogic/tools/terminal_capture_tool.py +293 -0
  24. quantalogic/tools/tool.py +4 -0
  25. quantalogic/tools/utilities/__init__.py +2 -0
  26. quantalogic/tools/utilities/download_file_tool.py +3 -5
  27. quantalogic/tools/utilities/llm_tool.py +283 -0
  28. quantalogic/tools/utilities/selenium_tool.py +296 -0
  29. quantalogic/tools/utilities/vscode_tool.py +1 -1
  30. quantalogic/tools/web_navigation/__init__.py +5 -0
  31. quantalogic/tools/web_navigation/web_tool.py +145 -0
  32. quantalogic/tools/write_file_tool.py +72 -36
  33. {quantalogic-0.59.3.dist-info → quantalogic-0.60.0.dist-info}/METADATA +1 -1
  34. {quantalogic-0.59.3.dist-info → quantalogic-0.60.0.dist-info}/RECORD +37 -28
  35. quantalogic/tools/rag_tool/document_metadata.py +0 -15
  36. quantalogic/tools/rag_tool/query_response.py +0 -20
  37. quantalogic/tools/rag_tool/rag_tool.py +0 -566
  38. quantalogic/tools/rag_tool/rag_tool_beta.py +0 -264
  39. {quantalogic-0.59.3.dist-info → quantalogic-0.60.0.dist-info}/LICENSE +0 -0
  40. {quantalogic-0.59.3.dist-info → quantalogic-0.60.0.dist-info}/WHEEL +0 -0
  41. {quantalogic-0.59.3.dist-info → quantalogic-0.60.0.dist-info}/entry_points.txt +0 -0
@@ -1,12 +1,12 @@
1
- """LLM Image Generation Tool for creating images using DALL-E or Stable Diffusion via AWS Bedrock."""
1
+ """DALL-E Image Generation Tool for creating images using DALL-E via AWS Bedrock."""
2
2
 
3
3
  import datetime
4
4
  import json
5
- from enum import Enum
6
5
  from pathlib import Path
7
6
  from typing import Any, Dict, Optional
8
7
 
9
- import requests
8
+ import aiohttp
9
+ import aiofiles
10
10
  from loguru import logger
11
11
  from pydantic import ConfigDict, Field
12
12
  from tenacity import retry, stop_after_attempt, wait_exponential
@@ -15,40 +15,28 @@ from quantalogic.generative_model import GenerativeModel
15
15
  from quantalogic.tools.tool import Tool, ToolArgument
16
16
 
17
17
 
18
- class ImageProvider(str, Enum):
19
- """Supported image generation providers."""
20
-
21
- DALLE = "dall-e"
22
- STABLE_DIFFUSION = "stable-diffusion"
23
-
24
-
25
- PROVIDER_CONFIGS = {
26
- ImageProvider.DALLE: {
27
- "model_name": "dall-e-3",
28
- "sizes": ["1024x1024", "1024x1792", "1792x1024"],
29
- "qualities": ["standard", "hd"],
30
- "styles": ["vivid", "natural"],
31
- },
32
- ImageProvider.STABLE_DIFFUSION: {
33
- # "model_name": "anthropic.claude-3-sonnet-20240229",
34
- "model_name": "amazon.titan-image-generator-v1",
35
- "sizes": ["1024x1024"], # Bedrock SD supported size
36
- "qualities": ["standard"], # SD quality is controlled by cfg_scale
37
- "styles": ["none"], # Style is controlled through prompting
38
- },
18
+ DALLE_CONFIG = {
19
+ "model_name": "dall-e-3",
20
+ "sizes": ["1024x1024", "1024x1792", "1792x1024"],
21
+ "qualities": ["standard", "hd"],
22
+ "styles": ["vivid", "natural"],
39
23
  }
40
24
 
41
25
 
42
26
  class LLMImageGenerationTool(Tool):
43
- """Tool for generating images using DALL-E or Stable Diffusion."""
27
+ """Tool for generating images using DALL-E."""
44
28
 
45
29
  model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow")
46
30
 
47
- name: str = Field(default="llm_image_generation_tool")
31
+ name: str = Field(default="dalle_image_generation_tool")
48
32
  description: str = Field(
49
33
  default=(
50
- "Generate images using DALL-E or Stable Diffusion. "
51
- "Supports different sizes, styles, and quality settings."
34
+ "Generate images using DALL-E 3. Simple to use with smart defaults:\n"
35
+ "1. Write a clear prompt describing your image\n"
36
+ "2. Choose image format: square(1024x1024), portrait(1024x1792), landscape(1792x1024)\n"
37
+ "3. Pick quality: standard(fast) or hd(detailed)\n"
38
+ "4. Select style: vivid(dramatic) or natural(realistic)\n"
39
+ "\nAll images are saved locally with metadata."
52
40
  )
53
41
  )
54
42
  arguments: list = Field(
@@ -56,22 +44,26 @@ class LLMImageGenerationTool(Tool):
56
44
  ToolArgument(
57
45
  name="prompt",
58
46
  arg_type="string",
59
- description="Text description of the image to generate",
47
+ description=(
48
+ "Describe what you want to see in the image. Include:\n"
49
+ "- Main subject or scene\n"
50
+ "- Style and mood\n"
51
+ "- Important details\n"
52
+ "- Colors or lighting\n"
53
+ "\nKeep it clear and specific"
54
+ ),
60
55
  required=True,
61
- example="A serene Japanese garden with a red maple tree",
62
- ),
63
- ToolArgument(
64
- name="provider",
65
- arg_type="string",
66
- description="Image generation provider (dall-e or stable-diffusion)",
67
- required=False,
68
- default="dall-e",
69
- example="dall-e",
56
+ example="A peaceful Japanese garden with a red maple tree, stone lanterns, and a koi pond at sunset",
70
57
  ),
71
58
  ToolArgument(
72
59
  name="size",
73
60
  arg_type="string",
74
- description="Size of the generated image",
61
+ description=(
62
+ "Image dimensions:\n"
63
+ "- 1024x1024: Square (social media)\n"
64
+ "- 1024x1792: Portrait (mobile)\n"
65
+ "- 1792x1024: Landscape (desktop)"
66
+ ),
75
67
  required=False,
76
68
  default="1024x1024",
77
69
  example="1024x1024",
@@ -79,7 +71,11 @@ class LLMImageGenerationTool(Tool):
79
71
  ToolArgument(
80
72
  name="quality",
81
73
  arg_type="string",
82
- description="Quality level for DALL-E",
74
+ description=(
75
+ "Image quality:\n"
76
+ "- standard: Faster, good for drafts\n"
77
+ "- hd: Detailed, best for final use"
78
+ ),
83
79
  required=False,
84
80
  default="standard",
85
81
  example="standard",
@@ -87,32 +83,19 @@ class LLMImageGenerationTool(Tool):
87
83
  ToolArgument(
88
84
  name="style",
89
85
  arg_type="string",
90
- description="Style preference for DALL-E",
86
+ description=(
87
+ "Visual style:\n"
88
+ "- vivid: Bold and dramatic\n"
89
+ "- natural: Subtle and realistic"
90
+ ),
91
91
  required=False,
92
92
  default="vivid",
93
93
  example="vivid",
94
94
  ),
95
- ToolArgument(
96
- name="negative_prompt",
97
- arg_type="string",
98
- description="What to avoid in the image (Stable Diffusion only)",
99
- required=False,
100
- default="",
101
- example="blurry, low quality",
102
- ),
103
- ToolArgument(
104
- name="cfg_scale",
105
- arg_type="string",
106
- description="Classifier Free Guidance scale (Stable Diffusion only)",
107
- required=False,
108
- default="7.5",
109
- example="7.5",
110
- ),
111
95
  ]
112
96
  )
113
97
 
114
- provider: ImageProvider = Field(default=ImageProvider.DALLE)
115
- model_name: str = Field(default=PROVIDER_CONFIGS[ImageProvider.DALLE]["model_name"])
98
+ model_name: str = Field(default=DALLE_CONFIG["model_name"])
116
99
  output_dir: Path = Field(default=Path("generated_images"))
117
100
  generative_model: Optional[GenerativeModel] = Field(default=None)
118
101
 
@@ -125,39 +108,28 @@ class LLMImageGenerationTool(Tool):
125
108
  # Create output directory if it doesn't exist
126
109
  self.output_dir.mkdir(parents=True, exist_ok=True)
127
110
 
128
- def _validate_dalle_params(self, size: str, quality: str, style: str) -> None:
129
- """Validate DALL-E specific parameters."""
130
- if size not in PROVIDER_CONFIGS[ImageProvider.DALLE]["sizes"]:
131
- raise ValueError(
132
- f"Invalid size for DALL-E. Must be one of: {PROVIDER_CONFIGS[ImageProvider.DALLE]['sizes']}"
133
- )
134
- if quality not in PROVIDER_CONFIGS[ImageProvider.DALLE]["qualities"]:
135
- raise ValueError(
136
- f"Invalid quality for DALL-E. Must be one of: {PROVIDER_CONFIGS[ImageProvider.DALLE]['qualities']}"
137
- )
138
- if style not in PROVIDER_CONFIGS[ImageProvider.DALLE]["styles"]:
139
- raise ValueError(
140
- f"Invalid style for DALL-E. Must be one of: {PROVIDER_CONFIGS[ImageProvider.DALLE]['styles']}"
141
- )
142
-
143
- def _validate_sd_params(self, size: str, cfg_scale: float) -> None:
144
- """Validate Stable Diffusion specific parameters."""
145
- if size not in PROVIDER_CONFIGS[ImageProvider.STABLE_DIFFUSION]["sizes"]:
146
- raise ValueError(
147
- f"Invalid size for Stable Diffusion. Must be one of: {PROVIDER_CONFIGS[ImageProvider.STABLE_DIFFUSION]['sizes']}"
148
- )
149
- if not 1.0 <= cfg_scale <= 20.0:
150
- raise ValueError("cfg_scale must be between 1.0 and 20.0")
111
+ def _validate_params(self, size: str, quality: str, style: str) -> None:
112
+ """Validate DALL-E parameters."""
113
+ if size not in DALLE_CONFIG["sizes"]:
114
+ raise ValueError(f"Invalid size. Must be one of: {DALLE_CONFIG['sizes']}")
115
+ if quality not in DALLE_CONFIG["qualities"]:
116
+ raise ValueError(f"Invalid quality. Must be one of: {DALLE_CONFIG['qualities']}")
117
+ if style not in DALLE_CONFIG["styles"]:
118
+ raise ValueError(f"Invalid style. Must be one of: {DALLE_CONFIG['styles']}")
151
119
 
152
120
  @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
153
- def _save_image(self, image_url: str, filename: str) -> Path:
121
+ async def _save_image(self, image_url: str, filename: str) -> Path:
154
122
  """Download and save image locally with retry logic."""
155
123
  try:
156
- response = requests.get(image_url, timeout=30)
157
- response.raise_for_status()
124
+ async with aiohttp.ClientSession() as session:
125
+ async with session.get(image_url, timeout=30) as response:
126
+ response.raise_for_status()
127
+ image_data = await response.read()
158
128
 
159
129
  file_path = self.output_dir / filename
160
- file_path.write_bytes(response.content)
130
+ async with aiofiles.open(file_path, mode='wb') as f:
131
+ await f.write(image_data)
132
+
161
133
  logger.info(f"Image saved successfully at: {file_path}")
162
134
  return file_path
163
135
 
@@ -165,74 +137,50 @@ class LLMImageGenerationTool(Tool):
165
137
  logger.error(f"Error saving image: {e}")
166
138
  raise
167
139
 
168
- def _save_metadata(self, metadata: Dict[str, Any]) -> None:
140
+ async def _save_metadata(self, metadata: Dict[str, Any]) -> None:
169
141
  """Save image metadata to JSON file."""
170
142
  try:
171
143
  metadata_path = self.output_dir / f"{metadata['filename']}.json"
172
- with open(metadata_path, "w") as f:
173
- json.dump(metadata, f, indent=2)
144
+ async with aiofiles.open(metadata_path, mode='w') as f:
145
+ await f.write(json.dumps(metadata, indent=2))
174
146
  logger.info(f"Metadata saved successfully at: {metadata_path}")
175
147
  except Exception as e:
176
148
  logger.error(f"Error saving metadata: {e}")
177
149
  raise
178
150
 
179
- def execute(
151
+ async def async_execute(
180
152
  self,
181
153
  prompt: str,
182
- provider: str = "dall-e",
183
154
  size: str = "1024x1024",
184
155
  quality: str = "standard",
185
156
  style: str = "vivid",
186
- negative_prompt: str = "",
187
- cfg_scale: str = "7.5",
188
157
  ) -> str:
189
- """Execute the tool to generate an image based on the prompt.
158
+ """Execute the tool to generate an image using DALL-E.
190
159
 
191
160
  Args:
192
161
  prompt: Text description of the image to generate
193
- provider: Provider to use (dall-e or stable-diffusion)
194
162
  size: Size of the generated image
195
163
  quality: Quality level for DALL-E
196
164
  style: Style preference for DALL-E
197
- negative_prompt: What to avoid in the image (Stable Diffusion only)
198
- cfg_scale: Classifier Free Guidance scale (Stable Diffusion only)
199
165
 
200
166
  Returns:
201
167
  Path to the locally saved image
202
168
  """
203
169
  try:
204
- provider_enum = ImageProvider(provider.lower())
205
-
206
- # Convert cfg_scale to float only if it's not empty and we're using Stable Diffusion
207
- cfg_scale_float = (
208
- float(cfg_scale) if cfg_scale and provider_enum == ImageProvider.STABLE_DIFFUSION else None
209
- )
210
-
211
- # Validate parameters based on provider
212
- if provider_enum == ImageProvider.DALLE:
213
- self._validate_dalle_params(size, quality, style)
214
- params = {
215
- "model": PROVIDER_CONFIGS[provider_enum]["model_name"],
216
- "size": size,
217
- "quality": quality,
218
- "style": style,
219
- "response_format": "url",
220
- }
221
- else: # Stable Diffusion
222
- if cfg_scale_float is None:
223
- cfg_scale_float = 7.5 # Default value
224
- self._validate_sd_params(size, cfg_scale_float)
225
- params = {
226
- "model": PROVIDER_CONFIGS[provider_enum]["model_name"],
227
- "negative_prompt": negative_prompt,
228
- "cfg_scale": cfg_scale_float,
229
- "size": size,
230
- "response_format": "url",
231
- }
170
+ # Validate parameters
171
+ self._validate_params(size, quality, style)
172
+
173
+ params = {
174
+ "model": self.model_name,
175
+ "size": size,
176
+ "quality": quality,
177
+ "style": style,
178
+ "response_format": "url",
179
+ }
232
180
 
233
181
  # Generate image
234
- logger.info(f"Generating image with {provider} using params: {params}")
235
- response = self.generative_model.generate_image(prompt=prompt, params=params)
182
+ logger.info(f"Generating DALL-E image with params: {params}")
183
+ response = await self.generative_model.async_generate_image(prompt=prompt, params=params)
236
184
 
237
185
  # Extract image data from response
238
186
  if not response.data:
@@ -247,34 +195,38 @@ class LLMImageGenerationTool(Tool):
247
195
 
248
196
  # Save image locally
249
197
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
250
- filename = f"{provider}_{timestamp}.png"
251
- local_path = self._save_image(image_url, filename)
198
+ filename = f"dalle_{timestamp}.png"
199
+ local_path = await self._save_image(image_url, filename)
252
200
 
253
201
  # Save metadata
254
202
  metadata = {
255
203
  "filename": str(filename),
256
204
  "prompt": str(prompt),
257
205
  "revised_prompt": str(revised_prompt),
258
- "provider": str(provider),
259
206
  "model": str(response.model),
260
207
  "created": str(response.created or ""),
261
208
  "parameters": {k: str(v) for k, v in {**params, "prompt": prompt}.items()},
262
209
  "image_url": str(image_url),
263
210
  "local_path": str(local_path),
264
211
  }
265
- self._save_metadata(metadata)
212
+ await self._save_metadata(metadata)
266
213
 
267
214
  logger.info(f"Image generated and saved at: {local_path}")
268
215
  return str(metadata)
269
216
 
270
217
  except Exception as e:
271
- logger.error(f"Error generating image with {provider}: {e}")
272
- raise Exception(f"Error generating image with {provider}: {e}") from e
218
+ logger.error(f"Error generating DALL-E image: {e}")
219
+ raise Exception(f"Error generating DALL-E image: {e}") from e
273
220
 
274
221
 
275
222
  if __name__ == "__main__":
276
223
  # Example usage
277
- tool = LLMImageGenerationTool()
278
- prompt = "A serene Japanese garden with a red maple tree"
279
- image_path = tool.execute(prompt=prompt)
280
- print(f"Image saved at: {image_path}")
224
+ import asyncio
225
+
226
+ async def main():
227
+ tool = LLMImageGenerationTool()
228
+ prompt = "A serene Japanese garden with a red maple tree"
229
+ image_path = await tool.async_execute(prompt=prompt)
230
+ print(f"Image saved at: {image_path}")
231
+
232
+ asyncio.run(main())
@@ -6,18 +6,11 @@ This module provides tools and utilities for Retrieval-Augmented Generation (RAG
6
6
 
7
7
  from loguru import logger
8
8
 
9
- # Explicit imports of all tools in the module
10
- from .document_metadata import DocumentMetadata
11
- from .query_response import QueryResponse
12
- from .rag_tool import RagTool
13
- from .rag_tool_beta import RagToolBeta
9
+ from .document_rag_sources_ import RagToolHf
14
10
 
15
11
  # Define __all__ to control what is imported with `from ... import *`
16
12
  __all__ = [
17
- 'DocumentMetadata',
18
- 'QueryResponse',
19
- 'RagTool',
20
- 'RagToolBeta',
13
+ 'RagToolHf'
21
14
  ]
22
15
 
23
16
  # Optional: Add logging for import confirmation