Skip to main content

Build custom AI agents

Using Squid's SDKs, you can customize your AI agents using an array of features. Add persistent instructions and context, manage chat sessions, customize AI logic, and more to create custom AI agents tailored to a specific use case.

How it Works

Under the hood, agents use a Large Language Model (LLM) to generate answers to users' questions. When a user asks a question, the persistent instructions and most relevant context are passed to the LLM as part of the prompt, providing the user with a contextualized answer.

Squid lets you choose the LLM for your AI agent, allowing you to find the best fit for your use case. The following LLM providers are currently available:

Do you need a specific model that is not currently supported? Please reach out to us to discuss your needs!

Building an Agent

An agent represents a distinct personality or setup for your AI workflow. Each agent is like a different persona or use case, distinguished by its own set of instructions and abilities. This design enables customized responses from the AI according to the specific agent.

Note

The following examples show how to create an agent using Squid's SDKs. If you are unfamiliar with developing using the Squid platform and SDKs, please read our documentation on fullstack development or check out a fullstack tutorial.

Upserting an Agent

To create or update an AI agent programmatically, use the upsert() method, specifying which agent ID should be created or updated:

Backend code
await squid
.ai()
.agent('banking-copilot')
.upsert({
options: {
model: 'gpt-4o',
},
isPublic: true,
});

When inserting an agent, pass an options object with a model field indicating the model the agent will use.

The isPublic parameter determines whether the chat functionality of the given agent can be accessed without setting up security rules.

Deleting an Agent

To delete an existing agent, use the delete() method:

Backend code
await squid.ai().agent('banking-copilot').delete();

This function results in an error if no agent exists for the provided agent ID.

Instructions

Instructions set the rules for how the agent responds to prompts and answers questions. They should be direct and simple, and explain the purpose of the agent. Instructions are supplied as a block of text.

Adding Instructions

To add or edit instructions for an AI agent, use the updateInstructions() method, passing the instruction data as a string.

Backend code
const instruction =
'You are a helpful copilot that assists customer support staff by providing answers to their questions about banking and finance products.';
await squid.ai().agent('banking-copilot').updateInstructions(instruction);

Context

Context tells the agent what knowledge to pull from when answering questions and is the same as the Knowledge Base ability in the Agent Studio. Adding context allows the agent to provide relevant answers on specific topics that may not be part of the underlying AI model.

The following are simple code examples, though the context you add can be much more complex. Some good examples of context include resources like code documentation, product manuals, business operations (e.g., store hours) and user-specific data. You can mix and match context types to create a robust knowledge base for your AI agent, ensuring that it can provide any information your users will need.

Upserting Context

To add or update agent context, use the upsertContext() method, passing the context and its type.

The upsertContext() method accepts a context ID. Providing a context ID allows you to more easily access context later for when you want to make changes.

const data = `Platinum Mastercard® Fair Credit, No annual fee. Flexible due dates...`;

await squid.ai().agent('banking-copilot').upsertContext({
type: 'text',
title: 'Credit Card Info',
text: data,
contextId: 'credit-cards',
});

Alternatively, use upsertContexts() to upsert an array of contexts.

const creditCard1 = `Platinum Mastercard® Fair Credit, No annual fee. Flexible due dates...`;
const creditCard2 = `Gold Mastercard®, $50 annual fee. Due dates once a month...`;

await squid
.ai()
.agent('banking-copilot')
.upsertContexts([
{
type: 'text',
title: 'Credit Card 1 Info',
text: creditCard1,
contextId: 'credit-cards1',
},
{
type: 'text',
title: 'Credit Card 2 Info',
text: creditCard2,
contextId: 'credit-cards2',
},
]);

Context Types

Two types of contexts are supported: text and file.

Text context is created with a string that contains the context:

const data = `Platinum Mastercard® Fair Credit, No annual fee. Flexible due dates...`;

await squid.ai().agent('banking-copilot').upsertContext({
type: 'text',
title: 'Credit Card Info',
text: data,
contextId: 'credit-cards',
});

File context is created by providing a File object as a second parameter to the upsertContext() method. The file is then uploaded to Squid and the context is created from the file contents.

const file = {
blob: contextBlob,
fileName: 'CreditCardList.pdf',
};

await squid.ai().agent('banking-copilot').upsertContext(
{
type: 'file',
contextId: 'credit-cards',
},
file
);
Note

Your context can be as long as you like; however since there are character limits to LLM prompts, only a portion of your context may actually be included alongside the user's inquiry. When constructing a prompt, Squid decides which portions of the supplied context are most relevant to the user's question.

Getting Context

To get a list of all contexts, use the listContexts() method. This method returns an array of agent context objects, which includes the contextId:

await squid.ai().agent('banking-copilot').listContexts();

To get a specific context item, use the getContext() method, passing the context ID:

await squid.ai().agent('banking-copilot').getContext('credit-cards');

Deleting Context

To delete a context entry, use the deleteContext() method:

await squid.ai().agent('banking-copilot').deleteContext('credit-cards');

This method results in an error if an entry has not yet been created for the context ID provided.

Context Metadata

When adding or updating the context of an AI agent, you can optionally provide context metadata. Metadata is an object where keys can have a type of string, number, or boolean. Adding metadata provides additional information about the context that can then be used when interacting with your agent. The following example shows adding a PDF as context and providing two key/value pairs as metadata:

const data = {
blob: contextBlob,
fileName: 'CreditCardList.pdf',
};

await squid
.ai()
.agent('banking-copilot')
.context()
.upsertContext(
{
contextId: 'credit-cards',
type: 'file',
metadata: { company: 'Bank of America', year: 2023 },
},
file
);

You can then use metadata when chatting with your AI agent, as shown in the filtering context with metadata section.

Interacting with your Agent

Once an agent has been created, you're ready to start asking questions or giving prompts. Use the ask() method on the desired AI agent.

const response = await squid
.ai()
.agent('banking-copilot')
.ask('Which credit card is best for students?');

Ask Options

In addition to a prompt, the ask function can accept an optional options parameter. This parameter provides various properties for configuring the AI agent. To view a full list of available options and their default values, refer to the API reference documentation.

await squid
.ai()
.agent('banking-copilot')
.ask('Which credit card is best for students?', {
chatId: 'my-chat',
maxTokens: 4096,
disableHistory: true,
/* ... */
});

Filtering Context with Metadata

When you have added metadata to your context, you can use the contextMetadataFilter chat option to instruct the AI agent to only consult specific contexts. Only contexts that meet the filter requirement will be used to respond to the client prompt.

The following example filters contexts to only include those with a metadata value of "company" that is equal to "Bank of America":

await squid
.ai()
.agent('banking-copilot')
.ask('Which Bank of America credit card is best for students?', {
contextMetadataFilter: { company: { $eq: 'Bank of America' } }, // filter context using metadata
/* ... */
});

The following metadata filters are supported:

FilterDescriptionSupported types
$eqMatches vectors with metadata values that are equal to a specified valuenumber, string, boolean
$neMatches vectors with metadata values that are not equal to a specified valuenumber, string, boolean
$gtMatches vectors with metadata values that are greater than a specified valuenumber
$gteMatches vectors with metadata values that are greater than or equal to a specified valuenumber
$ltMatches vectors with metadata values that are less than a specified valuenumber
$lteMatches vectors with metadata values that are less than or equal to a specified valuenumber
$inMatches vectors with metadata values that are in a specified arraystring, number
$ninMatches vectors with metadata values that are not in a specified arraystring, number
$existsMatches vectors with the specified metadata fieldboolean

AI Functions

Squid AI Agents can handle specific use cases and create more consistent responses using AI functions. To use AI functions, pass the AI function names as an array to the functions attribute of the options parameter:

const response = await squid
.ai()
.agent('banking-copilot')
.ask('What is my current credit limit?', {
functions: ['AI_FUNCTION_NAME_1', 'AI_FUNCTION_NAME_2'],
});

To learn more about AI functions, view the documentation. To see an example application that uses AI functions, check out this AI agent tutorial.

Securing your Agent

Securing your data is vital when using the Squid Client SDK to create agents and enable chatting. The AI agent and the chats conducted with them may contain sensitive information, so it is crucial to restrict access and updates to prevent unauthorized usage or modification.

To learn about securing your AI agent, check out the Securing AI agents documentation.