Skip to content

Latest commit

 

History

History
148 lines (108 loc) · 2.94 KB

File metadata and controls

148 lines (108 loc) · 2.94 KB

Getting Started

Get up and running with Tawk Agents SDK in minutes.

Installation

# Clone the repository
git clone https://github.com/Manoj-tawk/tawk-agents-sdk.git
cd tawk-agents-sdk
npm install

Install your preferred AI provider:

# OpenAI
npm install @ai-sdk/openai

# Or Anthropic
npm install @ai-sdk/anthropic

# Or Google
npm install @ai-sdk/google

Environment Setup

Create a .env file:

OPENAI_API_KEY=sk-...
# Or
ANTHROPIC_API_KEY=sk-ant-...
# Or
GOOGLE_GENERATIVE_AI_API_KEY=...

Your First Agent

Create agent.ts:

import { Agent, run } from '../../src';
import { openai } from '@ai-sdk/openai';

const agent = new Agent({
  name: 'assistant',
  model: openai('gpt-4o'),
  instructions: 'You are a helpful assistant.'
});

async function main() {
  const result = await run(agent, 'Hello!');
  console.log(result.finalOutput);
}

main();

Run it:

npx ts-node agent.ts

Adding Tools

Tools let your agent perform actions:

import { Agent, run, tool } from '../../src';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

const calculator = tool({
  description: 'Perform calculations',
  inputSchema: z.object({
    expression: z.string().describe('Mathematical expression to evaluate')
  }),
  execute: async ({ expression }) => {
    return { result: eval(expression) };
  }
});

const agent = new Agent({
  name: 'calculator',
  model: openai('gpt-4o'),
  instructions: 'You help users with calculations.',
  tools: { calculator }
});

const result = await run(agent, 'What is 15 + 23?');
console.log(result.finalOutput); // "The result is 38"

Adding Session Memory

Sessions maintain conversation history:

import { Agent, run, MemorySession } from '../../src';
import { openai } from '@ai-sdk/openai';

const agent = new Agent({
  name: 'assistant',
  model: openai('gpt-4o'),
  instructions: 'You are a helpful assistant.'
});

const session = new MemorySession('user-123', 50); // id, maxMessages

// First message
await run(agent, 'My name is Alice', { session });

// Second message - remembers context
const result = await run(agent, 'What is my name?', { session });
console.log(result.finalOutput); // "Your name is Alice"

Streaming Responses

Get real-time responses:

import { Agent, runStream } from '../../src';
import { openai } from '@ai-sdk/openai';

const agent = new Agent({
  name: 'assistant',
  model: openai('gpt-4o'),
  instructions: 'You are a helpful assistant.'
});

const stream = await runStream(agent, 'Tell me a story');

for await (const chunk of stream.textStream) {
  process.stdout.write(chunk);
}

const result = await stream.completed;

Next Steps