Skip to content
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

Migrate AgentRunner to Agent Workflow (Python) #502

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b4f0767
stg
leehuwuj Feb 11, 2025
bc2d503
raise error if there is no tools
leehuwuj Feb 11, 2025
cbebd03
stg
leehuwuj Feb 12, 2025
5ec1947
support request api
leehuwuj Feb 12, 2025
6d5749d
remove --no-files e2e test for python
leehuwuj Feb 12, 2025
22e4be9
use agent workflow for financial report use case
leehuwuj Feb 12, 2025
6ba5023
migrate form_filling to AgentWorkflow
leehuwuj Feb 13, 2025
0e4ee4a
refactor: chat message content
thucpn Feb 13, 2025
86610e6
rename function in chat-ui
thucpn Feb 14, 2025
8d3db71
Create cool-cars-promise.md
marcusschiesser Feb 17, 2025
5a230be
bump chat-ui
leehuwuj Feb 18, 2025
7e23d77
add new query index and weather card for agent workflows
leehuwuj Feb 18, 2025
0139a11
support source nodes
leehuwuj Feb 18, 2025
dae3249
remove unused function
leehuwuj Feb 18, 2025
798f378
fix empty chunk
leehuwuj Feb 19, 2025
d09ae65
keep the old code for financial report and form-filling
leehuwuj Feb 19, 2025
c7e4696
fix annotation message
leehuwuj Feb 19, 2025
c83fa96
fix mypy
leehuwuj Feb 19, 2025
25144dc
add artifact tool component
leehuwuj Feb 24, 2025
fe5982e
fix render empty div
leehuwuj Feb 24, 2025
1e90a6a
improve typing
leehuwuj Feb 24, 2025
087a45e
Merge remote-tracking branch 'origin' into lee/agent-workflows
leehuwuj Feb 25, 2025
d38eb3c
unify chat.py file
leehuwuj Feb 25, 2025
9fd6d0c
remove multiagent folder (python)
leehuwuj Feb 25, 2025
d0f606d
fix linting
leehuwuj Feb 25, 2025
21b7df1
fix missing import
leehuwuj Feb 25, 2025
c996508
support non-streaming api
leehuwuj Feb 25, 2025
be5870c
update citation prompt
leehuwuj Feb 25, 2025
8004c9f
remove dead code
leehuwuj Feb 25, 2025
b60618a
remove dead code
leehuwuj Feb 25, 2025
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
6 changes: 0 additions & 6 deletions helpers/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,6 @@ export const installPythonTemplate = async ({
}
}

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

// Copy router code
await copyRouterCode(root, tools ?? []);
}
Expand Down
10 changes: 6 additions & 4 deletions questions/datasources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ export const getDataSourceChoices = (
});
}
if (selectedDataSource === undefined || selectedDataSource.length === 0) {
choices.push({
title: "No datasource",
value: "none",
});
if (framework !== "fastapi") {
choices.push({
title: "No datasource",
value: "none",
});
}
choices.push({
title:
process.platform !== "linux"
Expand Down
47 changes: 0 additions & 47 deletions templates/components/engines/python/chat/engine.py

This file was deleted.

21 changes: 0 additions & 21 deletions templates/components/engines/python/chat/node_postprocessors.py

This file was deleted.

This file was deleted.

67 changes: 37 additions & 30 deletions templates/types/streaming/fastapi/app/api/routers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
from fastapi import APIRouter, BackgroundTasks, HTTPException, Request, status
from llama_index.core.llms import MessageRole

from app.api.routers.events import EventCallbackHandler
from app.api.callbacks.llamacloud import LlamaCloudFileDownload
from app.api.callbacks.next_question import SuggestNextQuestions
from app.api.callbacks.stream_handler import StreamHandler
from app.api.routers.models import (
ChatData,
Message,
Result,
SourceNodes,
)
from app.api.routers.vercel_response import VercelStreamResponse
from app.engine.engine import get_chat_engine
from app.engine.engine import get_engine
from app.engine.query_filter import generate_filters

chat_router = r = APIRouter()
Expand All @@ -36,15 +37,19 @@ async def chat(
logger.info(
f"Creating chat engine with filters: {str(filters)}",
)
event_handler = EventCallbackHandler()
chat_engine = get_chat_engine(
filters=filters, params=params, event_handlers=[event_handler]
)
response = chat_engine.astream_chat(last_message_content, messages)

return VercelStreamResponse(
request, event_handler, response, data, background_tasks
engine = get_engine(filters=filters, params=params)
handler = engine.run(
user_msg=last_message_content,
chat_history=messages,
stream=True,
)
return StreamHandler.from_default(
handler=handler,
callbacks=[
LlamaCloudFileDownload.from_default(background_tasks),
SuggestNextQuestions.from_default(data),
],
).vercel_stream()
except Exception as e:
logger.exception("Error in chat engine", exc_info=True)
raise HTTPException(
Expand All @@ -53,25 +58,27 @@ async def chat(
) from e


# non-streaming endpoint - delete if not needed
@r.post("/request")
async def chat_request(
data: ChatData,
) -> Result:
last_message_content = data.get_last_message_content()
messages = data.get_history_messages()
# TODO: Update non-streaming endpoint
# Would be better if we use same chat.py endpoint for both agent and multiagent templates
# # non-streaming endpoint - delete if not needed
# @r.post("/request")
# async def chat_request(
# data: ChatData,
# ) -> Result:
# last_message_content = data.get_last_message_content()
# messages = data.get_history_messages()

doc_ids = data.get_chat_document_ids()
filters = generate_filters(doc_ids)
params = data.data or {}
logger.info(
f"Creating chat engine with filters: {str(filters)}",
)
# doc_ids = data.get_chat_document_ids()
# filters = generate_filters(doc_ids)
# params = data.data or {}
# logger.info(
# f"Creating chat engine with filters: {str(filters)}",
# )

chat_engine = get_chat_engine(filters=filters, params=params)
# chat_engine = get_chat_engine(filters=filters, params=params)

response = await chat_engine.achat(last_message_content, messages)
return Result(
result=Message(role=MessageRole.ASSISTANT, content=response.response),
nodes=SourceNodes.from_source_nodes(response.source_nodes),
)
# response = await chat_engine.achat(last_message_content, messages)
# return Result(
# result=Message(role=MessageRole.ASSISTANT, content=response.response),
# nodes=SourceNodes.from_source_nodes(response.source_nodes),
# )
2 changes: 2 additions & 0 deletions templates/types/streaming/fastapi/app/api/routers/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def to_response(self):
return None


# TODO: Add an adapter for workflow events
# and remove callback handler
class EventCallbackHandler(BaseCallbackHandler):
_aqueue: asyncio.Queue
is_done: bool = False
Expand Down
Loading
Loading