mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-29 07:19:59 +00:00
This PR upgrades langchain-community to pydantic 2.
* Most of this PR was auto-generated using code mods with gritql
(https://github.com/eyurtsev/migrate-pydantic/tree/main)
* Subsequently, some code was fixed manually due to accommodate
differences between pydantic 1 and 2
Breaking Changes:
- Use TEXTEMBED_API_KEY and TEXTEMBEB_API_URL for env variables for text
embed integrations:
cbea780492
Other changes:
- Added pydantic_settings as a required dependency for community. This
may be removed if we have enough time to convert the dependency into an
optional one.
---------
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
import shutil
|
|
from typing import Optional, Type
|
|
|
|
from langchain_core.callbacks import CallbackManagerForToolRun
|
|
from langchain_core.tools import BaseTool
|
|
from pydantic import BaseModel, Field
|
|
|
|
from langchain_community.tools.file_management.utils import (
|
|
INVALID_PATH_TEMPLATE,
|
|
BaseFileToolMixin,
|
|
FileValidationError,
|
|
)
|
|
|
|
|
|
class FileCopyInput(BaseModel):
|
|
"""Input for CopyFileTool."""
|
|
|
|
source_path: str = Field(..., description="Path of the file to copy")
|
|
destination_path: str = Field(..., description="Path to save the copied file")
|
|
|
|
|
|
class CopyFileTool(BaseFileToolMixin, BaseTool):
|
|
"""Tool that copies a file."""
|
|
|
|
name: str = "copy_file"
|
|
args_schema: Type[BaseModel] = FileCopyInput
|
|
description: str = "Create a copy of a file in a specified location"
|
|
|
|
def _run(
|
|
self,
|
|
source_path: str,
|
|
destination_path: str,
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
try:
|
|
source_path_ = self.get_relative_path(source_path)
|
|
except FileValidationError:
|
|
return INVALID_PATH_TEMPLATE.format(
|
|
arg_name="source_path", value=source_path
|
|
)
|
|
try:
|
|
destination_path_ = self.get_relative_path(destination_path)
|
|
except FileValidationError:
|
|
return INVALID_PATH_TEMPLATE.format(
|
|
arg_name="destination_path", value=destination_path
|
|
)
|
|
try:
|
|
shutil.copy2(source_path_, destination_path_, follow_symlinks=False)
|
|
return f"File copied successfully from {source_path} to {destination_path}."
|
|
except Exception as e:
|
|
return "Error: " + str(e)
|
|
|
|
# TODO: Add aiofiles method
|