Skip to main content

Long-running AI conversations

Manage AI assistance instances, conversation threads, and context files to create a unique AI experience for users.

With the Squid AI assistant, you can create assistants with a specific knowledge base from files you provide. Conversations with the assistant take place in threads, allowing the assistant to incorporate information from the ongoing conversation. This creates a bespoke experience that allows you to connect with users on a level that is both personal and global.

Benefits

  • Provide personalized support to users on a global scale with a chatbot that can take in user files as context and respond accordingly.
  • Generate new assistants on the fly with unique instructions and contexts to suit varying business needs.
  • Access thread history so the right people have the right information when they need it. This is helpful for support staff, medical practitioners, educators, and anyone else who needs to get up to speed on a chatbot conversation.

Use cases

You can also handle specific use cases or create more consistent assistant responses using AI functions. Here are some common scenarios that could make use of this functionality:

  • Add specific messaging for user questions. For example, when a change is made to the company or product that users will likely ask about, you can provide a consistent, suitable response.
  • Run industry-specific formulas that regular chatbots won't know or consistently apply.
  • Run data queries or API calls that provide information the AI assistant uses in its response.

To interact with the assistant, use the Squid Client SDK.

Note

Squid AI assistant requires admin access, so its methods must be run in the Squid backend.

Create an assistant

To create a new AI assistant, use the createAssistant method. The method takes the following parameters:

  • name - The name of the assistant.
  • instructions - Instructions for the assistant. For example, "You are a pirate; only answer questions like a pirate would."
  • functions - An array of function names annotated with '@aiFunction' in your Squid backend that are available to the assistant. For more information, see the documentation on AI functions.
  • toolTypes - An optional array of tool types. The two available tool types are 'code_interpreter' and 'retrieval'. Code interpreter uses a working Python interpreter in a sandboxed, firewalled execution environment. Retrieval provides a flexible solution for semantic search and retrieval of personal or organizational documents using natural language queries.

The method returns a promise that resolves to the created assistant's ID. Use the assistant ID to manage actions on the assistant, like adding or deleting threads, adding or deleting files, and deleting the assistant. You can easily store this assistant ID in your database solution using the Squid Client SDK and your database integration, making it a breeze to connect to a user's profile.

If you want to use files for retrieval, you must add them using the addFileToAssistant method.

const assistantId = await createAssistant(
'YOUR_ASSISTANT_NAME',
"YOUR ASSISTANT'S INSTRUCTIONS",
['AI_FUNCTION_NAME_1', 'AI_FUNCTION_NAME_2'],
['ASSISTANT_TOOL_TYPE_1', 'ASSISTANT_TOOL_TYPE_2']
);

This example also includes two AI functions: 'AI_FUNCTION_NAME_1', and 'AI_FUNCTION_NAME_2'. The following example shows the format of an AI function:

Backend code
class AiService extends SquidService {
@aiFunction<{ shipName: string }>(
'This function returns the list of pirates in a ship'
)
async listPiratesInAShip(params: { shipName: string }): Promise<string[]> {
console.log('Got ai function call to listPiratesInAShip', params);
return ['Jack Sparrow', 'Black Beard'];
}
}

To learn more about Squid AI functions, view the AI functions documentation.

Create a thread

Conversations with the AI assistant take place in threads. Each thread is a long-lived conversation within the assistant that you can send questions to. AI assistants require at least one thread.

To create a new thread, use the createThread method, passing the ID of the AI assistant for which the thread is created. This method creates a new thread for an AI assistant.

This method returns a promise that resolves to the created thread's ID.

const threadId = await createThread(assistantId);

Like the assistant ID, you can store this thread ID in your database solution to access the conversation in the future using the Squid Client SDK and your database integration.

Add a file to the assistant

To add a file to an AI assistant for retrieval or code analyzing, use the addFileToAssistant method, passing the assistant Id and the file or blob name and file. The method returns a promise that resolves to the ID of the added file. When adding a file from a frontend, use the File type, and when adding a file from a backend environment, use the BlobAndFilename type.

const fileId = await addFileToAssistant(assistantId, YOUR_FILE);

Remove a file from the assistant

To remove a file from the assistant, use the removeFileFromAssistant method, passing the assistantId and the fileId of the file you are removing:

await removeFileFromAssistant(assistantId, fileId);

This method returns a promise that resolves when the file is removed.

Add a file to a thread

One feature that makes Squid AI useful in your applications is the ability to provide context on the fly during a conversation. Users can provide documents or images to a specific thread, and the assistant can then respond to queries based on those files.

To add a file to a thread to be used when asking a question, use the addFileToThread method, passing the thread ID and the file or blob and filename. When adding a file from a frontend, use the File type, and when adding a file from a backend environment, use the BlobAndFilename type.

const fileId = await addFileToThread(threadId, FILE_TO_ADD);

Like adding a file to an assistant, this method returns a promise that resolves to the ID of the added file.

Note

Files added to a specific thread are only accessible to that thread. The context provided by these files is not available to other threads for the assistant.

Query the assistant

To query the assistant, use the queryAssistant method, passing the assistant ID, thread ID, prompt, and the file IDs for any relevant files. File IDs are associated with the given thread.

This method returns a promise that resolves to the assistant's response.

const response = await queryAssistant(
assistantId,
threadId,
"What is our company's policy on bringing pets to the office?",
fileId
);

By default, query responses are in string format. Responses in JSON format can be very useful for automating tasks because they simplify the process of passing values to other functions or taking actions on a database like queries or writes.

To specify the query response type, include the optional options parameter with your queryAssistant call:

const response = await queryAssistant(
assistantId,
threadId,
"What is our company's policy on bringing pets to the office? Respond in JSON format",
fileId,
{ responseFormat: 'json_object' } // options
);

The options object currently has one attribute, responseFormat, with a value of text or json_object.

Note

when specifying json_object as the response format, your prompt must include the word "JSON".

Delete an assistant

To delete an assistant, use the deleteAssistant method, passing the assistant ID.

This function returns a promise that resolves when the assistant is deleted.

await deleteAssistant(assistantId);

Delete a thread

To delete a thread, use the deleteThread method, passing the thread ID.

This function returns a promise that resolves when the assistant is deleted.

await deleteThread(threadId);

With this basic functionality, you have everything you need to build rich, complex AI interactions for your users. Squid handles the complexity so you can focus on your product.