Skip to content

Commit

Permalink
Initial fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HHousen committed Aug 4, 2023
1 parent f8b24af commit 88abb8c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion playground/streaming/agent/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def receiver():
agent_response = typing.cast(AgentResponseMessage, response)

agent.transcript.add_bot_message(
agent_response.message.text, conversation_id
agent_response.message.text, conversation_id, agent_response.message_id
)
print(
"AI: "
Expand Down
7 changes: 5 additions & 2 deletions vocode/streaming/agent/base_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
TYPE_CHECKING,
)
import typing
import uuid
from opentelemetry import trace
from opentelemetry.trace import Span
from vocode.streaming.action.factory import ActionFactory
Expand Down Expand Up @@ -90,6 +91,7 @@ class AgentResponse(TypedModel, type=AgentResponseType.BASE.value):


class AgentResponseMessage(AgentResponse, type=AgentResponseType.MESSAGE.value):
message_id: str
message: BaseMessage
is_interruptible: bool = True

Expand Down Expand Up @@ -180,7 +182,7 @@ def start(self):
super().start()
if self.agent_config.initial_message is not None:
self.produce_interruptible_agent_response_event_nonblocking(
AgentResponseMessage(message=self.agent_config.initial_message),
AgentResponseMessage(message=self.agent_config.initial_message, message_id="initial_message"),
is_interruptible=False,
)

Expand Down Expand Up @@ -214,6 +216,7 @@ async def handle_generate_response(
agent_span_first = tracer.start_span(
f"{tracer_name_start}.generate_first" # type: ignore
)
response_id = str(uuid.uuid4())
responses = self.generate_response(
transcription.message,
is_interrupt=transcription.is_interrupt,
Expand All @@ -229,7 +232,7 @@ async def handle_generate_response(
agent_span_first.end()
is_first_response = False
self.produce_interruptible_agent_response_event_nonblocking(
AgentResponseMessage(message=BaseMessage(text=response)),
AgentResponseMessage(message_id=response_id, message=BaseMessage(text=response)),
is_interruptible=self.agent_config.allow_agent_to_be_cut_off,
)
# TODO: implement should_stop for generate_responses
Expand Down
21 changes: 18 additions & 3 deletions vocode/streaming/models/transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def to_string(self, include_timestamp: bool = False) -> str:


class Message(EventLog):
message_id: Optional[str]
text: str

def to_string(self, include_timestamp: bool = False) -> str:
Expand Down Expand Up @@ -73,9 +74,22 @@ def add_message(
text: str,
sender: Sender,
conversation_id: str,
message_id: Optional[str] = None,
):
timestamp = time.time()
self.event_logs.append(Message(text=text, sender=sender, timestamp=timestamp))
if (
self.event_logs
and (last_event := self.event_logs[-1]).sender == Sender.BOT
and message_id
and last_event.message_id == message_id
):
last_event.text += f" {text}"
else:
timestamp = time.time()
self.event_logs.append(
Message(
text=text, sender=sender, timestamp=timestamp, message_id=message_id
)
)
if self.events_manager is not None:
self.events_manager.publish_event(
TranscriptEvent(
Expand All @@ -93,11 +107,12 @@ def add_human_message(self, text: str, conversation_id: str):
conversation_id=conversation_id,
)

def add_bot_message(self, text: str, conversation_id: str):
def add_bot_message(self, text: str, conversation_id: str, message_id: str):
self.add_message(
text=text,
sender=Sender.BOT,
conversation_id=conversation_id,
message_id=message_id,
)

def get_last_user_message(self):
Expand Down
1 change: 1 addition & 0 deletions vocode/streaming/streaming_conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ async def process(self, item: InterruptibleAgentResponseEvent[AgentResponse]):
self.conversation.transcript.add_bot_message(
text=agent_response.message.text,
conversation_id=self.conversation.id,
message_id=agent_response.message_id,
)

agent_response_message = typing.cast(
Expand Down

0 comments on commit 88abb8c

Please sign in to comment.