DEV Community

Cover image for Quick tip: Using SingleStore with OpenAI's Swarm
Akmal Chaudhri for SingleStore

Posted on • Updated on

Quick tip: Using SingleStore with OpenAI's Swarm

Abstract

In this short article, we'll build a system designed to analyse and derive investment insights from blockchain trends using a sequential flow of specialised agents. The first agent, a blockchain expert, retrieves the latest developments and trends in blockchain technology specific to Northern Ireland. This response is stored in a shared context, accessible to the second agent - an investment expert - which further refines and identifies potential investment opportunities within the blockchain context. This layered approach uses domain-specific expertise, allowing each agent to contribute unique insights, resulting in a more comprehensive analysis of blockchain investment opportunities.

The notebook file used in this article is available on GitHub.

Introduction

OpenAI recently released Swarm. Swarm is a stateless framework that enables the creation and management of multiple AI agents, allowing them to collaborate and solve complex tasks by breaking them down into smaller, more manageable subtasks.

In this short article, we'll build a super simple example using two agents and a sequential flow.

Create a SingleStore Cloud account

A previous article showed the steps to create a free SingleStore Cloud account. We'll use the Free Shared Tier and take the default names for the Workspace and Database. We'll store our OpenAI API Key in the secrets vault using the name OPENAI_API_KEY.

Import the notebook

We'll download the notebook from GitHub.

From the left navigation pane in the SingleStore cloud portal, we'll select DEVELOP > Data Studio.

In the top right of the web page, we'll select New Notebook > Import From File. We'll use the wizard to locate and import the notebook we downloaded from GitHub.

Run the notebook

After checking that we are connected to our SingleStore workspace, we'll run the cells one by one.

We'll begin by installing the necessary libraries and importing dependencies.

Next, we'll ensure that we are connected to our database:

from sqlalchemy import *

conn = create_engine(connection_url)
Enter fullscreen mode Exit fullscreen mode

We'll download a file from the internet that contains details of investment opportunities in Northern Ireland:

loader = OnlinePDFLoader("https://www.investni.com/sites/default/files/2021-02/NI-fintech-document.pdf")

data = loader.load()
Enter fullscreen mode Exit fullscreen mode

and we'll get some details:

print (f"You have {len(data)} document(s) in your data")
print (f"There are {len(data[0].page_content)} characters in your document")
Enter fullscreen mode Exit fullscreen mode

Example output:

You have 1 document(s) in your data
There are 39674 characters in your document
Enter fullscreen mode Exit fullscreen mode

Next, we'll split the document into chunks with a small chunk overlap:

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size = 2000,
    chunk_overlap = 20
)
texts = text_splitter.split_documents(data)

print (f"You have {len(texts)} pages")
Enter fullscreen mode Exit fullscreen mode

Example output:

You have 23 pages
Enter fullscreen mode Exit fullscreen mode

Now we'll get the OpenAI API Key from the secrets vault:

os.environ["OPENAI_API_KEY"] = get_secret("OPENAI_API_KEY")
Enter fullscreen mode Exit fullscreen mode

and drop the database table if it already exists:

DROP TABLE IF EXISTS fintech_docs;
Enter fullscreen mode Exit fullscreen mode

Next, we'll use LangChain to get the embeddings and store the document and embeddings in SingleStore:

embedding = OpenAIEmbeddings(model = "text-embedding-3-small")

docsearch = SingleStoreDB.from_documents(
    texts,
    embedding,
    table_name = "fintech_docs"
)
Enter fullscreen mode Exit fullscreen mode

We'll now define two functions. The first function will query the database using a similarity search and the second function will transfer control from one agent to another agent:

# Define the search function
def search_s2(query_text: str) -> str:
    # Perform a similarity search and return the first result's content
    docs = docsearch.similarity_search(query_text)
    return docs[0].page_content

# Define a function to transfer control to Agent B
def transfer_to_agent_b():
    return agent_b
Enter fullscreen mode Exit fullscreen mode

Now we'll define our two agents:

# Define shared context to hold information between agents
shared_context = {}

# Define Agent A to focus on blockchain context
agent_a = Agent(
    name = "Blockchain Expert",
    model = "gpt-4o-mini",
    instructions = "You are an expert in blockchain technology.",
    functions = [search_s2, transfer_to_agent_b]
)

# Define Agent B to focus on investment opportunities
agent_b = Agent(
    name = "Investment Expert",
    model = "gpt-4o-mini",
    instructions = "You are an expert in stock market investment."
)
Enter fullscreen mode Exit fullscreen mode

Agent A is a Blockchain Expert. Agent B is an Investment Expert.

We'll now run Agent A and save its results for use by Agent B.

client = Swarm()

# Run Agent A with a blockchain-specific query
response_a = client.run(
    agent = agent_a,
    messages = [{
        "role": "user",
        "content": "What are the latest developments and trends in blockchain technology in Northern Ireland?"
    }],
)

# Store Agent A's response in shared context for Agent B to expand on
shared_context["blockchain_context"] = response_a.messages[-1]["content"]

# Output the response from Agent A
print(shared_context["blockchain_context"])
Enter fullscreen mode Exit fullscreen mode

Example output:

Northern Ireland is emerging as a significant hub for blockchain technology, particularly within its growing FinTech sector. Here are the latest developments and trends:

1. **FinTech as a Key Sector**: Northern Ireland has positioned itself as a center of excellence for financial technology (FinTech), leveraging its world-class talent, competitive operating costs, and a supportive business environment. This focus on FinTech has encouraged the integration of blockchain technology into various financial services.

2. **Corporate Investments**: Major international firms such as Citi, Allstate, Liberty Mutual, and FD Technologies are expanding their operations in Northern Ireland. Newer investors like CME Group, Vela, and TP ICAP have also established substantial delivery centers, indicating a growing confidence in the region's tech ecosystem.

3. **Start-Up Ecosystem**: The start-up and SME tech community in Northern Ireland is thriving. An entrepreneurial ecosystem bolstered by networking opportunities and collaboration among sectors is fostering innovation, including in blockchain applications.

4. **Emerging Technologies Integration**: Alongside blockchain, there is a notable emphasis on cybersecurity, machine learning, and AI. The convergence of these technologies is driving significant transformation within the financial sector, enabling more efficient and secure transactions.

5. **Resilience and Research Excellence**: The resilience of Northern Ireland’s infrastructure, paired with strong research capabilities, supports ongoing developments in blockchain technology, making it attractive for further investment and innovation.

These factors underscore the region's potential as a leading player in the global blockchain landscape, particularly within the financial services industry.
Enter fullscreen mode Exit fullscreen mode

Now we'll call Agent B.

# Run Agent B with a focus on investment opportunities within the blockchain context
response_b = client.run(
    agent = agent_b,
    messages = [{
        "role": "user",
        "content": f"{shared_context['blockchain_context']} Based on these developments, what are the best investment opportunities in blockchain in Northern Ireland?"
    }],
)

# Output the final response from Agent B
print(response_b.messages[-1]["content"])
Enter fullscreen mode Exit fullscreen mode

Example output:

Investing in blockchain technology in Northern Ireland, especially given its emerging status as a FinTech hub, presents several promising opportunities. Here are some potential investment avenues based on the latest developments and trends:

1. **Blockchain Startups**: With a thriving start-up ecosystem, consider investing in early-stage blockchain companies that are innovating within the FinTech space. Look for firms focusing on payment solutions, smart contracts, identity verification, and decentralization, as these can provide high growth potential.

2. **Corporate Ventures in Blockchain**: Given that major firms (Citi, Allstate, Liberty Mutual, etc.) are expanding their operations, exploring venture capital or equity investments in these corporations' blockchain initiatives or subsidiaries might yield returns as they evolve their platforms and products.

3. **Blockchain Infrastructure Companies**: Investment in companies developing the infrastructure necessary for blockchain implementation, such as storage, security, and interoperability solutions, can be beneficial. These firms often have recurring revenue models, making them potentially stable investments.

4. **Education and Training**: With Northern Ireland’s emphasis on research excellence and innovation, investing in educational platforms and training programs focused on blockchain and related technologies could be rewarding. As the technology matures, demand for skilled labor in this area will increase.

5. **Partnerships with Universities and Research Institutions**: Engaging with local universities and research institutions that are leading blockchain research initiatives can provide access to cutting-edge developments and investment opportunities in academic spin-offs or collaborations.

6. **FinTech Accelerators and Incubators**: Participate in or invest in local FinTech accelerators and incubators that focus on blockchain technology. These programs often provide access to a portfolio of startups and a network of mentors, increasing the chances of identifying potential unicorns.

7. **Cybersecurity Firms**: As blockchain technology is closely tied to enhanced security, firms that specialize in cybersecurity and are integrating blockchain for data protection measures can be attractive investment opportunities.

8. **Real Estate Tokenization**: Explore opportunities in real estate and asset tokenization projects. Northern Ireland may offer avenues for innovative ways to invest in property through blockchain-based fractional ownership models.

9. **Consulting and Advisory Firms**: Invest in consulting firms that provide advisory services on blockchain implementation for businesses. As adoption grows, these firms can gain more clients, enhancing their scale and profitability.

10. **Tokenized Finance Solutions**: Look for opportunities in companies that are working on solutions like decentralized finance (DeFi) platforms or tokenized investments, enabling users to access financial services without intermediaries.

As always, before making any investment, conduct thorough due diligence and consider seeking advice from financial advisors experienced in the blockchain and FinTech sectors. The rapid evolution of technology means that staying informed about the latest trends and regulatory developments is crucial to making informed investment decisions.
Enter fullscreen mode Exit fullscreen mode

Next, we'll refactor the code and simplify the process.

First, let's define a new function to return control to Agent A:

# Define the search function
def search_s2(query_text) -> str:
    # Perform a similarity search and return the first result's content
    docs = docsearch.similarity_search(query_text)
    return docs[0].page_content

# Define a function to transfer control to Agent A
def transfer_to_agent_a():
    return agent_a

# Define a function to transfer control to Agent B
def transfer_to_agent_b():
    return agent_b
Enter fullscreen mode Exit fullscreen mode

We'll also update the code for Agent B to return control to Agent A, as follows:

# Define Agent A to focus on blockchain context
agent_a = Agent(
    name = "Blockchain Expert",
    model = "gpt-4o-mini",
    instructions = "You are an expert in blockchain technology.",
    functions = [search_s2, transfer_to_agent_b]
)

# Define Agent B to focus on investment opportunities
agent_b = Agent(
    name = "Investment Expert",
    model = "gpt-4o-mini",
    instructions = "You are an expert in stock market investment.",
    functions = [search_s2, transfer_to_agent_a]
)
Enter fullscreen mode Exit fullscreen mode

Finally, we'll call Agent A:

client = Swarm()

# Run Agent
response = client.run(
    agent = agent_a,
    messages = [{
        "role": "user",
        "content": (
            "What are the latest developments and trends in blockchain technology "
            "in Northern Ireland? Based on these developments, what are the best "
            "investment opportunities in blockchain in Northern Ireland?"
        )
    }],
)

print(response.messages[-1]["content"])
Enter fullscreen mode Exit fullscreen mode

Example output:

### Latest Developments and Trends in Blockchain Technology in Northern Ireland

1. **FinTech Growth**: Northern Ireland is emerging as a center of excellence for FinTech, with significant investments from global firms like Citi, Allstate, and Liberty Mutual. This growth supports the development of blockchain technology within the financial services industry.

2. **Startup Ecosystem**: The startup and SME tech community in Northern Ireland is thriving, particularly in blockchain, due to a robust entrepreneurial ecosystem and a vibrant networking scene that promotes collaborative innovation.

3. **Technology Adoption**: Industries are increasingly looking to harness next-generation technologies, including blockchain, machine learning, and AI, to drive transformation. The growth in data and computing power, along with insights drawn from advanced analytics, is propelling this trend.

4. **Cybersecurity Focus**: With the rise of blockchain technology, there is a concurrent emphasis on cybersecurity, ensuring that new blockchain implementations are secure and trustworthy.

5. **Research Excellence**: Universities and research institutions in Northern Ireland are focusing on blockchain and related technologies, contributing to innovations and offering a workforce trained in cutting-edge tech.

### Best Investment Opportunities in Blockchain in Northern Ireland

1. **Blockchain Startups**: Investing in local startups focusing on blockchain applications within financial services, supply chain, healthcare, and other sectors could yield significant returns, given the supportive environment for innovation.

2. **Partnerships with Established Firms**: Collaborating with or investing in established financial firms and technology companies that are integrating blockchain into their operations can provide a solid foothold in the market.

3. **Educational Initiatives**: Supporting educational programs or workshops that train individuals in blockchain technology can be a strategic investment, as it contributes to a skilled workforce ready to support the industry.

4. **Infrastructure Development**: Investing in the technological infrastructure that supports blockchain applications, such as cloud services, cybersecurity, and digital identity solutions, is key as businesses seek secure and efficient systems.

5. **Consulting and Advisory Services**: There is a growing demand for consulting services that guide businesses in blockchain integration, regulatory compliance, and technological implementation, representing a profitable investment avenue.

### Summary
Northern Ireland is well-positioned for growth in blockchain technology, driven by its financial services sector, a dynamic startup ecosystem, and a strong emphasis on cybersecurity and technological advancements. The best investment opportunities will likely arise from innovative startups, partnerships with existing firms, educational initiatives, and infrastructure support focused on blockchain applications.
Enter fullscreen mode Exit fullscreen mode

Summary

In this short article, we've built a two-agent system that applied specialised expertise to analyse blockchain trends and derive investment insights. By dividing tasks between two agents - one focusing on blockchain developments and the other on investment potential - the system demonstrated an efficient, layered approach to complex data analysis. The blockchain expert identified and retrieved relevant trends specific to Northern Ireland, which were then passed to an investment expert. With access to this blockchain-specific context, the investment expert was able to identify investment opportunities within the blockchain landscape. This setup showed the benefits of multi-agent collaboration, where each agent's domain knowledge contributed to a deeper, more focused analysis, ultimately enhancing decision-making in investment strategies.

Top comments (0)