How can I emit a custom event from a custom LLM im...
# ask-ai
c
How can I emit a custom event from a custom LLM implementation, when the LLM logic runs in a separate service? In my setup, the worker only sees plain text because the tool call logic happens entirely in that other service.
t
Thanks for reaching out, Sergio. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to emit a custom event from your custom LLM implementation when the LLM logic runs in a separate service, here’s a simple way to do it: 1. Create a Custom Event Class: Start by defining a class that represents your custom event. Something like this works well:
Copy code
class CustomLLMEvent:
    def __init__(self, item_id: str, transcript: str, is_final: bool):
        self.item_id = item_id
        self.transcript = transcript
        self.is_final = is_final
1. Emit the Custom Event: Next, use the
emit()
method from
EventEmitter
to send out your custom event. Here’s an example:
Copy code
self.emit("custom_event_name", CustomLLMEvent(...))
Then, in your LLM service, you can listen for that custom event like this:
Copy code
@agent.on("custom_event_name")
def handle_custom_event(event: CustomLLMEvent):
    # Process the event
    pass
Just make sure your
EventEmitter
is properly set up and shared between the parts of your system. This way, your worker and LLM service can chat smoothly, even if they’re running separately. Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Agents Core Architecture | livekit/livekit_composite | DeepWiki
c
How can I share the EventEmitter? can you send me an example
t
Hey there! If you want to share the
EventEmitter
between your components, here’s a simple way to do it: 1. Extend the EventEmitter: Start by creating a custom class that extends the
EventEmitter
so you can define your own event types:
Copy code
const EventEmitter = require('events');

class CustomEmitter extends EventEmitter {
    constructor() {
        super();
    }
}
1. Initialize and Share: Then, create just one instance of your
CustomEmitter
and share it across the components:
Copy code
const sharedEmitter = new CustomEmitter();

// Component A
sharedEmitter.on('custom_event', (data) => {
    console.log('Event received in Component A:', data);
});

// Component B
sharedEmitter.emit('custom_event', { message: 'Hello from Component B!' });
This way, all components using the same
sharedEmitter
can easily talk to each other. If you want me to walk you through it more or need another example, just let me know—I’m here to help!
c
But remember that I have 2 components. 1. Worker running on machine A 2. Custom LLM running on machine B
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!