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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chat_completions_conversation-0.1.0a1.tar.gz.
File metadata
- Download URL: chat_completions_conversation-0.1.0a1.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a67c553b8aa6b90d5c871074b47d84d1fba79da9380170cca7b462e4f5a3fdb6
|
|
| MD5 |
c13aa1a8a3ba329dd6a23c1ea2fc012f
|
|
| BLAKE2b-256 |
3b8237b26640c4365a6b902875ad89857da6262d3ecd8bdfb23520f9a1beafbf
|
File details
Details for the file chat_completions_conversation-0.1.0a1-py2.py3-none-any.whl.
File metadata
- Download URL: chat_completions_conversation-0.1.0a1-py2.py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1eb18d96c85ece502d3572783b07ac2e976ac17275123313f3c70cdd4a7a9d49
|
|
| MD5 |
2b469d8657134393c173b5bd9706cbcb
|
|
| BLAKE2b-256 |
9a96b29680db399dcb31caa564237b8549ccdbf77ddb61303098777cf634816f
|