### Basic Tiny AI Agent Example Source: https://github.com/cubie-ai/tiny-ai/blob/main/README.md This example shows a complete basic setup for a Tiny AI agent, including creating a provider, defining a simple tool, configuring the agent with settings, and using it to generate text. ```typescript import { TinyAgent, TinyAnthropic, TinyTool } from "@cubie-ai/tiny-ai"; import { z } from "zod"; async function main() { // create the model provider const provider = new TinyAnthropic({ apiKey: "your-api-key", }); // create a simple tool const weather = new TinyTool("getWeather", { description: "Get the weather at a given location", parameters: z.object({ location: z.string() }), handler: ({ location }) => 100, }); // create the agent const agent = new TinyAgent({ provider, tools: [tool], settings: { // optional agent settings system: "You are a helpful and charming assistant", }, }); // use the agent to generateText const response = await agent.generateText({ prompt: "Hello, how are you?", maxSteps: 3, }); console.log(response.data?.text); } main(); ``` -------------------------------- ### Install Tiny AI via npm Source: https://github.com/cubie-ai/tiny-ai/blob/main/README.md This command adds the Tiny AI package to your project using the npm package manager. ```bash npm i @cubie-ai/tiny-ai ``` -------------------------------- ### Providing Tools to TinyAgent Source: https://github.com/cubie-ai/tiny-ai/blob/main/README.md This snippet illustrates three different methods for providing tools to a TinyAgent: during constructor initialization, using the `.putTool` method, and passing tools directly to the `.generateText` call. ```typescript import z from "zod"; import { TinyAgent } from "./agent"; import { TinyAnthropic } from "./providers"; import { TinyTool } from "./tools"; const populationTool = new TinyTool("getPopulation", { description: "Get the population of any location", parameters: z.object({ location: z.string(), }), handler: async (args) => { // call some external system return { population: 1000000 }; }, }); // Method 1: in the constructor const agent = new TinyAgent({ provider: new TinyAnthropic({ apiKey: "your-api-key", }), tools: [populationTool], }); // Method 2: Calling agent.putTool agent.putTool(populationTool); // Method 3: Passing the tools into a agent.generateText call const result = await agent.generateText({ prompt: "What is the population of the US?", tools: [populationTool], }); ``` -------------------------------- ### Setting up TinyAI Agent with Custom Tools (Typescript) Source: https://github.com/cubie-ai/tiny-ai/blob/main/README.md Initializes a TinyAI agent with an Anthropic provider and configures it with custom tools: 'loadContext' for fetching user information and 'tweet' for generating tweets based on the loaded context. Includes a main function to interact with the agent via command line input. ```typescript import { TinyAgent, TinyAnthropic, TinyTool } from "@cubie-ai/tiny-ai"; import { input } from "@inquirer/prompts"; import "dotenv/config"; import z from "zod"; // Prep the tiny agent const anthropic = new TinyAnthropic({ apiKey: process.env.ANTHROPIC_API_KEY, }); const agent = new TinyAgent({ provider: anthropic, settings: { maxSteps: 5 //required for the agent to chain tool calls and inference } }); // create a loading function that gets all information relevant for to the agent function mockLoadContext() { return { Name: "Cubie", Age: 5, Location: "Solana", Interests: ["AI", "Blockchain", "Web3"], FavoriteColor: "Blue", FavoriteFood: "Pizza", mentions: [ { from: "user1", text: "Hello, how are you?" }, { from: "user2", text: "What is your favorite color?" }, { from: "user3", text: "Do you like pizza?" }, ], recentTweets: [ { text: "Just had a great pizza!", date: "2023-10-01" }, { text: "Loving the new AI features!", date: "2023-10-02" }, { text: "Blockchain is the future!", date: "2023-10-03" }, ], }; } const loadContext = new TinyTool("loadContext", { description: "Load the context before writing a tweet. Must be called before calling 'tweet'. Return the context as a string.", handler: () => ({ context: formatContext(mockLoadContext()) }), }); // add the tool to the agent agent.putTool(loadContext); // create a tool that posts tweets. Here you would use the twitter-sdk const tweet = new TinyTool("tweet", { description: "Post a tweet to Twitter. ", parameters: z.object({ context: z.string() }), handler: async ({context}) => { const generateTweet = agent.generateText({ prompt: `${context} # Task Your task is to generate compelling tweets based on all the information above`, maxSteps: 5, temperature: 0.7, }); }, }); // Add the tool to the agent agent.putTool(tweet); async function main() { // Here if you ask the agent to write a tweet it will call the tweet tool const userMessage = await input({ message: "You: ", validate: (input) => { return input.trim() !== "" ? true : "Please enter a message."; }, }); const response = await agent.generateText({ prompt: userMessage, }); // Check if the response is valid if (!response || !response.success) { console.error("Error:", response.error); return; } console.log(`Agent: ${response.data?.text}`); } main(); ``` -------------------------------- ### Creating a TinyAgent with TinyAnthropic Source: https://github.com/cubie-ai/tiny-ai/blob/main/README.md This snippet demonstrates how to instantiate a TinyAgent by providing a TinyAnthropic provider instance, which requires an API key. ```typescript const provider = new TinyAnthropic({ apikey: "your-api-key" }); const agent = new TinyAgent({ provider }); ``` -------------------------------- ### Integrating MCP Clients with TinyAgent Source: https://github.com/cubie-ai/tiny-ai/blob/main/README.md This code demonstrates how to create a TinyMCP client instance using a StdioClientTransport and add it to a TinyAgent's configuration. ```typescript import { TinyAgent, TinyAnthropic, TinyMCP } from "@cubie-ai/tiny-ai"; import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; import path from "path"; // Create a TinyMCP instances and pass in the name, version and transport const solanaMcpClient = new TinyMCP({ name: "Solana MCP Client", version: "1.0.0", transport: new StdioClientTransport({ command: "node", args: ["dist/mcp/server.js"], }), }); // Add one or more clients to your agent. export const agent = new TinyAgent({ settings: { system: "You are a helpful assistant.", maxSteps: 5, }, provider: new TinyAnthropic({ apiKey: "your-api-key", }), clients: [solanaMcpClient], }); ``` -------------------------------- ### Creating TinyXAI Provider Instance (Typescript) Source: https://github.com/cubie-ai/tiny-ai/blob/main/guides/agents/creation.md This Typescript code snippet shows how to instantiate a specific provider, `TinyXAI`, which is a type of `TinyProvider`. It requires an `apiKey` for authentication with the underlying AI service. Replace "API_KEY" with your actual API key. ```typescript const provider = new TinyXAI({ apiKey: "API_KEY", }); ``` -------------------------------- ### Defining Optional TinyAgent Options and Settings Interfaces (Typescript) Source: https://github.com/cubie-ai/tiny-ai/blob/main/guides/agents/creation.md These Typescript interfaces extend the `TinyAgentOptions` to include optional fields like `tools`, `clients`, `name`, and `settings`. The `AgentSettings` interface details various optional parameters for controlling agent behavior, such as system prompts, step limits, token limits, and model generation parameters. ```typescript interface TinyAgentOptions> { tools?: TinyTool[] | Record; clients?: TinyMCP[]; name?: string; settings?: AgentSettings; } interface AgentSettings { system?: string; maxSteps?: number; maxTokens?: number; temperature?: number; topP?: number; frequencyPenalty?: number; presencePenalty?: number; } ``` -------------------------------- ### Creating TinyAgent Instance with Provider (Typescript) Source: https://github.com/cubie-ai/tiny-ai/blob/main/guides/agents/creation.md This Typescript code snippet demonstrates the creation of a `TinyAgent` instance. It passes the previously created `provider` instance to the agent's constructor, fulfilling the minimum requirement for agent initialization. ```typescript const agent = new TinyAgent({ provider, }); ``` -------------------------------- ### Defining Required TinyAgent Options Interface (Typescript) Source: https://github.com/cubie-ai/tiny-ai/blob/main/guides/agents/creation.md This Typescript interface defines the basic configuration required to instantiate a TinyAgent. It specifies that a `provider` of type `TinyProvider` is mandatory. ```typescript interface TinyAgentOptions> { provider: T; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.