Pular para o conteúdo principal

C# and JavaScript

The examples below use the official OpenAI SDKs with Maritaca's compatible endpoint. Both make a regular call and complete a full tool-calling cycle.

Set the API key before running the examples. The default model is sabia-4; set MARITACA_MODEL=sabia-4-thinking to use the reasoning model.

export MARITACA_API_KEY="your-key-here"
export MARITACA_MODEL="sabia-4"

Install the SDK and save the example as example.mjs:

npm install openai
node example.mjs
import OpenAI from "openai";

const apiKey = process.env.MARITACA_API_KEY;
if (!apiKey) throw new Error("Set MARITACA_API_KEY");

const model = process.env.MARITACA_MODEL ?? "sabia-4";
const client = new OpenAI({
apiKey,
baseURL: "https://chat.maritaca.ai/api",
});

const normal = await client.chat.completions.create({
model,
messages: [{ role: "user", content: "What is 25 + 27? Include 52 in the answer." }],
});
const normalText = normal.choices[0].message.content;
console.log("Regular call:", normalText);

const multiplyTool = {
type: "function",
function: {
name: "multiply",
description: "Multiply two integers.",
parameters: {
type: "object",
properties: {
a: { type: "integer" },
b: { type: "integer" },
},
required: ["a", "b"],
additionalProperties: false,
},
},
};

const messages = [
{ role: "user", content: "Use multiply to calculate 6 times 7." },
];
const toolRequest = await client.chat.completions.create({
model,
messages,
tools: [multiplyTool],
tool_choice: { type: "function", function: { name: "multiply" } },
});

const assistantMessage = toolRequest.choices[0].message;
const toolCall = assistantMessage.tool_calls?.[0];
if (!toolCall) throw new Error("The model did not request a tool");

const { a, b } = JSON.parse(toolCall.function.arguments);
messages.push(
assistantMessage,
{ role: "tool", tool_call_id: toolCall.id, content: String(a * b) },
);

const final = await client.chat.completions.create({
model,
messages,
tools: [multiplyTool],
});
console.log("Tool response:", final.choices[0].message.content); // 42

Do not rely on FinishReason alone to detect a tool request: check whether ToolCalls contains items. The same flow works with sabia-4 and sabia-4-thinking.