### Install OpenAI SDK Source: https://interfaze.ai/docs/openai Installs the OpenAI SDK using either npm or yarn package managers. This is the first step to begin integrating Interfaze AI. ```bash npm install openai # or yarn add openai ``` -------------------------------- ### Guardrail System Prompt Examples (Text) Source: https://interfaze.ai/docs/guard-rails Provides examples of system prompts for enabling various combinations of guardrail safety categories. These examples illustrate how to activate basic safety, comprehensive filtering, image NSFW detection, and custom combinations. ```text *GUARD* S1, S2, S3, S10, S11 *GUARD* ``` ```text *GUARD* S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14 *GUARD* ``` ```text *GUARD* S12_IMAGE *GUARD* ``` ```text *GUARD* S1, S3, S10, S12_IMAGE *GUARD* ``` -------------------------------- ### OpenAI SDK Basic Setup Source: https://interfaze.ai/docs/openai Set up the OpenAI SDK to connect with Interfaze AI by providing the base URL and your API key. ```APIDOC ## Basic Setup ```typescript import OpenAI from "openai"; const interfaze = new OpenAI({ baseURL: "https://api.interfaze.ai/v1", apiKey: "" }); ``` ``` -------------------------------- ### Basic OpenAI SDK Setup for Interfaze AI Source: https://interfaze.ai/docs/openai Initializes the OpenAI SDK with the Interfaze AI base URL and API key. This setup is crucial for making authenticated requests to Interfaze AI's services. ```typescript import OpenAI from "openai"; const interfaze = new OpenAI({ baseURL: "https://api.interfaze.ai/v1", apiKey: "" }); ``` -------------------------------- ### Install Vercel AI SDK for Interfaze Source: https://interfaze.ai/docs/vercel-ai-sdk Installs the necessary Vercel AI SDK packages for integrating with Interfaze AI. This includes the core 'ai' package and the OpenAI provider. ```bash npm install ai @ai-sdk/openai # or yarn add ai @ai-sdk/openai ``` -------------------------------- ### Install LangChain Dependencies for Interfaze AI Source: https://interfaze.ai/docs/langchain Installs the necessary LangChain packages for integrating with Interfaze AI. This includes the OpenAI and core LangChain libraries. Ensure you have Node.js and npm or yarn installed. ```bash npm install @langchain/openai @langchain/core # or yarn add @langchain/openai @langchain/core ``` -------------------------------- ### Define Tools and Call Functions with OpenAI SDK (TypeScript) Source: https://interfaze.ai/docs/function-calling This TypeScript example demonstrates how to define a list of callable tools for the Interfaze model, make a tool call, and process the tool's output to get a final response. It covers defining tool schemas, sending messages to the model, and handling assistant responses that include tool calls. ```typescript const tools = [ { type: "function" as const, function: { name: "get_horoscope", description: "Get today's horoscope for an astrological sign.", parameters: { type: "object", properties: { sign: { type: "string", description: "An astrological sign like Taurus or Aquarius", }, }, required: ["sign"], }, }, } ]; let messages: any[] = [ { role: "user" as const, content: "Get my horoscope for Taurus", } ]; // Step 2: Get tool call from model const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages, tools, tool_choice: "auto", }); // Step 3: Extract tool call and execute function const assistantMessage = response.choices[0].message; messages.push({ role: "assistant", content: "", tool_calls: response.choices[0].message.tool_calls, }); if (assistantMessage.tool_calls && assistantMessage.tool_calls.length > 0) { const toolCall = assistantMessage.tool_calls[0] as any; const args = JSON.parse(toolCall.function.arguments); const result = `Today's horoscope for ${args.sign}: You will have a great day!`; messages.push({ role: "tool" as const, tool_call_id: toolCall.id, content: result, }); const finalResponse = await interfaze.chat.completions.create({ model: "interfaze-beta", messages, tools, tool_choice: "auto", }); console.log(finalResponse.choices[0].message.content); } ``` -------------------------------- ### Basic Interfaze Setup with Vercel AI SDK Source: https://interfaze.ai/docs/vercel-ai-sdk Initializes the Interfaze AI client using the Vercel AI SDK. It configures the API base URL and requires your Interfaze API key. ```typescript import { createOpenAI } from "@ai-sdk/openai"; import { generateText } from "ai"; const interfaze = createOpenAI({ baseURL: "https://api.interfaze.ai/v1", apiKey: "" }); ``` -------------------------------- ### Basic Interfaze AI Chat Model Setup with LangChain Source: https://interfaze.ai/docs/langchain Demonstrates the basic setup for using an Interfaze AI chat model within LangChain. It initializes the ChatOpenAI class with the Interfaze API base URL and model name. Requires an Interfaze API key. ```typescript import { ChatOpenAI } from "@langchain/openai"; const interfaze = new ChatOpenAI({ configuration: { baseURL: "https://api.interfaze.ai/v1", }, apiKey: "" model: "interfaze-beta", }); ``` -------------------------------- ### Function Calling with Interfaze AI and LangChain Source: https://interfaze.ai/docs/langchain Illustrates how to implement function calling with Interfaze AI models using LangChain. This example defines a tool for fetching horoscopes, invokes the model to get a tool call, executes the function, and provides the result back to the model for a final response. Requires API key. ```typescript import { ChatOpenAI } from "@langchain/openai"; import { HumanMessage } from "@langchain/core/messages"; const interfaze = new ChatOpenAI({ baseURL: "https://api.interfaze.ai/v1", apiKey: "", model: "interfaze-beta" }); // STEP 1. Define tools const tools = [ { type: "function" as const, function: { name: "get_horoscope", description: "Get today's horoscope for an astrological sign.", parameters: { type: "object", properties: { sign: { type: "string", description: "An astrological sign like Taurus or Aquarius", }, }, required: ["sign"], }, }, } ]; // Step 2: Get tool call from model const response = await interfaze.invoke( [new HumanMessage("Get my horoscope for Taurus")], { tools: tools, tool_choice: "auto", }, ); // Step 3: Check if tool was called and execute function if ( response.additional_kwargs.tool_calls && response.additional_kwargs.tool_calls.length > 0 ) { const toolCall = response.additional_kwargs.tool_calls[0]; const args = JSON.parse(toolCall.function.arguments); // Execute the function const result = `Today's horoscope for ${args.sign}: You will have a great day!`; // Step 4: Provide result back to model const finalResponse = await interfaze.invoke( [ new HumanMessage("Get my horoscope for Taurus"), response, { role: "tool", tool_call_id: toolCall.id, content: result, }, ], { tools: tools, tool_choice: "auto", }, ); console.log("Final response:", finalResponse.content); } ``` -------------------------------- ### Generate Text with OpenAI SDK (TypeScript/Python) Source: https://interfaze.ai/docs/openai Demonstrates how to generate normal text completions using the Interfaze AI service via the OpenAI SDK. This example shows a simple chat completion request. ```typescript const completion = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: "Write a short story about a robot learning to paint" } ] }); console.log(completion.choices[0].message.content); ``` -------------------------------- ### Perform Complex Reasoning with OpenAI SDK (TypeScript) Source: https://interfaze.ai/docs/reasoning This TypeScript example demonstrates how to use the Interfaze OpenAI SDK to perform complex multi-step reasoning. It sends a user query to the 'interfaze-beta' model with a 'high' reasoning effort and logs the reasoning tokens and reasoning output. No external dependencies beyond the Interfaze SDK are explicitly mentioned. ```typescript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: "Explain why renewable energy is important for combating climate change. Consider economic, environmental, and social factors." } ], reasoning_effort: "high", }); console.log(response.usage?.completion_tokens_details?.reasoning_tokens); console.log(response?.reasoning); ``` -------------------------------- ### Interfaze AI System Message OpenAI SDK Request Source: https://interfaze.ai/docs/index Illustrates how to use system messages with the OpenAI SDK in TypeScript to customize model behavior. This includes setting instructions for the model, such as providing code examples and explanations. ```typescript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "system", content: "You are a helpful coding assistant. Always provide code examples and explain your reasoning." }, { role: "user", content: "How do I implement a binary search in Python?" } ], }); ``` -------------------------------- ### Function Calling with OpenAI SDK (TypeScript) Source: https://interfaze.ai/docs/openai Illustrates how to implement function calling with Interfaze AI using the OpenAI SDK. This example defines a tool for retrieving horoscopes and processes the model's response. ```typescript // STEP 1. Define a list of callable tools for the model const tools = [ { type: "function" as const, function: { name: "get_horoscope", description: "Get today's horoscope for an astrological sign.", parameters: { type: "object", properties: { sign: { type: "string", description: "An astrological sign like Taurus or Aquarius", }, }, required: ["sign"], }, }, } ]; let messages: any[] = [ { role: "user" as const, content: "Get my horoscope for Taurus", }, ]; // Step 2: Get tool call from model const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages, tools, tool_choice: "auto", }); // Step 3: Extract tool call and execute function const assistantMessage = response.choices[0].message; messages.push({ role: "assistant", content: "", tool_calls: response.choices[0].message.tool_calls, }); if (assistantMessage.tool_calls && assistantMessage.tool_calls.length > 0) { const toolCall = assistantMessage.tool_calls[0] as any; const args = JSON.parse(toolCall.function.arguments); const result = `Today's horoscope for ${args.sign}: You will have a great day!`; messages.push({ role: "tool" as const, tool_call_id: toolCall.id, content: result, }); const finalResponse = await interfaze.chat.completions.create({ model: "interfaze-beta", messages, tools, tool_choice: "auto", }); console.log(finalResponse.choices[0].message.content); } ``` -------------------------------- ### Access Precontext Data with OpenAI SDK (Python) Source: https://interfaze.ai/docs/precontext Shows how to retrieve precontext information, like OCR outputs, from Interfaze AI responses using the OpenAI SDK in Python. This example details sending a multimodal message and then extracting the process name and results from the `precontext` array. ```python response = client.chat.completions.create( model="interfaze-beta", messages=[ { "role": "user", "content": [ {"type": "text", "text": "Extract total price from this receipt"}, { "type": "image_url", "image_url": { "url": "https://jigsawstack.com/preview/vocr-example.jpg", }, }, ], } ], ) precontext = response.precontext print(f"OCR Results: {precontext[0]['result']}") print(f"Process used: {precontext[0]['name']}") ``` -------------------------------- ### Function Calling with Vercel AI SDK and Interfaze Source: https://interfaze.ai/docs/vercel-ai-sdk Illustrates how to enable function calling with Interfaze AI via the Vercel AI SDK. This example defines a 'weather' tool and processes the AI's response based on it. ```typescript const { text } = await generateText({ model: interfaze.chat("interfaze-beta"), prompt: "What's the weather like in San Francisco?", tools: { weather: tool({ description: "Get the weather in a location", parameters: z.object({ location: z.string().describe("The location to get the weather for"), }), execute: async ({ location }) => ({ location, temperature: 72 + Math.floor(Math.random() * 21) - 10, }), }), }, stopWhen: stepCountIs(5), toolChoice: "auto", }); console.log(text); ``` -------------------------------- ### Transcribe Audio with OpenAI SDK using TypeScript Source: https://interfaze.ai/docs/speech-to-text Demonstrates how to transcribe an audio file using the Interfaze AI's chat completions endpoint with the OpenAI SDK in TypeScript. It takes a user message containing text and a file object with the audio file's URL as input and outputs the transcribed text. Ensure the Interfaze SDK is installed and configured. ```typescript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: [ { type: "text", text: "Transcribe this audio file" }, { type: "file", file: { filename: "stt-example.wav", file_data: "https://jigsawstack.com/preview/stt-example.wav", }, }, ], }, ], }); console.log(response.choices[0].message.content); ``` -------------------------------- ### Detect Objects in Images using Interfaze AI SDK Source: https://interfaze.ai/docs/vision This code example shows how to use the Interfaze AI SDK for object detection within an image. It identifies and classifies objects, providing bounding boxes and metadata for each detected entity. The input includes a natural language query and an image URL. ```typescript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: [ { type: "text", text: "Get the things, I can drink with. " }, { type: "image_url", image_url: { url: "https://rogilvkqloanxtvjfrkm.supabase.co/storage/v1/object/public/demo/Collabo%201080x842.jpg?t=2024-03-22T09%3A22%3A48.442Z", }, }, ], }, ], }); console.log(response.choices[0].message.content); ``` -------------------------------- ### Chat Model Usage with Interfaze AI and LangChain Source: https://interfaze.ai/docs/langchain Shows how to use the Interfaze AI chat model for conversational interactions using LangChain. It sends system and human messages and logs the model's response content. Requires API key and Interfaze model setup. ```typescript import { ChatOpenAI } from "@langchain/openai"; import { HumanMessage, SystemMessage } from "@langchain/core/messages"; const interfaze = new ChatOpenAI({ baseURL: "https://api.interfaze.ai/v1", apiKey: "", model: "interfaze-beta" }); const response = await interfaze.invoke([ new SystemMessage("You are a helpful assistant."), new HumanMessage("Write a short story about a robot learning to paint") ]); console.log(response.content); ``` -------------------------------- ### OCR Structured Output for Receipt Total (TypeScript) Source: https://interfaze.ai/docs/structured-output Extracts the total price from a receipt image using structured output. This example combines OCR with Zod schemas to precisely capture numerical data from visual input. It depends on 'openai' and 'zod' packages. ```typescript import { z } from "zod"; import { zodResponseFormat } from "openai/helpers/zod"; const ReceiptSchema = z.object({ total_price: z.number().describe("Total price from the receipt"), }); const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: [ { type: "text", text: "Extract total price" }, { type: "image_url", image_url: { url: "https://media.istockphoto.com/id/1420767944/vector/register-sale-receipt-isolated-on-white-background-cash-receipt-printed.jpg?s=612x612&w=0&k=20&c=eV7CDJK0DZgKo7KVlGTDJeVMN_2xybqIPvt1ATl_kkM=", }, }, ], }, ], response_format: zodResponseFormat(ReceiptSchema, "receipt"), }); console.log(response.choices[0].message.content); // Response - {"total_price":29.69} ``` -------------------------------- ### Generate Complex Code with Interfaze AI SDK Source: https://interfaze.ai/docs/run-code This example shows how to use the Interfaze AI SDK to generate a Python script for a bouncing ball animation using Pygame. The request prompts the AI to create the code, which is then returned as a string. This showcases the AI's ability to perform complex code generation tasks. ```typescript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: "Create me a bouncing ball animation in python", } ], }); console.log(response.choices[0].message.content); /* Response - import sys import random import pygame def main(): # Initialize Pygame pygame.init() WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Bouncing Ball") # Colors BG = (18, 18, 18) BALL_COLOR = (52, 152, 219) # Ball properties radius = 25 x = WIDTH // 2 y = HEIGHT // 2 # Random initial velocity vx = random.choice([-5, -4, -3, 3, 4, 5]) vy = random.choice([-5, -4, -3, 3, 4, 5]) clock = pygame.time.Clock() FPS = 60 running = True while running: # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Update position x += vx y += vy # Bounce on walls if x - radius <= 0: x = radius vx = -vx elif x + radius >= WIDTH: x = WIDTH - radius vx = -vx if y - radius <= 0: y = radius vy = -vy elif y + radius >= HEIGHT: y = HEIGHT - radius vy = -vy # Draw screen.fill(BG) pygame.draw.circle(screen, BALL_COLOR, (int(x), int(y)), radius) pygame.display.flip() # Cap the frame rate clock.tick(FPS) pygame.quit() sys.exit() if __name__ == "__main__": main() */ ``` -------------------------------- ### Implementation Details Source: https://interfaze.ai/docs/guard-rails Details on how guardrails are implemented for text and image content. ```APIDOC ## Implementation ### Text Content Guarding When guardrails are enabled, the system automatically evaluates all text content against the specified safety categories. If content violates any enabled guard, the request will be blocked with an appropriate error message. ### Image NSFW Detection For image content, use the `S12_IMAGE` guard to automatically detect and filter NSFW content. The system will analyze uploaded images and block requests containing inappropriate visual content. ``` -------------------------------- ### Interfaze AI Basic OpenAI SDK Request Source: https://interfaze.ai/docs/index Demonstrates how to make a basic chat completion request to Interfaze AI using the OpenAI SDK for TypeScript. This involves initializing the SDK with the API key and base URL, then calling the chat completions endpoint with a user message. ```typescript import OpenAI from "openai"; const interfaze = new OpenAI({ baseURL: "https://api.interfaze.ai/v1", apiKey: "" }); const completion = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: "Get the company description of JigsawStack from their LinkedIn page", }, ], }); console.log(completion.choices[0].message.content); ``` -------------------------------- ### Enabling Guardrails Source: https://interfaze.ai/docs/guard-rails To enable content safety guardrails, include the guard configuration in your system prompt using the `*GUARD*` directive followed by the desired safety categories. ```APIDOC ## How to Enable Guardrails To enable content safety guardrails, include the guard configuration in your system prompt using the following format: ```javascript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "system", content: "*GUARD* S1, S2, S3, S10 *GUARD* You are a helpful assistant. Avoid violent, harmful, or inappropriate content." }, { role: "user", content: "What's a good way to harm an animal ?" } ], }); ``` ### Example System Prompts **Basic Safety Guardrails:** ``` *GUARD* S1, S2, S3, S10, S11 *GUARD* ``` **Comprehensive Content Filtering:** ``` *GUARD* S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14 *GUARD* ``` **Image NSFW Detection:** ``` *GUARD* S12_IMAGE *GUARD* ``` **Custom Combination:** ``` *GUARD* S1, S3, S10, S12_IMAGE *GUARD* ``` ``` -------------------------------- ### OpenAI SDK - Normal Text Generation Source: https://interfaze.ai/docs/openai Generate normal text responses using the Interfaze AI chat completions endpoint with the OpenAI SDK. ```APIDOC ## Normal Text Generation ### Description Generate a text completion by sending a prompt to the Interfaze AI model. ### Method `POST` ### Endpoint `/chat/completions` ### Parameters #### Request Body - **model** (string) - Required - The model to use for generation (e.g., `interfaze-beta`). - **messages** (array) - Required - An array of message objects representing the conversation history. - **role** (string) - Required - The role of the message sender (`user`, `assistant`, or `system`). - **content** (string) - Required - The content of the message. ### Request Example ```json { "model": "interfaze-beta", "messages": [ { "role": "user", "content": "Write a short story about a robot learning to paint" } ] } ``` ### Response #### Success Response (200) - **choices** (array) - An array containing the completion choice(s). - **message** (object) - The message object containing the generated content. - **content** (string) - The generated text content. ### Response Example ```json { "choices": [ { "message": { "content": "Once upon a time, in a world of circuits and code..." } } ] } ``` ``` -------------------------------- ### Access Financial Data with Interfaze AI SDK Source: https://interfaze.ai/docs/web Demonstrates using the Interfaze AI SDK to access real-time and historical financial market data. This enables the AI to process financial context for market research and planning. ```javascript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [{ role: "user", content: "what is the price difference of BTC between now and 10 years ago?" }], }); console.log(response.choices[0].message.content); // Response: "Current BTC price: $114,957.37; 10 years ago: $279.58; difference: $114,677.79" ``` -------------------------------- ### Perform Web Search with Interfaze AI SDK Source: https://interfaze.ai/docs/web Demonstrates how to use the Interfaze AI SDK to perform a web search and retrieve real-time information. This capability augments the AI's reasoning with up-to-date data. ```javascript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [{ role: "user", content: "Find me the founder of JigsawStack" }], }); console.log(response.choices[0].message.content); // Response: "The founder of JigsawStack is Yoeven D Khemlani." ``` -------------------------------- ### OpenAI SDK - Function Calling Source: https://interfaze.ai/docs/openai Implement function calling with Interfaze AI using the OpenAI SDK to enable models to call external functions. ```APIDOC ## Function Calling ### Description Enable the AI model to call external functions by defining tools that the model can invoke based on user input. This involves defining tools, getting tool calls from the model, executing the function, and sending the result back to the model. ### Method `POST` ### Endpoint `/chat/completions` ### Parameters #### Request Body - **model** (string) - Required - The model to use for generation (e.g., `interfaze-beta`). - **messages** (array) - Required - An array of message objects representing the conversation history. - **tools** (array) - Required - A list of tool definitions that the model can call. - **type** (string) - Required - The type of tool (e.g., `function`). - **function** (object) - Required - The function definition. - **name** (string) - Required - The name of the function. - **description** (string) - Optional - A description of what the function does. - **parameters** (object) - Optional - The parameters the function accepts. - **type** (string) - Required - The type of the parameters (e.g., `object`). - **properties** (object) - Optional - An object mapping parameter names to their schemas. - **required** (array) - Optional - An array of parameter names that are required. - **tool_choice** (string) - Optional - Controls how the model should be prompted to call a tool (`auto` or a specific tool name). ### Request Example (Initial Call) ```json { "model": "interfaze-beta", "messages": [ { "role": "user", "content": "Get my horoscope for Taurus" } ], "tools": [ { "type": "function" as const, "function": { "name": "get_horoscope", "description": "Get today's horoscope for an astrological sign.", "parameters": { "type": "object", "properties": { "sign": { "type": "string", "description": "An astrological sign like Taurus or Aquarius" } }, "required": ["sign"] } } } ], "tool_choice": "auto" } ``` ### Response (Model requesting tool call) #### Success Response (200) - **choices** (array) - An array containing the assistant's message. - **message** (object) - The message object. - **tool_calls** (array) - An array of tool calls requested by the model. - **id** (string) - The ID of the tool call. - **function** (object) - The function to call. - **name** (string) - The name of the function. - **arguments** (string) - A JSON string of arguments for the function. ### Request Example (After executing function) ```json { "model": "interfaze-beta", "messages": [ { "role": "user", "content": "Get my horoscope for Taurus" }, { "role": "assistant", "content": "", "tool_calls": [ { "id": "call_...", "function": { "name": "get_horoscope", "arguments": "{\"sign\": \"Taurus\"}" } } ] }, { "role": "tool", "tool_call_id": "call_...", "content": "Today's horoscope for Taurus: You will have a great day!" } ], "tools": [ { "type": "function" as const, "function": { "name": "get_horoscope", "description": "Get today's horoscope for an astrological sign.", "parameters": { "type": "object", "properties": { "sign": { "type": "string", "description": "An astrological sign like Taurus or Aquarius" } }, "required": ["sign"] } } } ], "tool_choice": "auto" } ``` ``` -------------------------------- ### Structured Output with OpenAI SDK (TypeScript) Source: https://interfaze.ai/docs/structured-output Demonstrates creating structured JSON output using Zod schemas with the OpenAI SDK. This is useful for predictable data parsing from AI responses, particularly for named entities. It requires the 'openai' package and 'zod'. ```typescript import { z } from "zod"; import { zodResponseFormat } from "openai/helpers/zod"; const founderSchema = z.object({ name: z.string().describe("The full name of the founder"), }); const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: "Who is the founder of JigsawStack?", }, ], response_format: zodResponseFormat(founderSchema, "founder_schema"), }); console.log(response.choices[0].message.content); ``` -------------------------------- ### OpenAI SDK - Streaming Text Generation Source: https://interfaze.ai/docs/openai Generate text responses in a streaming format using the Interfaze AI chat completions endpoint with the OpenAI SDK. ```APIDOC ## Streaming Text Generation ### Description Generate text completion in a streaming fashion, allowing for real-time updates as the content is generated. ### Method `POST` ### Endpoint `/chat/completions` ### Parameters #### Query Parameters - **stream** (boolean) - Required - Set to `true` to enable streaming. #### Request Body - **model** (string) - Required - The model to use for generation (e.g., `interfaze-beta`). - **messages** (array) - Required - An array of message objects representing the conversation history. - **role** (string) - Required - The role of the message sender (`user`, `assistant`, or `system`). - **content** (string) - Required - The content of the message. ### Request Example ```json { "model": "interfaze-beta", "messages": [ { "role": "user", "content": "Write a short story about a robot learning to paint" } ], "stream": true } ``` ### Response #### Success Response (200) - The response will be a stream of data chunks. - **choices** (array) - An array containing the completion chunk(s). - **delta** (object) - The delta object containing the content chunk. - **content** (string) - The generated text chunk. ### Response Example ```json // Example of a streamed chunk { "choices": [ { "delta": { "content": "Once upon a time..." } } ] } ``` ``` -------------------------------- ### Execute Code Script with Interfaze AI SDK Source: https://interfaze.ai/docs/run-code This snippet demonstrates how to use the Interfaze AI SDK to execute a Python code script for calculating factorial. The code runs in a secure, sandboxed environment, and the output is captured and logged. It requires the Interfaze SDK and a model supporting code execution. ```typescript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: "Run this code:\n\ndef factorial(n):\n if n == 0 or n == 1:\n return 1\n return n * factorial(n - 1)\n\nprint(factorial(5))", } ], }); console.log(response.choices[0].message.content); // Response: "120" ``` -------------------------------- ### Normal Text Generation with Vercel AI SDK Source: https://interfaze.ai/docs/vercel-ai-sdk Demonstrates generating a simple text response from Interfaze AI using the Vercel AI SDK. It takes a model and a prompt as input and returns the generated text. ```typescript const { text } = await generateText({ model: interfaze.chat('interfaze-beta'), prompt: "Write a short story about a robot learning to paint", }); console.log(text); ``` -------------------------------- ### Interfaze AI Streaming Response OpenAI SDK Source: https://interfaze.ai/docs/index Demonstrates how to enable and handle streaming responses from Interfaze AI using the OpenAI SDK in TypeScript. This allows for real-time content generation by processing chunks of the response as they become available. ```typescript const stream = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: "Write a short story about a robot learning to paint", }, ], stream: true, }); for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content; if (content) { process.stdout.write(content); } } ``` -------------------------------- ### Streaming Responses with Interfaze AI and LangChain Source: https://interfaze.ai/docs/langchain Demonstrates how to receive streaming responses from Interfaze AI models using LangChain. It initializes the chat model with streaming enabled and iterates over the stream to process and print chunks of content to standard output. Requires API key. ```typescript import { ChatOpenAI } from "@langchain/openai"; const interfaze = new ChatOpenAI({ baseURL: "https://api.interfaze.ai/v1", apiKey: "", streaming: true }); const stream = await interfaze.stream("interfaze-beta", { prompt: "Write a short story about a robot learning to paint", }); for await (const chunk of stream) { process.stdout.write(chunk.content); } ``` -------------------------------- ### Streaming Chat Completion with OpenAI SDK (TypeScript) Source: https://interfaze.ai/docs/streaming Demonstrates how to initiate and process a streaming chat completion request using the Interfaze OpenAI SDK in TypeScript. This allows for real-time output of responses as they are generated, enhancing user experience. ```typescript const stream = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: "Write a short story about a robot learning to paint" } ], stream: true }); for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content; if (content) { process.stdout.write(content); } } ``` -------------------------------- ### Scrape Web Page Data with Interfaze AI SDK Source: https://interfaze.ai/docs/web Illustrates how to use the Interfaze AI SDK to scrape structured data directly from live web pages. This is useful for extracting specific content like follower counts from social media. ```javascript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [{ role: "user", content: "https://x.com/yoeven get the number of followers" }], }); console.log(response.choices[0].message.content); // Response: "798" ``` -------------------------------- ### Enable Guardrails in System Prompt (JavaScript) Source: https://interfaze.ai/docs/guard-rails This snippet demonstrates how to enable specific guardrail categories (S1, S2, S3, S10) within the system prompt for a chat completion. It shows the structure for including guard directives and a user query that might trigger them. Dependencies include the Interfaze SDK. ```javascript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "system", content: "*GUARD* S1, S2, S3, S10 *GUARD* You are a helpful assistant. Avoid violent, harmful, or inappropriate content." }, { role: "user", content: "What's a good way to harm an animal ?" } ], }); ``` -------------------------------- ### Interfaze AI File Upload OpenAI SDK Request Source: https://interfaze.ai/docs/index Shows how to send a request to Interfaze AI that includes an image URL for analysis using the OpenAI SDK in TypeScript. This allows the model to process and extract information from images alongside text prompts. ```typescript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [{ role: "user", content: [ { type: "text", text: "Extract the first name and last name from this image" }, { type: "image_url", image_url: { url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", }, }, ], }], }); console.log(response.choices[0].message.content); ``` -------------------------------- ### Object Detection Structured Output with Bounding Boxes (TypeScript) Source: https://interfaze.ai/docs/structured-output Performs object detection on an image and returns detailed information, including bounding boxes and descriptions, in a structured JSON format. This leverages Zod schemas for complex, nested data structures. Requires 'openai' and 'zod'. ```typescript import { z } from "zod"; import { zodResponseFormat } from "openai/helpers/zod"; const BoundingBoxSchema = z.object({ x: z.number().describe("X coordinate of top-left corner"), y: z.number().describe("Y coordinate of top-left corner"), width: z.number().describe("Width of bounding box"), height: z.number().describe("Height of bounding box"), }); const DetectedObjectSchema = z.object({ object_name: z.string().describe("Name of the detected object"), bounding_box: BoundingBoxSchema, description: z.string().describe("Brief description of the object"), }); const RootSchema = z.object({ detected_objects: z .array(DetectedObjectSchema) .describe("List of detected objects in the image"), total_objects: z.number().describe("Total number of objects detected"), image_description: z.string().describe("Overall description of the image"), }); const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: [ { type: "text", text: "Detect a human in this image." }, { type: "image_url", image_url: { url: "https://rogilvkqloanxtvjfrkm.supabase.co/storage/v1/object/public/demo/Collabo%201080x842.jpg?t=2024-03-22T09%3A22%3A48.442Z", }, }, ], }, ], response_format: zodResponseFormat(RootSchema, "root"), }); console.log(response.choices[0].message.content); /* Response - { "detected_objects": [ { "object_name": "human", "bounding_box": { "x": 0, "y": 136, "width": 1080, "height": 706 }, "description": "A group of people sitting around a table, raising glasses in a toast and enjoying a meal together." } ], "total_objects": 1, "image_description": "The image shows a lively group of people gathered around a dining table, smiling and toasting with glasses, suggesting a celebratory or social gathering." } */ ``` -------------------------------- ### Streaming Text Responses with Vercel AI SDK Source: https://interfaze.ai/docs/vercel-ai-sdk Shows how to stream text responses from Interfaze AI using the Vercel AI SDK. This is useful for real-time applications where users can see the AI's output as it's generated. ```typescript const { textStream } = await streamText({ model: interfaze.chat('interfaze-beta'), prompt: "Write a short story about a robot learning to paint", }); for await (const delta of textStream) { process.stdout.write(delta); } ``` -------------------------------- ### Interfaze PDF Document Extraction using OpenAI SDK (TypeScript) Source: https://interfaze.ai/docs/document-extraction Demonstrates three methods for extracting text from PDF documents using the Interfaze API via the OpenAI SDK in TypeScript. Supports direct URL, file content type, and Base64 encoding. Requires the 'openai' package. ```typescript // Method 1: Direct URL const response1 = await openai.chat.completions.create({ model: "interfaze-beta", messages: [{"role": "user", "content": "Extract all text from this PDF: https://www.antennahouse.com/hubfs/xsl-fo-sample/pdf/basic-link-1.pdf"}] }); console.log(response1.choices[0].message.content); // Method 2: File content type const response2 = await openai.chat.completions.create({ model: "interfaze-beta", messages: [ { "role": "user", "content": [ { "type": "text", "text": "Extract the text from this PDF file" }, { "type": "file", "file": { "filename": "basic-link-1.pdf", "file_data": "https://www.antennahouse.com/hubfs/xsl-fo-sample/pdf/basic-link-1.pdf" } } ] } ] }); console.log(response2.choices[0].message.content); // Method 3: Base64 encoding import fs from 'fs'; import path from 'path'; const pdfPath = path.join(__dirname, "basic-link-1.pdf"); const pdfData = fs.readFileSync(pdfPath); const base64Data = pdfData.toString("base64"); const response3 = await openai.chat.completions.create({ model: "interfaze-beta", messages: [ { "role": "user", "content": [ { "type": "text", "text": "Extract the text from this PDF file" }, { "type": "file", "file": { "filename": "basic-link-1.pdf", "file_data": base64Data } } ] } ] }); console.log(response3.choices[0].message.content); ``` -------------------------------- ### Access Precontext Data with OpenAI SDK (TypeScript) Source: https://interfaze.ai/docs/precontext Demonstrates how to access precontext data, such as OCR results, from an Interfaze AI chat completion response using the OpenAI SDK in TypeScript. This involves making a chat completion request with text and an image URL and then accessing the `precontext` field of the response. ```typescript const response = await interfaze.chat.completions.create({ model: "interfaze-beta", messages: [ { role: "user", content: [ { type: "text", text: "Extract total price from this receipt" }, { type: "image_url", image_url: { url: "https://jigsawstack.com/preview/vocr-example.jpg", }, }, ], }, ], }); //@ts-expect-error precontext is not typed const precontext = response.precontext; console.log("OCR Results:", precontext[0]?.result); console.log("Process used:", precontext[0]?.name); ``` -------------------------------- ### Interfaze PDF Document Extraction using LangChain SDK (Python) Source: https://interfaze.ai/docs/document-extraction This section (indicated by 'python' tag) would typically show how to perform PDF document extraction using the Interfaze API with the LangChain SDK in Python. Please refer to the LangChain documentation for specific implementation details as the code is not provided in the input.