how to build a simple chat agent without stt and t...
# ask-ai
a
how to build a simple chat agent without stt and tts
t
Thanks for reaching out, Namit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To build a simple chat agent without speech-to-text (STT) and text-to-speech (TTS) in LiveKit, follow these steps: 1. Create a Text-Only Agent:
Copy code
class MyAgent(Agent):
    def __init__(self) -> None:
        super().__init__(
            instructions="You are a helpful assistant.",
        )
2. Configure the Agent Session for Text-Only Interaction:
Copy code
async def entrypoint(ctx: JobContext):
    session = AgentSession(
        llm=openai.LLM(model="gpt-4o-mini"),
    )
    await session.start(
        agent=MyAgent(),
        room=ctx.room,
        room_input_options=RoomInputOptions(text_enabled=True, audio_enabled=False),
        room_output_options=RoomOutputOptions(transcription_enabled=True, audio_enabled=False),
    )
3. Handle Text Input and Output: Use
session.generate_reply()
to send messages to users and listen for incoming messages using
session.on()
events. Here is the complete example code:
Copy code
import logging
from dotenv import load_dotenv

from livekit.agents import (
    Agent,
    AgentSession,
    JobContext,
    RoomInputOptions,
    RoomOutputOptions,
    WorkerOptions,
    cli,
)
from livekit.plugins import openai

logger = logging.getLogger("text-only")
logger.setLevel(<http://logging.INFO|logging.INFO>)

load_dotenv()

class MyAgent(Agent):
    def __init__(self) -> None:
        super().__init__(
            instructions="You are a helpful assistant.",
        )
    
async def entrypoint(ctx: JobContext):
    session = AgentSession(
        llm=openai.LLM(model="gpt-4o-mini"),
    )
    await session.start(
        agent=MyAgent(),
        room=ctx.room,
        room_input_options=RoomInputOptions(text_enabled=True, audio_enabled=False),
        room_output_options=RoomOutputOptions(transcription_enabled=True, audio_enabled=False),
    )

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
For more details, you can refer to the Text-Only Agent Example in the LiveKit repository.
a
how to run it in my console and interact with text
t
Here’s a friendly guide to help you run your LiveKit text-only chat agent right from the console and chat with it using text: 1. Run the Agent in Console Mode: Just use this command to get your agent up and running in console mode: `python agent.py console`This will launch the agent locally in your terminal, so you can chat with it through text. 2. Interact with the Agent: Once it’s running, simply type your messages straight into the console. The agent will read what you type and reply back. For example, you might send: `session.generate_reply(user_input="Hello, how can you help me today?")`You’ll see the agent’s responses right there in the console. 3. Make Sure It’s Set for Text-Only: Double-check your code to confirm the agent is set up for text-only chatting by using these settings:
Copy code
AgentSession(
    llm=openai.LLM(model="gpt-4o-mini"),
    room=ctx.room,
    room_input_options=RoomInputOptions(text_enabled=True, audio_enabled=False),
    room_output_options=RoomOutputOptions(transcription_enabled=True, audio_enabled=False),
)
4. Handle Conversation Events: You can also listen for events like
conversation_item_added
to catch new messages and respond accordingly:
session.on("conversation_item_added", callback_function)
Following these steps will have you chatting with your agent in the console in no time. If you have any questions along the way, I’m here to help! Sources: livekit-agents/livekit/agents/voice/chat_cli.py | Text and transcriptions | LiveKit Docs