Running an Agent Locally using BOSA Connectors

This example will show you how to create agent with BOSA connector.

Prerequisites

This example specifically requires:

Running The Code

All the code used in the following examples can be found in our GitHub repository gen-ai-examples/custom-tool-and-agent.

git clone https://github.com/GDP-ADMIN/gen-ai-examples.git
cd gen-ai-examples/examples/custom-tool-and-agent

Set up your OPENAI_API_KEY, BOSA_API_BASE_URL, and BOSA_API_KEY

Get your OpenAI API key from https://platform.openai.com/api-keys.

You can contact BOSA team (bosa-eng@gdplabs.id) for BOSA_API_KEY

  • Environment Variable Option

    export OPENAI_API_KEY="sk-..."
    export BOSA_API_BASE_URL="https://api.bosa.id"
    export BOSA_API_KEY="sk-..."

  • Environment File (.env) Option

    echo 'OPENAI_API_KEY="sk-..."' > .env
    echo 'BOSA_API_BASE_URL="https://api.bosa.id"' >> .env
    echo 'BOSA_API_KEY="sk-..."' >> .env

Agent Example using Tool Generator BOSA Connectors

This example will show you how to create agent with BOSA connector tool without creating their own tool. This approach will help you easily integrate and deploy with minimal setup time.

Snippet how to set Tools from BOSA Connectors from Agent
import os
from gllm_agents import Agent
from langchain_openai import ChatOpenAI
# Import the custom tool from the other file
from bosa_connectors import BOSAConnectorToolGenerator


# Initialize components
# Note: ChatOpenAI() will automatically look for the OPENAI_API_KEY env var.
llm = ChatOpenAI(model="gpt-4o")
bosa_connector_tool_generator = BOSAConnectorToolGenerator(
    api_base_url=os.getenv("BOSA_API_BASE_URL", "https://staging-api.bosa.id"),
    api_key=os.getenv("BOSA_API_KEY", ""),
    app_name="twitter",
)
tools = bosa_connector_tool_generator.generate_tools() #this is how to get tools from BOSA tools generator

# Create Agent
agent = Agent(
    name="BOSAConnectorAgent",
    # Revert to simpler instruction
    instruction="You are a helpful assistant that use BOSA to connect with third party API such as Twitter, Google, Google Docs, Github.",
    llm=llm,
    tools=tools,
    # Set verbose=True to see agent thoughts
    verbose=True,
)

# Run AgentTwitter
query = "Please search twitter using BOSA by username elonmusk."
response = agent.run(query)

# Print the final output from the response dictionary
print(response['output'])

# Expected output format is now modified by the tool's return value

You only need to set the tools in the Agent using bosa_connector_tool_generator.generate_tools(), after which you can utilize the tools provided by the BOSA Connectors SDK.

Execute the script

python hello_agent_bosa_connector_example.py

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

> Entering new AgentExecutor chain... Invoking: `bosa_twitter_tool` with `{'username': 'elonmusk'}`

I found the Twitter account for the username "elonmusk". The account details are:

*Name: gorklon rust Username: elonmusk User ID: 44196397

If you have any other questions or need further fell free to ask! > Finished chain. Agent 'BOSAConnectorAgent' completed its task. I've located the Twitter account for the user "elonmusk". Here are the details:

Name: gorklon rust

Username: elonmusk

User ID: 44196397

If you have any other questions or need further details, feel free to ask!

The key indicators of success:

  • The agent initialization completes without errors

  • The verbose output shows the tool being invoked

  • The final output shows the user detail of twitter elonmusk

Agent Using BOSA Connectors SDK

This is an example of how to run an agent that leverages BOSA Connectors locally using own tools that using BOSA Connector SDK. This will help you to tailor tools to meet specific requirements or advanced functionalities from extending BOSA Connectors SDK functionality.

Tool Example Code

This is the example tool that we use:ool Generator

Tool Code Example
# bosa_twitter_tool.py
import os
from bosa_connectors.connector import BosaConnector
from gllm_agents import BaseTool
from pydantic import BaseModel, Field
from typing import Any, Type
from dotenv import load_dotenv

load_dotenv()

# Optional: Define input schema if the tool needs arguments
class TwitterUserInput(BaseModel):
    username: str = Field(..., description="The username of target user to search for.")

class BosaTwitterGetUserTool(BaseTool):
    """A tool to search twitter user using BOSA by input username."""
    name: str = "bosa_twitter_tool"
    description: str = "Search twitter user using BOSA by input username."
    args_schema: Type[BaseModel] = TwitterUserInput

    def _run(self, username: str,  **kwargs: Any) -> str:
        """Uses the tool."""
        api_base_url = os.getenv("BOSA_API_BASE_URL", "https://staging-api.bosa.id")
        api_key = os.getenv("BOSA_API_KEY", "")
        bosa_connector = BosaConnector(api_base_url=api_base_url, api_key=api_key)

        params = {
            "username": username,
        }
        
        try:
            result = bosa_connector.execute("twitter", "get-users", max_attempts=1, input_=params)

            ### you can modify the return in BOSAConnector to processing the data or do something else.
            ### (Ex: return the result in a more readable format so AI can understand it better)
        except Exception as e:
            result = f"An error occurred: {str(e)}. Please try again."
        return result

as you can see from this tool code example, we implement our own tool and implement BOSA connector SDK to connect. The advantage using this you can tailor tools to meet specific requirements or advanced functionalities & develop unique processes that match your application’s needs.

Execute the script

python hello_agent_bosa_twitter_example.py

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

> Entering new AgentExecutor chain... Invoking: `bosa_twitter_tool` with `{'username': 'elonmusk'}`

I found the Twitter account for the username "elonmusk". The account details are:

*Name: gorklon rust Username: elonmusk User ID: 44196397

If you have any other questions or need further fell free to ask! > Finished chain. Agent 'BOSAConnectorTwitterAgent' completed its task. I've located the Twitter account for the user "elonmusk". Here are the details:

Name: gorklon rust

Username: elonmusk

User ID: 44196397

If you have any other questions or need further details, feel free to ask!

The key indicators of success:

  • The agent initialization completes without errors

  • The verbose output shows the tool being invoked

  • The final output shows the user detail of twitter elonmusk

Last updated