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

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
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