### Python: Multi-Tool Agent Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/llms-full.txt Illustrates how to use the `tools` and `tool_choice` parameters to enable the LLM to call external functions. This example defines functions for searching a database and sending emails. ```APIDOC ## POST /v1/chat/completions (Tool Use) ### Description Enables the LLM to use predefined tools (functions) to accomplish tasks. The gateway will determine which tool to call based on the user's request and the provided tool definitions. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Query Parameters None #### Request Body - **model** (string) - Required - The model to use. - **messages** (array) - Required - Conversation history. - **tools** (array) - Optional - A list of tool definitions the model can use. - **type** (string) - Required - Type of tool, e.g., 'function'. - **function** (object) - Function definition. - **name** (string) - Required - Name of the function. - **description** (string) - Optional - Description of what the function does. - **parameters** (object) - JSON schema for the function's arguments. - **tool_choice** (string or object) - Optional - Controls how the model uses tools. 'auto' lets the model decide, or specify a tool name. ### Request Example ```json { "model": "claude-sonnet-4-6", "messages": [ {"role": "user", "content": "Find the top 5 users by order count and send a summary to admin@company.com"} ], "tools": [ { "type": "function", "function": { "name": "search_database", "description": "Search a database for records matching a query", "parameters": { "type": "object", "properties": { "query": {"type": "string", "description": "Search query"}, "table": {"type": "string", "enum": ["users", "orders", "products"]}, "limit": {"type": "integer", "description": "Max results", "default": 10} }, "required": ["query", "table"] } } }, { "type": "function", "function": { "name": "send_email", "description": "Send an email to a recipient", "parameters": { "type": "object", "properties": { "to": {"type": "string", "description": "Recipient email"}, "subject": {"type": "string", "description": "Email subject"}, "body": {"type": "string", "description": "Email body text"} }, "required": ["to", "subject", "body"] } } } ], "tool_choice": "auto" } ``` ### Response #### Success Response (200) - **choices** (array) - Completion choices. - **message** (object) - The assistant's message, which may contain tool calls. - **tool_calls** (array) - If the model decided to call a tool. - **id** (string) - ID of the tool call. - **function** (object) - Details of the function call. - **name** (string) - Name of the function to call. - **arguments** (string) - JSON string of arguments for the function. #### Response Example (Tool Call) ```json { "choices": [ { "message": { "tool_calls": [ { "id": "call_abc123", "function": { "name": "search_database", "arguments": "{\"query\": \"top 5 users by order count\", \"table\": \"users\", \"limit\": 5}" } }, { "id": "call_def456", "function": { "name": "send_email", "arguments": "{\"to\": \"admin@company.com\", \"subject\": \"Top Users Summary\", \"body\": \"User data retrieved...\"}" } } ] } } ] } ``` ``` -------------------------------- ### curl Examples Source: https://context7.com/fasuizu-br/brainiall-llm-gateway/llms.txt Provides direct HTTP request examples using curl for various operations including basic chat completion, streaming responses, and JSON mode. ```APIDOC ## curl Examples Direct HTTP requests using curl for shell scripts and command-line usage. Supports all three authentication methods: Bearer token, api-key header, and Ocp-Apim-Subscription-Key header. Use `-N` flag for streaming to disable output buffering. ### Basic Chat Completion ```bash curl -s -X POST https://apim-ai-apis.azure-api.net/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_KEY" \ -d '{ "model": "claude-haiku-4-5", "messages": [ {"role": "system", "content": "You are a concise assistant."}, {"role": "user", "content": "What is the capital of France?"} ], "max_tokens": 100 }' | python3 -m json.tool ``` ### Streaming Response ```bash curl -s -N -X POST https://apim-ai-apis.azure-api.net/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_KEY" \ -d '{ "model": "nova-micro", "messages": [{"role": "user", "content": "Count from 1 to 10"}], "stream": true }' ``` ### JSON Mode ```bash curl -s -X POST https://apim-ai-apis.azure-api.net/v1/chat/completions \ -H "Content-Type: application/json" \ -H "api-key: YOUR_KEY" \ -d '{ "model": "claude-sonnet-4-6", "messages": [ {"role": "system", "content": "Extract data as JSON."}, {"role": "user", "content": "Alice, 28, engineer at Microsoft in Seattle"} ], "response_format": {"type": "json_object"}, "max_tokens": 200 }' | python3 -m json.tool ``` ``` -------------------------------- ### Quick Start: curl Chat Completion Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md Provides a command-line example using curl to send a chat completion request to the Brainiall LLM Gateway. This method is useful for testing and integration without SDKs. ```bash curl -X POST https://apim-ai-apis.azure-api.net/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_KEY" \ -d '{ \ "model": "claude-sonnet-4-6", \ "messages": [{"role": "user", "content": "Hello!"}] \ }' ``` -------------------------------- ### Create Chat Completion (Python) Source: https://context7.com/fasuizu-br/brainiall-llm-gateway/llms.txt Creates a chat completion using the OpenAI Python SDK. This example demonstrates how to send a system message and a user message to the API and retrieve the assistant's response and usage statistics. It requires the 'openai' library to be installed. ```python from openai import OpenAI client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) response = client.chat.completions.create( model="claude-sonnet-4-6", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Explain quantum computing in simple terms."} ], temperature=0.7, max_tokens=1024 ) print(response.choices[0].message.content) print(f"Tokens used: {response.usage.total_tokens}") # Output: A clear explanation of quantum computing # Tokens used: 245 ``` -------------------------------- ### Python and Bash List Available Models with OpenAI API Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md Provides code examples in both Python and Bash to list available models through the OpenAI API. This is useful for discovering which models are accessible for use, requiring an API key. ```python from openai import OpenAI client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) models = client.models.list() for model in models.data: print(f"{model.id} — owned by {model.owned_by}") ``` ```bash curl -s https://apim-ai-apis.azure-api.net/v1/models \ -H "Authorization: Bearer YOUR_KEY" | python3 -m json.tool ``` -------------------------------- ### Quick Start: JavaScript OpenAI SDK Chat Completion Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md Shows how to make a chat completion request using the OpenAI SDK in JavaScript, connecting to the Brainiall LLM Gateway. Ensure the 'openai' package is installed. ```javascript import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://apim-ai-apis.azure-api.net/v1", apiKey: "YOUR_KEY", }); const response = await client.chat.completions.create({ model: "claude-sonnet-4-6", messages: [{ role: "user", content: "Hello!" }], }); console.log(response.choices[0].message.content); ``` -------------------------------- ### Configure Aider for Brainiall Models Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md This example shows how to configure the Aider command-line tool to use Brainiall models. It involves setting environment variables for the API base URL and API key, and then specifying the desired model. ```bash export OPENAI_API_BASE=https://apim-ai-apis.azure-api.net/v1 export OPENAI_API_KEY=YOUR_KEY aider --model openai/claude-sonnet-4-6 ``` -------------------------------- ### TypeScript: Next.js API Route with Vercel AI SDK Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/llms-full.txt This TypeScript example shows how to create a Next.js API route using the Vercel AI SDK to stream text responses. It configures the SDK to use the BrainiAI gateway and handles incoming messages to generate a response. Ensure you have '@ai-sdk/openai' installed and your API key set in environment variables. ```typescript import { createOpenAI } from "@ai-sdk/openai"; import { streamText } from "ai"; const brainiall = createOpenAI({ baseURL: "https://apim-ai-apis.azure-api.net/v1", apiKey: process.env.BRAINIALL_API_KEY!, }); export async function POST(req: Request) { const { messages } = await req.json(); const result = streamText({ model: brainiall("claude-sonnet-4-6"), messages, }); return result.toDataStreamResponse(); } ``` -------------------------------- ### Quick Start: Python OpenAI SDK Chat Completion Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md Demonstrates how to perform a basic chat completion using the OpenAI SDK in Python with the Brainiall LLM Gateway. It requires the 'openai' library and an API key. ```python from openai import OpenAI client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) response = client.chat.completions.create( model="claude-sonnet-4-6", messages=[{"role": "user", "content": "Hello!"}] ) print(response.choices[0].message.content) ``` -------------------------------- ### Python: RAG Pipeline with LangChain Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/llms-full.txt This Python code demonstrates setting up a Retrieval-Augmented Generation (RAG) pipeline using LangChain. It configures the LLM to use the BrainiAI gateway and defines a prompt template for question answering based on provided context. Ensure you have 'langchain-openai' and 'langchain-core' installed. ```python from langchain_openai import ChatOpenAI, OpenAIEmbeddings from langchain_core.prompts import ChatPromptTemplate llm = ChatOpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY", model="claude-sonnet-4-6", temperature=0, streaming=True ) prompt = ChatPromptTemplate.from_messages([ ("system", "Answer the question based on the context:\n\n{context}"), ("user", "{question}") ]) chain = prompt | llm response = chain.invoke({ "context": "Python was created by Guido van Rossum and first released in 1991. It emphasizes code readability.", "question": "When was Python created and by whom?" }) print(response.content) ``` -------------------------------- ### curl: Chat Completion and Model Listing Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/llms-full.txt This section provides curl commands for interacting with the LLM Gateway. It includes examples for basic chat completion, streaming responses, and listing available models. Replace 'YOUR_KEY' with your API key. The model listing example uses Python for formatted output. ```bash # Basic chat completion curl -s -X POST https://apim-ai-apis.azure-api.net/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_KEY" \ -d '{ "model": "claude-haiku-4-5", "messages": [{"role": "system", "content": "You are a concise assistant."}, {"role": "user", "content": "What is the capital of France?"}] }' | python3 -m json.tool # Streaming curl -s -N -X POST https://apim-ai-apis.azure-api.net/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_KEY" \ -d '{ "model": "nova-micro", "messages": [{"role": "user", "content": "Count from 1 to 10"}], "stream": true }' # List models curl -s https://apim-ai-apis.azure-api.net/v1/models \ -H "Authorization: Bearer YOUR_KEY" | python3 -c " import json, sys data = json.load(sys.stdin) for m in sorted(data['data'], key=lambda x: x['id']): print(f\"{m['id']:40s} {m['owned_by']}\") " ``` -------------------------------- ### Tool / Function Calling: Python OpenAI SDK Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md Provides an example of implementing tool/function calling with the OpenAI SDK in Python. This allows the AI model to call external functions based on user requests. ```python from openai import OpenAI import json client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather for a location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "City name, e.g. 'San Francisco, CA'" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit" } }, "required": ["location"] } } } ] response = client.chat.completions.create( model="claude-sonnet-4-6", messages=[{"role": "user", "content": "What's the weather in Tokyo?"}], tools=tools, tool_choice="auto" ) message = response.choices[0].message if message.tool_calls: for tool_call in message.tool_calls: print(f"Function: {tool_call.function.name}") print(f"Arguments: {tool_call.function.arguments}") args = json.loads(tool_call.function.arguments) print(f"Location: {args['location']}") ``` -------------------------------- ### LangChain with Tool Calling using Brainiall Models (Python) Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md This Python example extends the LangChain integration to include tool calling capabilities with Brainiall models. It defines simple 'multiply' and 'add' tools and binds them to the ChatOpenAI model for function execution. ```python from langchain_openai import ChatOpenAI from langchain_core.tools import tool @tool def multiply(a: int, b: int) -> int: """Multiply two numbers together.""" return a * b @tool def add(a: int, b: int) -> int: """Add two numbers together.""" return a + b llm = ChatOpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY", model="claude-sonnet-4-6" ) llm_with_tools = llm.bind_tools([multiply, add]) response = llm_with_tools.invoke("What is 3 * 12 + 5?") print(response.tool_calls) ``` -------------------------------- ### Python: Streaming Chat Completion and Token Counting Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/llms-full.txt Shows how to receive chat completion responses in a streaming fashion using the OpenAI Python client. This example iterates over the stream to print content chunks as they arrive and calculates the total character length of the response. ```python from openai import OpenAI client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) stream = client.chat.completions.create( model="deepseek-r1", messages=[{"role": "user", "content": "Solve: what is 25! / 23!"}], stream=True, max_tokens=2048 ) full_response = "" for chunk in stream: delta = chunk.choices[0].delta if delta.content: full_response += delta.content print(delta.content, end="", flush=True) print(f"\n\nTotal length: {len(full_response)} chars") ``` -------------------------------- ### JavaScript Streaming Chat Example Source: https://context7.com/fasuizu-br/brainiall-llm-gateway/llms.txt Demonstrates streaming support in JavaScript/Node.js using the OpenAI SDK with async iterators. The `stream: true` option enables real-time output, and responses are consumed using `for await` loops to process each chunk as it arrives. ```APIDOC ## JavaScript Streaming Chat Example ### Description This example shows how to handle streaming chat completions in JavaScript using the OpenAI SDK. It utilizes async iterators to process response chunks in real-time. ### Method POST ### Endpoint /v1/chat/completions ### Parameters (See POST /v1/chat/completions for detailed parameters. Key parameters for streaming are `stream: true`) ### Request Example (Conceptual - SDK handles JSON) ```javascript import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://apim-ai-apis.azure-api.net/v1", apiKey: "YOUR_KEY", }); async function streamChat(prompt) { const stream = await client.chat.completions.create({ model: "claude-sonnet-4-6", messages: [{ role: "user", content: prompt }], stream: true, }); let fullResponse = ""; for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content; if (content) { fullResponse += content; process.stdout.write(content); // Write chunk to console immediately } } console.log(`\nResponse length: ${fullResponse.length} chars`); return fullResponse; } // Example usage: // await streamChat("Write a Python function to implement binary search"); ``` ### Response (See POST /v1/chat/completions for detailed response structure. Chunks are processed iteratively.) ``` -------------------------------- ### JavaScript: Vision Analysis with OpenAI SDK Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/llms-full.txt This snippet demonstrates how to perform vision analysis using the OpenAI JavaScript SDK. It sends a text prompt along with an image URL to the specified model for detailed description. Ensure you have the 'openai' package installed and replace 'YOUR_KEY' with your actual API key. ```javascript import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://apim-ai-apis.azure-api.net/v1", apiKey: "YOUR_KEY", }); const response = await client.chat.completions.create({ model: "claude-sonnet-4-6", messages: [ { role: "user", content: [ { type: "text", text: "Describe this image in detail." }, { type: "image_url", image_url: { url: "https://example.com/image.jpg" }, }, ], }, ], max_tokens: 1024, }); console.log(response.choices[0].message.content); ``` -------------------------------- ### JavaScript: Streaming Chat Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/llms-full.txt Provides a JavaScript example for performing a streaming chat completion request using the OpenAI Node.js client. ```APIDOC ## POST /v1/chat/completions (JavaScript Streaming) ### Description Demonstrates how to perform a streaming chat completion request using JavaScript and the OpenAI Node.js library. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Query Parameters None #### Request Body - **model** (string) - Required - The model to use. - **messages** (array) - Required - Conversation history. - **stream** (boolean) - Required - Set to `true` to enable streaming. ### Request Example ```javascript import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://apim-ai-apis.azure-api.net/v1", apiKey: "YOUR_KEY", }); async function streamChat(prompt) { const stream = await client.chat.completions.create({ model: "claude-sonnet-4-6", messages: [{ role: "user", content: prompt }], stream: true, }); let fullResponse = ""; for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content; if (content) { fullResponse += content; process.stdout.write(content); } } console.log("\n---"); console.log(`Response length: ${fullResponse.length} chars`); return fullResponse; } await streamChat("Write a Python function to implement binary search"); ``` ### Response #### Success Response (200) - **choices** (array) - Streaming choices. - **delta** (object) - Content delta for the current chunk. - **content** (string) - The text content for this part of the response. #### Response Example (each chunk) ```json { "choices": [ { "delta": { "content": "def binary_search(arr, target):\n low = 0\n high = len(arr) - 1\n\n while low <= high:\n mid = (low + high) // 2\n mid_val = arr[mid]\n\n if mid_val == target:\n return mid # Target found, return its index elif mid_val < target:\n low = mid + 1 else: # mid_val > target high = mid - 1 return -1 # Target not found" } } ] } ``` ``` -------------------------------- ### GET /v1/models Source: https://context7.com/fasuizu-br/brainiall-llm-gateway/llms.txt Lists all available models with their IDs and provider information. The response is an array of model objects, each containing `id`, `object`, and `owned_by` fields. ```APIDOC ## GET /v1/models Lists all available models with their IDs and provider information. Returns an array of model objects containing `id` (model identifier to use in requests), `object` ("model"), and `owned_by` (provider name like "anthropic", "meta", "amazon"). ### Method GET ### Endpoint /v1/models ### Response #### Success Response (200) - **data** (array) - An array of model objects. - **id** (string) - The identifier for the model. - **object** (string) - The type of object, always "model". - **owned_by** (string) - The provider of the model (e.g., "anthropic", "meta"). ### Response Example ```json { "data": [ { "id": "claude-haiku-4-5", "object": "model", "owned_by": "anthropic" }, { "id": "claude-opus-4-5", "object": "model", "owned_by": "anthropic" } // ... more models ] } ``` ``` -------------------------------- ### Configure n8n for Brainiall Models Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md This guide explains how to set up n8n to use Brainiall models. It involves creating a new 'OpenAI API' credential, providing the API key and base URL, and then using the 'Chat Model' or 'OpenAI' node. ```text 1. Create a new credential: "OpenAI API" type 2. API Key: `YOUR_KEY` 3. Base URL: `https://apim-ai-apis.azure-api.net/v1` 4. Use the "Chat Model" or "OpenAI" node with your credential ``` -------------------------------- ### Python: Multi-Tool Agent Implementation Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/llms-full.txt Illustrates the creation of a multi-tool agent using the OpenAI Python client. It defines available tools (search_database, send_email) with their parameters and demonstrates how the model can choose and utilize these tools based on a user's request. ```python from openai import OpenAI import json client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) tools = [ { "type": "function", "function": { "name": "search_database", "description": "Search a database for records matching a query", "parameters": { "type": "object", "properties": { "query": {"type": "string", "description": "Search query"}, "table": {"type": "string", "enum": ["users", "orders", "products"]}, "limit": {"type": "integer", "description": "Max results", "default": 10} }, "required": ["query", "table"] } } }, { "type": "function", "function": { "name": "send_email", "description": "Send an email to a recipient", "parameters": { "type": "object", "properties": { "to": {"type": "string", "description": "Recipient email"}, "subject": {"type": "string", "description": "Email subject"}, "body": {"type": "string", "description": "Email body text"} }, "required": ["to", "subject", "body"] } } } ] messages = [ {"role": "user", "content": "Find the top 5 users by order count and send a summary to admin@company.com"} ] response = client.chat.completions.create( model="claude-sonnet-4-6", messages=messages, tools=tools, tool_choice="auto" ) # Process tool calls message = response.choices[0].message if message.tool_calls: for tc in message.tool_calls: print(f"Tool: {tc.function.name}") print(f"Args: {json.dumps(json.loads(tc.function.arguments), indent=2)}") ``` -------------------------------- ### Python: Basic Chat Completion with OpenAI Client Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/llms-full.txt Demonstrates how to perform a basic chat completion request using the OpenAI Python client. It configures the client with a base URL and API key, sends a system message and a user prompt, and prints the assistant's response and token usage. ```python from openai import OpenAI client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) response = client.chat.completions.create( model="claude-sonnet-4-6", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Explain quantum computing in simple terms."} ], temperature=0.7, max_tokens=1024 ) print(response.choices[0].message.content) print(f"Tokens used: {response.usage.total_tokens}") ``` -------------------------------- ### Configure Open WebUI for Brainiall Models Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md This command demonstrates how to run Open WebUI as a Docker container, configuring it to use Brainiall models. It maps the container's port and sets environment variables for the OpenAI API base URLs and keys. ```bash docker run -d -p 3000:8080 \ -e OPENAI_API_BASE_URLS="https://apim-ai-apis.azure-api.net/v1" \ -e OPENAI_API_KEYS="YOUR_KEY" \ ghcr.io/open-webui/open-webui:main ``` -------------------------------- ### JSON Mode / Structured Outputs Source: https://context7.com/fasuizu-br/brainiall-llm-gateway/llms.txt This section explains how to enforce JSON output from the model by setting `response_format`. It includes a Python example demonstrating its usage. ```APIDOC ## JSON Mode / Structured Outputs Forces the model to return valid JSON by setting `response_format: {"type": "json_object"}`. For strict schema enforcement, use `{"type": "json_schema", "json_schema": {...}}` with a complete JSON Schema definition. The model's response is guaranteed to be parseable JSON. ### Request Example ```python from openai import OpenAI import json client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) response = client.chat.completions.create( model="claude-sonnet-4-6", messages=[ {"role": "system", "content": "Extract structured data from the user's text. Return valid JSON."}, {"role": "user", "content": "John Smith is 35 years old and works at Google as a senior engineer in Mountain View."} ], response_format={"type": "json_object"}, max_tokens=256 ) data = json.loads(response.choices[0].message.content) print(json.dumps(data, indent=2)) ``` ### Response Example ```json { "name": "John Smith", "age": 35, "company": "Google", "title": "Senior Engineer", "location": "Mountain View" } ``` ``` -------------------------------- ### Python: Streaming with Token Counting Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/llms-full.txt Shows how to use streaming to receive tokens as they are generated and count the total characters in the response. Uses the 'deepseek-r1' model. ```APIDOC ## POST /v1/chat/completions (Streaming) ### Description Sends a chat completion request with streaming enabled, allowing for real-time token delivery. Useful for interactive applications. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Query Parameters None #### Request Body - **model** (string) - Required - The model to use for completion. - **messages** (array) - Required - A list of message objects representing the conversation history. - **stream** (boolean) - Required - Set to `true` to enable streaming. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate. ### Request Example ```json { "model": "deepseek-r1", "messages": [{"role": "user", "content": "Solve: what is 25! / 23!"}], "stream": true, "max_tokens": 2048 } ``` ### Response #### Success Response (200) - **choices** (array) - A list of streaming choices. - **delta** (object) - The content delta for the current chunk. - **content** (string) - The content of the assistant's reply for this chunk. #### Response Example (each chunk) ```json { "choices": [ { "delta": { "content": "The result is " } } ] } ``` ```json { "choices": [ { "delta": { "content": "552" } } ] } ``` ``` -------------------------------- ### Python: Implement Multi-Tool Agent Loop for Iterative Task Completion with OpenAI API Source: https://context7.com/fasuizu-br/brainiall-llm-gateway/llms.txt Shows how to create an agentic workflow where the model can call multiple tools iteratively to complete a task. Tool results are fed back into the conversation, allowing the model to reason and make follow-up calls until a final response is generated. ```python from openai import OpenAI import json client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) tools = [ { "type": "function", "function": { "name": "search_products", "description": "Search for products in the catalog", "parameters": { "type": "object", "properties": { "query": {"type": "string", "description": "Search query"}, "category": {"type": "string", "enum": ["electronics", "clothing", "books"]}, "max_price": {"type": "number", "description": "Maximum price in USD"} }, "required": ["query"] } } }, { "type": "function", "function": { "name": "add_to_cart", "description": "Add a product to the shopping cart", "parameters": { "type": "object", "properties": { "product_id": {"type": "string"}, "quantity": {"type": "integer", "default": 1} }, "required": ["product_id"] } } } ] def execute_tool(name, args): if name == "search_products": return json.dumps({"results": [{"id": "prod-001", "name": "Wireless Headphones", "price": 79.99}]}) elif name == "add_to_cart": return json.dumps({"success": True, "cart_total": 79.99}) return json.dumps({"error": "Unknown tool"}) messages = [ {"role": "system", "content": "You are a shopping assistant."}, {"role": "user", "content": "Find wireless headphones under $100 and add them to my cart"} ] for _ in range(5): # Max iterations response = client.chat.completions.create( model="claude-sonnet-4-6", messages=messages, tools=tools, tool_choice="auto" ) message = response.choices[0].message messages.append(message) if not message.tool_calls: print(f"Final: {message.content}") break for tc in message.tool_calls: result = execute_tool(tc.function.name, json.loads(tc.function.arguments)) print(f"Called {tc.function.name} -> {result}") messages.append({"role": "tool", "tool_call_id": tc.id, "content": result}) ``` -------------------------------- ### Continue.dev Configuration for Brainiall Models Source: https://context7.com/fasuizu-br/brainiall-llm-gateway/llms.txt Configuration for Continue.dev VS Code extension to use Brainiall models for AI-assisted coding. Add models to `~/.continue/config.yaml` with the OpenAI provider and gateway base URL. Requires replacing `YOUR_KEY` with your actual API key. ```yaml # ~/.continue/config.yaml models: - model: claude-sonnet-4-6 title: Brainiall Claude Sonnet provider: openai apiBase: https://apim-ai-apis.azure-api.net/v1 apiKey: YOUR_KEY - model: claude-haiku-4-5 title: Brainiall Claude Haiku provider: openai apiBase: https://apim-ai-apis.azure-api.net/v1 apiKey: YOUR_KEY - model: deepseek-r1 title: Brainiall DeepSeek R1 provider: openai apiBase: https://apim-ai-apis.azure-api.net/v1 apiKey: YOUR_KEY ``` -------------------------------- ### JavaScript Tool Calling with OpenAI API Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md Demonstrates how to use the OpenAI client in JavaScript to enable tool calling. This allows the LLM to invoke functions based on user queries, requiring the 'openai' package and an API key. ```javascript import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://apim-ai-apis.azure-api.net/v1", apiKey: "YOUR_KEY", }); const tools = [ { type: "function", function: { name: "get_weather", description: "Get the current weather for a location", parameters: { type: "object", properties: { location: { type: "string", description: "City name, e.g. 'San Francisco, CA'", }, unit: { type: "string", enum: ["celsius", "fahrenheit"], description: "Temperature unit", }, }, required: ["location"], }, }, }, ]; const response = await client.chat.completions.create({ model: "claude-sonnet-4-6", messages: [{ role: "user", content: "What's the weather in Tokyo?" }], tools: tools, tool_choice: "auto", }); const message = response.choices[0].message; if (message.tool_calls) { for (const toolCall of message.tool_calls) { console.log(`Function: ${toolCall.function.name}`); console.log(`Arguments: ${toolCall.function.arguments}`); } } ``` -------------------------------- ### Configure Continue.dev for Brainiall Models Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md This configuration allows Continue.dev to use Brainiall's Claude Sonnet and Haiku models. It specifies the model names, a display title, the provider as 'openai', the API base URL, and requires your API key. ```yaml models: - model: claude-sonnet-4-6 title: Brainiall Claude Sonnet provider: openai apiBase: https://apim-ai-apis.azure-api.net/v1 apiKey: YOUR_KEY - model: claude-haiku-4-5 title: Brainiall Claude Haiku provider: openai apiBase: https://apim-ai-apis.azure-api.net/v1 apiKey: YOUR_KEY ``` -------------------------------- ### List Available Models with Python Source: https://context7.com/fasuizu-br/brainiall-llm-gateway/llms.txt Provides a Python script to list all available models supported by the gateway. It uses the `client.models.list()` method and prints the model ID and its provider. This helps in selecting the appropriate model for API requests. ```python from openai import OpenAI client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) models = client.models.list() for model in sorted(models.data, key=lambda m: m.id): print(f"{model.id:40s} — {model.owned_by}") # Output: # claude-haiku-4-5 — anthropic # claude-opus-4-5 — anthropic # claude-opus-4-6 — anthropic # claude-sonnet-4-6 — anthropic # deepseek-r1 — deepseek # deepseek-v3 — deepseek # llama-3.1-405b — meta # llama-3.1-70b — meta # llama-3.1-8b — meta # ... ``` -------------------------------- ### POST /v1/chat/completions Source: https://context7.com/fasuizu-br/brainiall-llm-gateway/llms.txt Creates a chat completion with the specified model. Supports streaming responses via Server-Sent Events (SSE) when `stream: true`. Request parameters include `model` (required), `messages` array (required), `temperature`, `max_tokens`, `tools` for function calling, `response_format` for JSON mode, and `top_p` for nucleus sampling. Returns completion with `id`, `choices` array containing the assistant message, and `usage` statistics. ```APIDOC ## POST /v1/chat/completions ### Description Creates a chat completion with the specified model. Supports streaming responses via Server-Sent Events (SSE) when `stream: true`. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Query Parameters None #### Request Body - **model** (string) - Required - The model to use for chat completions. - **messages** (array) - Required - An array of message objects representing the conversation history. - **role** (string) - Required - The role of the author of the message (e.g., `system`, `user`, `assistant`). - **content** (string) - Required - The content of the message. - **temperature** (number) - Optional - Controls randomness. Lower values make the output more focused and deterministic. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate in the completion. - **tools** (array) - Optional - A list of tools the model may call. - **response_format** (object) - Optional - Controls the output format. Use `{"type": "json_object"}` for JSON mode. - **top_p** (number) - Optional - Controls nucleus sampling. An alternative to sampling with temperature, where the model considers the smallest set of tokens whose cumulative probability exceeds `top_p`. ### Request Example ```json { "model": "claude-sonnet-4-6", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Explain quantum computing in simple terms."} ], "temperature": 0.7, "max_tokens": 1024 } ``` ### Response #### Success Response (200) - **id** (string) - Unique identifier for the completion. - **choices** (array) - An array of completion choices. - **message** (object) - The message content from the assistant. - **role** (string) - The role of the author (assistant). - **content** (string) - The content of the assistant's message. - **usage** (object) - Usage statistics for the completion. - **prompt_tokens** (integer) - Number of tokens in the prompt. - **completion_tokens** (integer) - Number of tokens in the completion. - **total_tokens** (integer) - Total number of tokens used. #### Response Example ```json { "id": "chatcmpl-12345", "choices": [ { "message": { "role": "assistant", "content": "Quantum computing uses quantum-mechanical phenomena like superposition and entanglement to perform calculations." } } ], "usage": { "prompt_tokens": 30, "completion_tokens": 25, "total_tokens": 55 } } ``` ``` -------------------------------- ### List Available Models via cURL and Python Source: https://context7.com/fasuizu-br/brainiall-llm-gateway/llms.txt This snippet demonstrates how to list all available models from the Brainiall LLM Gateway using a cURL command and a Python script for parsing the JSON output. It requires an API key for authentication and outputs a sorted list of model IDs and their owners. ```shell curl -s https://apim-ai-apis.azure-api.net/v1/models \ -H "Authorization: Bearer YOUR_KEY" | python3 -c " import json, sys data = json.load(sys.stdin) for m in sorted(data['data'], key=lambda x: x['id']): print(f"{m['id']:40s} {m['owned_by']}") " ``` -------------------------------- ### Integrate Vercel AI SDK with Brainiall Models (TypeScript) Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md This TypeScript code demonstrates integrating the Vercel AI SDK with Brainiall models. It uses `createOpenAI` to configure the SDK with the Brainiall API endpoint and key, then generates text using a specified model. ```typescript import { createOpenAI } from "@ai-sdk/openai"; import { generateText } from "ai"; const brainiall = createOpenAI({ baseURL: "https://apim-ai-apis.azure-api.net/v1", apiKey: "YOUR_KEY", }); const { text } = await generateText({ model: brainiall("claude-sonnet-4-6"), prompt: "Write a TypeScript function to reverse a string.", }); console.log(text); ``` -------------------------------- ### Python Vision (Image Inputs) with OpenAI API Source: https://github.com/fasuizu-br/brainiall-llm-gateway/blob/main/README.md Illustrates how to send image inputs along with text prompts to the OpenAI API using Python. This enables visual understanding capabilities, requiring the 'openai' package and an API key. ```python from openai import OpenAI client = OpenAI( base_url="https://apim-ai-apis.azure-api.net/v1", api_key="YOUR_KEY" ) response = client.chat.completions.create( model="claude-sonnet-4-6", messages=[ { "role": "user", "content": [ {"type": "text", "text": "What do you see in this image?"}, { "type": "image_url", "image_url": { "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Camponotus_flavomarginatus_ant.jpg/320px-Camponotus_flavomarginatus_ant.jpg" } } ] } ] ) print(response.choices[0].message.content) ```