### Configuration Examples Source: https://docs.edenai.co/v3/integrations/ai-assistants/claude-code Examples of configuration files demonstrating how to set up custom headers and timeouts for API requests. ```APIDOC ## Configuration Examples ### Custom Headers If you need to pass custom headers (e.g., for analytics): ```json { "api_key": "YOUR_EDEN_AI_API_KEY", "base_url": "https://api.edenai.run/v3/llm", "model": "anthropic/claude-3-5-sonnet-20241022", "headers": { "X-Custom-Header": "value" } } ``` ### Timeout Configuration Adjust timeouts for longer requests: ```json { "api_key": "YOUR_EDEN_AI_API_KEY", "base_url": "https://api.edenai.run/v3/llm", "timeout": 120000 } ``` ``` -------------------------------- ### Install OpenAI Python SDK Source: https://docs.edenai.co/v3/integrations/sdks/python-openai Install the official OpenAI Python SDK using pip or poetry. This is the first step to integrate Eden AI's V3 API with Python. ```bash pip install openai ``` ```bash poetry add openai ``` -------------------------------- ### Git Integration Example Source: https://docs.edenai.co/v3/integrations/ai-assistants/claude-code Example of integrating Eden AI's Claude Code into Git pre-commit hooks for automated code review. ```APIDOC ## Git Integration Example ### .git/hooks/pre-commit This script demonstrates how to use Claude Code within a Git pre-commit hook to perform automated security reviews on Python files. ```bash #!/bin/bash # Export Eden AI configuration export ANTHROPIC_API_KEY="YOUR_EDEN_AI_API_KEY" export ANTHROPIC_BASE_URL="https://api.edenai.run/v3/llm" # Get changed files changed_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.py$') if [ -n "$changed_files" ]; then echo "Reviewing changed Python files..." for file in $changed_files; do claude-code "Quick security review" < "$file" done fi ``` ``` -------------------------------- ### Complete Chat Example with Conversation Management in Python Source: https://docs.edenai.co/v3/integrations/sdks/python-openai This comprehensive Python example demonstrates a full chat interaction with an AI model, including conversation history management. It allows users to input messages, receives streaming responses from the AI, and maintains the context of the conversation for follow-up interactions. The example uses the `openai` library and includes a loop for continuous chatting until the user types 'quit'. ```python from openai import OpenAI import sys def chat_with_ai(): client = OpenAI( api_key="YOUR_EDEN_AI_API_KEY", base_url="https://api.edenai.run/v3/llm" ) messages = [ {"role": "system", "content": "You are a helpful assistant."} ] print("Chat with AI (type 'quit' to exit)") print("-" * 50) while True: # Get user input user_input = input("\nYou: ").strip() if user_input.lower() == 'quit': break if not user_input: continue # Add user message messages.append({"role": "user", "content": user_input}) # Get AI response print("\nAssistant: ", end='') stream = client.chat.completions.create( model="anthropic/claude-3-5-sonnet-20241022", messages=messages, temperature=0.7, stream=True ) assistant_response = "" for chunk in stream: if chunk.choices[0].delta.content: content = chunk.choices[0].delta.content print(content, end='', flush=True) assistant_response += content print() # New line after response # Add assistant response to history messages.append({"role": "assistant", "content": assistant_response}) if __name__ == "__main__": chat_with_ai() ``` -------------------------------- ### Token Naming Conventions Example (Python) Source: https://docs.edenai.co/v3/how-to/user-management/manage-tokens Demonstrates recommended naming conventions for custom API tokens to ensure clarity and consistency. It shows examples of good patterns based on environment, application, platform, or purpose, and contrasts them with generic or temporary names to avoid. ```Python # Good naming patterns "prod-web-app" # Environment + application "staging-mobile-ios" # Environment + platform "dev-john-testing" # Environment + developer "ci-cd-pipeline" # Purpose "partner-acme-corp" # External usage # Avoid "token1", "test", "temp" # Too generic ``` -------------------------------- ### Install LangChain and Dependencies (Bash) Source: https://docs.edenai.co/v3/integrations/frameworks/langchain Installs the necessary LangChain libraries for Python and Node.js environments. This is a prerequisite for using Eden AI with LangChain. ```bash pip install langchain langchain-openai ``` ```bash npm install langchain @langchain/openai ``` -------------------------------- ### Setup Multi-Environment Token System (Python) Source: https://docs.edenai.co/v3/tutorials/multi-environment-tokens Python script to set up development, staging, and production environments with different token configurations. It utilizes TokenManager and TokenLifecycleManager for managing API tokens, sandbox tokens, balance, and expiration. ```python #!/usr/bin/env python3 import os from token_manager import TokenManager from token_lifecycle import TokenLifecycleManager def setup_complete_system(): """Set up tokens for all environments""" API_KEY = os.getenv("EDENAI_API_KEY") manager = TokenManager(API_KEY) lifecycle = TokenLifecycleManager(manager) # Development environment - sandbox tokens, no limits dev_config = { "web-app": { "type": "sandbox_api_token" }, "mobile-app": { "type": "sandbox_api_token" }, "testing": { "type": "sandbox_api_token" } } # Staging environment - real tokens, moderate limits staging_config = { "web-app": { "type": "api_token", "balance": 50.0, "expire_days": 90 }, "mobile-app": { "type": "api_token", "balance": 50.0, "expire_days": 90 } } # Production environment - real tokens, high limits prod_config = { "web-app": { "type": "api_token", "balance": 500.0, "expire_days": 90 }, "mobile-app": { "type": "api_token", "balance": 500.0, "expire_days": 90 }, "api-gateway": { "type": "api_token", "balance": 1000.0, "expire_days": 90 } } # Set up all environments print("Setting up Development environment...") lifecycle.setup_environment("dev", dev_config) print("\nSetting up Staging environment...") lifecycle.setup_environment("staging", staging_config) print("\nSetting up Production environment...") lifecycle.setup_environment("prod", prod_config) print("\n✅ All environments set up successfully!") if __name__ == "__main__": setup_complete_system() ``` -------------------------------- ### Prompting Strategies for Vision API (Python) Source: https://docs.edenai.co/v3/how-to/llm/vision-capabilities Illustrates effective prompting techniques for vision tasks, emphasizing specificity, requesting structured output (like JSON), and providing context for better analysis. These examples guide users on how to formulate clear and actionable prompts. ```python # Vague "What's in this image?" # Specific "List all furniture items visible in this room photo, including their approximate positions and colors." ``` ```python "Extract the following from this business card and format as JSON: - name - title - company - email - phone" ``` ```python "This is a medical X-ray of a chest. Identify any abnormalities or concerning features." ``` -------------------------------- ### Verify MONGO_URI in .env (bash) Source: https://docs.edenai.co/v3/integrations/chat-platforms/librechat Example of the `MONGO_URI` environment variable as it should appear in the `.env` file for connecting to the MongoDB service within Docker Compose. ```bash MONGO_URI=mongodb://mongodb:27017/LibreChat ``` -------------------------------- ### Docker Deployment Commands (bash) Source: https://docs.edenai.co/v3/integrations/chat-platforms/librechat Provides essential Docker Compose commands for deploying LibreChat in production, including pulling the latest image, starting services, viewing logs, and checking status. ```bash # Pull latest image docker compose pull # Start services docker compose up -d # View logs docker compose logs -f api # Check status docker compose ps ``` -------------------------------- ### OCR Text Extraction Example (Python) Source: https://docs.edenai.co/v3/how-to/universal-ai/getting-started A Python example for extracting text from documents using OCR via the Universal AI endpoint. It first uploads a file using the '/upload' endpoint to get a file ID, then uses this ID in the Universal AI request for OCR processing. ```Python import requests # First upload the file upload_response = requests.post( "https://api.edenai.run/v3/upload", headers={"Authorization": f"Bearer {API_KEY}"}, files={"file": open("document.pdf", "rb")} ) file_id = upload_response.json()["file_id"] # Then use it in Universal AI payload = { "model": "ocr/financial_parser/google", "input": {"file": file_id} } response = requests.post(url, headers=headers, json=payload) extracted_text = response.json()["output"]["text"] ``` -------------------------------- ### Utilize System Messages to Guide Chatbot Behavior Source: https://docs.edenai.co/v3/how-to/llm/chat-completions Shows how to use system messages in the `messages` array to define the AI's persona or behavior. This Python example sets the assistant to speak like a pirate and then asks a question. ```Python import requests payload = { "model": "openai/gpt-4", "messages": [ { "role": "system", "content": "You are a helpful assistant that speaks like a pirate." }, { "role": "user", "content": "Tell me about artificial intelligence." } ], "stream": True } response = requests.post(url, headers=headers, json=payload, stream=True) ``` -------------------------------- ### Initialize and Use RouterAlertManager in Python Source: https://docs.edenai.co/v3/how-to/router/monitoring Demonstrates how to initialize the RouterAlertManager with API keys and webhook URLs, and then simulate chat requests to test its functionality. This code is crucial for setting up and testing the alert system. ```python from edenai_router.alert_manager import RouterAlertManager import time # Usage alert_manager = RouterAlertManager( "YOUR_API_KEY", alert_webhook="https://hooks.slack.com/services/YOUR/WEBHOOK/URL" ) # Simulate requests (some will trigger alerts if thresholds are exceeded) for i in range(50): result = alert_manager.chat( f"Question {i+1}", candidates=["openai/gpt-4o", "anthropic/claude-sonnet-4-5"] ) time.sleep(0.1) ``` -------------------------------- ### Define Preset Prompts for Custom Tasks in librechat.yaml Source: https://docs.edenai.co/v3/integrations/chat-platforms/librechat This configuration allows users to define custom preset prompts for specific tasks in LibreChat when using Eden AI. Each preset includes a title, the model to use, temperature, and a system message to guide the AI's behavior. Examples include a Code Assistant, Creative Writer, and Data Analyst. ```yaml endpoints: custom: - name: "Eden AI" apiKey: "${OPENAI_API_KEY}" baseURL: "https://api.edenai.run/v3/llm" models: default: - "anthropic/claude-3-5-sonnet-20241022" - "openai/gpt-4" # Add custom presets presets: - title: "Code Assistant" model: "anthropic/claude-3-5-sonnet-20241022" temperature: 0.1 system_message: "You are an expert programmer. Provide clean, well-documented code." - title: "Creative Writer" model: "openai/gpt-4" temperature: 0.9 system_message: "You are a creative writer. Be imaginative and engaging." - title: "Data Analyst" model: "google/gemini-1.5-pro" temperature: 0.3 system_message: "You are a data analyst. Provide clear, data-driven insights." ``` -------------------------------- ### Quick Start with OpenAI SDK and Eden AI V3 Source: https://docs.edenai.co/v3/integrations/sdks/python-openai Configure the OpenAI client to point to Eden AI's V3 endpoint. This allows you to access various AI models through a familiar interface. Streaming is mandatory in V3. ```python from openai import OpenAI # Initialize client with Eden AI endpoint client = OpenAI( api_key="YOUR_EDEN_AI_API_KEY", # Get from https://app.edenai.run base_url="https://api.edenai.run/v3/llm" ) # Use streaming (mandatory in V3) stream = client.chat.completions.create( model="openai/gpt-4", messages=[ {"role": "user", "content": "Hello! How are you?"} ], stream=True ) # Print the response for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end='') ``` -------------------------------- ### Prompting Strategies for AI Models Source: https://docs.edenai.co/v3/how-to/llm/working-with-media Demonstrates effective prompting techniques for AI models, including being specific, providing context, and requesting structured output. These examples are language-agnostic and illustrate the desired input format for different scenarios. ```json { "type": "text", "text": "This is a medical chart showing patient vitals over 24 hours. Identify any concerning trends." } ``` ```json { "type": "text", "text": "Extract data as JSON with fields: date, vendor, total, items[]." } ``` -------------------------------- ### Test Token Management System (Bash) Source: https://docs.edenai.co/v3/tutorials/multi-environment-tokens Bash commands to test the token management system. This includes setting the API key, running the environment setup script, performing a health check, and rotating a specific token using an inline Python script. ```bash # Set up environments export EDENAI_API_KEY="your_main_api_key" python setup_environments.py # Run health check python main.py # Rotate a specific token python -c " from token_manager import TokenManager from token_rotation import TokenRotationManager import os manager = TokenManager(os.getenv('EDENAI_API_KEY')) rotation = TokenRotationManager(manager) result = rotation.rotate_token('prod-web-app') print(result['migration_steps']) " ``` -------------------------------- ### Configure User Authentication Settings in .env Source: https://docs.edenai.co/v3/integrations/chat-platforms/librechat This `.env` file configuration enables user registration and authentication features in LibreChat. It includes options to allow registration, set up email verification with service credentials, and configure social authentication providers like Google. ```bash # Allow user registration ALLOW_REGISTRATION=true # Email verification (optional) EMAIL_SERVICE=gmail EMAIL_USERNAME=your-email@gmail.com EMAIL_PASSWORD=your-app-password # Social auth (optional) GOOGLE_CLIENT_ID=your-google-client-id GOOGLE_CLIENT_SECRET=your-google-client-secret GOOGLE_CALLBACK_URL=http://localhost:3080/oauth/google/callback ``` -------------------------------- ### Build Chat CLI with TypeScript Source: https://docs.edenai.co/v3/integrations/sdks/typescript-openai This example demonstrates how to build a command-line chat interface using TypeScript and the OpenAI SDK, configured to use the EdenAI API. It handles user input, streams responses from the AI, and maintains conversation history. Requires the 'openai' and 'node-fetch' packages and an EDEN_AI_API_KEY environment variable. ```typescript import OpenAI from 'openai'; import readline from 'readline'; import type { ChatCompletionMessageParam } from 'openai/resources/chat/completions'; const client = new OpenAI({ apiKey: process.env.EDEN_AI_API_KEY!, baseURL: 'https://api.edenai.run/v3/llm', }); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); async function main() { const messages: ChatCompletionMessageParam[] = [ { role: 'system', content: 'You are a helpful assistant.' }, ]; console.log('Chat with AI (type "quit" to exit)'); console.log('-'.repeat(50)); const askQuestion = () => { rl.question('\nYou: ', async (input) => { const userInput = input.trim(); if (userInput.toLowerCase() === 'quit') { rl.close(); return; } if (!userInput) { askQuestion(); return; } messages.push({ role: 'user', content: userInput }); process.stdout.write('\nAssistant: '); try { const stream = await client.chat.completions.create({ model: 'anthropic/claude-3-5-sonnet-20241022', messages, temperature: 0.7, stream: true, }); let assistantResponse = ''; for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content; if (content) { process.stdout.write(content); assistantResponse += content; } } console.log(); // New line messages.push({ role: 'assistant', content: assistantResponse }); } catch (error) { console.error('\nError:', error); } askQuestion(); }); }; askQuestion(); } main(); ``` -------------------------------- ### Monitor Usage - Basic Example (cURL, Python, JavaScript) Source: https://docs.edenai.co/v3/how-to/cost-management/monitor-usage Retrieve the last 30 days of API usage, aggregated daily. This example requires an API key and demonstrates fetching raw usage data. ```bash curl -X GET "https://api.edenai.run/v2/cost_management/?begin=2024-01-01&end=2024-01-31&step=1" \ -H "Authorization: Bearer sk_xxxxxxxxxxxxxxxxxxxxxxxx" ``` ```python import requests from datetime import datetime, timedelta API_KEY = "eyJhbG...xxxxxxxxxxxxxxxxxxxxxxxx" # Get last 30 days end_date = datetime.now() begin_date = end_date - timedelta(days=30) headers = {"Authorization": f"Bearer {API_KEY}"} params = { "begin": begin_date.strftime("%Y-%m-%d"), "end": end_date.strftime("%Y-%m-%d"), "step": 1 # Daily aggregation } response = requests.get( "https://api.edenai.run/v2/cost_management/", headers=headers, params=params ) data = response.json() print(f"Usage data retrieved for {len(data['response'])} token(s)") ``` ```javascript const API_KEY = 'sk_xxxxxxxxxxxxxxxxxxxxxxxx'; // Get last 30 days const endDate = new Date(); const beginDate = new Date(); beginDate.setDate(beginDate.getDate() - 30); const params = new URLSearchParams({ begin: beginDate.toISOString().split('T')[0], end: endDate.toISOString().split('T')[0], step: '1' // Daily aggregation }); const response = await fetch( `https://api.edenai.run/v2/cost_management/?${params}`, { headers: { 'Authorization': `Bearer ${API_KEY}` } } ); const data = await response.json(); console.log(`Usage data retrieved for ${data.response.length} token(s)`); ``` -------------------------------- ### Install OpenAI SDK for Node.js Source: https://docs.edenai.co/v3/integrations/sdks/typescript-openai Install the official OpenAI SDK for Node.js using npm, yarn, or pnpm. This is the first step to integrating Eden AI's LLM capabilities into your TypeScript or JavaScript project. ```bash npm install openai ``` ```bash yarn add openai ``` ```bash pnpm add openai ``` -------------------------------- ### Troubleshoot Performance Issues Source: https://docs.edenai.co/v3/integrations/chat-platforms/open-webui Provides guidance for addressing UI slowness. This involves checking container resource usage with `docker stats`, increasing memory allocation in the Docker Compose file, and optimizing the database. ```bash docker stats open-webui ``` ```yaml services: open-webui: deploy: resources: limits: memory: 4G ``` ```bash docker compose exec open-webui python manage.py optimize-db ``` -------------------------------- ### List Available LLM Models with TypeScript Source: https://docs.edenai.co/v3/integrations/sdks/typescript-openai This TypeScript example shows how to programmatically discover and list available Large Language Models (LLMs) from the EdenAI API. It fetches the model list, groups them by provider, and prints them to the console. Requires the 'node-fetch' package and an EDEN_AI_API_KEY environment variable. ```typescript import fetch from 'node-fetch'; async function listModels() { const response = await fetch('https://api.edenai.run/v3/llm/models', { headers: { Authorization: `Bearer ${process.env.EDEN_AI_API_KEY}`, }, }); const data = await response.json(); // Group by provider const providers: Record = {}; for (const model of data.data) { const provider = model.owned_by; if (!providers[provider]) { providers[provider] = []; } providers[provider].push(model.id); } console.log('Available Models:\n'); for (const [provider, models] of Object.entries(providers)) { console.log(`${provider}:`); models.forEach(m => console.log(` - ${m}`)); console.log(); } } listModels(); ``` -------------------------------- ### Make a Universal AI Call to Detect AI-Generated Text Source: https://docs.edenai.co/v3/get-started/introduction Shows how to use the Universal AI endpoint to detect if a given text was AI-generated. This example specifies the model and provides the input text. It includes examples for cURL, Python, and JavaScript. ```bash curl -X POST https://api.edenai.run/v3/universal-ai \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "text/ai_detection/openai/gpt-4", "input": { "text": "This is a sample text to check if it was AI-generated." } }' ``` ```python import requests url = "https://api.edenai.run/v3/universal-ai" headers = { "Authorization": "Bearer YOUR_API_KEY", "Content-Type": "application/json" } payload = { "model": "text/ai_detection/openai/gpt-4", "input": { "text": "This is a sample text to check if it was AI-generated." } } response = requests.post(url, headers=headers, json=payload) result = response.json() print(result) ``` ```javascript const url = 'https://api.edenai.run/v3/universal-ai'; const headers = { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' }; const payload = { model: 'text/ai_detection/openai/gpt-4', input: { text: 'This is a sample text to check if it was AI-generated.' } }; const response = await fetch(url, { method: 'POST', headers: headers, body: JSON.stringify(payload) }); const result = await response.json(); console.log(result); ``` -------------------------------- ### POST /llm/chat/completions Source: https://docs.edenai.co/v3/integrations/ai-assistants/claude-code Example of how to test your API key for chat completions. ```APIDOC ## POST /llm/chat/completions ### Description This endpoint is used for chat completions and can be used to test your API key authentication. ### Method POST ### Endpoint `https://api.edenai.run/v3/llm/chat/completions` ### Parameters #### Headers - **Authorization** (string) - Required - Bearer token for authentication. - **Content-Type** (string) - Required - `application/json` #### Request Body - **model** (string) - Required - The model to use for the chat completion. - **messages** (array) - Required - An array of message objects, each with a `role` and `content`. - **stream** (boolean) - Optional - Whether to stream the response. ### Request Example ```bash curl -X POST https://api.edenai.run/v3/llm/chat/completions \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d "{ \"model\": \"anthropic/claude-3-5-sonnet-20241022\", \"messages\": [{\"role\": \"user\", \"content\": \"test\"}], \"stream\": true }" ``` ### Response #### Success Response (200) - **id** (string) - Unique identifier for the response. - **object** (string) - Type of object returned. - **created** (integer) - Timestamp of creation. - **model** (string) - The model used for the response. - **choices** (array) - An array of choices, each containing a message. - **usage** (object) - Information about token usage. #### Response Example ```json { "id": "chatcmpl-12345", "object": "chat.completion", "created": 1677652288, "model": "anthropic/claude-3-5-sonnet-20241022", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "This is a test response." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30 } } ``` ``` -------------------------------- ### Scheduling Eden AI Cost Monitoring (Bash and Python) Source: https://docs.edenai.co/v3/tutorials/track-optimize-spending Demonstrates two methods for scheduling the Eden AI cost monitoring script. The first uses a cron job for daily execution, redirecting output to a log file. The second uses Python's `schedule` library for more dynamic in-script scheduling. ```bash # Run daily at 9 AM 0 9 * * * /usr/bin/python3 /path/to/main.py >> /var/log/edenai_monitor.log 2>&1 ``` ```python import schedule import time def job(): # Run your monitoring main() # Schedule daily at 9 AM schedule.every().day.at("09:00").do(job) # Or every hour schedule.every().hour.do(job) while True: schedule.run_pending() time.sleep(60) ``` -------------------------------- ### GET /cost-management/usage Source: https://docs.edenai.co/v3/integrations/ai-assistants/claude-code Retrieve information about your current Eden AI usage. ```APIDOC ## GET /cost-management/usage ### Description This endpoint allows you to monitor your Eden AI usage and costs. ### Method GET ### Endpoint `https://api.edenai.run/v3/cost-management/usage` ### Parameters #### Headers - **Authorization** (string) - Required - Bearer token for authentication. ### Request Example ```bash curl https://api.edenai.run/v3/cost-management/usage \ -H "Authorization: Bearer YOUR_API_KEY" ``` ### Response #### Success Response (200) - **usage** (object) - Object containing detailed usage information. - **total_cost** (number) - Total cost incurred. - **model_costs** (array) - Array of objects, each detailing cost per model. #### Response Example ```json { "usage": { "total_cost": 15.75, "model_costs": [ { "model": "anthropic/claude-3-5-sonnet-20241022", "cost": 10.50 }, { "model": "openai/gpt-4", "cost": 5.25 } ] } } ``` ``` -------------------------------- ### Create Autonomous Agents with Python Source: https://docs.edenai.co/v3/integrations/frameworks/langchain Demonstrates how to create and execute autonomous agents using Langchain and EdenAI's ChatOpenAI integration. It initializes an LLM, defines tools for searching and calculation, sets up a prompt, and runs an agent executor to process an input query. ```python from langchain_openai import ChatOpenAI from langchain.agents import AgentExecutor, create_openai_functions_agent from langchain.tools import Tool from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder import requests # Initialize LLM llm = ChatOpenAI( model="openai/gpt-4", api_key="YOUR_EDEN_AI_API_KEY", base_url="https://api.edenai.run/v3/llm", temperature=0 ) # Define tools def search_tool(query: str) -> str: """Search for information""" # Implement your search logic return f"Search results for: {query}" def calculator_tool(expression: str) -> str: """Calculate mathematical expressions""" try: return str(eval(expression)) except Exception as e: return f"Error: {str(e)}" tools = [ Tool( name="Search", func=search_tool, description="Useful for searching information" ), Tool( name="Calculator", func=calculator_tool, description="Useful for mathematical calculations" ) ] # Create agent prompt prompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant."), ("human", "{input}"), MessagesPlaceholder(variable_name="agent_scratchpad"), ]) # Create agent agent = create_openai_functions_agent(llm, tools, prompt) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) # Run agent result = agent_executor.invoke({ "input": "What is 25 * 4 + 10?" }) print(result["output"]) ``` -------------------------------- ### Testing Authentication Source: https://docs.edenai.co/v3/how-to/authentication/bearer-token-auth Verify your API authentication setup with sample requests. ```APIDOC ## Testing Authentication Test your authentication with a simple request: ### cURL **Description:** Use cURL to send a POST request to the chat completions endpoint. **Code:** ```bash curl -X POST https://api.edenai.run/v3/llm/chat/completions \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model": "openai/gpt-4", "messages": [{"role": "user", "content": "Test"}]}' ``` ### Python **Description:** Use the `requests` library in Python to test authentication. **Code:** ```python import requests # Assume API_KEY is loaded from environment variables # API_KEY = os.getenv("EDENAI_API_KEY") headers = { "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json" } response = requests.post( "https://api.edenai.run/v3/llm/chat/completions", headers=headers, json={ "model": "openai/gpt-4", "messages": [{"role": "user", "content": "Test"}] } ) if response.status_code == 200: print("Authentication successful!") print(response.json()) else: print(f"Authentication failed: {response.status_code}") print(response.json()) ``` ``` -------------------------------- ### Get Current Credits Source: https://docs.edenai.co/v3/how-to/cost-management/monitor-usage Retrieves the current available credits for your account. ```APIDOC ## GET /v2/cost_management/credits/ ### Description Fetches the current credit balance for the authenticated user. ### Method GET ### Endpoint `https://api.edenai.run/v2/cost_management/credits/` ### Parameters No parameters required. ### Request Example ```python import requests API_KEY = "YOUR_API_KEY" headers = {"Authorization": f"Bearer {API_KEY}"} response = requests.get( "https://api.edenai.run/v2/cost_management/credits/", headers=headers ) data = response.json() print(data) ``` ### Response #### Success Response (200) - **credits** (float) - The current number of available credits. #### Response Example ```json { "credits": 500.75 } ``` ``` -------------------------------- ### Securely Load API Key from .env (Python) Source: https://docs.edenai.co/v3/integrations/frameworks/langchain This example shows how to securely store and load your EdenAI API key using a .env file and the python-dotenv library. It first defines the API key in a .env file and then loads it into the environment for use with LangChain. This prevents hardcoding sensitive credentials directly in your code. ```bash EDEN_AI_API_KEY=your_api_key_here ``` ```python import os from dotenv import load_dotenv from langchain_openai import ChatOpenAI load_dotenv() llm = ChatOpenAI( model="anthropic/claude-3-5-sonnet-20241022", api_key=os.getenv("EDEN_AI_API_KEY"), base_url="https://api.edenai.run/v3/llm" ) ``` -------------------------------- ### Check MongoDB Service Status (bash) Source: https://docs.edenai.co/v3/integrations/chat-platforms/librechat Command to check if the MongoDB service is running within the Docker Compose setup. ```bash docker compose ps mongodb ``` -------------------------------- ### List Available Models (Python) Source: https://docs.edenai.co/v3/how-to/router/getting-started This Python script retrieves a list of all available models from the EdenAI API's `/v3/llm/models` endpoint. It makes a GET request, parses the JSON response, and then iterates through the models to print their IDs, descriptions, context lengths, and pricing information. Remember to replace 'YOUR_API_KEY' with your actual API key. ```Python import requests url = "https://api.edenai.run/v3/llm/models" headers = {"Authorization": "Bearer YOUR_API_KEY"} response = requests.get(url, headers=headers) models = response.json() # List all models for model in models['data']: print(f"{model['id']} - {model['description']}") print(f" Context: {model['context_length']} tokens") print(f" Pricing: {model['pricing']}") print() ``` -------------------------------- ### Error Handling Examples for API Requests Source: https://docs.edenai.co/v3/how-to/llm/working-with-media Illustrates common API errors such as 'file_too_large', 'unsupported_format', and 'invalid_base64' with their corresponding JSON error messages. Includes a Python example using the requests library to handle HTTP errors during API interactions, specifically checking for 413 (Payload Too Large) and 422 (Unprocessable Entity) status codes. ```json { "error": { "code": "file_too_large", "message": "File size exceeds provider limit of 20 MB" } } ``` ```json { "error": { "code": "unsupported_format", "message": "Image format .bmp is not supported" } } ``` ```json { "error": { "code": "invalid_base64", "message": "Invalid base64 data in data URL" } } ``` ```python import requests try: response = requests.post(url, headers=headers, json=payload, stream=True) response.raise_for_status() for line in response.iter_lines(): if line: print(line.decode('utf-8')) except requests.exceptions.HTTPError as e: if e.response.status_code == 413: print("File too large. Try compressing or resizing.") elif e.response.status_code == 422: print("Invalid request:", e.response.json()) else: print(f"HTTP error: {e}") ``` -------------------------------- ### Python Budget-Aware Router Usage Source: https://docs.edenai.co/v3/how-to/router/advanced-usage This Python code demonstrates initializing and using the BudgetAwareRouter. It makes several chat requests, printing the cost and remaining budget for each successful request. It requires an API key and a daily budget to be set. ```python from edenai_api_client.utils.budget_aware_router import BudgetAwareRouter # Usage router = BudgetAwareRouter("YOUR_API_KEY", daily_budget=10.0) # Make requests for i in range(5): result = router.chat(f"Question {i+1}: Explain AI") if result["success"]: print(f"Request {i+1}:") print(f" Model: {result['model']}") print(f" Cost: ${result['budget_info']['estimated_request_cost']}") print(f" Remaining: ${result['budget_info']['remaining']}") else: print(f"Request {i+1}: {result['error']}") ``` -------------------------------- ### Async Support for Concurrent Requests with Python Source: https://docs.edenai.co/v3/integrations/sdks/python-openai This Python example demonstrates how to use async/await syntax for making concurrent requests to the EdenAI API. It utilizes the `AsyncOpenAI` client and the `asyncio` library. This approach is beneficial for improving performance in applications requiring multiple simultaneous API calls. ```python from openai import AsyncOpenAI import asyncio async def main(): client = AsyncOpenAI( api_key="YOUR_EDEN_AI_API_KEY", base_url="https://api.edenai.run/v3/llm" ) stream = await client.chat.completions.create( model="openai/gpt-4", messages=[{"role": "user", "content": "Hello!"}], stream=True ) async for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end='') asyncio.run(main()) ``` -------------------------------- ### Python: Analyze Sequential Images for Process Guide with Anthropic Claude 3.5 Sonnet Source: https://docs.edenai.co/v3/how-to/llm/vision-capabilities This Python snippet shows how to analyze a series of sequential images using the EdenAI API and Anthropic's Claude 3.5 Sonnet model. It's designed to describe each step in a process and generate a numbered guide. The response is streamed. Replace 'YOUR_API_KEY' with your actual API key. ```python import requests payload = { "model": "anthropic/claude-3-5-sonnet-20241022", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "These are sequential steps of a process. Describe each step and create a numbered guide." }, { "type": "image_url", "image_url": {"url": "https://example.com/step1.jpg"} }, { "type": "image_url", "image_url": {"url": "https://example.com/step2.jpg"} }, { "type": "image_url", "image_url": {"url": "https://example.com/step3.jpg"} } ] } ], "stream": True, "max_tokens": 1200 } response = requests.post(url, headers=headers, json=payload, stream=True) ``` -------------------------------- ### Set up MongoDB Service (docker-compose.yml) Source: https://docs.edenai.co/v3/integrations/chat-platforms/librechat Defines the MongoDB service for Docker Compose, specifying the image, container name, restart policy, volume for data persistence, and port mapping. ```yaml services: mongodb: image: mongo container_name: chat-mongodb restart: always volumes: - ./data-node:/data/db ports: - 27017:27017 ```