camel-ai 0.2.59__py3-none-any.whl → 0.2.61__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.

Potentially problematic release.


This version of camel-ai might be problematic. Click here for more details.

Files changed (55) hide show
  1. camel/__init__.py +1 -1
  2. camel/agents/chat_agent.py +158 -7
  3. camel/configs/anthropic_config.py +6 -5
  4. camel/configs/cohere_config.py +1 -1
  5. camel/configs/mistral_config.py +1 -1
  6. camel/configs/openai_config.py +3 -0
  7. camel/configs/reka_config.py +1 -1
  8. camel/configs/samba_config.py +2 -2
  9. camel/datagen/cot_datagen.py +29 -34
  10. camel/datagen/evol_instruct/scorer.py +22 -23
  11. camel/datagen/evol_instruct/templates.py +46 -46
  12. camel/datasets/static_dataset.py +144 -0
  13. camel/embeddings/jina_embedding.py +8 -1
  14. camel/embeddings/sentence_transformers_embeddings.py +2 -2
  15. camel/embeddings/vlm_embedding.py +9 -2
  16. camel/loaders/__init__.py +5 -2
  17. camel/loaders/chunkr_reader.py +117 -91
  18. camel/loaders/mistral_reader.py +148 -0
  19. camel/memories/blocks/chat_history_block.py +1 -2
  20. camel/memories/records.py +3 -0
  21. camel/messages/base.py +15 -3
  22. camel/models/azure_openai_model.py +1 -0
  23. camel/models/model_factory.py +2 -2
  24. camel/models/model_manager.py +7 -3
  25. camel/retrievers/bm25_retriever.py +1 -2
  26. camel/retrievers/hybrid_retrival.py +2 -2
  27. camel/societies/workforce/workforce.py +65 -24
  28. camel/storages/__init__.py +2 -0
  29. camel/storages/vectordb_storages/__init__.py +2 -0
  30. camel/storages/vectordb_storages/faiss.py +712 -0
  31. camel/storages/vectordb_storages/oceanbase.py +1 -2
  32. camel/toolkits/__init__.py +2 -0
  33. camel/toolkits/async_browser_toolkit.py +80 -524
  34. camel/toolkits/bohrium_toolkit.py +318 -0
  35. camel/toolkits/browser_toolkit.py +221 -541
  36. camel/toolkits/browser_toolkit_commons.py +568 -0
  37. camel/toolkits/dalle_toolkit.py +4 -0
  38. camel/toolkits/excel_toolkit.py +8 -2
  39. camel/toolkits/file_write_toolkit.py +76 -29
  40. camel/toolkits/github_toolkit.py +43 -25
  41. camel/toolkits/image_analysis_toolkit.py +3 -0
  42. camel/toolkits/jina_reranker_toolkit.py +194 -77
  43. camel/toolkits/mcp_toolkit.py +134 -16
  44. camel/toolkits/page_script.js +40 -28
  45. camel/toolkits/twitter_toolkit.py +6 -1
  46. camel/toolkits/video_analysis_toolkit.py +3 -0
  47. camel/toolkits/video_download_toolkit.py +3 -0
  48. camel/toolkits/wolfram_alpha_toolkit.py +51 -23
  49. camel/types/enums.py +27 -6
  50. camel/utils/__init__.py +2 -0
  51. camel/utils/commons.py +27 -0
  52. {camel_ai-0.2.59.dist-info → camel_ai-0.2.61.dist-info}/METADATA +17 -9
  53. {camel_ai-0.2.59.dist-info → camel_ai-0.2.61.dist-info}/RECORD +55 -51
  54. {camel_ai-0.2.59.dist-info → camel_ai-0.2.61.dist-info}/WHEEL +0 -0
  55. {camel_ai-0.2.59.dist-info → camel_ai-0.2.61.dist-info}/licenses/LICENSE +0 -0
@@ -398,3 +398,147 @@ class StaticDataset(Dataset):
398
398
  f"got {type(item).__name__}"
399
399
  )
400
400
  return data
401
+
402
+ def save_to_json(self, file_path: Union[str, Path]) -> None:
403
+ r"""Save the dataset to a local JSON file.
404
+
405
+ Args:
406
+ file_path (Union[str, Path]): Path to the output JSON file.
407
+ If a string is provided, it will be converted to a Path object.
408
+
409
+ Raises:
410
+ TypeError: If file_path is not a string or Path object.
411
+ OSError: If there's an error writing to the file.
412
+ """
413
+ if isinstance(file_path, str):
414
+ file_path = Path(file_path)
415
+ elif not isinstance(file_path, Path):
416
+ raise TypeError(
417
+ f"Expected file_path to be a string or Path object, "
418
+ f"got {type(file_path).__name__}"
419
+ )
420
+
421
+ # Convert DataPoint objects to dictionaries
422
+ data_dicts = [datapoint.to_dict() for datapoint in self.data]
423
+
424
+ # Ensure the parent directory exists
425
+ file_path.parent.mkdir(parents=True, exist_ok=True)
426
+
427
+ try:
428
+ logger.debug(f"Saving dataset to {file_path}")
429
+ with file_path.open('w', encoding='utf-8') as f:
430
+ json.dump(data_dicts, f, ensure_ascii=False, indent=2)
431
+ logger.info(
432
+ f"Successfully saved {len(data_dicts)} items to {file_path}"
433
+ )
434
+ except OSError as e:
435
+ logger.error(f"Error saving dataset to {file_path}: {e}")
436
+ raise
437
+
438
+ def save_to_huggingface(
439
+ self,
440
+ dataset_name: str,
441
+ token: Optional[str] = None,
442
+ filepath: str = "records/records.json",
443
+ private: bool = False,
444
+ description: Optional[str] = None,
445
+ license: Optional[str] = None,
446
+ version: Optional[str] = None,
447
+ tags: Optional[List[str]] = None,
448
+ language: Optional[List[str]] = None,
449
+ task_categories: Optional[List[str]] = None,
450
+ authors: Optional[List[str]] = None,
451
+ **kwargs: Any,
452
+ ) -> str:
453
+ r"""Save the dataset to the Hugging Face Hub using the project's
454
+ HuggingFaceDatasetManager.
455
+
456
+ Args:
457
+ dataset_name (str): The name of the dataset on Hugging Face Hub.
458
+ Should be in the format 'username/dataset_name' .
459
+ token (Optional[str]): The Hugging Face API token. If not provided,
460
+ the token will be read from the environment variable `HF_TOKEN`
461
+ (default: :obj:`None`)
462
+ filepath (str): The path in the repository where the dataset
463
+ will be saved. (default: :obj:`"records/records.json"`)
464
+ private (bool): Whether the dataset should be private.
465
+ (default: :obj:`False`)
466
+ description (Optional[str]): A description of the dataset.
467
+ (default: :obj:`None`)
468
+ license (Optional[str]): The license of the dataset.
469
+ (default: :obj:`None`)
470
+ version (Optional[str]): The version of the dataset.
471
+ (default: :obj:`None`)
472
+ tags (Optional[List[str]]): A list of tags for the dataset.
473
+ (default: :obj:`None`)
474
+ language (Optional[List[str]]): A list of languages the dataset is
475
+ in. (default: :obj:`None`)
476
+ task_categories (Optional[List[str]]): A list of task categories.
477
+ (default: :obj:`None`)
478
+ authors (Optional[List[str]]): A list of authors of the dataset.
479
+ (default: :obj:`None`)
480
+ **kwargs (Any): Additional keyword arguments to pass to the
481
+ Hugging Face API.
482
+
483
+ Returns:
484
+ str: The URL of the dataset on the Hugging Face Hub.
485
+
486
+ Raises:
487
+ OSError: If there's an error uploading the dataset.
488
+ """
489
+ # lazy import to avoid heavy dependencies
490
+ from camel.datahubs.huggingface import HuggingFaceDatasetManager
491
+ from camel.datahubs.models import Record
492
+
493
+ # Initialize the HuggingFaceDatasetManager
494
+ manager = HuggingFaceDatasetManager(token=token)
495
+
496
+ # Convert DataPoint objects to Record objects
497
+ records = []
498
+ for datapoint in self.data:
499
+ datapoint_dict = datapoint.to_dict()
500
+
501
+ record_dict = {
502
+ "question": datapoint_dict.get("question", ""),
503
+ "final_answer": datapoint_dict.get("final_answer", ""),
504
+ "rationale": datapoint_dict.get("rationale", ""),
505
+ "metadata": datapoint_dict.get("metadata", {}),
506
+ }
507
+ record = Record(**record_dict)
508
+ records.append(record)
509
+
510
+ logger.debug(f"Creating dataset {dataset_name}")
511
+ try:
512
+ # Create the dataset
513
+ dataset_url = manager.create_dataset(
514
+ name=dataset_name, private=private, **kwargs
515
+ )
516
+
517
+ # Add records to the dataset
518
+ manager.add_records(
519
+ dataset_name=dataset_name,
520
+ records=records,
521
+ filepath=filepath,
522
+ )
523
+
524
+ # Create dataset card if description is provided
525
+ if description:
526
+ manager.create_dataset_card(
527
+ dataset_name=dataset_name,
528
+ description=description,
529
+ license=license,
530
+ version=version,
531
+ tags=tags,
532
+ authors=authors,
533
+ language=language,
534
+ task_categories=task_categories,
535
+ )
536
+
537
+ logger.info(
538
+ f"Successfully uploaded dataset to {dataset_name}, "
539
+ f"the url is {dataset_url}"
540
+ )
541
+ return dataset_url
542
+ except Exception as e:
543
+ logger.error(f"Error uploading dataset to Hugging Face: {e}")
544
+ raise
@@ -12,6 +12,9 @@
12
12
  # limitations under the License.
13
13
  # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
14
 
15
+ # Enables postponed evaluation of annotations (for string-based type hints)
16
+ from __future__ import annotations
17
+
15
18
  import base64
16
19
  import io
17
20
  import os
@@ -104,6 +107,7 @@ class JinaEmbedding(BaseEmbedding[Union[str, Image.Image]]):
104
107
  ValueError: If the input type is not supported.
105
108
  RuntimeError: If the API request fails.
106
109
  """
110
+
107
111
  input_data = []
108
112
  for obj in objs:
109
113
  if isinstance(obj, str):
@@ -111,7 +115,10 @@ class JinaEmbedding(BaseEmbedding[Union[str, Image.Image]]):
111
115
  input_data.append({"text": obj})
112
116
  else:
113
117
  input_data.append(obj) # type: ignore[arg-type]
114
- elif isinstance(obj, Image.Image):
118
+ elif (
119
+ obj.__class__.__module__ == "PIL.Image"
120
+ and obj.__class__.__name__ == "Image"
121
+ ):
115
122
  if self.model_type != EmbeddingModelType.JINA_CLIP_V2:
116
123
  raise ValueError(
117
124
  f"Model {self.model_type} does not support "
@@ -15,8 +15,6 @@ from __future__ import annotations
15
15
 
16
16
  from typing import Any
17
17
 
18
- from numpy import ndarray
19
-
20
18
  from camel.embeddings.base import BaseEmbedding
21
19
 
22
20
 
@@ -61,6 +59,8 @@ class SentenceTransformerEncoder(BaseEmbedding[str]):
61
59
  list[list[float]]: A list that represents the generated embedding
62
60
  as a list of floating-point numbers.
63
61
  """
62
+ from numpy import ndarray
63
+
64
64
  if not objs:
65
65
  raise ValueError("Input text list is empty")
66
66
  embeddings = self.model.encode(
@@ -11,6 +11,10 @@
11
11
  # See the License for the specific language governing permissions and
12
12
  # limitations under the License.
13
13
  # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
+
15
+ # Enables postponed evaluation of annotations (for string-based type hints)
16
+ from __future__ import annotations
17
+
14
18
  from typing import Any, List, Optional, Union
15
19
 
16
20
  from PIL import Image
@@ -70,7 +74,7 @@ class VisionLanguageEmbedding(BaseEmbedding[Union[str, Image.Image]]):
70
74
  def embed_list(
71
75
  self, objs: List[Union[Image.Image, str]], **kwargs: Any
72
76
  ) -> List[List[float]]:
73
- """Generates embeddings for the given images or texts.
77
+ r"""Generates embeddings for the given images or texts.
74
78
 
75
79
  Args:
76
80
  objs (List[Image.Image|str]): The list of images or texts for
@@ -98,7 +102,10 @@ class VisionLanguageEmbedding(BaseEmbedding[Union[str, Image.Image]]):
98
102
 
99
103
  result_list = []
100
104
  for obj in objs:
101
- if isinstance(obj, Image.Image):
105
+ if (
106
+ obj.__class__.__module__ == "PIL.Image"
107
+ and obj.__class__.__name__ == "Image"
108
+ ):
102
109
  image_input = self.processor(
103
110
  images=obj,
104
111
  return_tensors="pt",
camel/loaders/__init__.py CHANGED
@@ -14,12 +14,13 @@
14
14
 
15
15
  from .apify_reader import Apify
16
16
  from .base_io import File, create_file, create_file_from_raw_bytes
17
- from .chunkr_reader import ChunkrReader
17
+ from .chunkr_reader import ChunkrReader, ChunkrReaderConfig
18
18
  from .crawl4ai_reader import Crawl4AI
19
19
  from .firecrawl_reader import Firecrawl
20
20
  from .jina_url_reader import JinaURLReader
21
21
  from .markitdown import MarkItDownLoader
22
22
  from .mineru_extractor import MinerU
23
+ from .mistral_reader import MistralReader
23
24
  from .pandas_reader import PandasReader
24
25
  from .scrapegraph_reader import ScrapeGraphAI
25
26
  from .unstructured_io import UnstructuredIO
@@ -32,10 +33,12 @@ __all__ = [
32
33
  'JinaURLReader',
33
34
  'Firecrawl',
34
35
  'Apify',
35
- 'ChunkrReader',
36
36
  'PandasReader',
37
+ 'ChunkrReader',
38
+ 'ChunkrReaderConfig',
37
39
  'MinerU',
38
40
  'Crawl4AI',
39
41
  'MarkItDownLoader',
40
42
  'ScrapeGraphAI',
43
+ 'MistralReader',
41
44
  ]
@@ -13,16 +13,38 @@
13
13
  # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
14
 
15
15
  import json
16
- import logging
17
16
  import os
18
- import time
19
- from typing import IO, Any, Optional, Union
17
+ from typing import TYPE_CHECKING, Optional
20
18
 
21
- import requests
19
+ if TYPE_CHECKING:
20
+ from chunkr_ai.models import Configuration
22
21
 
22
+ from camel.logger import get_logger
23
23
  from camel.utils import api_keys_required
24
24
 
25
- logger = logging.getLogger(__name__)
25
+ logger = get_logger(__name__)
26
+
27
+
28
+ class ChunkrReaderConfig:
29
+ r"""Defines the parameters for configuring the task.
30
+
31
+ Args:
32
+ chunk_processing (int, optional): The target chunk length.
33
+ (default: :obj:`512`)
34
+ high_resolution (bool, optional): Whether to use high resolution OCR.
35
+ (default: :obj:`True`)
36
+ ocr_strategy (str, optional): The OCR strategy. Defaults to 'Auto'.
37
+ """
38
+
39
+ def __init__(
40
+ self,
41
+ chunk_processing: int = 512,
42
+ high_resolution: bool = True,
43
+ ocr_strategy: str = "Auto",
44
+ ):
45
+ self.chunk_processing = chunk_processing
46
+ self.high_resolution = high_resolution
47
+ self.ocr_strategy = ocr_strategy
26
48
 
27
49
 
28
50
  class ChunkrReader:
@@ -35,8 +57,6 @@ class ChunkrReader:
35
57
  `CHUNKR_API_KEY`. (default: :obj:`None`)
36
58
  url (Optional[str], optional): The url to the Chunkr service.
37
59
  (default: :obj:`https://api.chunkr.ai/api/v1/task`)
38
- timeout (int, optional): The maximum time in seconds to wait for the
39
- API responses. (default: :obj:`30`)
40
60
  **kwargs (Any): Additional keyword arguments for request headers.
41
61
  """
42
62
 
@@ -49,111 +69,80 @@ class ChunkrReader:
49
69
  self,
50
70
  api_key: Optional[str] = None,
51
71
  url: Optional[str] = "https://api.chunkr.ai/api/v1/task",
52
- timeout: int = 30,
53
- **kwargs: Any,
54
72
  ) -> None:
73
+ from chunkr_ai import Chunkr
74
+
55
75
  self._api_key = api_key or os.getenv('CHUNKR_API_KEY')
56
- self._url = os.getenv('CHUNKR_API_URL') or url
57
- self._headers = {
58
- "Authorization": f"{self._api_key}",
59
- **kwargs,
60
- }
61
- self.timeout = timeout
62
-
63
- def submit_task(
76
+ self._chunkr = Chunkr(api_key=self._api_key)
77
+
78
+ async def submit_task(
64
79
  self,
65
80
  file_path: str,
66
- model: str = "Fast",
67
- ocr_strategy: str = "Auto",
68
- target_chunk_length: str = "512",
81
+ chunkr_config: Optional[ChunkrReaderConfig] = None,
69
82
  ) -> str:
70
83
  r"""Submits a file to the Chunkr API and returns the task ID.
71
84
 
72
85
  Args:
73
86
  file_path (str): The path to the file to be uploaded.
74
- model (str, optional): The model to be used for the task.
75
- (default: :obj:`Fast`)
76
- ocr_strategy (str, optional): The OCR strategy. Defaults to 'Auto'.
77
- target_chunk_length (str, optional): The target chunk length.
78
- (default: :obj:`512`)
87
+ chunkr_config (ChunkrReaderConfig, optional): The configuration
88
+ for the Chunkr API. Defaults to None.
79
89
 
80
90
  Returns:
81
91
  str: The task ID.
82
92
  """
83
- with open(file_path, 'rb') as file:
84
- files: dict[
85
- str, Union[tuple[None, IO[bytes]], tuple[None, str]]
86
- ] = {
87
- 'file': (
88
- None,
89
- file,
90
- ), # Properly pass the file as a binary stream
91
- 'model': (None, model),
92
- 'ocr_strategy': (None, ocr_strategy),
93
- 'target_chunk_length': (None, target_chunk_length),
94
- }
95
- try:
96
- response = requests.post(
97
- self._url, # type: ignore[arg-type]
98
- headers=self._headers,
99
- files=files,
100
- timeout=self.timeout,
101
- )
102
- response.raise_for_status()
103
- task_id = response.json().get('task_id')
104
- if not task_id:
105
- raise ValueError("Task ID not returned in the response.")
106
- logger.info(f"Task submitted successfully. Task ID: {task_id}")
107
- return task_id
108
- except Exception as e:
109
- logger.error(f"Failed to submit task: {e}")
110
- raise ValueError(f"Failed to submit task: {e}") from e
111
-
112
- def get_task_output(self, task_id: str, max_retries: int = 5) -> str:
93
+ chunkr_config = self._to_chunkr_configuration(
94
+ chunkr_config or ChunkrReaderConfig()
95
+ )
96
+
97
+ try:
98
+ task = await self._chunkr.create_task(
99
+ file=file_path, config=chunkr_config
100
+ )
101
+ logger.info(
102
+ f"Task submitted successfully. Task ID: {task.task_id}"
103
+ )
104
+ return task.task_id
105
+ except Exception as e:
106
+ logger.error(f"Failed to submit task: {e}")
107
+ raise ValueError(f"Failed to submit task: {e}") from e
108
+
109
+ async def get_task_output(self, task_id: str) -> str | None:
113
110
  r"""Polls the Chunkr API to check the task status and returns the task
114
111
  result.
115
112
 
116
113
  Args:
117
114
  task_id (str): The task ID to check the status for.
118
- max_retries (int, optional): Maximum number of retry attempts.
119
- (default: :obj:`5`)
120
115
 
121
116
  Returns:
122
- str: The formatted task result in JSON format.
123
-
124
- Raises:
125
- ValueError: If the task status cannot be retrieved.
126
- RuntimeError: If the maximum number of retries is reached without
127
- a successful task completion.
117
+ Optional[str]: The formatted task result in JSON format, or `None`
118
+ if the task fails or is canceld.
128
119
  """
129
- url_get = f"{self._url}/{task_id}"
130
- attempts = 0
131
-
132
- while attempts < max_retries:
133
- try:
134
- response = requests.get(
135
- url_get, headers=self._headers, timeout=self.timeout
120
+ from chunkr_ai.models import Status
121
+
122
+ try:
123
+ task = await self._chunkr.get_task(task_id)
124
+ except Exception as e:
125
+ logger.error(f"Failed to get task by task id: {task_id}: {e}")
126
+ raise ValueError(
127
+ f"Failed to get task by task id: {task_id}: {e}"
128
+ ) from e
129
+
130
+ try:
131
+ await task.poll()
132
+ if task.status == Status.SUCCEEDED:
133
+ logger.info(f"Task {task_id} completed successfully.")
134
+ return self._pretty_print_response(task.json())
135
+ elif task.status == Status.FAILED:
136
+ logger.warning(
137
+ f"Task {task_id} encountered an error: {task.message}"
136
138
  )
137
- response.raise_for_status()
138
- task_status = response.json().get('status')
139
-
140
- if task_status == "Succeeded":
141
- logger.info(f"Task {task_id} completed successfully.")
142
- return self._pretty_print_response(response.json())
143
- else:
144
- logger.info(
145
- f"Task {task_id} is still {task_status}. Retrying "
146
- "in 5 seconds..."
147
- )
148
- except Exception as e:
149
- logger.error(f"Failed to retrieve task status: {e}")
150
- raise ValueError(f"Failed to retrieve task status: {e}") from e
151
-
152
- attempts += 1
153
- time.sleep(5)
154
-
155
- logger.error(f"Max retries reached for task {task_id}.")
156
- raise RuntimeError(f"Max retries reached for task {task_id}.")
139
+ return None
140
+ else:
141
+ logger.warning(f"Task {task_id} was manually cancelled.")
142
+ return None
143
+ except Exception as e:
144
+ logger.error(f"Failed to retrieve task status: {e}")
145
+ raise ValueError(f"Failed to retrieve task status: {e}") from e
157
146
 
158
147
  def _pretty_print_response(self, response_json: dict) -> str:
159
148
  r"""Pretty prints the JSON response.
@@ -164,4 +153,41 @@ class ChunkrReader:
164
153
  Returns:
165
154
  str: Formatted JSON as a string.
166
155
  """
167
- return json.dumps(response_json, indent=4, ensure_ascii=False)
156
+ from datetime import datetime
157
+
158
+ return json.dumps(
159
+ response_json,
160
+ default=lambda o: o.isoformat()
161
+ if isinstance(o, datetime)
162
+ else None,
163
+ indent=4,
164
+ )
165
+
166
+ def _to_chunkr_configuration(
167
+ self, chunkr_config: ChunkrReaderConfig
168
+ ) -> "Configuration":
169
+ r"""Converts the ChunkrReaderConfig to Chunkr Configuration.
170
+
171
+ Args:
172
+ chunkr_config (ChunkrReaderConfig):
173
+ The ChunkrReaderConfig to convert.
174
+
175
+ Returns:
176
+ Configuration: Chunkr SDK configuration.
177
+ """
178
+ from chunkr_ai.models import (
179
+ ChunkProcessing,
180
+ Configuration,
181
+ OcrStrategy,
182
+ )
183
+
184
+ return Configuration(
185
+ chunk_processing=ChunkProcessing(
186
+ target_length=chunkr_config.chunk_processing
187
+ ),
188
+ high_resolution=chunkr_config.high_resolution,
189
+ ocr_strategy={
190
+ "Auto": OcrStrategy.AUTO,
191
+ "All": OcrStrategy.ALL,
192
+ }.get(chunkr_config.ocr_strategy, OcrStrategy.ALL),
193
+ )
@@ -0,0 +1,148 @@
1
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
+ import os
15
+ from typing import TYPE_CHECKING, List, Optional
16
+
17
+ if TYPE_CHECKING:
18
+ from mistralai.models import OCRResponse
19
+
20
+ from camel.logger import get_logger
21
+ from camel.utils import api_keys_required
22
+
23
+ logger = get_logger(__name__)
24
+
25
+
26
+ class MistralReader:
27
+ r"""Mistral Document Loader."""
28
+
29
+ @api_keys_required(
30
+ [
31
+ ("api_key", "MISTRAL_API_KEY"),
32
+ ]
33
+ )
34
+ def __init__(
35
+ self,
36
+ api_key: Optional[str] = None,
37
+ model: Optional[str] = "mistral-ocr-latest",
38
+ ) -> None:
39
+ r"""Initialize the MistralReader.
40
+
41
+ Args:
42
+ api_key (Optional[str]): The API key for the Mistral API.
43
+ (default: :obj:`None`)
44
+ model (Optional[str]): The model to use for OCR.
45
+ (default: :obj:`"mistral-ocr-latest"`)
46
+ """
47
+ from mistralai import Mistral
48
+
49
+ self._api_key = api_key or os.environ.get("MISTRAL_API_KEY")
50
+ self.client = Mistral(api_key=self._api_key)
51
+ self.model = model
52
+
53
+ def _encode_file(self, file_path: str) -> str:
54
+ r"""Encode the pdf to base64.
55
+
56
+ Args:
57
+ file_path (str): Path to the input file.
58
+
59
+ Returns:
60
+ str: base64 version of the file.
61
+ """
62
+
63
+ import base64
64
+
65
+ try:
66
+ with open(file_path, "rb") as pdf_file:
67
+ return base64.b64encode(pdf_file.read()).decode('utf-8')
68
+ except FileNotFoundError:
69
+ logger.error(f"Error: The file {file_path} was not found.")
70
+ return ""
71
+ except Exception as e:
72
+ logger.error(f"Error: {e}")
73
+ return ""
74
+
75
+ def extract_text(
76
+ self,
77
+ file_path: str,
78
+ is_image: bool = False,
79
+ pages: Optional[List[int]] = None,
80
+ include_image_base64: Optional[bool] = None,
81
+ ) -> "OCRResponse":
82
+ r"""Converts the given file to Markdown format.
83
+
84
+ Args:
85
+ file_path (str): Path to the input file or a remote URL.
86
+ is_image (bool): Whether the file or URL is an image. If True,
87
+ uses image_url type instead of document_url.
88
+ (default: :obj:`False`)
89
+ pages (Optional[List[int]]): Specific pages user wants to process
90
+ in various formats: single number, range, or list of both.
91
+ Starts from 0. (default: :obj:`None`)
92
+ include_image_base64 (Optional[bool]): Whether to include image
93
+ URLs in response. (default: :obj:`None`)
94
+
95
+ Returns:
96
+ OCRResponse: page wise extractions.
97
+
98
+ Raises:
99
+ FileNotFoundError: If the specified local file does not exist.
100
+ ValueError: If the file format is not supported.
101
+ Exception: For other errors during conversion.
102
+ """
103
+ # Check if the input is a URL (starts with http:// or https://)
104
+ is_url = file_path.startswith(('http://', 'https://'))
105
+
106
+ if not is_url and not os.path.isfile(file_path):
107
+ logger.error(f"File not found: {file_path}")
108
+ raise FileNotFoundError(f"File not found: {file_path}")
109
+ try:
110
+ if is_url:
111
+ logger.info(f"Processing URL: {file_path}")
112
+ if is_image:
113
+ document_config = {
114
+ "type": "image_url",
115
+ "image_url": file_path,
116
+ }
117
+ else:
118
+ document_config = {
119
+ "type": "document_url",
120
+ "document_url": file_path,
121
+ }
122
+ else:
123
+ logger.info(f"Converting local file: {file_path}")
124
+ base64_file = self._encode_file(file_path)
125
+ if is_image:
126
+ document_config = {
127
+ "type": "image_url",
128
+ "image_url": f"data:image/jpeg;base64,{base64_file}",
129
+ }
130
+ else:
131
+ document_config = {
132
+ "type": "document_url",
133
+ "document_url": f"data:application/"
134
+ f"pdf;base64,{base64_file}",
135
+ }
136
+
137
+ ocr_response = self.client.ocr.process(
138
+ model=self.model,
139
+ document=document_config, # type: ignore[arg-type]
140
+ pages=None if is_image else pages,
141
+ include_image_base64=include_image_base64,
142
+ )
143
+
144
+ logger.info(f"Processing completed successfully for: {file_path}")
145
+ return ocr_response
146
+ except Exception as e:
147
+ logger.error(f"Error processing '{file_path}': {e}")
148
+ raise ValueError(f"Error processing '{file_path}': {e}")
@@ -73,7 +73,6 @@ class ChatHistoryBlock(MemoryBlock):
73
73
  warnings.warn("The `ChatHistoryMemory` is empty.")
74
74
  return list()
75
75
 
76
- chat_records: List[MemoryRecord] = []
77
76
  if window_size is not None and window_size >= 0:
78
77
  # Initial preserved index: Keep first message
79
78
  # if it's SYSTEM/DEVELOPER (index 0)
@@ -117,7 +116,7 @@ class ChatHistoryBlock(MemoryBlock):
117
116
  # Return full records when no window restriction
118
117
  final_records = record_dicts
119
118
 
120
- chat_records = [
119
+ chat_records: List[MemoryRecord] = [
121
120
  MemoryRecord.from_dict(record) for record in final_records
122
121
  ]
123
122