Skip to main content

A Python 2+ library for conversations with LLMs providing an OpenAI Chat Completions-compatible API. That includes OpenAI's models, Google Gemini, DeepSeek, and anything you can run on ollama.

Project description

A Python 2+ library for conversations with LLMs providing an OpenAI Chat Completions-compatible API. That includes OpenAI's models, Google Gemini, DeepSeek, and anything you can run on ollama.

Features

  • Compatibility: Unlike openai, works on Python 2+ with zero non-Python dependencies.
  • Streaming Responses: Obtain responses as they're generated.
  • Conversation Persistence: Save/load conversations in JSON format.

Installation

pip install chat-completions-conversation

Usage

Suppose we interface with a VLM, and have the following messages.json file under the current working directory:

[
    
    {
        "role": "user",
        "content": [
            {
                "type": "image_url",
                "image_url": {
                    "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/640px-PNG_transparency_demonstration_1.png"
                }
            },
            {"type": "text", "text": "Describe what's in this image."},
            {"type": "text", "text": "Then, describe this dog."},
            {
                "type": "image_url",
                "image_url": {
                    "url": "https://images.unsplash.com/photo-1518717758536-85ae29035b6d"
                }
            },
            {"type": "text", "text": "What are the main differences between them?"}
        ]
    }
]

By running this code:

# coding=utf-8
from __future__ import print_function
from chat_completions_conversation import ChatCompletionsConversation

# Initialize conversation
conv = ChatCompletionsConversation(
    api_key=u'...',
    base_url=u'https://api.openai.com/v1',
    model=u'gpt-4.1'
)

# Load conversation
conv.load_from_json_file(u'messages.json')

# Add a message to the model's message list without obtaining a response
conv.append_user_message(u'Also give me subjective feedback regarding the images.')

# Print messages
print(u'Messages: """', end=u'')
print(conv.export_to_text(), end=u'')
print(u'"""')
print()

# Create message and stream response
print(u'Assistant response: """', end=u'')
for chunk in conv.send_and_stream_response(u'Help me with the above tasks.'):
    print(chunk, end=u'')
print(u'"""')
print()

# Save conversation
conv.save_to_json_file(u'new-messages.json')

# Print messages
print(u'Messages after assistant response: """', end=u'')
print(conv.export_to_text(), end=u'')
print(u'"""')

we can get this output:

Messages: """..."""

Assistant response: """..."""

Messages after assistant response: """..."""

What ... actually varies from model to model and from run to run. We embrace randomness and make ZERO effort in regularizing model output.

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

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

chat_completions_conversation-0.1.0a1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

chat_completions_conversation-0.1.0a1-py2.py3-none-any.whl (7.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file chat_completions_conversation-0.1.0a1.tar.gz.

File metadata

File hashes

Hashes for chat_completions_conversation-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 a67c553b8aa6b90d5c871074b47d84d1fba79da9380170cca7b462e4f5a3fdb6
MD5 c13aa1a8a3ba329dd6a23c1ea2fc012f
BLAKE2b-256 3b8237b26640c4365a6b902875ad89857da6262d3ecd8bdfb23520f9a1beafbf

See more details on using hashes here.

File details

Details for the file chat_completions_conversation-0.1.0a1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for chat_completions_conversation-0.1.0a1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1eb18d96c85ece502d3572783b07ac2e976ac17275123313f3c70cdd4a7a9d49
MD5 2b469d8657134393c173b5bd9706cbcb
BLAKE2b-256 9a96b29680db399dcb31caa564237b8549ccdbf77ddb61303098777cf634816f

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