### Example API Response Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/quick-start.mdx This is an example of a successful response from the MegaLLM API for a chat completion request. It includes details like the conversation ID, model used, creation timestamp, choices, and token usage. ```json { "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "model": "gpt-4", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Hello! I'm an AI assistant powered by MegaLLM..." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 25, "total_tokens": 35 } } ``` -------------------------------- ### Make First API Request using JavaScript Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/quick-start.mdx An example of making a chat completion request to the MegaLLM API using the fetch API in JavaScript. It includes setting the Authorization header and sending a JSON payload via the request body. ```javascript const response = await fetch("https://ai.megallm.io/v1/chat/completions", { method: "POST", headers: { "Authorization": `Bearer ${process.env.MEGALLM_API_KEY}`, "Content-Type": "application/json" }, body: JSON.stringify({ model: "gpt-4", messages: [ { role: "user", content: "Hello! Can you introduce yourself?" } ], max_tokens: 100 }) }); const data = await response.json(); console.log(data); ``` -------------------------------- ### Export MegaLLM API Key Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/quick-start.mdx This command exports your MegaLLM API key as an environment variable. This key is required for authenticating all subsequent API requests to MegaLLM services. ```bash export MEGALLM_API_KEY="your-api-key" ``` -------------------------------- ### Configure OpenAI API Format Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/quick-start.mdx Sets the base URL and API key for using OpenAI-compatible endpoints with MegaLLM. This allows you to leverage existing OpenAI client libraries or tools. ```bash export OPENAI_BASE_URL="https://ai.megallm.io/v1" export OPENAI_API_KEY=$MEGALLM_API_KEY ``` -------------------------------- ### Configure Anthropic API Format Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/quick-start.mdx Sets the base URL and API key for using Anthropic-compatible endpoints with MegaLLM. This enables integration with tools designed for the Anthropic API. ```bash export ANTHROPIC_BASE_URL="https://ai.megallm.io" export ANTHROPIC_API_KEY=$MEGALLM_API_KEY ``` -------------------------------- ### Example of Clear vs. Vague Instructions Python Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/anthropic/messages.mdx Demonstrates the difference between clear and vague user instructions for a language model. The 'Good' example provides specific, numbered tasks (identify bugs, suggest improvements, rate quality) and includes the code directly. The 'Less effective' example is vague, asking the model to 'tell me about it,' which yields less precise results. ```python # Good - Clear and specific messages = [{ "role": "user", "content": """Analyze this Python code: 1. Identify any bugs 2. Suggest performance improvements 3. Rate code quality (1-10) Code: ```python def fibonacci(n): if n <= 1: return n return fibonacci(n-1) + fibonacci(n-2) ```""" }] # Less effective - Vague messages = [{ "role": "user", "content": "Look at this fibonacci function and tell me about it" }] ``` -------------------------------- ### POST /v1/chat/completions Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/quick-start.mdx This endpoint is used for chat completions, allowing you to send a conversation history and receive a model's response. It supports OpenAI-compatible formatting. ```APIDOC ## POST /v1/chat/completions ### Description This endpoint facilitates chat-based interactions with the MegaLLM API, enabling you to generate AI responses based on conversation history. ### Method POST ### Endpoint https://ai.megallm.io/v1/chat/completions ### Parameters #### Headers - **Authorization** (string) - Required - Bearer token for authentication. - **Content-Type** (string) - Required - Must be set to `application/json`. #### Request Body - **model** (string) - Required - The model to use for chat completions (e.g., "gpt-4"). - **messages** (array) - Required - A list 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. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate in the response. ### Request Example ```json { "model": "gpt-4", "messages": [ { "role": "user", "content": "Hello! Can you introduce yourself?" } ], "max_tokens": 100 } ``` ### Response #### Success Response (200) - **id** (string) - Unique identifier for the chat completion. - **object** (string) - Type of object returned (e.g., "chat.completion"). - **created** (integer) - Unix timestamp of when the completion was created. - **model** (string) - The model used for the completion. - **choices** (array) - A list of completion choices. - **index** (integer) - The index of the choice. - **message** (object) - The message content from the assistant. - **role** (string) - The role of the message sender ('assistant'). - **content** (string) - The assistant's response. - **finish_reason** (string) - The reason the model stopped generating tokens (e.g., "stop"). - **usage** (object) - Token usage statistics. - **prompt_tokens** (integer) - Number of tokens in the prompt. - **completion_tokens** (integer) - Number of tokens in the completion. - **total_tokens** (integer) - Total tokens used. #### Response Example ```json { "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "model": "gpt-4", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Hello! I'm an AI assistant powered by MegaLLM..." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 25, "total_tokens": 35 } } ``` ``` -------------------------------- ### Run Development Server (Bash) Source: https://github.com/megallm/megallm-docs/blob/main/README.md Commands to start the Next.js development server using different package managers (npm, pnpm, yarn). This is essential for local development and testing. ```bash npm run dev # or pnpm dev # or yarn dev ``` -------------------------------- ### Environment Variable Setup for API Key Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/authentication.mdx Shows how to set the MEGALLM_API_KEY environment variable across different operating systems and Docker configurations to securely manage API keys. ```bash # Add to ~/.bashrc or ~/.zshrc export MEGALLM_API_KEY="your_api_key_here" # Or use a .env file echo "MEGALLM_API_KEY=your_api_key_here" >> .env ``` ```powershell # Set environment variable [System.Environment]::SetEnvironmentVariable("MEGALLM_API_KEY", "your_api_key_here", "User") # Or use command prompt setx MEGALLM_API_KEY "your_api_key_here" ``` ```dockerfile # In Dockerfile ENV MEGALLM_API_KEY=${MEGALLM_API_KEY} # Or in docker-compose.yml environment: - MEGALLM_API_KEY=${MEGALLM_API_KEY} ``` -------------------------------- ### Make First API Request using cURL Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/quick-start.mdx Demonstrates how to make a chat completion request to the MegaLLM API using cURL. It includes setting the Authorization header and sending a JSON payload with a user message. ```bash curl https://ai.megallm.io/v1/chat/completions \ -H "Authorization: Bearer $MEGALLM_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4", "messages": [ { "role": "user", "content": "Hello! Can you introduce yourself?" } ], "max_tokens": 100 }' ``` -------------------------------- ### Python Quick Example: Anthropic API Call Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/anthropic/index.mdx Demonstrates how to initialize the Anthropic client with MegaLLM's base URL and make a message creation request. Requires the 'anthropic' Python SDK. ```python from anthropic import Anthropic # Initialize client client = Anthropic( base_url="https://ai.megallm.io", api_key="your-api-key" ) # Create a message message = client.messages.create( model="claude-3.5-sonnet", max_tokens=100, messages=[ { "role": "user", "content": "Explain the theory of relativity in simple terms" } ] ) print(message.content[0].text) ``` -------------------------------- ### Make First API Request using Go Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/quick-start.mdx This Go code snippet demonstrates how to make a chat completion request to the MegaLLM API. It constructs the JSON payload, sets the necessary headers including Authorization, and sends the HTTP POST request. ```go package main import ( "bytes" "encoding/json" "net/http" "os" ) func main() { payload := map[string]interface{}{ "model": "gpt-4", "messages": []map[string]string{ { "role": "user", "content": "Hello! Can you introduce yourself?", }, }, "max_tokens": 100, } body, _ := json.Marshal(payload) req, _ := http.NewRequest("POST", "https://ai.megallm.io/v1/chat/completions", bytes.NewBuffer(body)) req.Header.Set("Authorization", "Bearer " + os.Getenv("MEGALLM_API_KEY")) req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, _ := client.Do(req) defer resp.Body.Close() } ``` -------------------------------- ### Device Flow Authentication - Start Process Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/authentication.mdx Initiates the device flow authentication process via a POST request to the MegaLLM API. This method returns device_code, user_code, and verification_uri for user interaction. ```bash curl -X POST https://ai.megallm.io/auth/device \ -H "Content-Type: application/json" ``` -------------------------------- ### JavaScript: Function Calling with OpenAI SDK Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/openai/function-calling.mdx This JavaScript example showcases how to use the OpenAI SDK to define tools (functions), send messages to the model with tool definitions, and handle tool calls. It includes defining a `getWeather` function and processing the model's response to execute the function and get a follow-up. ```javascript import OpenAI from 'openai'; const openai = new OpenAI({ baseURL: 'https://ai.megallm.io/v1', apiKey: process.env.GITHUB_TOKEN, }); // Define your function function getWeather(location, unit = 'celsius') { // Simulate API call return { location, temperature: 22, unit, condition: 'sunny' }; } // Define tools const tools = [ { type: 'function', function: { name: 'get_weather', description: 'Get the current weather in a given location', parameters: { type: 'object', properties: { location: { type: 'string', description: 'The city and state, e.g. San Francisco, CA' }, unit: { type: 'string', enum: ['celsius', 'fahrenheit'], description: 'Temperature unit' } }, required: ['location'] } } } ]; // Send message with tools const response = await openai.chat.completions.create({ model: 'gpt-4', messages: [ { role: 'user', content: "What's the weather in London?" } ], tools, tool_choice: 'auto' }); const message = response.choices[0].message; if (message.tool_calls) { const messages = [ { role: 'user', content: "What's the weather in London?" }, message ]; for (const toolCall of message.tool_calls) { const functionName = toolCall.function.name; const functionArgs = JSON.parse(toolCall.function.arguments); if (functionName === 'get_weather') { const result = getWeather(functionArgs.location, functionArgs.unit); messages.push({ role: 'tool', tool_call_id: toolCall.id, content: JSON.stringify(result) }); } } const followUp = await openai.chat.completions.create({ model: 'gpt-4', messages }); console.log(followUp.choices[0].message.content); } ``` -------------------------------- ### Bash: Anthropic API Authentication Example Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/anthropic/index.mdx Demonstrates how to make an authenticated request to the Anthropic-compatible endpoints using cURL, including the necessary headers for API key and version. ```bash curl https://ai.megallm.io/v1/messages \ -H "x-api-key: $MEGALLM_API_KEY" \ -H "anthropic-version: 2023-06-01" \ -H "content-type: application/json" \ -d '{ "model": "claude-3.5-sonnet", "max_tokens": 100, "messages": [{"role": "user", "content": "Hello!"}] }' ``` -------------------------------- ### Python SDK Integration Source: https://context7.com/megallm/megallm-docs/llms.txt Examples demonstrating how to use the official Anthropic Python SDK with the MegaLLM endpoint for various use cases including basic messages, system messages, multi-turn conversations, and vision support. ```APIDOC ### Python Anthropic SDK Integration Use MegaLLM with the official Anthropic Python SDK. Access Claude models and other LLMs through the same familiar interface. ```python from anthropic import Anthropic import os # Initialize client with MegaLLM endpoint client = Anthropic( base_url="https://ai.megallm.io", api_key=os.environ.get("MEGALLM_API_KEY") ) # Basic message message = client.messages.create( model="claude-3.5-sonnet", max_tokens=1024, messages=[ { "role": "user", "content": "Write a Python function to validate email addresses" } ] ) print(message.content[0].text) # With system message message = client.messages.create( model="claude-opus-4-1-20250805", max_tokens=2048, system="You are a creative writing assistant specializing in science fiction.", messages=[ { "role": "user", "content": "Write the opening paragraph of a sci-fi novel set in 2157" } ], temperature=0.9 ) print(message.content[0].text) # Multi-turn conversation class ConversationManager: def __init__(self, model="claude-3.5-sonnet", system=None): self.client = Anthropic( base_url="https://ai.megallm.io", api_key=os.environ.get("MEGALLM_API_KEY") ) self.model = model self.system = system self.messages = [] def send_message(self, content, max_tokens=1024): self.messages.append({ "role": "user", "content": content }) response = self.client.messages.create( model=self.model, max_tokens=max_tokens, system=self.system, messages=self.messages ) assistant_message = response.content[0].text self.messages.append({ "role": "assistant", "content": assistant_message }) return assistant_message # Usage conversation = ConversationManager( model="claude-3.5-sonnet", system="You are a helpful Python programming tutor." ) response1 = conversation.send_message("What are Python decorators?") print(response1) response2 = conversation.send_message("Can you show me an example?") print(response2) # Vision support message = client.messages.create( model="claude-3.5-sonnet", max_tokens=1024, messages=[ { "role": "user", "content": [ { "type": "image", "source": { "type": "url", "url": "https://example.com/diagram.png" } }, { "type": "text", "text": "Explain what this diagram shows" } ] } ] ) print(message.content[0].text) ``` ``` -------------------------------- ### Make First API Request using Python Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/quick-start.mdx Shows how to send a chat completion request to the MegaLLM API using Python's requests library. It configures the request headers and sends a JSON body containing the model, messages, and max_tokens. ```python import requests import os response = requests.post( "https://ai.megallm.io/v1/chat/completions", headers={ "Authorization": f"Bearer {os.getenv('MEGALLM_API_KEY')}", "Content-Type": "application/json" }, json={ "model": "gpt-4", "messages": [ { "role": "user", "content": "Hello! Can you introduce yourself?" } ], "max_tokens": 100 } ) print(response.json()) ``` -------------------------------- ### Python Chat Completion with OpenAI SDK Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/openai/index.mdx Example demonstrating how to initialize the OpenAI client with MegaLLM's base URL and make a chat completion request. Requires the 'openai' Python package. ```python from openai import OpenAI # Initialize client client = OpenAI( base_url="https://ai.megallm.io/v1", api_key="your-api-key" ) # Simple chat completion response = client.chat.completions.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Explain quantum computing in simple terms."} ], temperature=0.7, max_tokens=150 ) print(response.choices[0].message.content) ``` -------------------------------- ### Python: Anthropic Tool Use Schema Example Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/anthropic/index.mdx Shows the schema format for defining tools when using the Anthropic API, specifically highlighting the use of 'input_schema' for function definitions. ```python tools = [ { "name": "get_weather", "description": "Get weather for a location", "input_schema": { # Note: input_schema, not parameters "type": "object", "properties": { "location": { "type": "string", "description": "City name" } }, "required": ["location"] } } ] ``` -------------------------------- ### Python Function Calling Example Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/openai/function-calling.mdx Demonstrates how to set up and use function calling with the OpenAI client for MegaLLM. It includes defining a sample function, creating tool descriptions in JSON Schema format, and making a chat completion request with tools enabled. ```python from openai import OpenAI import json client = OpenAI( base_url="https://ai.megallm.io/v1", api_key="your-api-key" ) # Define your functions def get_weather(location: str, unit: str = "celsius"): """Get the current weather for a location""" # Simulate API call return { "location": location, "temperature": 22, "unit": unit, "condition": "sunny" } # Define tools for the AI tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit" } }, "required": ["location"] } } } ] # Send message with tools response = client.chat.completions.create( model="gpt-4", messages=[ {"role": "user", "content": "What's the weather in London?"} ], tools=tools, tool_choice="auto" # Let the model decide ) ``` -------------------------------- ### Chat Completions API - Bash Source: https://context7.com/megallm/megallm-docs/llms.txt Demonstrates how to create chat completions using the MegaLLM API in an OpenAI-compatible format. This example uses cURL to send a request with a system message, user message, and parameters like max_tokens and temperature. It requires an API key for authorization. ```bash # Basic Chat Completion curl https://ai.megallm.io/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "model": "gpt-5", "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "What is the capital of France?" } ], "max_tokens": 100, "temperature": 0.7 }' # Response: # { # "id": "chatcmpl-abc123", # "object": "chat.completion", # "created": 1732291200, # "model": "gpt-5", # "choices": [{ # "index": 0, # "message": { # "role": "assistant", # "content": "The capital of France is Paris." # }, ``` -------------------------------- ### Provider-Diversified Fallback Configuration Source: https://context7.com/megallm/megallm-docs/llms.txt This code example illustrates configuring fallback models from different AI providers. By listing models from various providers (OpenAI, Anthropic, Google, Meta), it enhances resilience against provider-specific outages or performance issues. The primary model is also specified. ```python response = client.chat.completions.create( model="gpt-5", # OpenAI messages=messages, fallback_models=[ "claude-opus-4-1-20250805", # Anthropic "gemini-2.5-pro", # Google "llama-3.3-70b" # Meta ] ) ``` -------------------------------- ### Python Embeddings Integration Source: https://context7.com/megallm/megallm-docs/llms.txt Provides Python code examples for generating embeddings using the Megallm API via the OpenAI SDK. Includes examples for single and batch embeddings, and calculating cosine similarity. ```APIDOC ## Python Embeddings Integration ### Description This section provides practical Python code snippets for utilizing the Megallm Embeddings API. It demonstrates how to initialize the client, generate embeddings for single or multiple text inputs, and includes a utility function for calculating cosine similarity to find related documents. ### Language Python ### Setup Ensure you have the `openai` and `numpy` libraries installed: ```bash pip install openai numpy ``` ### Client Initialization ```python from openai import OpenAI import numpy as np client = OpenAI( base_url="https://ai.megallm.io/v1", api_key="your-api-key" ) ``` ### Single Text Embedding ```python response = client.embeddings.create( model="text-embedding-3-large", input="Machine learning is transforming software development" ) embedding = response.data[0].embedding print(f"Embedding dimension: {len(embedding)}") ``` ### Batch Embeddings ```python documents = [ "Python is a programming language", "JavaScript is used for web development", "Machine learning uses neural networks", "Databases store structured data" ] response = client.embeddings.create( model="text-embedding-3-large", input=documents ) embeddings = [item.embedding for item in response.data] print(f"Created {len(embeddings)} embeddings") ``` ### Cosine Similarity Function ```python def cosine_similarity(a, b): return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) ``` ### Finding Most Similar Documents ```python query = "What programming languages are there?" query_response = client.embeddings.create( model="text-embedding-3-large", input=query ) query_embedding = query_response.data[0].embedding similarities = [] for i, doc_embedding in enumerate(embeddings): similarity = cosine_similarity(query_embedding, doc_embedding) similarities.append((documents[i], similarity)) similarities.sort(key=lambda x: x[1], reverse=True) print(f"\nQuery: {query}") print("\nMost similar documents:") for doc, score in similarities: print(f" {score:.4f}: {doc}") ``` ``` -------------------------------- ### Get Function Calling Models in Python Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/api/models.mdx Retrieves models that support function calling from the Megallm API. This function makes a GET request to the API endpoint and filters the JSON response to return only models with the 'supports_function_calling' capability enabled. It requires the 'requests' library. ```python import requests def get_function_calling_models(): response = requests.get('https://yourdomain.com/api/models') data = response.json() return [ model for model in data['data'] if model['capabilities']['supports_function_calling'] ] ``` -------------------------------- ### JavaScript/TypeScript Node.js MegaLLM Integration Source: https://context7.com/megallm/megallm-docs/llms.txt This snippet shows how to integrate MegaLLM with Node.js applications using the OpenAI JavaScript SDK. It covers initialization, basic chat completion, streaming responses, function calling, generating embeddings, managing multi-model conversations, and implementing error handling with retries. Ensure you have the 'openai' package installed (`npm install openai`) and set your `MEGALLM_API_KEY` environment variable. ```javascript // npm install openai import OpenAI from 'openai'; const client = new OpenAI({ baseURL: 'https://ai.megallm.io/v1', apiKey: process.env.MEGALLM_API_KEY }); // Basic chat completion async function basicChat() { const completion = await client.chat.completions.create({ model: 'gpt-5', messages: [ { role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: 'Write a function to reverse a string in JavaScript' } ], max_tokens: 500 }); console.log(completion.choices[0].message.content); } // Streaming response async function streamingChat() { const stream = await client.chat.completions.create({ model: 'claude-3.5-sonnet', messages: [{ role: 'user', content: 'Count from 1 to 20' }], stream: true }); for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content || ''; process.stdout.write(content); } console.log('\n'); } // Function calling async function functionCallingExample() { const completion = await client.chat.completions.create({ model: 'gpt-5', messages: [ { role: 'user', content: 'What is the weather in Tokyo and Paris?' } ], tools: [{ type: 'function', function: { name: 'get_weather', description: 'Get the current weather in a location', parameters: { type: 'object', properties: { location: { type: 'string', description: 'The city name' }, unit: { type: 'string', enum: ['celsius', 'fahrenheit'] } }, required: ['location'] } } }], tool_choice: 'auto' }); const message = completion.choices[0].message; if (message.tool_calls) { console.log('Function calls requested:'); message.tool_calls.forEach(call => { console.log(` ${call.function.name}(${call.function.arguments})`); }); } } // Embeddings async function createEmbeddings() { const response = await client.embeddings.create({ model: 'text-embedding-3-large', input: 'Machine learning is fascinating' }); const embedding = response.data[0].embedding; console.log(`Embedding dimension: ${embedding.length}`); console.log(`First 5 values: ${embedding.slice(0, 5)}`); } // Multi-model conversation manager class MultiModelChat { constructor(apiKey) { this.client = new OpenAI({ baseURL: 'https://ai.megallm.io/v1', apiKey: apiKey }); this.messages = []; } async send(content, model = 'gpt-5') { this.messages.push({ role: 'user', content }); const completion = await this.client.chat.completions.create({ model: model, messages: this.messages }); const response = completion.choices[0].message.content; this.messages.push({ role: 'assistant', content: response }); return response; } switchModel(newModel) { console.log(`Switching to model: ${newModel}`); return this; } clear() { this.messages = []; } } // Usage const chat = new MultiModelChat(process.env.MEGALLM_API_KEY); await chat.send('Explain async/await', 'gpt-5'); await chat.send('Give me an example', 'claude-3.5-sonnet'); await chat.switchModel('gemini-2.5-pro').send('Can you simplify it?'); // Error handling with retry async function chatWithRetry(messages, model = 'gpt-5', maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { return await client.chat.completions.create({ model, messages, timeout: 30000 }); } catch (error) { console.error(`Attempt ${i + 1} failed:`, error.message); if (i === maxRetries - 1) throw error; await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1))); } } } // Run examples await basicChat(); await streamingChat(); await functionCallingExample(); await createEmbeddings(); ``` -------------------------------- ### CLI Tool Authentication Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/authentication.mdx Retrieves an API token using the MegaLLM command-line interface tool. This is a convenient method for users who have the CLI installed. ```bash megallm auth token ``` -------------------------------- ### OpenAI API - Base URL and Client Initialization Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/openai/index.mdx Information on how to configure your client to use MegaLLM's OpenAI-compatible API, including the base URL and API key. ```APIDOC ## OpenAI API Base URL All OpenAI-compatible endpoints are available at the following base URL: `https://ai.megallm.io/v1` ### Client Initialization Example (Python) ```python from openai import OpenAI client = OpenAI( base_url="https://ai.megallm.io/v1", api_key="your-api-key" ) ``` ### Migration Guide To migrate from OpenAI to MegaLLM, simply update the `base_url` and use your MegaLLM API key. ```python # Before (OpenAI) # client = OpenAI(api_key="sk-...") # After (MegaLLM) client = OpenAI( base_url="https://ai.megallm.io/v1", api_key="your-api-key" ) ``` ``` -------------------------------- ### POST /v1/chat/completions Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/openai/streaming.mdx The chat completions endpoint allows you to send a prompt to the model and receive a streaming or non-streaming response. This documentation provides examples for browser JavaScript, React, and cURL. ```APIDOC ## POST /v1/chat/completions ### Description This endpoint provides chat completion functionality, allowing users to interact with the AI model by sending messages and receiving responses. It supports streaming responses for real-time updates. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Request Body - **model** (string) - Required - The model to use for chat completions (e.g., "gpt-4"). - **messages** (array) - Required - An array of message objects, where each object has a `role` (user or assistant) and `content`. - **stream** (boolean) - Optional - Whether to stream the response. Defaults to false. ### Request Example ```json { "model": "gpt-4", "messages": [{"role": "user", "content": "Hello!"}], "stream": true } ``` ### Response #### Success Response (200) - **id** (string) - Unique identifier for the chat completion. - **object** (string) - Type of object, e.g., "chat.completion.chunk". - **created** (integer) - Unix timestamp of creation. - **model** (string) - The model used for the completion. - **choices** (array) - An array of completion choices. Each choice contains: - **index** (integer) - Index of the choice. - **delta** (object) - The generated content delta. May contain `role`, `content`, or be empty. - **finish_reason** (string|null) - The reason the model stopped generating text (e.g., "stop", null). #### Response Example (Streaming Chunk) ```json data: { "id": "chatcmpl-abc123", "object": "chat.completion.chunk", "created": 1677858242, "model": "gpt-4", "choices": [ { "index": 0, "delta": { "content": "Hello" }, "finish_reason": null } ] } ``` #### Response Example (Stream End) ``` data: [DONE] ``` ### Error Handling - **400 Bad Request**: Invalid request body or parameters. - **401 Unauthorized**: Invalid API key or authentication. - **404 Not Found**: Endpoint not found. - **500 Internal Server Error**: Server-side error. ``` -------------------------------- ### Python Example: Switching Models with MegaLLM Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/index.mdx Demonstrates how to use the OpenAI client with MegaLLM's base URL to switch between different AI models for various tasks like reasoning, code generation, and creative writing. It highlights the flexibility of using a single API endpoint for diverse model capabilities. ```python from openai import OpenAI client = OpenAI( base_url="https://ai.megallm.io/v1", api_key="your-api-key" ) # Try GPT-5 for complex reasoning response = client.chat.completions.create( model="gpt-5", messages=[{"role": "user", "content": "Analyze this data..."}] ) # Switch to Grok for code generation response = client.chat.completions.create( model="xai/grok-code-fast-1", messages=[{"role": "user", "content": "Write a Python function..."}] ) # Use Claude for creative writing response = client.chat.completions.create( model="claude-opus-4-1-20250805", messages=[{"role": "user", "content": "Write a story about..."}] ) ``` -------------------------------- ### GET /api/models Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/api/models.mdx Retrieves a list of all available models and their detailed specifications, including pricing, capabilities, and context length. This endpoint provides the same information as the Models Catalog page. ```APIDOC ## GET /api/models ### Description Retrieves a list of all available models and their detailed specifications, including pricing, capabilities, and context length. This endpoint provides the same information as the Models Catalog page. ### Method GET ### Endpoint /api/models ### Parameters This endpoint does not accept any path or query parameters. ### Request Body This endpoint does not accept a request body. ### Request Example ```bash curl -X GET https://yourdomain.com/api/models ``` ### Response #### Success Response (200) Returns a JSON object containing a success status, an array of model objects, the total count of models, and the last updated timestamp. - **success** (boolean) - Indicates if the request was successful. - **data** (array) - An array of model objects, each detailing a specific model. - **total** (number) - The total number of models available. - **lastUpdated** (string) - The ISO timestamp of when the model data was last updated. ##### Model Object Fields - **id** (string) - The unique identifier for the model, used in API calls. - **object** (string) - The type of object, always "model". - **type** (string) - The category of the model (e.g., "chat", "embedding"). - **created_at** (string) - The ISO timestamp indicating when the model was created. - **owned_by** (string) - The entity or organization that owns the model (e.g., "openai"). - **display_name** (string) - A human-readable name for the model. - **capabilities** (object) - An object detailing the model's supported features. - **pricing** (object) - An object containing pricing information for the model. - **context_length** (number) - The maximum number of tokens the model can process in its context window. - **max_output_tokens** (number) - The maximum number of tokens the model can generate in a single response. ###### Capabilities Object - **supports_function_calling** (boolean) - True if the model supports function or tool calling. - **supports_vision** (boolean) - True if the model supports image or vision processing. - **supports_streaming** (boolean) - True if the model supports streaming responses. - **supports_structured_output** (boolean) - True if the model supports generating structured output (e.g., JSON). ###### Pricing Object - **input_tokens_cost_per_million** (number) - The cost per million input tokens. - **output_tokens_cost_per_million** (number) - The cost per million output tokens. - **currency** (string) - The currency of the pricing (always "USD"). #### Response Example ```json { "success": true, "data": [ { "id": "gpt-4o-mini", "object": "model", "type": "chat", "created_at": "2024-07-18T00:00:00.000Z", "owned_by": "openai", "display_name": "GPT-4o mini", "capabilities": { "supports_function_calling": true, "supports_vision": true, "supports_streaming": true, "supports_structured_output": true }, "pricing": { "input_tokens_cost_per_million": 0.15, "output_tokens_cost_per_million": 0.6, "currency": "USD" }, "context_length": 128000, "max_output_tokens": 16384 } ], "total": 89, "lastUpdated": "2025-01-29T10:30:00.000Z" } ``` #### Error Response (Example - 500 Internal Server Error) ```json { "success": false, "error": { "code": "internal_error", "message": "An unexpected error occurred on the server." } } ``` ``` -------------------------------- ### Initialize MegaLLM Client with Anthropic Format Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/faq.mdx Demonstrates how to initialize the MegaLLM client using the Anthropic format. This involves specifying the base URL and your API key, allowing access to models through a familiar interface. ```python from megallm import Anthropic client = Anthropic( base_url="https://ai.megallm.io", api_key="your-megallm-key" ) ``` -------------------------------- ### Python SDK: Message with System Prompt Source: https://context7.com/megallm/megallm-docs/llms.txt This Python example shows how to send a message to MegaLLM with a system prompt using the Anthropic SDK. This allows you to define the AI's persona or provide specific instructions. The code initializes the client and then uses `messages.create` with `system`, `model`, `max_tokens`, and `messages` parameters. ```python from anthropic import Anthropic import os # Initialize client with MegaLLM endpoint client = Anthropic( base_url="https://ai.megallm.io", api_key=os.environ.get("MEGALLM_API_KEY") ) # With system message message = client.messages.create( model="claude-opus-4-1-20250805", max_tokens=2048, system="You are a creative writing assistant specializing in science fiction.", messages=[{ "role": "user", "content": "Write the opening paragraph of a sci-fi novel set in 2157" }], temperature=0.9 ) print(message.content[0].text) ``` -------------------------------- ### Automatic Fallback Configuration (Python) Source: https://context7.com/megallm/megallm-docs/llms.txt Configures the OpenAI client in Python to automatically fall back to alternative models when primary models are unavailable or rate-limited. This example demonstrates setting the base URL for the API. ```python from openai import OpenAI client = OpenAI( base_url="https://ai.megallm.io/v1", api_key="your-api-key" ) ``` -------------------------------- ### Python SDK: Basic Chat Completion Source: https://context7.com/megallm/megallm-docs/llms.txt Initializes the OpenAI Python SDK client with the MegaLLM base URL and an API key. Sends a basic chat completion request and prints the assistant's response. ```python from openai import OpenAI import os # Initialize client with MegaLLM endpoint client = OpenAI( base_url="https://ai.megallm.io/v1", api_key=os.environ.get("MEGALLM_API_KEY") ) # Basic completion with GPT-5 response = client.chat.completions.create( model="gpt-5", messages=[ {"role": "system", "content": "You are a helpful coding assistant."}, {"role": "user", "content": "Write a Python function to calculate fibonacci numbers"} ], max_tokens=500, temperature=0.7 ) print(response.choices[0].message.content) ``` -------------------------------- ### Python SDK: Vision Support Source: https://context7.com/megallm/megallm-docs/llms.txt This Python example demonstrates using the Anthropic SDK with MegaLLM for vision capabilities. It shows how to send a message that includes both an image (referenced by URL) and text instructions. The `content` parameter is a list containing a dictionary for the image source and another for the text prompt. ```python from anthropic import Anthropic import os # Initialize client with MegaLLM endpoint client = Anthropic( base_url="https://ai.megallm.io", api_key=os.environ.get("MEGALLM_API_KEY") ) # Vision support message = client.messages.create( model="claude-3.5-sonnet", max_tokens=1024, messages=[ { "role": "user", "content": [ { "type": "image", "source": { "type": "url", "url": "https://example.com/diagram.png" } }, { "type": "text", "text": "Explain what this diagram shows" } ] } ] ) ``` -------------------------------- ### OpenAI API Error Response Example Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/openai/index.mdx Shows the structure of an error response returned by MegaLLM, which is compatible with OpenAI's error format. Useful for understanding and handling API errors. ```json { "error": { "message": "Invalid request parameter", "type": "invalid_request_error", "param": "temperature", "code": null } } ``` -------------------------------- ### OpenAI SDK Integration with MegaLLM Source: https://github.com/megallm/megallm-docs/blob/main/content/docs/getting-started/authentication.mdx Demonstrates how to use the OpenAI Python SDK to interact with MegaLLM API. It configures the client with the MegaLLM base URL and API key from environment variables. ```python from openai import OpenAI import os client = OpenAI( base_url="https://ai.megallm.io/v1", api_key=os.getenv("MEGALLM_API_KEY") ) response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "Hello!"}] ) ```