Skip to main content

This library provides the functions to complete the conversation via SSE between client and LLM

Project description

AXMP AI Conversation Completor

A Python library for handling Server-Sent Events (SSE) communication between clients and Large Language Models (LLMs). This library provides a robust way to stream AI responses and handle tool calls in real-time conversations.

Features

  • Server-Sent Events (SSE) streaming support
  • Real-time AI message streaming
  • Tool calls handling and streaming
  • Structured response formatting
  • Async/await support
  • Type hints and validation using Pydantic models

Installation

pip install axmp-ai-conversation-completor

Quick Start

from axmp_ai_conversation_completor import generate_sse_response

@router.post(
    "/conversations/{thread_id}/completion",
    summary="Chat with the alert AI agent",
    response_class=EventSourceResponse,
    response_model=EventStreamResponse,
)
async def conversations_completion(
    request: Request,
    aiops_chat_request: AIOpsChatRequest,
    thread_id: str = Path(..., description="Thread ID"),
) -> EventSourceResponse:
    """docstring..."""
    # some source...
    config = RunnableConfig(
        configurable=Configuration(
            thread_id=thread_id,
            user_id=user_id,
            model=llm_model,
            temperature=temperature,
            max_tokens=aiops_settings.chatops_model_max_tokens,
        ).model_dump(),
        recursion_limit=recursion_limit,
    )

    messages = [chat_request.prompt]

    inputs = ChatOpsState(
        messages=[
            HumanMessage(content=messages[0]),
        ]
    )

    response = chatops_agent.astream(inputs, config, stream_mode=stream_mode)

    return EventSourceResponse(
        generate_sse_response(
            response,
            thread_id,
            llm_model=llm_model,
            stream_mode=stream_mode
        )
    )

API Reference

generate_sse_response(response, thread_id, *, llm_model=None, stream_mode="messages")

Generates Server-Sent Events (SSE) responses for AI conversations.

Parameters:

  • response (AsyncIterator[dict[str, Any] | Any]): The response iterator from the LLM
  • thread_id (str): Unique identifier for the conversation thread
  • llm_model (str | None): Name of the LLM model being used
  • stream_mode (str): Mode of streaming, defaults to "messages"

Returns:

  • AsyncIterator[EventStreamResponse]: An async iterator yielding SSE events

Event Types:

  • MESSAGE_START: Indicates the start of a new message
  • CONTENT_BLOCK_START: Indicates the start of a content block
  • CONTENT_BLOCK_DELTA: Contains incremental updates to the content
  • CONTENT_BLOCK_STOP: Indicates the end of a content block
  • MESSAGE_STOP: Indicates the end of a message

Project Structure

src/axmp_ai_conversation_completor/
├── __init__.py
├── completion/
│   └── sse_reponse_generator.py
├── scheme/
│   └── chat_messages.py
└── util/
    └── graph_stream_utils.py

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Authors

Acknowledgments

  • Thanks to all contributors who have helped shape this library

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

axmp_ai_conversation_completor-0.1.8.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file axmp_ai_conversation_completor-0.1.8.tar.gz.

File metadata

File hashes

Hashes for axmp_ai_conversation_completor-0.1.8.tar.gz
Algorithm Hash digest
SHA256 c91e0fe773b730fef6f5c40aaeeca78cc70736457ebd7a6177b29f7b0535f48b
MD5 9471c9ab75bbf123b8b1223a9e6ef48d
BLAKE2b-256 5a17621022536650560469384795ce746210e708a060f35e9e9b4819073be097

See more details on using hashes here.

File details

Details for the file axmp_ai_conversation_completor-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for axmp_ai_conversation_completor-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 35e96997d2f01f690110562d1ca9c61b6f331f7427bf55a886e65bb0f0b59adf
MD5 85b9732649eb122d80745b50197c7e48
BLAKE2b-256 d3c818f1e969abcba46cba6d323807aec51899552f8f11fe69b9580b8d56e649

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page