Skip to content

Commit

Permalink
start outsourcing to an app controller
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdai committed Jul 25, 2024
1 parent 07f373b commit 6110a7f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 61 deletions.
59 changes: 59 additions & 0 deletions snowflake_cybersyn_demo/apps/controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Generator, List, Optional

import streamlit as st
from llama_agents import LlamaAgentsClient
from llama_index.core.llms import ChatMessage, ChatResponseGen


class TaskStatus(str, Enum):
HUMAN_REQUIRED = "human_required"
COMPLETED = "completed"
SUBMITTED = "submitted"


@dataclass
class TaskModel:
task_id: str
input: str
status: TaskStatus
prompt: Optional[str] = None
chat_history: List[ChatMessage] = field(default_factory=list)


class Controller:
def __init__(self, llama_agents_client: Optional[LlamaAgentsClient]):
self._client = llama_agents_client

def _llama_index_stream_wrapper(
self,
llama_index_stream: ChatResponseGen,
) -> Generator[str, Any, Any]:
for chunk in llama_index_stream:
yield chunk.delta

def _handle_task_submission(
self, llama_agents_client: LlamaAgentsClient
) -> None:
"""Handle the user submitted message. Clear task submission box, and
add the new task to the submitted list.
"""

# create new task and store in state
# task_input = st.session_state.task_input
# task_id = self._client.create_task(task_input)
# task = TaskModel(
# task_id=task_id,
# input=task_input,
# chat_history=[
# ChatMessage(role="user", content=task_input),
# ChatMessage(
# role="assistant",
# content=f"Successfully submitted task: {task_id}.",
# ),
# ],
# status=TaskStatus.SUBMITTED,
# )
st.session_state.submitted_pills.append(st.session_state.task_input)
# st.session_state.tasks.append(task)
78 changes: 17 additions & 61 deletions snowflake_cybersyn_demo/apps/streamlit.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,24 @@
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Generator, List, Optional

import pandas as pd
import streamlit as st
from llama_agents import LlamaAgentsClient
from llama_index.core.llms import ChatMessage, ChatResponseGen
from llama_index.core.llms import ChatMessage
from llama_index.llms.openai import OpenAI


class TaskStatus(str, Enum):
HUMAN_REQUIRED = "human_required"
COMPLETED = "completed"
SUBMITTED = "submitted"


@dataclass
class TaskModel:
task_id: str
input: str
status: TaskStatus
prompt: Optional[str] = None
chat_history: List[ChatMessage] = field(default_factory=list)


def _llama_index_stream_wrapper(
llama_index_stream: ChatResponseGen,
) -> Generator[str, Any, Any]:
for chunk in llama_index_stream:
yield chunk.delta


def _handle_task_submission(llama_agents_client: LlamaAgentsClient) -> None:
"""Handle the user submitted message. Clear task submission box, and
add the new task to the submitted list.
"""

# create new task and store in state
task_input = st.session_state.task_input
task_id = llama_agents_client.create_task(task_input)
task = TaskModel(
task_id=task_id,
input=task_input,
chat_history=[
message,
ChatMessage(
role="assistant",
content=f"Successfully submitted task: {task_id}.",
),
],
status=TaskStatus.SUBMITTED,
)
st.session_state.submitted_pills.append(st.session_state.task_input)
st.session_state.tasks.append(task)

from snowflake_cybersyn_demo.apps.controller import Controller

llm = OpenAI(model="gpt-4o-mini")
control_plane_host = "0.0.0.0"
control_plane_port = 8001
llama_agents_client = LlamaAgentsClient(
control_plane_url=(
f"http://{control_plane_host}:{control_plane_port}"
if control_plane_port
else f"http://{control_plane_host}"
)
)
# llama_agents_client = LlamaAgentsClient(
# control_plane_url=(
# f"http://{control_plane_host}:{control_plane_port}"
# if control_plane_port
# else f"http://{control_plane_host}"
# )
# )
llama_agents_client = None
controller = Controller(llama_agents_client)

### App
st.set_page_config(layout="wide")
st.title("Human In The Loop W/ LlamaAgents")

Expand All @@ -84,7 +38,7 @@ def _handle_task_submission(llama_agents_client: LlamaAgentsClient) -> None:
"Task input",
placeholder="Enter a task input.",
key="task_input",
on_change=_handle_task_submission,
on_change=controller._handle_task_submission,
args=(llama_agents_client,),
)

Expand All @@ -108,7 +62,9 @@ def _handle_task_submission(llama_agents_client: LlamaAgentsClient) -> None:
for m in st.session_state.messages
]
)
response = st.write_stream(_llama_index_stream_wrapper(stream))
response = st.write_stream(
controller._llama_index_stream_wrapper(stream)
)
st.session_state.messages.append(
{"role": "assistant", "content": response}
)
Expand Down

0 comments on commit 6110a7f

Please sign in to comment.