Skip to content

Commit

Permalink
Add taskweaver init for pip install (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShilinHe authored May 15, 2024
2 parents 3f61635 + 7d51b7c commit 9c9cc17
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ test_workspace


# extensions template
taskweaver/cli/taskweaver-ext.zip
taskweaver/cli/taskweaver-project.zip

# General
.DS_Store
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ cd TaskWeaver
pip install -r requirements.txt
```

If you want to install an earlier version of TaskWeaver, you may check the [release](https://github.com/microsoft/TaskWeaver/releases) page, find the tag (e.g., `v0.0.1`) and install it by
```
pip install git+https://github.com/microsoft/TaskWeaver@<TAG>
```

### 🖊️ Step 2: Configure the LLMs
Before running TaskWeaver, you need to provide your LLM configurations. Taking OpenAI as an example, you can configure `taskweaver_config.json` file as follows.
Expand Down Expand Up @@ -115,6 +119,8 @@ TaskWeaver also supports WebUI for demo purpose, please refer to [web UI docs](h
#### or 📋 Import as a Library
TaskWeaver can be imported as a library to integrate with your existing project, more information can be found in [docs](https://microsoft.github.io/TaskWeaver/docs/usage/library)



## 📖 Documentation
More documentations can be found on [TaskWeaver Website](https://microsoft.github.io/TaskWeaver).

Expand Down
24 changes: 24 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ def revert():
long_description = fh.read()


# create zip file for ext
def create_zip_file():
import zipfile
from pathlib import Path

root_dir = Path(__file__).parent
ext_zip_file = root_dir / "taskweaver" / "cli" / "taskweaver-project.zip"
if os.path.exists(ext_zip_file):
os.remove(ext_zip_file)

content_root = root_dir / "project"
zipf = zipfile.ZipFile(ext_zip_file, "w", zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(content_root):
for file in files:
zipf.write(
os.path.join(root, file),
os.path.relpath(Path(root) / file, root_dir),
)
zipf.close()


create_zip_file()

cur_dir = os.path.dirname(
os.path.abspath(
__file__,
Expand Down Expand Up @@ -77,6 +100,7 @@ def revert():
],
package_data={
"taskweaver": ["**/*.yaml", "**/*.yml"],
"taskweaver.cli": ["taskweaver-project.zip"],
},
entry_points={
"console_scripts": ["taskweaver=taskweaver.__main__:main"],
Expand Down
58 changes: 12 additions & 46 deletions taskweaver/cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,21 @@ def init(
if not os.path.exists(project):
os.mkdir(project)

def get_dir(*dir: str):
return os.path.join(project, *dir)

dir_list = [
"codeinterpreter_examples",
"planner_examples",
"plugins",
"config",
"workspace",
]
for dir in dir_list:
dir_path = get_dir(dir)
if not os.path.exists(dir_path):
os.mkdir(dir_path)

init_temp_dir = get_dir("init")
import zipfile
from pathlib import Path

tpl_dir = os.path.join(init_temp_dir, "template")
ext_zip_file = Path(__file__).parent / "taskweaver-ext.zip"
tpl_dir = os.path.join(project, "temp")
if not os.path.exists(tpl_dir):
os.mkdir(tpl_dir)

ext_zip_file = Path(__file__).parent / "taskweaver-project.zip"
if os.path.exists(ext_zip_file):
with zipfile.ZipFile(ext_zip_file, "r") as zip_ref:
# Extract all files to the current directory
zip_ref.extractall(tpl_dir)

tpl_planner_example_dir = os.path.join(tpl_dir, "taskweaver-ext", "planner_examples")
tpl_ci_example_dir = os.path.join(tpl_dir, "taskweaver-ext", "codeinterpreter_examples")
tpl_plugin_dir = os.path.join(tpl_dir, "taskweaver-ext", "plugins")
tpl_config_dir = os.path.join(tpl_dir, "taskweaver-ext")
planner_example_dir = get_dir("planner_examples")
ci_example_dir = get_dir("codeinterpreter_examples")
plugin_dir = get_dir("plugins")
copy_files(tpl_planner_example_dir, planner_example_dir)
copy_files(tpl_ci_example_dir, ci_example_dir)
copy_files(tpl_plugin_dir, plugin_dir)
copy_file(tpl_config_dir, "taskweaver_config.json", get_dir(""))

copy_files(os.path.join(tpl_dir, "project"), project)
try:
shutil.rmtree(init_temp_dir)
shutil.rmtree(tpl_dir)
except Exception:
click.secho("Failed to remove temporary directory", fg="yellow")
click.secho(
Expand All @@ -105,17 +80,8 @@ def get_dir(*dir: str):


def copy_files(src_dir: str, dst_dir: str):
# Get a list of all files in the source directory
files = os.listdir(src_dir)

# Loop through the files and copy each one to the destination directory
for file in files:
if os.path.isfile(os.path.join(src_dir, file)):
copy_file(src_dir, file, dst_dir)


def copy_file(src_dir: str, filename: str, dst_dir: str):
shutil.copy(
os.path.join(src_dir, filename),
os.path.join(dst_dir, filename),
)
# Check if the destination folder exists. If not, create it.
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
# Copy the content of source_folder to destination_folder
shutil.copytree(src_dir, dst_dir, dirs_exist_ok=True)
2 changes: 1 addition & 1 deletion taskweaver/cli/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def new_func(ctx: click.Context, *args: Any, **kwargs: Any):
else:
click.echo(
"The current directory is not a valid Task Weaver project directory. "
"There needs to be a `taskweaver-config.json` in the root of the project directory. "
"There needs to be a `taskweaver_config.json` in the root of the project directory. "
"Please change the working directory to a valid project directory or initialize a new one. "
"Refer to --help for more information.",
)
Expand Down

0 comments on commit 9c9cc17

Please sign in to comment.