AI Tools Marketplace Client SDK
npm install toolsyai-client-sdkA TypeScript SDK for easily consuming AI tools from the AI Tools Marketplace platform.
``bash`
npm install aiapi-client-sdk
`typescript
import { initialize } from "aiapi-client-sdk";
// Initialize with your API key
const client = initialize("your-api-key");
// Call a tool
async function translateText() {
const result = await client.callTool("translateText", {
text: "Hello, world!",
targetLanguage: "fr",
});
console.log(result.translatedText); // [fr] Hello, world!
}
translateText();
`
The SDK can be used with LangChain to create agents that use marketplace tools:
`typescript
import { ChatOpenAI } from "@langchain/openai";
import { initialize, convertToLangChainTools } from "aiapi-client-sdk";
import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
import { ChatPromptTemplate } from "@langchain/core/prompts";
async function main() {
// Initialize the client
const client = initialize("your-api-key");
// Convert tools to LangChain format
const tools = await convertToLangChainTools(client);
// Create an agent with the tools
const model = new ChatOpenAI({ modelName: "gpt-4" });
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant. Use the available tools when appropriate.",
],
["user", "{input}"],
]);
const agent = await createOpenAIFunctionsAgent({
llm: model,
tools,
prompt,
});
const agentExecutor = new AgentExecutor({
agent,
tools,
maxIterations: 5,
});
// Run the agent
const result = await agentExecutor.invoke({
input: "What's the weather like in New York?",
});
console.log(result.output);
}
`
The SDK can be used directly with the OpenAI SDK:
`typescript
import { OpenAI } from "openai";
import { initialize, convertToOpenAIFunctions } from "aiapi-client-sdk";
async function main() {
// Initialize the client
const client = initialize("your-api-key");
// Get tools as OpenAI functions
const functions = await convertToOpenAIFunctions(client);
// Use with OpenAI
const openai = new OpenAI({ apiKey: "your-openai-key" });
const response = await openai.chat.completions.create({
model: "gpt-4-turbo",
messages: [{ role: "user", content: "What's the weather in New York?" }],
tools: functions,
tool_choice: "auto",
});
const message = response.choices[0].message;
// Handle tool calls
if (message.tool_calls && message.tool_calls.length > 0) {
const toolResults = await Promise.all(
message.tool_calls.map(async (toolCall) => {
const toolName = toolCall.function.name;
const toolArgs = JSON.parse(toolCall.function.arguments);
// Call the tool via the marketplace client
const result = await client.callTool(toolName, toolArgs);
return {
tool_call_id: toolCall.id,
role: "tool",
name: toolName,
content: JSON.stringify(result),
};
})
);
// Get final response with tool results
const finalResponse = await openai.chat.completions.create({
model: "gpt-4-turbo",
messages: [
{ role: "user", content: "What's the weather in New York?" },
message,
...toolResults,
],
});
console.log(finalResponse.choices[0].message.content);
}
}
`
The SDK can be used with Google's Gemini API:
`typescript
import { GoogleGenerativeAI } from "@google/generative-ai";
import { initialize, convertToGeminiFunctions } from "aiapi-client-sdk";
async function main() {
// Initialize the client
const client = initialize("your-api-key");
// Get tools as Gemini functions
const functionDeclarations = await convertToGeminiFunctions(client);
// Initialize Gemini client
const genAI = new GoogleGenerativeAI("your-gemini-api-key");
// Create a model with the tools
const model = genAI.getGenerativeModel({
model: "gemini-1.5-pro",
tools: { functionDeclarations },
});
// Start a chat session
const chat = model.startChat();
// Send a message that might trigger tool use
const result = await chat.sendMessage("What's the weather in New York?");
// Handle tool calls if any
if (
result.response.functionCalls &&
result.response.functionCalls.length > 0
) {
for (const functionCall of result.response.functionCalls) {
const { name, args } = functionCall;
// Call the tool via the marketplace client
const toolResult = await client.callTool(name, args);
// Send the result back to Gemini
const followupResult = await chat.sendMessage({
functionResponse: {
name: name,
response: { result: toolResult },
},
});
console.log(followupResult.response.text());
}
}
}
`
The SDK can be used with Anthropic's Claude AI:
`typescript
import Anthropic from "@anthropic-ai/sdk";
import { initialize, convertToAnthropicTools } from "aiapi-client-sdk";
async function main() {
// Initialize the client
const client = initialize("your-api-key");
// Get tools as Anthropic tools
const tools = await convertToAnthropicTools(client);
// Initialize Anthropic client
const anthropic = new Anthropic({
apiKey: "your-anthropic-api-key",
});
// Create a message with tool use
const response = await anthropic.messages.create({
model: "claude-3-opus-20240229",
max_tokens: 1024,
system: "You are a helpful assistant. Use tools when appropriate.",
messages: [{ role: "user", content: "What's the weather in New York?" }],
tools: tools,
});
// Handle tool calls if any
const toolUses = response.content
.filter((content) => content.type === "tool_use")
.map((content) => (content.type === "tool_use" ? content : null))
.filter(Boolean);
if (toolUses.length > 0) {
// Process each tool call
const toolResults = await Promise.all(
toolUses.map(async (toolUse) => {
const { name, input } = toolUse;
// Call the tool via the marketplace client
const result = await client.callTool(name, input);
return {
role: "user",
content: [
{
type: "tool_result",
tool_use_id: toolUse.id,
content: JSON.stringify(result),
},
],
};
})
);
// Get final response with tool results
const finalResponse = await anthropic.messages.create({
model: "claude-3-opus-20240229",
max_tokens: 1024,
system: "You are a helpful assistant. Use tools when appropriate.",
messages: [
{ role: "user", content: "What's the weather in New York?" },
{ role: "assistant", content: response.content },
...toolResults,
],
});
console.log(
finalResponse.content
.map((c) => (c.type === "text" ? c.text : "[non-text content]"))
.join("\n")
);
}
}
`
This SDK provides seamless integration with the Vercel AI SDK:
`typescript
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";
import { initialize, convertToAISDKTools } from "aiapi-client-sdk";
async function main() {
// Initialize the client
const client = initialize("your-api-key");
// Convert marketplace tools to AI SDK format
const { tools, handleToolCall } = await convertToAISDKTools(client);
// Use with the AI SDK
const result = await streamText({
model: openai("gpt-4o"),
messages: [{ role: "user", content: 'Translate "Hello" to French' }],
tools,
onToolCall: async ({ name, input }) => {
return handleToolCall(name, input);
},
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
}
`
Initialize the client with your API key.
`typescript`
const client = initialize("your-api-key", {
apiUrl: "https://your-custom-api-url.com", // Optional custom API URL
cacheSchemas: true, // Whether to cache schemas (default: true)
});
#### client.initialize()
Initializes the client by fetching available tools. Called automatically when needed.
#### client.callTool(name, input)
Call an AI tool by name with the given input parameters.
`typescript`
const result = await client.callTool("toolName", {
param1: "value1",
param2: "value2",
});
#### client.getToolNames()
Get an array of available tool names.
`typescript`
const toolNames = await client.getToolNames();
console.log(toolNames); // ['translateText', 'summarizeText', ...]
#### client.getTools()
Get detailed information about all available tools.
`typescript`
const tools = await client.getTools();
#### client.getTool(name)
Get detailed information about a specific tool.
`typescript`
const tool = await client.getTool("translateText");
console.log(tool.description); // "Translate text to another language"
#### convertToLangChainTools(client)
Convert marketplace tools to LangChain-compatible tools.
`typescript`
const tools = await convertToLangChainTools(client);
#### getLangChainTool(client, toolName)
Get a specific tool as a LangChain tool.
`typescript`
const tool = await getLangChainTool(client, "translateText");
#### convertToOpenAIFunctions(client)
Convert marketplace tools to OpenAI function format.
`typescript`
const functions = await convertToOpenAIFunctions(client);
#### handleOpenAIFunctionCall(client, response)
Handle an OpenAI function call response.
`typescript`
const result = await handleOpenAIFunctionCall(client, openaiResponse);
#### getOpenAIFunction(client, toolName)
Get a specific tool as an OpenAI function.
`typescript`
const function = await getOpenAIFunction(client, "translateText");
#### convertToGeminiFunctions(client)
Convert marketplace tools to Gemini function format.
`typescript`
const functionDeclarations = await convertToGeminiFunctions(client);
#### handleGeminiFunctionCall(client, functionCall)
Handle a Gemini function call.
`typescript`
const result = await handleGeminiFunctionCall(client, geminiFunctionCall);
#### getGeminiFunction(client, toolName)
Get a specific tool as a Gemini function.
`typescript`
const function = await getGeminiFunction(client, "translateText");
#### convertToAnthropicTools(client)
Convert marketplace tools to Anthropic Claude format.
`typescript`
const tools = await convertToAnthropicTools(client);
#### handleAnthropicToolCall(client, toolCall)
Handle an Anthropic tool call.
`typescript`
const result = await handleAnthropicToolCall(client, claudeToolCall);
#### getAnthropicTool(client, toolName)
Get a specific tool as an Anthropic tool.
`typescript`
const tool = await getAnthropicTool(client, "translateText");
#### convertToAISDKTools(client)
Convert the marketplace client to AI SDK compatible tools.
`typescript`
const { tools, handleToolCall } = await convertToAISDKTools(client);
See the examples/` directory for complete usage examples.