API Doc-CometAPI
HomeDashBoardModel Marketplace
HomeDashBoardModel Marketplace
Discord_Support
  1. Integration Guides
  • GET START
    • Quick Start
    • Important Guidelines
    • Release Notes
  • API Reference
    • Error Codes & Handling
    • Text Models-openai format
      • Chat
      • response
      • gpt-4o-image generates image
      • Images
      • Image Editing (gpt-image-1)
      • Recognizing Images
      • Embeddings
      • Realtime
      • Models
      • Hunyuan3D
    • Anthropic Compatiable
      • Anthropic Claude
    • Image Models
      • Midjourney(images)
        • Quick Tutorial - Complete Process in One Go
        • Task Fetching API
          • List by Condition
          • Fetch Single Task (most recommended)
        • Imagine
        • Submit Video
        • Submit Editor
        • Action (UPSCALE; VARIATION; REROLL; ZOOM, etc.)
        • Blend (image -> image)
        • Describe (image -> text)
        • Modal (Area Redesign & Zoom)
      • Flux(images)
        • Generate image (replicate format)
        • Create Task - General
        • flux fine-tune images(Temporarily unavailable)
        • flux generate image(Temporarily unavailable)
        • flux query
      • Replicate(image)
        • Create Task - General
        • Create Task -flux-kontext-pro、max
        • Create Task -flux-1.1-pro
        • Create Task -flux-1.1-pro-ultra
        • replicate query
      • Recraft(images)
        • Appendix
        • Recraft Generate Image
        • Recraft Vectorize Image
        • Recraft Remove Background
        • Recraft Clarity Upscale
        • Recraft Create style
        • Recraft Generative Upscale
      • Ideogram(images)(Temporarily removed)
        • Official documentation (updated in real time)
        • Generate 3.0 (text to image)
        • Remix 3.0 (hybrid image)
        • Reframe 3.0(Reconstruction)
        • Replace Background 3.0(Background replacement)
        • Edit 3.0(Editing images)
        • ideogram Text Raw Image
        • ideogram Hybrid image
        • ideogram enlargement HD
        • ideogram describes the image
        • ideogram Edit image((legacy))
    • Music Models
      • Suno
        • Setting suno Version
        • Suno API Scenario Application Guide
        • Generate lyrics
        • Generate music clip
        • Upload clip
        • Submit concatenation
        • Full Track Audio Separation
        • Single Track Audio Separation
        • Create New Persona
        • Single task query
        • Generate mp4 mv video
        • Timing: lyrics, audio timeline
        • Get wav format file
        • Batch query tasks
      • Udio(Temporarily unavailable)
        • Generate music
        • Task query
    • Video Models
      • veo3
        • veo3-chat format
        • Submit video generation task
        • Query video generation status
      • runway(video)
        • official format
          • runway images raw video
          • Generate a video from a video
          • Generate an image from text
          • Upscale a video
          • Control a character
          • runway to get task details
        • Reverse Format
          • generate(text)
          • generate(Reference images)
          • Video to Video Style Redraw
          • Act-one Expression Migration
          • feed-get task
      • kling (video)
        • callback_url
        • testing
          • Multimodal Video Editing (In Testing)
            • Initialize Video for Editing
            • Add Video Selection
            • Delete Video Selection
            • Clear Video Selection
            • Preview Selected Video Area
            • Create Task
        • Generating images
        • Expanded
        • Text Generation Video
        • Image Generation Video
        • Multi-Image To Video
        • Multi-Image to Image
        • Video Extension
        • virtual try-on
        • lip sync
        • effects
        • Video to audio
        • Text to audio
        • Individual queries
      • MiniMax Conch(video)
        • MiniMax Conch Official Documentation
        • MiniMax Conch Generation
        • MiniMax Conch Query
        • MiniMax Conch Download
      • luma (video)
        • Official api interface format
          • luma generate
          • luma search
      • bytedance
        • bytedance-video
        • bytedance-video get
        • bytedance-image-generation
        • bytedance-Image Editing
      • PIKA(video)
        • pika feed
        • PIKA Reference Video Generation
        • PIKA Reference Image Generation
        • PIKA reference text generation
      • sora(temporarily dismantle)
        • Reverse Format
          • Create Video
          • Query Video Task
          • Create Video
    • Audio Models
      • Create speech
      • Create transcription
      • Create translation
  • CODE EXAMPLES
    • Code example
  • Guides & Tutorials
    • Integration Guides
      • COMET API API Call Testing
      • OpenManus
      • Chatbox
      • CherryStudio
      • Cursor
      • COZE
      • Cline
      • ChatHub
      • Dify
      • LiteLLM
      • zapier
      • n8n
      • AnythingLLM
      • Immersive Translation
      • NEXT CHAT (ChatGPT Next Web)
      • ChatAll Translation
      • FastGPT
      • Lobe-Chat
      • Zotero
      • LangChain
      • Open WebUI
      • OpenAI Translator
      • Pot Translation
      • Obsidian's Text Generator Plugin
      • GPT Academic Optimization (gpt_academic)
      • gptme
      • avante.nvim
      • Eudic Translation
      • librechat
      • utools-ChatGPT Friend
      • IntelliJ Translation Plugin
      • Lazy Customer Service
      • MAKE
    • Best Practices
      • Claude Code Installation and Usage Guide
      • Gemini CLI Installation and Usage Guide
      • CometAPI Account Balance Query API Usage Instructions
      • Retry Logic Documentation for CometAPI and OpenAI Official API
      • Midjourney Best Practices
      • Runway Best Practices
  • Pricing & Billing
    • About Pricing
  • Support
    • Help Center
    • Confusion about use
    • Common Misconceptions
    • Terms of service
    • Privacy policy
    • Interface Stability
  1. Integration Guides

LiteLLM

Preparation
• Log in to cometapi. Click "ADD API key" in the API keys to get your token key: sk-xxxxx and baseurl: https://api.cometapi.com
image.png
image.png
image.png

Prerequisites#

Python 3.6+ installed.
Access to a CometAPI account. Generate an API key from the API Keys page.
Jupyter Notebook or a Python environment for running the examples (optional, but recommended for interactive testing).

Step 1: Install LiteLLM#

Install the LiteLLM library using pip. This is a one-time setup.

Step 2: Set Up Your API Key#

You need Paste the key you just got from CometAPI to authenticate requests.
1.
Set it as an environment variable (recommended for security) or pass it directly in your code.
Here's an example in Python:
Note: Using the environment variable is safer as it avoids hardcoding sensitive information in your scripts.

Step 3: Make a Basic Completion Call#

Use LiteLLM's completion function to send messages to a CometAPI model. You can specify models like cometapi/gpt-5 or cometapi/gpt-4o.
Method 1: Use the environment variable for the API key (recommended).
Method 2: Pass the API key explicitly.
Example:
Expected Output:
The code will print the model's responses, e.g.:
I'm doing well, thank you! How about you?
Hello! I'm doing great, thanks for asking. How can I assist you today?
This sends a simple user message and retrieves the model's completion. You can customize the messages array for more complex conversations (e.g., add system prompts or multi-turn chats).

Step 4: Asynchronous and Streaming Calls#

For non-blocking or real-time applications, use LiteLLM's acompletion function for asynchronous calls. This is useful with Python's asyncio for handling concurrency.
You can also enable streaming to receive responses in chunks (e.g., for live chat interfaces).
Example:
Explanation:
acompletion is the asynchronous version of completion.
stream=True enables streaming, where the response is yielded in real-time chunks.
Use asyncio to run the function (e.g., in a Jupyter Notebook with await or via asyncio.run() in scripts).
If an error occurs, it's caught and printed for debugging.
Expected Output:
You'll see the response object and individual chunks printed, e.g.:
Testing asynchronous completion with streaming
Response object: <async_generator object acompletion at 0x...>
Chunk: {'choices': [{'delta': {'content': 'Hello'}, 'index': 0}]}
Chunk: {'choices': [{'delta': {'content': '!'}, 'index': 0}]}
... (full response streamed in parts)

Additional Tips#

Supported Models: CometAPI models follow the format cometapi/<model-name>, e.g., cometapi/gpt-5, cometapi/gpt-4o, cometapi/chatgpt-4o-latest. Check the CometAPI documentation for the latest models.
Error Handling: Always wrap calls in try-except blocks to handle issues like invalid keys or network errors.
Advanced Features: LiteLLM supports parameters like temperature, max_tokens, and top_p for fine-tuning responses. Add them to the completion or acompletion calls, e.g., completion(..., temperature=0.7).
Security: Never commit your API key to version control. Use environment variables or secret managers.
Troubleshooting: If you encounter issues, ensure your API key is valid and check LiteLLM's logs. For more details, refer to the LiteLLM Documentation or CometAPI Doc.
Rate Limits and Costs: Monitor your API usage in the CometAPI console.

code example
Previous
Dify
Next
zapier
Built with