Skip to content

Commit

Permalink
feat: Use yaml format instead of json for loaders and tools config (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
leehuwuj authored Mar 28, 2024
1 parent 65bc28d commit ce2f24d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-candles-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-llama": patch
---

Update loaders and tools config to yaml format
4 changes: 2 additions & 2 deletions create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ export async function createApp({
console.log(
yellow(
`You have selected tools that require configuration. Please configure them in the ${terminalLink(
"config/tools.json",
`file://${root}/config/tools.json`,
"config/tools.yaml",
`file://${root}/config/tools.yaml`,
)} file.`,
),
);
Expand Down
31 changes: 17 additions & 14 deletions helpers/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "path";
import { cyan, red } from "picocolors";
import { parse, stringify } from "smol-toml";
import terminalLink from "terminal-link";
import yaml, { Document } from "yaml";
import { copy } from "./copy";
import { templatesDir } from "./dir";
import { isPoetryAvailable, tryPoetryInstall } from "./poetry";
Expand Down Expand Up @@ -242,20 +243,17 @@ export const installPythonTemplate = async ({
tools.forEach((tool) => {
configContent[tool.name] = tool.config ?? {};
});
const configFilePath = path.join(root, "config/tools.json");
const configFilePath = path.join(root, "config/tools.yaml");
await fs.mkdir(path.join(root, "config"), { recursive: true });
await fs.writeFile(
configFilePath,
JSON.stringify(configContent, null, 2),
);
await fs.writeFile(configFilePath, yaml.stringify(configContent));
} else {
await copy("**", enginePath, {
parents: true,
cwd: path.join(compPath, "engines", "python", "chat"),
});
}

const loaderConfigs: Record<string, any> = {};
const loaderConfigs = new Document({});
const loaderPath = path.join(enginePath, "loaders");

// Copy loaders to enginePath
Expand All @@ -277,22 +275,27 @@ export const installPythonTemplate = async ({
depth: dsConfig.depth,
};
});
loaderConfigs["web"] = webLoaderConfig;
// Create YamlNode from array of YAMLMap
const node = loaderConfigs.createNode(webLoaderConfig);
node.commentBefore = `
Config for web loader
- base_url: The url to start crawling with
- prefix: the prefix of next URLs to crawl
- depth: the maximum depth in DFS
You can add more web loaders by adding more config below`;
loaderConfigs.set("web", node);
}
// File loader config
if (dataSources.some((ds) => ds.type === "file")) {
loaderConfigs["file"] = {
loaderConfigs.set("file", {
use_llama_parse: useLlamaParse,
};
});
}
// Write loaders config
if (Object.keys(loaderConfigs).length > 0) {
const loaderConfigPath = path.join(root, "config/loaders.json");
const loaderConfigPath = path.join(root, "config/loaders.yaml");
await fs.mkdir(path.join(root, "config"), { recursive: true });
await fs.writeFile(
loaderConfigPath,
JSON.stringify(loaderConfigs, null, 2),
);
await fs.writeFile(loaderConfigPath, yaml.stringify(loaderConfigs));
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"typescript": "^5.3.3",
"eslint-config-prettier": "^8.10.0",
"ora": "^8.0.1",
"fs-extra": "11.2.0"
"fs-extra": "11.2.0",
"yaml": "2.4.1"
},
"engines": {
"node": ">=16.14.0"
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 3 additions & 3 deletions templates/components/engines/python/agent/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import yaml
import importlib

from llama_index.core.tools.tool_spec.base import BaseToolSpec
Expand Down Expand Up @@ -26,8 +26,8 @@ def create_tool(tool_name: str, **kwargs) -> list[FunctionTool]:
@staticmethod
def from_env() -> list[FunctionTool]:
tools = []
with open("config/tools.json", "r") as f:
tool_configs = json.load(f)
with open("config/tools.yaml", "r") as f:
tool_configs = yaml.safe_load(f)
for name, config in tool_configs.items():
tools += ToolFactory.create_tool(name, **config)
return tools
6 changes: 3 additions & 3 deletions templates/components/loaders/python/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import json
import yaml
import importlib
import logging
from typing import Dict
Expand All @@ -10,8 +10,8 @@


def load_configs():
with open("config/loaders.json") as f:
configs = json.load(f)
with open("config/loaders.yaml") as f:
configs = yaml.safe_load(f)
return configs


Expand Down

0 comments on commit ce2f24d

Please sign in to comment.