Infuzu Documentation Help

Getting Started

Welcome to the Infuzu API! This guide will help you quickly set up and start using the Infuzu API to leverage the power of Intelligent Model Selection (IMS) for your applications. Whether you're a seasoned developer or just getting started, this guide provides all the essential steps to integrate the Infuzu API into your workflow.

Prerequisites

Before you begin, ensure you have the following:

  • Python 3.8 or higher installed on your machine. You can download it from the official Python website.

  • cURL installed for making HTTP requests from the command line. Most Unix-based systems come with cURL pre-installed. For Windows, you can download it from the cURL official website.

  • An Infuzu API Key. If you don't have one yet, follow the instructions in the Obtaining Your API Key section.

Obtaining Your API Key

To interact with the Infuzu API, you'll need a valid API key. Follow these steps to obtain your API key:

  1. Access the Infuzu Dashboard:

  2. Generate a New API Key:

    • Click on the "Create New API Key" button.

    • Provide a name or description for the key to help you identify its usage.

    • Click "Generate" to create the key.

  3. Copy Your API Key:

    • Once generated, copy the API key and store it securely.

Installing the Python Library

Infuzu provides a Python library to simplify interactions with the API. Follow these steps to install and set up the library:

  1. Install via pip:

    Open your terminal or command prompt and run:

    pip install infuzu
  2. Verify Installation:

    After installation, you can verify it by importing the library in Python:

    import infuzu print(infuzu.get_version())

    This should display the installed version of the infuzu library.

Setting Up Environment Variables

For security and convenience, it's recommended to store your API key as an environment variable. This way, you don't have to include the key directly in your code.

Setting INFUZU_API_KEY

On Unix/Linux/macOS

  1. Temporary (Session Only):

    export INFUZU_API_KEY="your_api_key_here"

    This sets the variable for the current terminal session.

  2. Permanent (Across Sessions):

    Add the following line to your ~/.bashrc, ~/.bash_profile, or ~/.zshrc file:

    export INFUZU_API_KEY="your_api_key_here"

    After adding, reload the file:

    source ~/.bashrc

On Windows

  1. Using Command Prompt (Temporary):

    set INFUZU_API_KEY=your_api_key_here
  2. Using PowerShell (Temporary):

    $env:INFUZU_API_KEY="your_api_key_here"
  3. Permanent (Across Sessions):

    • Open System Properties > Advanced > Environment Variables.

    • Click New under User variables.

    • Set Variable name to INFUZU_API_KEY and Variable value to your API key.

    • Click OK to save.

Making Your First API Request

Once you've set up your environment, you're ready to make your first API request. We'll demonstrate how to create a chat completion using both cURL and Python.

Using cURL

Here's how you can make a basic chat completion request using cURL:

curl -X POST https://chat.infuzu.com/api/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Infuzu-API-Key: your_api_key_here" \ -d '{ "messages": [ { "role": "user", "content": "Hello, how are you?" } ], "model": "infuzu-ims" }'

Explanation:

  • Endpoint: https://chat.infuzu.com/api/v1/chat/completions

  • Headers:

    • Content-Type: application/json: Specifies the request body format.

    • Infuzu-API-Key: Your unique API key for authentication.

  • Payload:

    • messages: A list of message objects. Each message has a role (e.g., "user" or "assistant") and content.

    • model: Specifies which model to use. "infuzu-ims" uses Infuzu's Intelligent Model Selection.

Using Python

Below is a Python example using the Infuzu Python library to create a chat completion:

import os from infuzu import ( create_chat_completion, ChatCompletionsHandlerRequestMessage, InfuzuAPIError ) # Ensure the INFUZU_API_KEY environment variable is set api_key = os.getenv("INFUZU_API_KEY") if not api_key: raise ValueError("INFUZU_API_KEY environment variable not set.") # Define the messages messages = [ ChatCompletionsHandlerRequestMessage( role="user", content="Hello, how are you?" ) ] # Make the API request try: response = create_chat_completion( messages=messages, api_key=api_key, # You can ommit this as it is the default to check for the environment variable model="infuzu-ims" # You can also pass an InfuzuModelParams object for advanced settings ) print("Response ID:", response.id) print("Response Message:", response.choices[0].message.content) except InfuzuAPIError as e: print("An error occurred:", str(e))

Explanation:

  1. Import Required Modules:

    • os for accessing environment variables.

    • create_chat_completion and related classes from the infuzu library.

  2. Retrieve API Key:

    • The API key is fetched from the INFUZU_API_KEY environment variable.

  3. Define Messages:

    • Create a list of ChatCompletionsHandlerRequestMessage objects representing the conversation history.

  4. Make the API Request:

    • Call create_chat_completion with the messages, API key, and model.

  5. Handle the Response:

    • Upon success, print the response ID and message content.

    • Handle potential errors using InfuzuAPIError.

Understanding the Basic Workflow

Here's a high-level overview of how to interact with the Infuzu API:

  1. Setup:

    • Install the Python library.

    • Obtain and securely store your API key.

    • Set up environment variables for easy access.

  2. Constructing the Request:

    • Define your conversation messages with appropriate roles and content.

    • Optionally, customize model selection parameters like preferred models, cost limits, and weighting factors.

  3. Making the Request:

    • Use either cURL or the Python library to send a POST request to the /v1/chat/completions endpoint.

    • Include necessary headers and the JSON payload.

  4. Processing the Response:

    • Receive a ChatCompletionsObject containing the AI's response(s), metadata, and usage information.

    • Handle any errors or warnings as needed.

  5. Managing Usage and Billing:

    • Monitor your API usage and credits via the provided dashboard links.

    • Implement error handling to manage rate limits and billing constraints.

Managing API Credits and Usage

Efficiently managing your API credits and understanding your usage patterns is crucial for optimizing costs and ensuring seamless integration. Here are key resources and steps to help you manage your API usage effectively:

  • Managing API Credits:

    • Billing Overview

    • Manage your API credits, view billing history, and update payment methods.

  • Viewing Rate Limits:

    • Rate Limits

    • Check your organization's rate limits to ensure you stay within allowed usage.

  • Monitoring Usage Patterns:

  • Managing API Keys:

  • Setting Organization API Pricing:

Best Practices

  • Monitor Regularly:

    • Regularly check your usage dashboards to stay informed about your consumption and costs.

  • Set Alerts:

    • Configure alerts for when you approach your rate limits or budget thresholds to prevent unexpected overages.

  • Optimize Requests:

    • Structure your requests efficiently to minimize unnecessary usage. For example, avoid excessive message history if not needed.

  • Manage API Keys:

    • Rotate API keys periodically and revoke any that are no longer in use to maintain security.

For comprehensive on each topic, navigate through the rest of our documentation. Happy coding!

Last modified: 25 February 2025