### JavaScript: Streaming Chat Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Provides an example of streaming chat completions using the OpenAI JavaScript client. ```APIDOC ## POST /v1/chat/completions (JavaScript Streaming) ### Description Streams chat completion responses using the OpenAI JavaScript client, processing each chunk as it arrives. ### 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. - **stream** (boolean) - Required - Set to true to enable streaming. ### Request Example ```javascript const stream = await client.chat.completions.create({ model: "claude-sonnet-4-6", messages: [{ role: "user", content: "Write a Python function to implement binary search" }], stream: true, }); ``` ### Response #### Success Response (200) - **choices** (array) - A list of stream chunks. Each chunk may contain: - **delta** (object) - The content delta, which might include: - **content** (string) - A piece of the generated text. #### Response Example (Processing Chunks) ```javascript let fullResponse = ""; for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content; if (content) { fullResponse += content; process.stdout.write(content); // Example: print to console } } console.log(`\n---`); console.log(`Response length: ${fullResponse.length} chars`); ``` ``` -------------------------------- ### Vision Analysis Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Utilize multimodal models to analyze images. Provide an image URL along with a text prompt to get a detailed description or answer questions about the image. ```APIDOC ## POST /v1/chat/completions (with image input) ### Description Analyzes images by sending them as part of the message content to a multimodal model. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Request Body - **model** (string) - Required - The ID of a multimodal model (e.g., "claude-sonnet-4-6"). - **messages** (array) - Required - A list of messages. The user message should contain both 'text' and 'image_url' types. - **content** (array) - Contains message parts. - **type** (string) - "text" or "image_url". - **text** (string) - The text prompt. - **image_url** (object) - Contains the URL of the image. - **url** (string) - The URL of the image to analyze. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate. ### Request Example ```json { "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 } ``` ### Response #### Success Response (200) - **choices** (array) - A list of completion choices, where the assistant's message contains the analysis of the image. #### Response Example ```json { "choices": [ { "message": { "role": "assistant", "content": "The image shows a serene landscape with rolling hills under a clear blue sky..." } } ] } ``` ``` -------------------------------- ### Python: Basic Chat Completion with OpenAI Client Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Demonstrates how to perform a basic chat completion request using the OpenAI Python client. It sends a system message and a user prompt, then prints the assistant's response and token usage. Requires the 'openai' library. ```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}") ``` -------------------------------- ### Python: Multi-Tool Agent Implementation Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Illustrates how to build a multi-tool agent using the OpenAI Python client. It defines two tools (search_database and send_email) and processes tool calls from the model's response. This enables the LLM to interact with external functions. Requires 'openai' and 'json' libraries. ```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: RAG Pipeline with LangChain Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt This Python snippet demonstrates setting up a Retrieval-Augmented Generation (RAG) pipeline using LangChain. It configures `ChatOpenAI` with a custom base URL and API key, defines a prompt template, and invokes the chain with context and a question. ```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) ``` -------------------------------- ### Python: Multi-Tool Agent Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Illustrates how to use the chat completions API with tools (functions) for agent-like behavior, enabling the model to call predefined functions. ```APIDOC ## POST /v1/chat/completions (Tool Use) ### Description Enables the model to use predefined tools (functions) to accomplish tasks. The model can decide which tool to call and with what arguments based on the user's request. ### 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. - **tools** (array) - Required - A list of tool definitions, each specifying the function's name, description, and parameters. - **tool_choice** (string) - Optional - Controls how the model chooses tools. 'auto' lets the model decide. ### 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) - Contains the model's response. If a tool is called: - **message** (object) - Contains: - **tool_calls** (array) - A list of tool calls the model decided to make. - **function** (object) - Details of the function call: - **name** (string) - The 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", "type": "function", "function": { "name": "search_database", "arguments": "{\"query\": \"top 5 users by order count\", \"table\": \"users\", \"limit\": 5}" } }, { "id": "call_def456", "type": "function", "function": { "name": "send_email", "arguments": "{\"to\": \"admin@company.com\", \"subject\": \"Top Users Summary\", \"body\": \"Summary of top 5 users...\"}" } } ] } } ] } ``` ``` -------------------------------- ### Python: Streaming Chat Completion with Token Counting Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Shows how to receive chat completion responses in a streaming fashion using the OpenAI Python client. It prints each token as it arrives and calculates the total character length of the response. Useful for real-time applications. Requires the 'openai' library. ```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: Vision Analysis with OpenAI SDK Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt This snippet demonstrates how to perform vision analysis using the OpenAI JavaScript SDK. It requires the 'openai' package and sends a user query along with an image URL to the specified API endpoint. ```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); ``` -------------------------------- ### Python: Basic Chat Completion Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Demonstrates a basic chat completion request using the OpenAI Python client, targeting a specific model and providing system and user messages. ```APIDOC ## POST /v1/chat/completions ### Description Performs a basic chat completion request. ### 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, each with a 'role' and 'content'. - **temperature** (number) - Optional - Controls randomness. Lower values make output more focused and deterministic. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate in the completion. ### 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) - **choices** (array) - A list of completion choices. Each choice contains: - **message** (object) - The generated message, with 'role' and 'content'. - **usage** (object) - Information about token usage, including 'total_tokens'. #### Response Example ```json { "choices": [ { "message": { "role": "assistant", "content": "Quantum computing is a new type of computing that uses the principles of quantum mechanics to solve complex problems that are intractable for classical computers." }, "usage": { "total_tokens": 50 } } ] } ``` ``` -------------------------------- ### Python: Streaming with Token Counting Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Shows how to stream chat completion responses and count the total characters in the streamed output. ```APIDOC ## POST /v1/chat/completions (Streaming) ### Description Performs a chat completion request with streaming enabled, allowing for real-time response generation. ### 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, each with a 'role' and 'content'. - **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 stream chunks. Each chunk may contain: - **delta** (object) - The content delta, which might include: - **content** (string) - A piece of the generated text. #### Response Example (Streaming Chunks) ```json { "choices": [ { "delta": { "content": "The result is " } } ] } ``` ```json { "choices": [ { "delta": { "content": "600." } } ] } ``` ``` -------------------------------- ### JavaScript: Streaming Chat Completion Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Demonstrates how to perform a streaming chat completion request using the OpenAI JavaScript client. It asynchronously iterates over the stream, processes content chunks, and logs the total response length. Requires the 'openai' library. ```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"); ``` -------------------------------- ### List Models API Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Retrieves a list of all available models that can be used with the API. ```APIDOC ## GET /v1/models ### Description Lists all available models that can be used for inference. ### Method GET ### Endpoint /v1/models ### Parameters None ### Request Example (No request body or specific parameters needed for this GET request) ### Response #### Success Response (200) - **data** (array) - A list of model objects. Each object contains: - **id** (string) - The unique identifier for the model. - **owned_by** (string) - The entity that owns the model. #### Response Example ```json { "data": [ { "id": "claude-sonnet-4-6", "owned_by": "brainiall" }, { "id": "claude-haiku-4-5", "owned_by": "brainiall" } ] } ``` ``` -------------------------------- ### TypeScript: Next.js Route with Vercel AI SDK Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt This TypeScript code defines a Next.js API route using the Vercel AI SDK to handle chat messages. It configures the `createOpenAI` client with the BrainiAI endpoint and streams text responses. ```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(); } ``` -------------------------------- ### Curl: Chat Completion and Model Listing Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt This section provides curl commands for interacting with the chat completion API, including basic requests, streaming responses, and listing available models. It requires `curl` and `python3` for JSON formatting. ```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?"}] , "max_tokens": 100 }' | 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-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']}\") " ``` -------------------------------- ### Models API Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Endpoint for listing all available language models supported by the gateway. ```APIDOC ## GET /v1/models ### Description List all available models. ### Method GET ### Endpoint /v1/models ### Response #### Success Response (200) - **object** (string) - "list" - **data** (array) - Array of model objects - **id** (string) - Model ID - **object** (string) - "model" - **owned_by** (string) - Provider name #### Response Example ```json { "object": "list", "data": [ { "id": "claude-opus-4-6", "object": "model", "owned_by": "Anthropic" }, { "id": "llama-3.3-70b", "object": "model", "owned_by": "Meta" } ] } ``` ``` -------------------------------- ### Chat Completions API Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt This endpoint allows you to interact with various language models for chat-based conversations. You can send messages, specify models, and control generation parameters like max tokens and streaming. ```APIDOC ## POST /v1/chat/completions ### Description Performs chat completions using specified language models. Supports text and image inputs for multimodal models. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Query Parameters - **stream** (boolean) - Optional - If true, streams the response. #### Request Body - **model** (string) - Required - The ID of the model to use. - **messages** (array) - Required - A list of messages comprising the conversation. Each message has a 'role' (system, user, or assistant) and 'content'. For multimodal models, content can include text and image URLs. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate. ### Request Example ```json { "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 } ``` ### Response #### Success Response (200) - **choices** (array) - A list of completion choices. Each choice contains a 'message' with 'role' and 'content'. #### Response Example ```json { "choices": [ { "message": { "role": "assistant", "content": "The capital of France is Paris." } } ] } ``` ``` -------------------------------- ### Chat Completions API Source: https://raw.githubusercontent.com/fasuizu-br/brainiall-llm-gateway/main/llms-full.txt Endpoint for creating chat completions, supporting streaming, tool/function calling, and structured output formats. ```APIDOC ## POST /v1/chat/completions ### Description Create a chat completion. Supports streaming via `stream: true`. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Request Body - **model** (string) - Required - Model ID from the models list - **messages** (array) - Required - Array of message objects with role and content - **stream** (boolean) - Optional - Enable streaming (default: false) - **temperature** (number) - Optional - Sampling temperature 0-2 (default: 1) - **max_tokens** (integer) - Optional - Maximum tokens to generate - **tools** (array) - Optional - List of tool definitions for function calling - **tool_choice** (string/object) - Optional - Tool selection strategy ("auto", "none", "required", or specific) - **response_format** (object) - Optional - Force JSON output with {"type": "json_object"} or {"type": "json_schema", "json_schema": {...}} - **top_p** (number) - Optional - Nucleus sampling parameter - **stop** (string/array) - Optional - Stop sequences ### Request Example ```json { "model": "claude-opus-4-6", "messages": [ {"role": "user", "content": "Hello!"} ], "stream": false } ``` ### Response #### Success Response (200) - **id** (string) - Unique completion ID - **object** (string) - "chat.completion" - **model** (string) - Model used - **choices** (array) - Array of choice objects - **index** (integer) - Choice index - **message** (object) - Assistant message with role, content, and optional tool_calls - **finish_reason** (string) - "stop", "tool_calls", "length", or "content_filter" - **usage** (object) - Token usage with prompt_tokens, completion_tokens, total_tokens #### Response Example ```json { "id": "chatcmpl-12345", "object": "chat.completion", "model": "claude-opus-4-6", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Hello there! How can I help you today?" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 15, "total_tokens": 25 } } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.