llm-dialog-manager 0.4.4__tar.gz → 0.4.5__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/PKG-INFO +1 -1
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager/__init__.py +1 -1
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager/agent.py +80 -22
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager.egg-info/PKG-INFO +1 -1
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/pyproject.toml +1 -1
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/LICENSE +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/README.md +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager/chat_history.py +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager/key_manager.py +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager.egg-info/SOURCES.txt +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager.egg-info/dependency_links.txt +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager.egg-info/requires.txt +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager.egg-info/top_level.txt +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/setup.cfg +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/tests/test_agent.py +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/tests/test_chat_history.py +0 -0
- {llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/tests/test_key_manager.py +0 -0
@@ -2,7 +2,7 @@
|
|
2
2
|
import json
|
3
3
|
import os
|
4
4
|
import uuid
|
5
|
-
from typing import List, Dict, Optional,
|
5
|
+
from typing import List, Dict, Union, Optional, Any
|
6
6
|
import logging
|
7
7
|
from pathlib import Path
|
8
8
|
import random
|
@@ -97,13 +97,30 @@ def completion(model: str, messages: List[Dict[str, Union[str, List[Union[str, I
|
|
97
97
|
api_key = os.getenv(f"{service.upper()}_API_KEY")
|
98
98
|
base_url = os.getenv(f"{service.upper()}_BASE_URL")
|
99
99
|
|
100
|
-
def format_messages_for_api(
|
101
|
-
|
100
|
+
def format_messages_for_api(
|
101
|
+
model: str,
|
102
|
+
messages: List[Dict[str, Union[str, List[Union[str, Image.Image, Dict]]]]]
|
103
|
+
) -> tuple[Optional[str], List[Dict[str, Any]]]:
|
104
|
+
"""
|
105
|
+
Convert ChatHistory messages to the format required by the specific API.
|
106
|
+
|
107
|
+
Args:
|
108
|
+
model: The model name (e.g., "claude", "gemini", "gpt")
|
109
|
+
messages: List of message dictionaries with role and content
|
110
|
+
|
111
|
+
Returns:
|
112
|
+
tuple: (system_message, formatted_messages)
|
113
|
+
- system_message is extracted system message for Claude, None for others
|
114
|
+
- formatted_messages is the list of formatted message dictionaries
|
115
|
+
"""
|
102
116
|
if "claude" in model and "openai" not in model:
|
103
117
|
formatted = []
|
104
118
|
system_msg = ""
|
119
|
+
|
120
|
+
# Extract system message if present
|
105
121
|
if messages and messages[0]["role"] == "system":
|
106
122
|
system_msg = messages.pop(0)["content"]
|
123
|
+
|
107
124
|
for msg in messages:
|
108
125
|
content = msg["content"]
|
109
126
|
if isinstance(content, str):
|
@@ -113,9 +130,12 @@ def completion(model: str, messages: List[Dict[str, Union[str, List[Union[str, I
|
|
113
130
|
combined_content = []
|
114
131
|
for block in content:
|
115
132
|
if isinstance(block, str):
|
116
|
-
combined_content.append({
|
133
|
+
combined_content.append({
|
134
|
+
"type": "text",
|
135
|
+
"text": block
|
136
|
+
})
|
117
137
|
elif isinstance(block, Image.Image):
|
118
|
-
#
|
138
|
+
# Convert PIL.Image to base64
|
119
139
|
buffered = io.BytesIO()
|
120
140
|
block.save(buffered, format="PNG")
|
121
141
|
image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
@@ -145,9 +165,12 @@ def completion(model: str, messages: List[Dict[str, Union[str, List[Union[str, I
|
|
145
165
|
"data": block["image_base64"]["data"]
|
146
166
|
}
|
147
167
|
})
|
148
|
-
formatted.append({
|
168
|
+
formatted.append({
|
169
|
+
"role": msg["role"],
|
170
|
+
"content": combined_content
|
171
|
+
})
|
149
172
|
return system_msg, formatted
|
150
|
-
|
173
|
+
|
151
174
|
elif ("gemini" in model or "gpt" in model or "grok" in model) and "openai" not in model:
|
152
175
|
formatted = []
|
153
176
|
for msg in messages:
|
@@ -160,40 +183,75 @@ def completion(model: str, messages: List[Dict[str, Union[str, List[Union[str, I
|
|
160
183
|
if isinstance(block, str):
|
161
184
|
parts.append(block)
|
162
185
|
elif isinstance(block, Image.Image):
|
186
|
+
# Keep PIL.Image objects as is for Gemini
|
163
187
|
parts.append(block)
|
164
188
|
elif isinstance(block, dict):
|
165
189
|
if block.get("type") == "image_url":
|
166
|
-
parts.append({
|
190
|
+
parts.append({
|
191
|
+
"type": "image_url",
|
192
|
+
"image_url": {
|
193
|
+
"url": block["image_url"]["url"]
|
194
|
+
}
|
195
|
+
})
|
167
196
|
elif block.get("type") == "image_base64":
|
168
|
-
parts.append({
|
169
|
-
|
197
|
+
parts.append({
|
198
|
+
"type": "image_base64",
|
199
|
+
"image_base64": {
|
200
|
+
"data": block["image_base64"]["data"],
|
201
|
+
"media_type": block["image_base64"]["media_type"]
|
202
|
+
}
|
203
|
+
})
|
204
|
+
formatted.append({
|
205
|
+
"role": msg["role"],
|
206
|
+
"parts": parts
|
207
|
+
})
|
170
208
|
return None, formatted
|
171
|
-
|
209
|
+
|
172
210
|
else: # OpenAI models
|
173
211
|
formatted = []
|
174
212
|
for msg in messages:
|
175
213
|
content = msg["content"]
|
176
214
|
if isinstance(content, str):
|
177
|
-
formatted.append({
|
215
|
+
formatted.append({
|
216
|
+
"role": msg["role"],
|
217
|
+
"content": content
|
218
|
+
})
|
178
219
|
elif isinstance(content, list):
|
179
|
-
|
180
|
-
# You can convert images to URLs or descriptions if needed
|
181
|
-
combined_content = ""
|
220
|
+
formatted_content = []
|
182
221
|
for block in content:
|
183
222
|
if isinstance(block, str):
|
184
|
-
|
223
|
+
formatted_content.append({
|
224
|
+
"type": "text",
|
225
|
+
"text": block
|
226
|
+
})
|
185
227
|
elif isinstance(block, Image.Image):
|
186
|
-
# Convert PIL.Image to base64
|
228
|
+
# Convert PIL.Image to base64
|
187
229
|
buffered = io.BytesIO()
|
188
230
|
block.save(buffered, format="PNG")
|
189
231
|
image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
190
|
-
|
232
|
+
formatted_content.append({
|
233
|
+
"type": "image_url",
|
234
|
+
"image_url": {
|
235
|
+
"url": f"data:image/jpeg;base64,{image_base64}"
|
236
|
+
}
|
237
|
+
})
|
191
238
|
elif isinstance(block, dict):
|
192
239
|
if block.get("type") == "image_url":
|
193
|
-
|
240
|
+
formatted_content.append({
|
241
|
+
"type": "image_url",
|
242
|
+
"image_url": block["image_url"]
|
243
|
+
})
|
194
244
|
elif block.get("type") == "image_base64":
|
195
|
-
|
196
|
-
|
245
|
+
formatted_content.append({
|
246
|
+
"type": "image_url",
|
247
|
+
"image_url": {
|
248
|
+
"url": f"data:image/jpeg;base64,{block['image_base64']['data']}"
|
249
|
+
}
|
250
|
+
})
|
251
|
+
formatted.append({
|
252
|
+
"role": msg["role"],
|
253
|
+
"content": formatted_content
|
254
|
+
})
|
197
255
|
return None, formatted
|
198
256
|
|
199
257
|
system_msg, formatted_messages = format_messages_for_api(model, messages.copy())
|
@@ -546,7 +604,7 @@ class Agent:
|
|
546
604
|
if __name__ == "__main__":
|
547
605
|
# Example Usage
|
548
606
|
# Create an Agent instance (Gemini model)
|
549
|
-
agent = Agent("gemini-1.5-flash", "you are Jack101", memory_enabled=True)
|
607
|
+
agent = Agent("gemini-1.5-flash-openai", "you are Jack101", memory_enabled=True)
|
550
608
|
|
551
609
|
# Add an image
|
552
610
|
agent.add_image(image_path="example.png")
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
4
4
|
|
5
5
|
[project]
|
6
6
|
name = "llm_dialog_manager"
|
7
|
-
version = "0.4.
|
7
|
+
version = "0.4.5"
|
8
8
|
description = "A Python package for managing LLM chat conversation history"
|
9
9
|
readme = "README.md"
|
10
10
|
classifiers = [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: Scientific/Engineering :: Artificial Intelligence",]
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager.egg-info/SOURCES.txt
RENAMED
File without changes
|
File without changes
|
{llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager.egg-info/requires.txt
RENAMED
File without changes
|
{llm_dialog_manager-0.4.4 → llm_dialog_manager-0.4.5}/llm_dialog_manager.egg-info/top_level.txt
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|