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

[Bug]: When the user closes the browser tab or clicks disconnect, the current PID process is not terminated #710

Open
giang13297 opened this issue Sep 30, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@giang13297
Copy link

Brief Description

When the user closes the browser tab or clicks disconnect, I expect a command to kill the current process (PID). However, upon checking, I only see the process being terminated but no actual disconnect happening. This leads to resource consumption, causing the system to slow down and disconnect when new users join the room.

LLM

None

Transcription Services

None

Synthesis Services

None

Telephony Services

None

Conversation Type and Platform

No response

Steps to Reproduce

  1. User click connect the room
  2. Live kit server get the log
  3. User meet the agent
  4. User close the tab or click disconnect
  5. Live kit server receive Request leave from agent but the current PID not be killed
  6. 4-5 users do the same 5 first steps
  7. 6th user wait too long to connect and disconnect while talk

Expected Behavior

  1. When user close the tab or click disconnect, the current PID of this user must be stop or killed

Screenshots

import asyncio
import os
import re

from datetime import datetime

from livekit.agents import JobContext, JobRequest, WorkerOptions, cli
from loguru import logger
from pydantic_settings import BaseSettings, SettingsConfigDict

from vocode.logging import configure_pretty_logging
from vocode.streaming.agent.chat_gpt_agent import ChatGPTAgent
from vocode.streaming.livekit.livekit_conversation import LiveKitConversation

from vocode.streaming.output_device.livekit_output_device import LiveKitOutputDevice
from vocode.streaming.models.events import Event, EventType, Sender
from vocode.streaming.utils.events_manager import EventsManager
from vocode.streaming.models.transcript import TranscriptCompleteEvent, TranscriptEvent
from utils.helpers import get_room_info
from utils.constant import SYSTEM_PROMPT
import json
import time
from utils.agents import create_llm_agent, create_transcriber_agent, create_synthesizer_agent
from utils.mongo_logger import insert_log
from datetime import datetime

class CustomEventsManager(EventsManager):
def init(self, agent: ChatGPTAgent = None):
super().init([EventType.TRANSCRIPT_COMPLETE, EventType.TRANSCRIPT, EventType.PHONE_CALL_ENDED])
if agent:
self.agent = agent

async def handle_event(self, event: Event):
    if isinstance(event, TranscriptCompleteEvent):
        conversation_dict = {"system": self.agent.agent_config.prompt_preamble}
        print("The call has finished, the transcript was", event.transcript)
        for eventlog in event.transcript.event_logs:
            if eventlog.sender == Sender.BOT:
                conversation_dict['assistant'] = eventlog.text
            elif eventlog.sender == Sender.HUMAN:
                conversation_dict['user'] = eventlog.text
                
        # add timestamp
        conversation_dict['timestamp'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        insert_log(conversation_dict)
                        
    # logger.info(f"Event received: {event}, {type(event)}")
    # logger.info(f"prompt_preamble: {self.agent.agent_config.prompt_preamble}")
    # logger.info(f"actions: {self.agent.functions}")

class Settings(BaseSettings):

livekit_api_key: str = "ENTER_YOUR_LIVE_KIT_API_KEY"
livekit_api_secret: str = "ENTER_YOUR_LIVE_KIT_API_SECRET"
livekit_ws_url: str = "ENTER_YOUR_LIVE_KIT_WS_URL"

# This means a .env file can be used to overload these settings
# ex: "OPENAI_API_KEY=my_key" will set openai_api_key over the default above
model_config = SettingsConfigDict(
    env_file=".env",
    env_file_encoding="utf-8",
    extra="ignore",
)

async def wait_for_termination(conversation: LiveKitConversation, ctx: JobContext):
await conversation.wait_for_termination()
await conversation.terminate()
await ctx.room.disconnect()

async def entrypoint(ctx: JobContext):
global SYSTEM_PROMPT
LOCAL_SYSTEM_PROMPT = SYSTEM_PROMPT.format(rooms_info=get_room_info(), time=datetime.now().strftime("%Y-%m-%d"))

configure_pretty_logging()
output_device = LiveKitOutputDevice()
# chatgpt_agent = create_llm_agent("Ollama", LOCAL_SYSTEM_PROMPT, base_url='http://localhost:11434/v1', model_name='llama3.1')
chatgpt_agent = create_llm_agent("ChatGPT", LOCAL_SYSTEM_PROMPT)
custom_trascriber = create_transcriber_agent("Deepgram")
custom_synthesizer = create_synthesizer_agent("Azure", output_device)
conversation = LiveKitConversation(
    output_device=output_device,
    transcriber=custom_trascriber,
    agent=chatgpt_agent,
    synthesizer=custom_synthesizer,
    events_manager=CustomEventsManager(agent=chatgpt_agent),
    
)
await conversation.start_room(ctx.room)
asyncio.create_task(wait_for_termination(conversation, ctx))

async def request_fnc(req: JobRequest) -> None:
logger.info(f"received request {req}")
await req.accept(entrypoint)

if name == "main":
settings = Settings()
cli.run_app(
WorkerOptions(
request_fnc,
api_key=settings.livekit_api_key,
api_secret=settings.livekit_api_secret,
ws_url=settings.livekit_ws_url,
)
)

@giang13297 giang13297 added the bug Something isn't working label Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant