Skip to content

feat: Add publisher agent, merge code with streaming template #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 77 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
5be66d7
add artifact generator agent
leehuwuj Sep 25, 2024
c3f2e05
enhance workflow
leehuwuj Sep 25, 2024
17c34f1
rename to publisher
leehuwuj Sep 25, 2024
4691c3c
add changeset
leehuwuj Sep 25, 2024
ab2be63
enhance code
leehuwuj Sep 25, 2024
7ec56c9
refactor code
leehuwuj Sep 26, 2024
6b8109f
add write file error handling
leehuwuj Sep 26, 2024
13ad714
keep old workflow
leehuwuj Sep 26, 2024
f0d58ca
add artifact generator tool
leehuwuj Sep 26, 2024
72e57ee
add changeset and format code
leehuwuj Sep 26, 2024
d28e422
improve pdf looking
leehuwuj Sep 26, 2024
540e850
add duckduckgo image search
leehuwuj Sep 26, 2024
ea1f112
add duckduckgo tool and improve prompting
leehuwuj Sep 26, 2024
9af221c
add missing duckduckgo package
leehuwuj Sep 26, 2024
fd5d86e
improve publisher prompt
leehuwuj Sep 26, 2024
c2df7a5
Refine the planner by include chat history
leehuwuj Sep 26, 2024
10ec398
change role to description
leehuwuj Sep 26, 2024
c217f04
fix missing change
leehuwuj Sep 26, 2024
818a0bb
add document generator tool
leehuwuj Sep 27, 2024
4298256
rename artifact to document generator tool
leehuwuj Sep 27, 2024
2af3c41
add tools for multiagent
leehuwuj Sep 27, 2024
4169525
update python to change artifact to document generator
leehuwuj Sep 27, 2024
73c50b4
update python to change artifact to document generator
leehuwuj Sep 27, 2024
251e842
fix wordings
leehuwuj Sep 27, 2024
1e37edb
fix linting
leehuwuj Sep 27, 2024
73a3967
fix doubled html
leehuwuj Sep 27, 2024
f89e377
Update helpers/tools.ts
marcusschiesser Sep 27, 2024
8fda798
enhance error handling
leehuwuj Sep 27, 2024
2fcbe2b
remove redundant code
leehuwuj Sep 27, 2024
9bd02f0
update python multiagent template
leehuwuj Sep 27, 2024
aff5beb
reuse python streaming for multiagent
leehuwuj Sep 27, 2024
ce3a5f3
refactor vercel response to for reusing
leehuwuj Sep 27, 2024
a84e885
add tools usage for ts
leehuwuj Sep 27, 2024
48614d4
add publisher for TS
leehuwuj Sep 30, 2024
ca82a22
only use query engine if index is not None
leehuwuj Sep 30, 2024
ab94dc0
fix chat issue
leehuwuj Sep 30, 2024
f9f19b4
ignore typing F821
leehuwuj Sep 30, 2024
e96c861
fix: always add tool dependencies for TS
leehuwuj Sep 30, 2024
461eb4e
fix: wrong engine template
leehuwuj Sep 30, 2024
ababbe1
fix: ts type checking
leehuwuj Sep 30, 2024
eadca2a
fix: e2e
leehuwuj Sep 30, 2024
498ad16
remove pdf generator in TS
leehuwuj Sep 30, 2024
6c65469
fix ts typing
leehuwuj Sep 30, 2024
138b9c0
always use agent template for multi-agent
leehuwuj Sep 30, 2024
ee4fdc5
format code
leehuwuj Oct 1, 2024
56e29a6
fix python vercel stream generator
leehuwuj Oct 1, 2024
ab9890a
Update .changeset/flat-singers-share.md
marcusschiesser Oct 1, 2024
7df29e6
Update .changeset/gorgeous-penguins-shout.md
marcusschiesser Oct 1, 2024
42d0a05
rename doc generator
marcusschiesser Oct 1, 2024
45932cb
separate chat api for multi-agent code
leehuwuj Oct 1, 2024
7cedd55
use saveDocument from file server and simplify tool factory for agents
marcusschiesser Oct 1, 2024
01410e2
add resolve package test for document generator tool
leehuwuj Oct 1, 2024
5248530
add duckduckgo image search and tunning the prompt
leehuwuj Oct 1, 2024
5540935
revert change in streaming chat.py
leehuwuj Oct 1, 2024
610f0d0
fix wrong wikipedia tool name
leehuwuj Oct 1, 2024
dd850c6
fix linting
leehuwuj Oct 1, 2024
8f097e2
only use VercelStreamResponse
leehuwuj Oct 1, 2024
d8dd59c
improve URL handling of doc gen in TS
marcusschiesser Oct 1, 2024
bd64972
revised python chat api
leehuwuj Oct 1, 2024
6def1e3
fix vercel stream for multiagent
leehuwuj Oct 1, 2024
efd16e3
add previous agent message to chat history and refine the prompt
leehuwuj Oct 1, 2024
341f39a
tunning publisher prompt for publish file or reply the content
leehuwuj Oct 2, 2024
74cda77
remove duckduckgo image search in TS
leehuwuj Oct 2, 2024
1f17805
fix wrong output dir
leehuwuj Oct 2, 2024
7ccdecb
refine prompt image link
leehuwuj Oct 2, 2024
65474af
update in consistent paths
leehuwuj Oct 2, 2024
800d79e
update refined prompt for python
leehuwuj Oct 2, 2024
cdfa99f
refine python agent prompt
leehuwuj Oct 2, 2024
c09de15
fix prompt grammar
leehuwuj Oct 2, 2024
be0deb5
fix prompt
leehuwuj Oct 2, 2024
af84764
remove checking finished task event
leehuwuj Oct 2, 2024
7c687ac
fix missing rename readme in python template
leehuwuj Oct 2, 2024
c4ce77a
remove rendering system prompt env for multiagent template
leehuwuj Oct 2, 2024
1887950
fix: overlapping messages in executor (start only one sub task)
marcusschiesser Oct 2, 2024
1100274
added selector to workflow
marcusschiesser Oct 2, 2024
0263aef
revise researcher prompt
leehuwuj Oct 2, 2024
4a1b965
small tweak
leehuwuj Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-singers-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-llama": patch
---

Add publisher agent to multi-agents for generating documents (PDF and HTML)
5 changes: 5 additions & 0 deletions .changeset/gorgeous-penguins-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-llama": patch
---

Allow tool selection for multi-agents (Python and TS)
1 change: 1 addition & 0 deletions e2e/python/resolve_dependencies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ if (
const toolOptions = [
"wikipedia.WikipediaToolSpec",
"google.GoogleSearchToolSpec",
"document_generator",
];

const dataSources = [
Expand Down
4 changes: 3 additions & 1 deletion e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export async function runCreateLlama({
if (appType) {
commandArgs.push(appType);
}
if (!useLlamaParse) {
if (useLlamaParse) {
commandArgs.push("--use-llama-parse");
} else {
commandArgs.push("--no-llama-parse");
}

Expand Down
39 changes: 20 additions & 19 deletions helpers/env-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,34 +426,35 @@ const getToolEnvs = (tools?: Tool[]): EnvVar[] => {
const getSystemPromptEnv = (
tools?: Tool[],
dataSources?: TemplateDataSource[],
framework?: TemplateFramework,
template?: TemplateType,
): EnvVar[] => {
const defaultSystemPrompt =
"You are a helpful assistant who helps users with their questions.";

const systemPromptEnv: EnvVar[] = [];
// build tool system prompt by merging all tool system prompts
let toolSystemPrompt = "";
tools?.forEach((tool) => {
const toolSystemPromptEnv = tool.envVars?.find(
(env) => env.name === TOOL_SYSTEM_PROMPT_ENV_VAR,
);
if (toolSystemPromptEnv) {
toolSystemPrompt += toolSystemPromptEnv.value + "\n";
}
});
// multiagent template doesn't need system prompt
if (template !== "multiagent") {
let toolSystemPrompt = "";
tools?.forEach((tool) => {
const toolSystemPromptEnv = tool.envVars?.find(
(env) => env.name === TOOL_SYSTEM_PROMPT_ENV_VAR,
);
if (toolSystemPromptEnv) {
toolSystemPrompt += toolSystemPromptEnv.value + "\n";
}
});

const systemPrompt = toolSystemPrompt
? `\"${toolSystemPrompt}\"`
: defaultSystemPrompt;
const systemPrompt = toolSystemPrompt
? `\"${toolSystemPrompt}\"`
: defaultSystemPrompt;

const systemPromptEnv = [
{
systemPromptEnv.push({
name: "SYSTEM_PROMPT",
description: "The system prompt for the AI model.",
value: systemPrompt,
},
];

});
}
if (tools?.length == 0 && (dataSources?.length ?? 0 > 0)) {
const citationPrompt = `'You have provided information from a knowledge base that has been passed to you in nodes of information.
Each node has useful metadata such as node ID, file name, page, etc.
Expand Down Expand Up @@ -559,7 +560,7 @@ export const createBackendEnvFile = async (
...getToolEnvs(opts.tools),
...getTemplateEnvs(opts.template),
...getObservabilityEnvs(opts.observability),
...getSystemPromptEnv(opts.tools, opts.dataSources, opts.framework),
...getSystemPromptEnv(opts.tools, opts.dataSources, opts.template),
];
// Render and write env file
const content = renderEnvVar(envVars);
Expand Down
40 changes: 32 additions & 8 deletions helpers/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,12 @@ export const installPythonTemplate = async ({
| "modelConfig"
>) => {
console.log("\nInitializing Python project with template:", template, "\n");
const templatePath = path.join(templatesDir, "types", template, framework);
let templatePath;
if (template === "extractor") {
templatePath = path.join(templatesDir, "types", "extractor", framework);
} else {
templatePath = path.join(templatesDir, "types", "streaming", framework);
}
await copy("**", root, {
parents: true,
cwd: templatePath,
Expand Down Expand Up @@ -401,23 +406,42 @@ export const installPythonTemplate = async ({
cwd: path.join(compPath, "services", "python"),
});
}

if (template === "streaming") {
// For the streaming template only:
// Copy engine code
if (template === "streaming" || template === "multiagent") {
// Select and copy engine code based on data sources and tools
let engine;
if (dataSources.length > 0 && (!tools || tools.length === 0)) {
console.log("\nNo tools selected - use optimized context chat engine\n");
engine = "chat";
} else {
// Multiagent always uses agent engine
if (template === "multiagent") {
engine = "agent";
} else {
// For streaming, use chat engine by default
// Unless tools are selected, in which case use agent engine
if (dataSources.length > 0 && (!tools || tools.length === 0)) {
console.log(
"\nNo tools selected - use optimized context chat engine\n",
);
engine = "chat";
} else {
engine = "agent";
}
}

// Copy engine code
await copy("**", enginePath, {
parents: true,
cwd: path.join(compPath, "engines", "python", engine),
});
}

if (template === "multiagent") {
// Copy multi-agent code
await copy("**", path.join(root), {
parents: true,
cwd: path.join(compPath, "multiagent", "python"),
rename: assetRelocator,
});
}

console.log("Adding additional dependencies");

const addOnDependencies = getAdditionalDependencies(
Expand Down
23 changes: 23 additions & 0 deletions helpers/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,29 @@ For better results, you can specify the region parameter to get results from a s
},
],
},
{
display: "Document generator",
name: "document_generator",
supportedFrameworks: ["fastapi", "nextjs", "express"],
dependencies: [
{
name: "xhtml2pdf",
version: "^0.2.14",
},
{
name: "markdown",
version: "^3.7",
},
],
type: ToolType.LOCAL,
envVars: [
{
name: TOOL_SYSTEM_PROMPT_ENV_VAR,
description: "System prompt for document generator tool.",
value: `If user request for a report or a post, use document generator tool to create a file and reply with the link to the file.`,
},
],
},
{
display: "Code Interpreter",
name: "interpreter",
Expand Down
5 changes: 4 additions & 1 deletion helpers/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ export const installTSTemplate = async ({
// Select and copy engine code based on data sources and tools
let engine;
tools = tools ?? [];
if (dataSources.length > 0 && tools.length === 0) {
// multiagent template always uses agent engine
if (template === "multiagent") {
engine = "agent";
} else if (dataSources.length > 0 && tools.length === 0) {
console.log("\nNo tools selected - use optimized context chat engine\n");
engine = "chat";
} else {
Expand Down
16 changes: 8 additions & 8 deletions questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,10 @@ export const getDataSourceChoices = (
});
}
if (selectedDataSource === undefined || selectedDataSource.length === 0) {
if (template !== "multiagent") {
choices.push({
title: "No datasource",
value: "none",
});
}
choices.push({
title: "No datasource",
value: "none",
});
choices.push({
title:
process.platform !== "linux"
Expand Down Expand Up @@ -734,8 +732,10 @@ export const askQuestions = async (
}
}

if (!program.tools && program.template === "streaming") {
// TODO: allow to select tools also for multi-agent framework
if (
!program.tools &&
(program.template === "streaming" || program.template === "multiagent")
) {
if (ciInfo.isCI) {
program.tools = getPrefOrDefault("tools");
} else {
Expand Down
2 changes: 1 addition & 1 deletion templates/components/engines/python/agent/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from llama_index.core.tools.query_engine import QueryEngineTool


def get_chat_engine(filters=None, params=None, event_handlers=None):
def get_chat_engine(filters=None, params=None, event_handlers=None, **kwargs):
system_prompt = os.getenv("SYSTEM_PROMPT")
top_k = int(os.getenv("TOP_K", 0))
tools = []
Expand Down
27 changes: 20 additions & 7 deletions templates/components/engines/python/agent/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import importlib
import os

import yaml
import importlib
from llama_index.core.tools.tool_spec.base import BaseToolSpec
from llama_index.core.tools.function_tool import FunctionTool
from llama_index.core.tools.tool_spec.base import BaseToolSpec


class ToolType:
Expand Down Expand Up @@ -40,14 +41,26 @@ def load_tools(tool_type: str, tool_name: str, config: dict) -> list[FunctionToo
raise ValueError(f"Failed to load tool {tool_name}: {e}")

@staticmethod
def from_env() -> list[FunctionTool]:
tools = []
def from_env(
map_result: bool = False,
) -> list[FunctionTool] | dict[str, FunctionTool]:
"""
Load tools from the configured file.
Params:
- use_map: if True, return map of tool name and the tool itself
"""
if map_result:
tools = {}
else:
tools = []
if os.path.exists("config/tools.yaml"):
with open("config/tools.yaml", "r") as f:
tool_configs = yaml.safe_load(f)
for tool_type, config_entries in tool_configs.items():
for tool_name, config in config_entries.items():
tools.extend(
ToolFactory.load_tools(tool_type, tool_name, config)
)
tool = ToolFactory.load_tools(tool_type, tool_name, config)
if map_result:
tools[tool_name] = tool
else:
tools.extend(tool)
return tools
Loading
Loading