import { createServer } from '@hari7261/ainative-server-node';
const server = createServer({
// Provider configuration
openai: {
apiKey: process.env.OPENAI_API_KEY,
model: 'gpt-4o',
},
anthropic: {
apiKey: process.env.ANTHROPIC_API_KEY,
model: 'claude-3-5-sonnet-20241022',
},
ollama: {
baseURL: 'http://localhost:11434',
model: 'llama2',
},
// Server configuration
defaultProvider: 'openai',
port: 3001,
cors: true,
registerBuiltInTools: true,
});
server.listen();server.registerTool({
name: 'get_weather',
description: 'Get weather for a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City name',
},
},
required: ['location'],
},
handler: async (args) => {
// Fetch weather data
return { temperature: 72, condition: 'Sunny' };
},
});Non-streaming action execution.
Request:
{
"action": "chat",
"params": { "message": "Hello" },
"context": { "userId": "123" }
}Response:
{
"messages": [
{ "role": "assistant", "content": "Hi there!" }
],
"context": { "userId": "123" }
}Streaming action execution (SSE).
Request:
{
"action": "chat",
"params": { "message": "Hello" },
"context": {}
}Response: Server-Sent Events
data: {"type":"token","data":"Hi"}
data: {"type":"token","data":" there"}
data: {"type":"token","data":"!"}
data: {"type":"done"}
data: [DONE]
Execute a registered tool.
Request:
{
"tool": "get_weather",
"args": { "location": "San Francisco" },
"context": {}
}Response:
{
"success": true,
"result": { "temperature": 72, "condition": "Sunny" }
}List all registered tools.
Response:
{
"tools": [
{
"name": "get_weather",
"description": "Get weather for a location",
"parameters": { "..." }
}
]
}from ainative import create_server, ServerConfig
config = ServerConfig(
openai_api_key="sk-...",
openai_model="gpt-4o",
default_provider="openai",
)
server = create_server(config)
app = server.get_app()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=3001)from ainative import Tool
async def get_weather_handler(args, context=None):
location = args.get("location")
# Fetch weather data
return {"temperature": 72, "condition": "Sunny"}
tool = Tool(
name="get_weather",
description="Get weather for a location",
parameters={
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
},
handler=get_weather_handler
)
server.register_tool(tool)uvicorn main:app --reload --port 3001openai: {
apiKey: string;
model?: string; // default: 'gpt-4o'
baseURL?: string;
organization?: string;
}anthropic: {
apiKey: string;
model?: string; // default: 'claude-3-5-sonnet-20241022'
}ollama: {
baseURL?: string; // default: 'http://localhost:11434'
model?: string; // default: 'llama2'
}Get the current time in a timezone.
Parameters:
timezone(string, optional): Timezone name
Perform a mathematical calculation.
Parameters:
expression(string, required): Math expression to evaluate
All endpoints return standardized error responses:
{
"error": "Error message here"
}HTTP status codes:
400: Bad Request (invalid input)500: Internal Server Error