Use Models Directly

This example shows how to use Athena’s language models directly when building with our TypeScript SDK. You can use the General Agent without tools to access models directly for simple text generation tasks.

Key features:

  • Use any model available in your workspace
  • Full TypeScript support with type safety
  • Efficient async/await patterns
  • Batch processing capabilities
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 Model Usage

Use the General Agent without tools to access models directly:

1const request: AthenaIntelligence.GeneralAgentRequest = {
2 config: {
3 model: 'gpt-4-turbo-preview',
4 },
5 messages: [
6 {
7 content: 'What is the capital of France?',
8 role: 'user',
9 type: 'user',
10 },
11 ],
12};
13
14const response = await client.agents.general.invoke(request);
15console.log('Response:', JSON.stringify(response, null, 2));
4

Available Models

Specify models explicitly in the config. Common models include:

1// Use Claude models
2const claudeRequest: AthenaIntelligence.GeneralAgentRequest = {
3 config: {
4 model: 'claude-3-5-sonnet-20241022',
5 },
6 messages: [
7 {
8 content: 'Who are you and what can you help me with?',
9 role: 'user',
10 type: 'user',
11 },
12 ],
13};
14
15const claudeResponse = await client.agents.general.invoke(claudeRequest);
16
17// Use GPT models
18const gptRequest: AthenaIntelligence.GeneralAgentRequest = {
19 config: {
20 model: 'gpt-4-turbo-preview',
21 },
22 messages: [
23 {
24 content: 'Explain quantum computing in simple terms',
25 role: 'user',
26 type: 'user',
27 },
28 ],
29};
30
31const gptResponse = await client.agents.general.invoke(gptRequest);
32
33// Use other models
34const mistralRequest: AthenaIntelligence.GeneralAgentRequest = {
35 config: {
36 model: 'mistral-large-2407',
37 },
38 messages: [
39 {
40 content: 'Write a haiku about programming',
41 role: 'user',
42 type: 'user',
43 },
44 ],
45};
46
47const mistralResponse = await client.agents.general.invoke(mistralRequest);
5

System Messages and Context

Use system messages to prime the model’s behavior:

1const systemRequest: AthenaIntelligence.GeneralAgentRequest = {
2 config: {
3 model: 'gpt-4-turbo-preview',
4 },
5 messages: [
6 {
7 content: 'You are a helpful coding assistant specializing in TypeScript and Node.js.',
8 role: 'system',
9 type: 'system',
10 },
11 {
12 content: 'How do I handle async errors in TypeScript?',
13 role: 'user',
14 type: 'user',
15 },
16 ],
17};
18
19const systemResponse = await client.agents.general.invoke(systemRequest);
20console.log('System-primed response:', systemResponse);
6

Batch Processing

Process multiple prompts efficiently using Promise.all:

1const prompts = [
2 'Explain the theory of relativity',
3 'What is machine learning?',
4 'How does photosynthesis work?',
5 'Describe the water cycle',
6];
7
8// Create requests for all prompts
9const batchRequests = prompts.map(prompt => ({
10 config: {
11 model: 'gpt-4-turbo-preview',
12 },
13 messages: [
14 {
15 content: prompt,
16 role: 'user',
17 type: 'user',
18 },
19 ],
20}));
21
22// Process all requests in parallel
23const batchPromises = batchRequests.map(request =>
24 client.agents.general.invoke(request)
25);
26
27const batchResults = await Promise.all(batchPromises);
28
29// Process results
30batchResults.forEach((result, index) => {
31 console.log(`Response ${index + 1} (${prompts[index]}):`);
32 console.log(JSON.stringify(result, null, 2));
33 console.log('-'.repeat(50));
34});
7

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({
5 config: {
6 model: 'gpt-4-turbo-preview',
7 },
8 messages: [
9 {
10 content: 'Hello, world!',
11 role: 'user',
12 type: 'user',
13 },
14 ],
15 });
16
17 console.log('Response received:', response);
18} catch (error) {
19 if (error instanceof AthenaIntelligenceError) {
20 console.error(`Athena API error (${error.statusCode}): ${error.message}`);
21 } else {
22 console.error('Unexpected error:', error);
23 }
24}
8

Complete Example

Here’s a complete working example that demonstrates direct model usage:

1import type { AthenaIntelligence } from 'athena-intelligence';
2import { AthenaIntelligenceClient, AthenaIntelligenceError } from 'athena-intelligence';
3
4async function runDirectModelExample() {
5 try {
6 const client = new AthenaIntelligenceClient({
7 apiKey: process.env.ATHENA_API_KEY,
8 });
9
10 // Simple text generation request
11 const request: AthenaIntelligence.GeneralAgentRequest = {
12 config: {
13 model: 'gpt-4-turbo-preview',
14 },
15 messages: [
16 {
17 content: 'You are a creative writing assistant.',
18 role: 'system',
19 type: 'system',
20 },
21 {
22 content: 'Write a short story about a robot learning to paint',
23 role: 'user',
24 type: 'user',
25 },
26 ],
27 };
28
29 console.log('Sending request to model...');
30 const response = await client.agents.general.invoke(request);
31
32 console.log('Response received:');
33 console.log(JSON.stringify(response, null, 2));
34 } catch (error) {
35 if (error instanceof AthenaIntelligenceError) {
36 console.error(`Athena API error (${error.statusCode}): ${error.message}`);
37 } else {
38 console.error('Unexpected error:', error);
39 }
40 }
41}
42
43runDirectModelExample();