voice agent with gpt-4.1-mini as llm - enable defa...
# ask-ai
a
voice agent with gpt-4.1-mini as llm - enable default web search
t
Thanks for reaching out, Nico. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to guide you through enabling default web search for a voice agent using
gpt-4.1-mini
as the LLM. Just follow these simple steps: 1. Define a Web Search Tool: Start by creating a function for web search using a tool like DuckDuckGo. Here's an example to get you started:
Copy code
@function_tool
async def search_web(ctx: RunContext[AppData], query: str):
    ddgs_client = ctx.userdata.ddgs_client
    search = await asyncio.to_thread(ddgs_client.text, query)
    if len(search) == 0:
        raise ToolError("No results found.")
    return search
1. Initialize the Agent: Next, add the search tool to your agent's configuration like this:
Copy code
agent = Agent(instructions="You are a helpful assistant.", tools=[search_web])
1. Set the LLM: Then, make sure to use
gpt-4.1-mini
in your agent session:
Copy code
session = AgentSession(
    llm=openai.LLM(model="gpt-4.1-mini"),
    userdata=app_data
)
1. Ensure Dependencies: Finally, don't forget to install the necessary packages like
duckduckgo-search
,
livekit-agents
, and the OpenAI plugin. Also, initialize the DuckDuckGo client within your app data. This setup will allow your agent to perform web searches whenever needed. If you have any questions or need a hand with any step, just let me know—I’m here to help! Sources: examples/voice_agents/basic_agent.py | Large language models (LLM) | LiveKit Docs | basics/say_in_voice.py
a
is it possible to enable native gpt-4.1-mini web search tool if I am using it as llm in my voice cascade? or i MUST create a custom function_tool myself to enable web search?
t
I wanted to let you know that the
gpt-4.1-mini
model doesn’t come with a built-in web search tool in LiveKit. But no worries! You can add web search functionality by creating a custom
function_tool
that connects with an external search service like DuckDuckGo or another API. If you'd like, I’m here to help walk you through the setup or share some examples—just let me know! Sources: OpenAI LLM models | LiveKit Docs | plugins/openai/src/models.ts | Build voice, video, and physical AI | LiveKit | Large language models (LLM) | LiveKit Docs | examples/voice_agents/realtime_joke_teller.py
a
what's the fastest api I can use for web search? also - what if I use gpt-4.1-mini via plugin instead? would that give me native web search abilities?
t
Hi there! When it comes to web search integration, a popular choice in LiveKit is the DuckDuckGo API, which is nicely implemented through the DDGS client. Another great option you might consider is the OpenRouter web search plugin—it’s quite flexible, letting you adjust settings like
max_results
and
search_prompt
to suit your needs. The speed can vary depending on things like network latency and how quickly the search provider responds, so trying out both could really help you find what works best for you. About
gpt-4.1-mini
, just a heads-up that using it via a plugin doesn’t come with built-in web search capabilities. You’d still need to set up web search as a custom tool or plugin alongside the language model. If you’d like, I’m here to help guide you through implementing either option—just let me know! Sources: OpenAI and LiveKit | LiveKit Docs | examples/voice_agents/requirements.txt | Large language models (LLM) | LiveKit Docs
a
how about grounding with Bing? is that a better service than duckduckgo?
t
Thanks for reaching out! I want to let you know that Bing isn’t natively supported for web search in LiveKit, unlike DuckDuckGo, which is fully integrated and featured in the official examples. But don’t worry—you can still use Bing by setting it up manually with a custom tool using the Bing Search API. Here’s a simple way to get started with Bing: 1. First, you’ll need to get an API key from Microsoft Azure Cognitive Services. 2. Next, create a custom function tool that calls the Bing API using an HTTP client like
aiohttp
. 3. Finally, parse the API response and send back the relevant results to your agent. If you’d like, I’d be happy to walk you through a detailed example of how to add Bing Search to your LiveKit agent. Just let me know—I’m here to help! Sources: examples/voice_agents/background_audio.py | tooling/api-documenter/src/documenters/OfficeYamlDocumenter.ts
a
yes please can you walk me through a detailed example of how to use Bing API (https://learn.microsoft.com/en-us/azure/ai-foundry/agents/how-to/tools/bing-grounding)
t
Here’s a detailed example of how to integrate the Bing Search API into your LiveKit agent: 1. Obtain an API Key: First, sign up for Microsoft Azure Cognitive Services and enable the Bing Web Search API. Be sure to note down your API key—it’s essential for the next steps. 2. Install Required Libraries: Make sure you have
aiohttp
installed to handle HTTP requests smoothly. You can easily install it by running:
Copy code
pip install aiohttp
1. Create a Custom Function Tool: Now, let’s define a tool that performs web searches using the Bing API. Here’s an example to guide you:
Copy code
from livekit.agents import function_tool, ToolError
import aiohttp

@function_tool
async def search_web_with_bing(ctx, query: str):
    """
    Perform a web search using Bing Search API.

    Args:
        query: The search query string.
    """
    bing_url = "https://api.bing.microsoft.com/v7.0/search"
    headers = {"Ocp-Apim-Subscription-Key": "your-api-key"}
    params = {"q": query, "count": 5}

    async with aiohttp.ClientSession() as session:
        async with session.get(bing_url, headers=headers, params=params) as resp:
            if resp.status != 200:
                raise ToolError("Bing search failed with status code: {}".format(resp.status))
            result = await resp.json()
            # Extract relevant data (e.g., webPages.value)
            return result.get("webPages", {}).get("value", [])
1. Integrate the Tool into Your Agent: Next, add this custom tool to your agent configuration so your agent knows how to use it:
Copy code
from livekit.agents import Agent

agent = Agent(
    instructions="You are a helpful assistant.",
    tools=[search_web_with_bing]
)
1. Start the Agent Session: Finally, use the
AgentSession
to initialize your agent with the LLM and the custom tool you just created:
Copy code
from livekit.plugins import openai
from livekit.agents import AgentSession

session = AgentSession(
    llm=openai.LLM(model="gpt-4.1-mini"),
    userdata={}
)
await session.start(agent=agent)
This setup will empower your agent to perform web searches using Bing seamlessly. Just remember to replace
"your-api-key"
with your actual Bing API key. If you have any questions or need a hand with any part of this, please don’t hesitate to reach out—I’m here to help!
a
oh wait should I define tools outside of the agent class and pass them when I create the agent instance? explain
t
I'm happy to help you understand how to define tools for your Agent! You have two great options: you can either define tools outside the Agent class and pass them in when you create an instance, or you can define them directly inside the Agent class. Both ways work well, and it really depends on what fits your needs best: • Define tools outside the Agent class: This approach is especially handy if you want to share tools between different agents or keep your tools organized in separate modules. Here's a simple example:
Copy code
# Define tools externally
tools = [weather_tool, calendar_tool]
agent = Agent(
    instructions="You are a helpful assistant",
    tools=tools
)
• Define tools inside the Agent class: This works nicely if the tools are unique to that specific agent or if you like keeping related features bundled together. Here's how that might look:
Copy code
class MyAgent(Agent):
    @function_tool
    def get_weather(self, location: str) -> str:
        return f"Weather in {location} is sunny"
For bigger projects, defining tools outside the Agent class usually gives you more flexibility and makes your code easier to reuse. If anything’s unclear or you want to dive deeper, just let me know—I’m here to help! Sources: Tool definition and use | LiveKit Docs | Agents v0.x migration guide - Node.js | LiveKit Docs