Skip to content

Latest commit

 

History

History
97 lines (71 loc) · 1.95 KB

File metadata and controls

97 lines (71 loc) · 1.95 KB

@forestadmin/ai-proxy

AI Proxy client for Forest Admin.

Quick Start

import { createAiProxyClient } from '@forestadmin/ai-proxy/client';

const client = createAiProxyClient({ baseUrl: 'https://my-agent.com/forest' });

const response = await client.chat('Hello!');
console.log(response.choices[0].message.content);

Installation

Client only (frontend, no extra dependencies):

npm install @forestadmin/ai-proxy

Server side (Router, ProviderDispatcher):

npm install @forestadmin/ai-proxy @langchain/core @langchain/openai

Configuration

Choose one mode:

// Simple mode (recommended)
const client = createAiProxyClient({
  baseUrl: 'https://my-agent.com/forest',
  headers: { Authorization: 'Bearer token' },  // optional
  timeout: 30000,                               // optional (default: 30s)
});

// Custom fetch mode
const client = createAiProxyClient({
  fetch: myCustomFetch,
  timeout: 30000,
});

API

chat(input)

// Simple
await client.chat('Hello!');

// With options
await client.chat({
  messages: [{ role: 'user', content: 'Hello!' }],
  tools: [...],         // optional
  toolChoice: 'auto',   // optional
  aiName: 'gpt-4',      // optional - server AI config name
});

getTools()

const tools = await client.getTools();
// [{ name: 'brave_search', description: '...', schema: {...} }]

callTool(name, inputs)

const result = await client.callTool('brave_search', [
  { role: 'user', content: 'cats' }
]);

Error Handling

import { AiProxyClientError } from '@forestadmin/ai-proxy/client';

try {
  await client.chat('Hello');
} catch (error) {
  if (error instanceof AiProxyClientError) {
    console.error(error.status, error.message);

    if (error.isNetworkError) { /* status 0 */ }
    if (error.isClientError)  { /* status 4xx */ }
    if (error.isServerError)  { /* status 5xx */ }
  }
}