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