API Integration - Large Language Model.md

API Reference for Large Language Model (LLM)

Using LiteLLM

Installation

pip install litellm

Basic Usage with Streaming

Async Streaming Completion

The AI Marketplace API uses API keys for authentication. Please contact our team to acquire your API Key.

from litellm import acompletion
import json
import asyncio

async def stream_response():
    try:
        # Initialize the completion request
        response = await acompletion(
            model="{model-name}", 
            api_base="https://mkp-api.fptcloud.com",    # Base URL for API
            api_key="{api-key}",          # Your API key
            messages=[                    # List of message objects. Please update the System prompt to have the model respond appropriately
                {
                    "role": "system",
                    "content": "You are a helpful assistant capable of understanding a user's needs through conversation to recommend suitable services. Based on the conversation history and the user's last message, list services that can address the user's needs. Respond only in Vietnamese or English, matching the language of the user's input."
                },
                {
                    "role": "user",
                    "content": "{your-input-text}"
                }
            ],
            stream=True  # Enable streaming
        )
        # Process streaming response
        async for chunk in response:
            if chunk.choices[0].delta.content:
                yield f"data: {json.dumps({'content': chunk.choices[0].delta.content})}\n\n"
    except Exception as e:
        yield f"data: {json.dumps({'error': str(e)})}\n\n"
    yield "data: [DONE]\n\n"

async def main():
    async for data in stream_response():
        print(data)

if __name__ == '__main__':
    asyncio.run(main())

Python

cURL

Langchain

OpenAI

Nodejs

Key Parameters Explained

  • model: String identifier for the model you want to use

  • api_base: The base URL endpoint for your API

  • api_key: Your authentication API key

  • messages: List of message objects with the following structure:

    • role: Can be "system", "user", or "assistant"

    • content: The actual message content

  • stream: Boolean flag to enable streaming (set to True for streaming responses)

Response Format

The streaming response will yield chunks in the following format:

If an error occurs:

End of stream marker:

Error Handling

The code includes try-catch block to handle potential errors during the API call and streaming process. Any errors will be returned as JSON-encoded error messages in the stream.

Notes

  • Make sure to handle the async nature of the function with appropriate async/await syntax

  • The streaming response is formatted as Server-Sent Events (SSE)

  • Each chunk is JSON-encoded and prefixed with "data: "

  • The stream ends with a [DONE] marker

Last updated

Was this helpful?