akili-sdk
v0.0.4
Published
[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Fturnkeyafrica%2Fakili-ts-sdk) [![npm shield
Downloads
78
Readme
Akili TypeScript Library
The Akili TypeScript library provides convenient access to the Akili API from TypeScript.
Installation
npm i -s akili-sdkUsage
Instantiate and use the client with the following:
import { AkiliApiClient } from "akili-sdk";
import * as fs from "fs";
const client = new AkiliApiClient({ token: "YOUR_TOKEN" });
await client.ingestFilesIngestPost([fs.createReadStream("/path/to/your/file")], {
config: JSON.stringify({ configurable: { assistant_id: "5c825599-b6d2-495d-83e6-b492b6a6dd2c" } }),
});Examples
List Assistants
const assistants = await client.assistants.listAssistant();Create Assistant
const assistant = await client.assistants.createAssistant(
{
name: "test sdk assistant",
description: "test assistant",
config: {
configurable: {
type: "agent",
"type==agent/model_provider": "openai",
"type==agent/model": "gpt-4o-mini",
"type==agent/tools": [
{
id: "app.turnkey.actions.lms_quotations.lms_quotations_get_web_quote",
name: "lms_quotations_get_web_quote",
type: "lms_quotations_get_web_quote",
},
{
type: "retrieval",
name: "retrieval",
id: "app.turnkey.actions.core.retrieval",
},
{
type: "lms_parties_search_web_clients",
name: "lms_parties_search_web_clients",
id: "app.turnkey.actions.lms_parties.lms_parties_search_web_clients",
},
],
"type==agent/system_message":
"a **helpful** pleasant AI assistant for insurance underwriters",
},
},
}
);Delete Assistant
await client.assistants.deleteAssistant(
"1599d411-9af5-40cd-baa4-d775bd8206e3",
)Create New Chat/Thread
const thread = await client.threads.createThread(
{
name: "Test sdk chat1",
assistantId: "5c825599-b6d2-495d-83e6-b492b6a6dd2c",
}
);List Current User Chats/Threads
const threads = await client.threads.listThreads()Stream Chat
const stream = await client.runs.streamRun({
threadId: "73021468-1a54-43bf-84cc-08d4594eb320",
input: {
messages: [
{
type: "human",
content: "ping!",
},
],
},
});
for await (const message of stream) {
console.log("====================================");
console.log(message);
}Get Thread State/Chat history
const state = await client.threads.getThreadState("73021468-1a54-43bf-84cc-08d4594eb320")Get Thread History
const state = await client.threads.getThreadHistory("73021468-1a54-43bf-84cc-08d4594eb320")List Runbook Templates
const runbooks = await client.configs.listRunbookTemplates()List Tools
const tools = await client.configs.listTools()List Model Providers
const modelProviders = await client.configs.listProviders()Request And Response Types
The SDK exports all request and response types as TypeScript interfaces. Simply import them with the following namespace:
import { AkiliApi } from "akili-sdk";
const request: AkiliApi.SetLatestVersionAssistantsAidLatestPostRequest = {
...
};Exception Handling
When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error will be thrown.
import { AkiliApiError } from "akili-sdk";
try {
await client.ingestFilesIngestPost(...);
} catch (err) {
if (err instanceof AkiliApiError) {
console.log(err.statusCode);
console.log(err.message);
console.log(err.body);
}
}Advanced
Retries
The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retriable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).
A request is deemed retriable when any of the following HTTP status codes is returned:
Use the maxRetries request option to configure this behavior.
const response = await client.ingestFilesIngestPost(..., {
maxRetries: 0 // override maxRetries at the request level
});Timeouts
The SDK defaults to a 60 second timeout. Use the timeoutInSeconds option to configure this behavior.
const response = await client.ingestFilesIngestPost(..., {
timeoutInSeconds: 30 // override timeout to 30s
});Aborting Requests
The SDK allows users to abort requests at any point by passing in an abort signal.
const controller = new AbortController();
const response = await client.ingestFilesIngestPost(..., {
abortSignal: controller.signal
});
controller.abort(); // aborts the requestRuntime Compatibility
The SDK defaults to node-fetch but will use the global fetch client if present. The SDK works in the following
runtimes:
- Node.js 18+
- Vercel
- Cloudflare Workers
- Deno v1.25+
- Bun 1.0+
- React Native
Customizing Fetch Client
The SDK provides a way for your to customize the underlying HTTP client / Fetch function. If you're running in an unsupported environment, this provides a way for you to break glass and ensure the SDK works.
import { AkiliApiClient } from "akili-sdk";
const client = new AkiliApiClient({
...
fetcher: // provide your implementation here
});Contributing
While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!
On the other hand, contributions to the README are always very welcome!
