---
description: >-
  This is an example of how to run a custom tool and agent locally, independent
  of GLChat.
---

# Running a Custom Tool and Agent Locally

{% hint style="warning" %}
This documentation has been deprecated and moved to the [GL SDK Gitbook](https://gdplabs.gitbook.io/sdk/glchat/tutorials/genai-sdk-and-glchat-hello-world)
{% endhint %}

<details>

<summary>Prerequisites</summary>

This example specifically requires:

* [Python v3.11 or v3.12](https://glair.gitbook.io/hello-world/prerequisites#python-v3.11-or-v3.12)
* [Pip](https://pip.pypa.io/en/stable/installation/)
* [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)

</details>

## Installation

```bash
# you can use a Conda environment
pip install gllm-agents-binary
```

## Running The Code

{% hint style="info" %}
All the code used in the following examples can be found in our GitHub repository [gen-ai-examples/custom-tool-and-agent](https://github.com/GDP-ADMIN/gen-ai-examples/tree/main/examples/custom-tool-and-agent).

This example uses [`hello_tool.py`](https://github.com/GDP-ADMIN/gen-ai-examples/blob/main/examples/custom-tool-and-agent/hello_tool.py) for the tool definition and [`hello_agent_example.py`](https://github.com/GDP-ADMIN/gen-ai-examples/blob/main/examples/custom-tool-and-agent/hello_agent_example.py) to run the agent.
{% endhint %}

{% tabs %}
{% tab title="Clone from GitHub" %}
```bash
git clone https://github.com/GDP-ADMIN/gen-ai-examples.git
cd gen-ai-examples/examples/custom-tool-and-agent
```
{% endtab %}

{% tab title="Copy & Paste the Code" %}
*   [ ] [`hello_tool.py`](https://github.com/GDP-ADMIN/gen-ai-examples/blob/main/examples/custom-tool-and-agent/hello_tool.py)\


    ```python
    # Tool Definition
    from gllm_agents import BaseTool
    from pydantic import BaseModel, Field
    from typing import Any, Type

    class HelloInput(BaseModel):
        name: str = Field(..., description="The name to say hello to")

    class SimpleHelloTool(BaseTool):
        """A simple tool that says hello."""
        name: str = "simple_hello_tool"
        description: str = "Greets the user by name."
        args_schema: Type[BaseModel] = HelloInput

        def _run(self, name: str, **kwargs: Any) -> str:
            return f"Hello, {name}!"
    ```
*   [ ] [`hello_agent_example.py`](https://github.com/GDP-ADMIN/gen-ai-examples/blob/main/examples/custom-tool-and-agent/hello_agent_example.py)\


    ```python
    # Agent Runner Script
    from gllm_agents import Agent
    from langchain_openai import ChatOpenAI
    from hello_tool import SimpleHelloTool

    # Initialize components
    llm = ChatOpenAI(model="gpt-4o")
    tool = SimpleHelloTool()

    # Create an agent
    agent = Agent(
        name="HelloAgent",
        instruction="You are a helpful assistant that can greet people by name using the provided tool.",
        llm=llm,
        tools=[tool],
        verbose=True
    )

    # Run the agent
    query = "Please greet World"
    response = agent.run(query)
    print(response['output'])
    ```
{% endtab %}
{% endtabs %}

#### Set up your `OPENAI_API_KEY`&#x20;

{% hint style="info" %}
## Get your OpenAI API key from [https://platform.openai.com/api-keys](https://platform.openai.com/api-keys).
{% endhint %}

{% tabs %}
{% tab title="Linux, macOS, or Windows WSL" %}
```bash
export OPENAI_API_KEY="sk-..."
```
{% endtab %}

{% tab title="Windows Powershell" %}
```powershell
$env:OPENAI_API_KEY = "sk-..."
```
{% endtab %}

{% tab title="Windows Command Prompt" %}
```bash
set OPENAI_API_KEY="sk-..."
```
{% endtab %}
{% endtabs %}

#### Execute the script

```bash
python hello_agent_example.py
```

With `verbose=True`, you will see the agent's thinking process, which may look like this:

> **> Entering new AgentExecutor chain...**\
> &#xNAN;_<mark style="color:green;">Invoking: \`simple\_hello\_tool\` with \`{'name': 'World'}\`</mark>_\
> &#xNAN;_<mark style="color:green;">Hello, World!</mark>_\
> \
> &#xNAN;**> Finished chain.**\
> Hello, World!

The key indicators of success:

* The agent initialization completes without errors
* The verbose output shows the tool being invoked
* The final output includes the greeting "Hello, World!"

<details>

<summary>Troubleshooting</summary>

For common issues and their solutions, please refer to the centralized [FAQ document](https://glair.gitbook.io/hello-world/faq).

</details>

