voice agent

voice agent - in development

voice agent with langchain and langchain-groq - code

import os

from langchain.agents import create_agent
from langchain_groq import ChatGroq


# --------------------------------------------------
# Configuration
# --------------------------------------------------

GROQ_API_KEY = os.environ["GROQ_API_KEY"]


# --------------------------------------------------
# Tools
# --------------------------------------------------


def check_inventory(product_name: str) -> dict:
    """Check whether an ophthalmic product is available."""

    products = {
        "mask": 100,
        "camera": 5,
    }

    product_name = product_name.lower().strip()
    quantity = products.get(product_name, 0)

    return {
        "product": product_name,
        "available": quantity > 0,
        "quantity": quantity,
    }


# --------------------------------------------------
# Model
# --------------------------------------------------

model = ChatGroq(
    model="openai/gpt-oss-120b",
    api_key=GROQ_API_KEY,
)


# --------------------------------------------------
# Agent
# --------------------------------------------------

agent = create_agent(
    model=model,
    tools=[check_inventory],
    system_prompt="""
You are a helpful ophthalmic sales assistant.

Your goal is to handle customer support and sales requests.

Be concise and friendly.

Do not use emojis, markdown, or unnecessary formatting.

Your responses will be read aloud by a text to speech engine.

When a customer asks about product availability, use the check_inventory tool.

Only provide information that you know or that is returned by your tools.
""",
)


# --------------------------------------------------
# Main
# --------------------------------------------------


def main() -> None:
    print("Emma is ready.")
    print("Type 'exit' or 'quit' to stop.")
    print()

    while True:
        try:
            user_input = input("Customer: ").strip()

            if not user_input:
                continue

            if user_input.lower() in {"exit", "quit"}:
                print("Gracefully shutting down")
                break

            response = agent.invoke(
                {
                    "messages": [
                        {
                            "role": "user",
                            "content": user_input,
                        }
                    ]
                }
            )

            answer = response["messages"][-1].content

            print(f"Emma: {answer}")
            print()

        except KeyboardInterrupt:
            print("\nGracefully shutting down")
            break

        except Exception as e:
            print(f"Error: {e}")


if __name__ == "__main__":
    main()