Build with Agents

This example shows how to use the General Agent with tools for research, browsing, and multi-step conversations. The General Agent is the default agent in Spaces.

Key features:

  • Use prebuilt tools like web browsing and search
  • Full TypeScript support with type safety
  • Support for multi-step conversations
  • Custom system prompts for specialized behavior
1

Install Package

$pnpm add athena-intelligence
2

Set Up Client

1import type { AthenaIntelligence } from 'athena-intelligence';
2import { AthenaIntelligenceClient } from 'athena-intelligence';
3
4// Basic client setup (uses default production API)
5const client = new AthenaIntelligenceClient({
6 apiKey: process.env.ATHENA_API_KEY,
7});
8
9// Override baseUrl for custom API endpoints
10const customClient = new AthenaIntelligenceClient({
11 apiKey: process.env.ATHENA_API_KEY,
12 baseUrl: 'https://your-custom-api.example.com', // Custom API endpoint
13});
3

Basic Agent Usage

Start with a simple search request to understand the basic pattern:

1const request: AthenaIntelligence.GeneralAgentRequest = {
2 config: {
3 enabled_tools: ['search'],
4 model: 'gpt-4-turbo-preview',
5 },
6 messages: [
7 {
8 content: 'Search for information about Athena Intelligence and summarize what you find.',
9 role: 'user',
10 type: 'user',
11 },
12 ],
13};
14
15const response = await client.agents.general.invoke(request);
16console.log('Response received:', JSON.stringify(response, null, 2));
4

Multi-Tool Workflows

Combine multiple tools with custom system prompts for more sophisticated tasks:

1const multiToolRequest: AthenaIntelligence.GeneralAgentRequest = {
2 config: {
3 enabled_tools: ['search', 'browse'],
4 system_prompt: 'You are a helpful research assistant. Use search and browse tools to find comprehensive information.',
5 model: 'gpt-4-turbo-preview',
6 },
7 messages: [
8 {
9 content: 'Research the latest developments in AI and provide a detailed summary with sources.',
10 role: 'user',
11 type: 'user',
12 },
13 ],
14};
15
16const multiToolResponse = await client.agents.general.invoke(multiToolRequest);
17console.log('Multi-tool response:', JSON.stringify(multiToolResponse, null, 2));
5

Maintain Conversation Context

Build multi-turn conversations by maintaining the message history:

1// Start with system message and initial user question
2const messages: AthenaIntelligence.InputMessage[] = [
3 {
4 content: 'You are a knowledgeable assistant specializing in technology and business analysis.',
5 role: 'system',
6 type: 'system',
7 },
8 {
9 content: 'What are the key trends in artificial intelligence for 2024?',
10 role: 'user',
11 type: 'user',
12 },
13];
14
15const config: AthenaIntelligence.GeneralAgentConfig = {
16 enabled_tools: ['search'],
17 model: 'gpt-4-turbo-preview',
18};
19
20// First request
21let conversationRequest: AthenaIntelligence.GeneralAgentRequest = {
22 config,
23 messages,
24};
25
26const firstResponse = await client.agents.general.invoke(conversationRequest);
27console.log('First response received');
28
29// Add the assistant's response to conversation history
30if (firstResponse.messages && firstResponse.messages.length > 0) {
31 const lastMessage = firstResponse.messages[firstResponse.messages.length - 1];
32 messages.push({
33 content: lastMessage.kwargs?.content || 'No content',
34 role: 'assistant',
35 type: 'assistant',
36 });
37}
38
39// Add follow-up question
40messages.push({
41 content: 'Can you provide more specific examples of companies implementing these trends?',
42 role: 'user',
43 type: 'user',
44});
45
46// Continue conversation with full context
47conversationRequest = {
48 config,
49 messages,
50};
51
52const secondResponse = await client.agents.general.invoke(conversationRequest);
53console.log('Conversation completed:', JSON.stringify(secondResponse, null, 2));
6

Error Handling

Always include proper error handling for production applications:

1import { AthenaIntelligenceError } from 'athena-intelligence';
2
3try {
4 const response = await client.agents.general.invoke(request);
5 console.log('Response received:', response);
6} catch (error) {
7 if (error instanceof AthenaIntelligenceError) {
8 console.error(`Athena API error (${error.statusCode}): ${error.message}`);
9 } else {
10 console.error('Unexpected error:', error);
11 }
12}
7

Complete Example

Here’s a complete working example that demonstrates all the concepts:

1import type { AthenaIntelligence } from 'athena-intelligence';
2import { AthenaIntelligenceClient, AthenaIntelligenceError } from 'athena-intelligence';
3
4async function runAgentExample() {
5 try {
6 const client = new AthenaIntelligenceClient({
7 apiKey: process.env.ATHENA_API_KEY,
8 // Optional: override baseUrl for custom environments
9 // baseUrl: 'https://your-custom-api.example.com',
10 });
11
12 // Multi-tool research request
13 const request: AthenaIntelligence.GeneralAgentRequest = {
14 config: {
15 enabled_tools: ['search', 'browse'],
16 system_prompt: 'You are a helpful research assistant. Use search and browse tools to find comprehensive information.',
17 model: 'gpt-4-turbo-preview',
18 },
19 messages: [
20 {
21 content: 'Research the latest developments in AI and provide a detailed summary with sources.',
22 role: 'user',
23 type: 'user',
24 },
25 ],
26 };
27
28 console.log('Sending request to General Agent...');
29 const response = await client.agents.general.invoke(request);
30
31 console.log('Response received:');
32 console.log(JSON.stringify(response, null, 2));
33 } catch (error) {
34 if (error instanceof AthenaIntelligenceError) {
35 console.error(`Athena API error (${error.statusCode}): ${error.message}`);
36 } else {
37 console.error('Unexpected error:', error);
38 }
39 }
40}
41
42runAgentExample();