-
Notifications
You must be signed in to change notification settings - Fork 2.4k
src backend src modules puterai api_examples
KernelDeimos edited this page May 14, 2025
·
1 revision
This document provides examples of API requests to the PuterAI services. These examples demonstrate how to interact with various AI capabilities of the Puter platform.
Example of using AWS Textract for OCR:
await (await fetch("http://api.puter.localhost:4100/drivers/call", {
"headers": {
"Content-Type": "application/json",
"Authorization": `Bearer ${puter.authToken}`,
},
"body": JSON.stringify({
interface: 'puter-ocr',
driver: 'aws-textract',
method: 'recognize',
args: {
source: '~/Desktop/testocr.png',
},
}),
"method": "POST",
})).json();
Example of using OpenAI for chat completion:
await (await fetch("http://api.puter.localhost:4100/drivers/call", {
"headers": {
"Content-Type": "application/json",
"Authorization": `Bearer ${puter.authToken}`,
},
"body": JSON.stringify({
interface: 'puter-chat-completion',
driver: 'openai-completion',
method: 'complete',
args: {
messages: [
{
role: 'system',
content: 'Act like Spongebob'
},
{
role: 'user',
content: 'How do I make my code run faster?'
},
]
},
}),
"method": "POST",
})).json();
Example of using OpenAI for image generation:
URL.createObjectURL(await (await fetch("http://api.puter.localhost:4100/drivers/call", {
"headers": {
"Content-Type": "application/json",
"Authorization": `Bearer ${puter.authToken}`,
},
"body": JSON.stringify({
interface: 'puter-image-generation',
driver: 'openai-image-generation',
method: 'generate',
args: {
prompt: 'photorealistic teapot made of swiss cheese',
}
}),
"method": "POST",
})).blob());
Example of using tool functions with AI:
await puter.ai.chat('What\'s the weather like in Vancouver?', {
tools: [
{
type: 'function',
'function': {
name: 'get_weather',
description: 'A string describing the weather',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'city',
},
},
required: ['location'],
additionalProperties: false,
},
strict: true
},
}
]
})
Example with tool response:
await puter.ai.chat([
{ content: `What's the weather like in Vancouver?` },
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_vcfEOmDczXq7KGMirPGGiNEe",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\":\"Vancouver\"}"
}
}
],
"refusal": null
},
{
role: 'tool',
tool_call_id: 'call_vcfEOmDczXq7KGMirPGGiNEe',
content: 'Sunny with a chance of rain'
},
], {
tools: [
{
type: 'function',
'function': {
name: 'get_weather',
description: 'A string describing the weather',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'city',
},
},
required: ['location'],
additionalProperties: false,
},
strict: true
},
}
]
})
Example of using Claude with streaming:
gen = await puter.ai.chat('What\'s the weather like in Vancouver?', {
model: 'claude',
stream: true,
tools: [
{
type: 'function',
'function': {
name: 'get_weather',
description: 'A string describing the weather',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'city',
},
},
required: ['location'],
additionalProperties: false,
},
strict: true
},
}
]
})
for await ( const thing of gen ) { console.log('thing', thing) }
Last item in the stream looks like this:
{
"tool_use": {
"type": "tool_use",
"id": "toolu_01Y4naZhXygjUVRjGBvrL9z8",
"name": "get_weather",
"input": {
"location": "Vancouver"
}
}
}
Responding to tool use:
gen = await puter.ai.chat([
{ role: 'user', content: `What's the weather like in Vancouver?` },
{
"role": "assistant",
"content": [
{ type: 'text', text: "I'll check the weather in Vancouver for you." },
{ type: 'tool_use', name: 'get_weather', id: 'toolu_01Y4naZhXygjUVRjGBvrL9z8', input: { location: 'Vancouver' } },
]
},
{
role: 'user',
content: [
{
type: 'tool_result',
tool_use_id: 'toolu_01Y4naZhXygjUVRjGBvrL9z8',
content: 'Sunny with a chance of rain'
}
]
},
], {
model: 'claude',
stream: true,
tools: [
{
type: 'function',
'function': {
name: 'get_weather',
description: 'A string describing the weather',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'city',
},
},
required: ['location'],
additionalProperties: false,
},
strict: true
},
}
]
})
for await ( const item of gen ) { console.log(item) }
This wiki is generated from the repository. Do not edit files the wiki.
You are reading documentation for Puter, an open-source high-level operating system.
Getting started with Puter on localhost is as simple as:
git clone https://github.com/HeyPuter/puter.git
npm install
npm run start
- Index (README.md)
- api drivers
- Group Endpoints
- Notification Endpoints
- Share Endpoints
- Type-Tagged Objects
- Comment Prefixes
- contributors vscode
- Local Email Testing
- Puter Extensions
- Repository Structure and Tooling
- Configuring Domains for Self-Hosted Puter
- Configuring Puter
- First Run Issues
- self_hosters config_values
- self_hosters credit_context
- self_hosters support
- Self-Hosting Puter
- Backend Style
- Puter Backend - Directory Structure
- Puter Backend Boot Sequence
- Puter Kernel Moduels and Services
- Index (README.md)
- Configuring AI Services
- PuterAI API Request Examples
- src backend src modules puterai config
####### For Contributors