Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
bhancockio authored Oct 28, 2024
2 parents 2fb9eb1 + 26afee9 commit 1c50df3
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 48 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies = [
"uv>=0.4.25",
"tomli-w>=1.1.0",
"chromadb>=0.4.24",
"tomli>=2.0.2",
]

[project.urls]
Expand Down
27 changes: 13 additions & 14 deletions src/crewai/agent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import shutil
import subprocess
from inspect import signature
from typing import Any, List, Literal, Optional, Union

from pydantic import Field, InstanceOf, PrivateAttr, model_validator
Expand Down Expand Up @@ -395,26 +394,26 @@ def _render_text_description(self, tools: List[Any]) -> str:
def _render_text_description_and_args(self, tools: List[Any]) -> str:
"""Render the tool name, description, and args in plain text.
Output will be in the format of:
Output will be in the format of:
.. code-block:: markdown
.. code-block:: markdown
search: This tool is used for search, args: {"query": {"type": "string"}}
calculator: This tool is used for math, \
args: {"expression": {"type": "string"}}
args: {"expression": {"type": "string"}}
"""
tool_strings = []
for tool in tools:
args_schema = str(tool.model_fields)
if hasattr(tool, "func") and tool.func:
sig = signature(tool.func)
description = (
f"Tool Name: {tool.name}{sig}\nTool Description: {tool.description}"
)
else:
description = (
f"Tool Name: {tool.name}\nTool Description: {tool.description}"
)
args_schema = {
name: {
"description": field.description,
"type": field.annotation.__name__,
}
for name, field in tool.args_schema.model_fields.items()
}
description = (
f"Tool Name: {tool.name}\nTool Description: {tool.description}"
)
tool_strings.append(f"{description}\nTool Arguments: {args_schema}")

return "\n".join(tool_strings)
Expand Down
10 changes: 7 additions & 3 deletions src/crewai/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,14 @@ def test(n_iterations: int, model: str):
evaluate_crew(n_iterations, model)


@crewai.command()
def install():
@crewai.command(context_settings=dict(
ignore_unknown_options=True,
allow_extra_args=True,
))
@click.pass_context
def install(context):
"""Install the Crew."""
install_crew()
install_crew(context.args)


@crewai.command()
Expand Down
5 changes: 3 additions & 2 deletions src/crewai/cli/install_crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import click


def install_crew() -> None:
def install_crew(proxy_options: list[str]) -> None:
"""
Install the crew by running the UV command to lock and install.
"""
try:
subprocess.run(["uv", "sync"], check=True, capture_output=False, text=True)
command = ["uv", "sync"] + proxy_options
subprocess.run(command, check=True, capture_output=False, text=True)

except subprocess.CalledProcessError as e:
click.echo(f"An error occurred while running the crew: {e}", err=True)
Expand Down
13 changes: 4 additions & 9 deletions src/crewai/cli/run_crew.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import subprocess

import click
import tomllib
from packaging import version

from crewai.cli.utils import get_crewai_version
from crewai.cli.utils import get_crewai_version, read_toml


def run_crew() -> None:
Expand All @@ -15,10 +14,9 @@ def run_crew() -> None:
crewai_version = get_crewai_version()
min_required_version = "0.71.0"

with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)
pyproject_data = read_toml()

if data.get("tool", {}).get("poetry") and (
if pyproject_data.get("tool", {}).get("poetry") and (
version.parse(crewai_version) < version.parse(min_required_version)
):
click.secho(
Expand All @@ -35,10 +33,7 @@ def run_crew() -> None:
click.echo(f"An error occurred while running the crew: {e}", err=True)
click.echo(e.output, err=True, nl=True)

with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)

if data.get("tool", {}).get("poetry"):
if pyproject_data.get("tool", {}).get("poetry"):
click.secho(
"It's possible that you are using an old version of crewAI that uses poetry, please run `crewai update` to update your pyproject.toml to use uv.",
fg="yellow",
Expand Down
40 changes: 20 additions & 20 deletions src/crewai/cli/update_crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import shutil

import tomli_w
import tomllib

from crewai.cli.utils import read_toml


def update_crew() -> None:
Expand All @@ -18,10 +19,9 @@ def migrate_pyproject(input_file, output_file):
And it will be used to migrate the pyproject.toml to the new format when uv is used.
When the time comes that uv supports the new format, this function will be deprecated.
"""

poetry_data = {}
# Read the input pyproject.toml
with open(input_file, "rb") as f:
pyproject = tomllib.load(f)
pyproject_data = read_toml()

# Initialize the new project structure
new_pyproject = {
Expand All @@ -30,30 +30,30 @@ def migrate_pyproject(input_file, output_file):
}

# Migrate project metadata
if "tool" in pyproject and "poetry" in pyproject["tool"]:
poetry = pyproject["tool"]["poetry"]
new_pyproject["project"]["name"] = poetry.get("name")
new_pyproject["project"]["version"] = poetry.get("version")
new_pyproject["project"]["description"] = poetry.get("description")
if "tool" in pyproject_data and "poetry" in pyproject_data["tool"]:
poetry_data = pyproject_data["tool"]["poetry"]
new_pyproject["project"]["name"] = poetry_data.get("name")
new_pyproject["project"]["version"] = poetry_data.get("version")
new_pyproject["project"]["description"] = poetry_data.get("description")
new_pyproject["project"]["authors"] = [
{
"name": author.split("<")[0].strip(),
"email": author.split("<")[1].strip(">").strip(),
}
for author in poetry.get("authors", [])
for author in poetry_data.get("authors", [])
]
new_pyproject["project"]["requires-python"] = poetry.get("python")
new_pyproject["project"]["requires-python"] = poetry_data.get("python")
else:
# If it's already in the new format, just copy the project section
new_pyproject["project"] = pyproject.get("project", {})
new_pyproject["project"] = pyproject_data.get("project", {})

# Migrate or copy dependencies
if "dependencies" in new_pyproject["project"]:
# If dependencies are already in the new format, keep them as is
pass
elif "dependencies" in poetry:
elif poetry_data and "dependencies" in poetry_data:
new_pyproject["project"]["dependencies"] = []
for dep, version in poetry["dependencies"].items():
for dep, version in poetry_data["dependencies"].items():
if isinstance(version, dict): # Handle extras
extras = ",".join(version.get("extras", []))
new_dep = f"{dep}[{extras}]"
Expand All @@ -67,10 +67,10 @@ def migrate_pyproject(input_file, output_file):
new_pyproject["project"]["dependencies"].append(new_dep)

# Migrate or copy scripts
if "scripts" in poetry:
new_pyproject["project"]["scripts"] = poetry["scripts"]
elif "scripts" in pyproject.get("project", {}):
new_pyproject["project"]["scripts"] = pyproject["project"]["scripts"]
if poetry_data and "scripts" in poetry_data:
new_pyproject["project"]["scripts"] = poetry_data["scripts"]
elif pyproject_data.get("project", {}) and "scripts" in pyproject_data["project"]:
new_pyproject["project"]["scripts"] = pyproject_data["project"]["scripts"]
else:
new_pyproject["project"]["scripts"] = {}

Expand All @@ -87,8 +87,8 @@ def migrate_pyproject(input_file, output_file):
new_pyproject["project"]["scripts"]["run_crew"] = f"{module_name}.main:run"

# Migrate optional dependencies
if "extras" in poetry:
new_pyproject["project"]["optional-dependencies"] = poetry["extras"]
if poetry_data and "extras" in poetry_data:
new_pyproject["project"]["optional-dependencies"] = poetry_data["extras"]

# Backup the old pyproject.toml
backup_file = "pyproject-old.toml"
Expand Down
8 changes: 8 additions & 0 deletions src/crewai/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, Dict, List

import click
import tomli
from rich.console import Console

from crewai.cli.authentication.utils import TokenManager
Expand Down Expand Up @@ -54,6 +55,13 @@ def simple_toml_parser(content):
return result


def read_toml(file_path: str = "pyproject.toml"):
"""Read the content of a TOML file and return it as a dictionary."""
with open(file_path, "rb") as f:
toml_dict = tomli.load(f)
return toml_dict


def parse_toml(content):
if sys.version_info >= (3, 11):
return tomllib.loads(content)
Expand Down
2 changes: 2 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1c50df3

Please sign in to comment.