dm-aioaiagent 0.6.0__tar.gz → 0.7.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,387 @@
1
+ Metadata-Version: 2.4
2
+ Name: dm-aioaiagent
3
+ Version: 0.7.0
4
+ Summary: This is my custom aioaiagent client
5
+ Home-page: https://pypi.org/project/dm-aioaiagent
6
+ Author: dimka4621
7
+ Author-email: mismartconfig@gmail.com
8
+ Project-URL: GitHub, https://github.com/MykhLibs/dm-aioaiagent
9
+ Keywords: dm aioaiagent
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Requires-Python: >=3.9
14
+ Description-Content-Type: text/markdown
15
+ Requires-Dist: dm-logger<0.7.0,>=0.6.6
16
+ Requires-Dist: python-dotenv>=1.0.0
17
+ Requires-Dist: pydantic<3.0.0,>=2.9.2
18
+ Requires-Dist: langchain<2.0.0,>=1.0.0
19
+ Requires-Dist: langchain-core<2.0.0,>=1.0.0
20
+ Requires-Dist: langchain-openai<2.0.0,>=1.0.0
21
+ Requires-Dist: langgraph<2.0.0,>=1.0.0
22
+ Requires-Dist: langsmith<1.0.0,>=0.4.0
23
+ Requires-Dist: grandalf<0.9.0,>=0.8.0
24
+ Provides-Extra: anthropic
25
+ Requires-Dist: langchain-anthropic<2.0.0,>=1.0.0; extra == "anthropic"
26
+ Provides-Extra: gemini
27
+ Requires-Dist: langchain-google-genai<5.0.0,>=4.0.0; extra == "gemini"
28
+ Requires-Dist: langchain-google-vertexai<4.0.0,>=3.0.0; extra == "gemini"
29
+ Provides-Extra: groq
30
+ Requires-Dist: langchain-groq<2.0.0,>=1.0.0; extra == "groq"
31
+ Provides-Extra: mistral
32
+ Requires-Dist: langchain-mistralai<2.0.0,>=1.0.0; extra == "mistral"
33
+ Provides-Extra: deepseek
34
+ Requires-Dist: langchain-deepseek<2.0.0,>=1.0.0; extra == "deepseek"
35
+ Provides-Extra: ollama
36
+ Requires-Dist: langchain-ollama<2.0.0,>=1.0.0; extra == "ollama"
37
+ Provides-Extra: all
38
+ Requires-Dist: langchain-anthropic<2.0.0,>=1.0.0; extra == "all"
39
+ Requires-Dist: langchain-google-genai<5.0.0,>=4.0.0; extra == "all"
40
+ Requires-Dist: langchain-google-vertexai<4.0.0,>=3.0.0; extra == "all"
41
+ Requires-Dist: langchain-groq<2.0.0,>=1.0.0; extra == "all"
42
+ Requires-Dist: langchain-mistralai<2.0.0,>=1.0.0; extra == "all"
43
+ Requires-Dist: langchain-deepseek<2.0.0,>=1.0.0; extra == "all"
44
+ Requires-Dist: langchain-ollama<2.0.0,>=1.0.0; extra == "all"
45
+ Provides-Extra: test
46
+ Requires-Dist: Pillow<12.0.0,>=10.0.0; extra == "test"
47
+ Dynamic: author
48
+ Dynamic: author-email
49
+ Dynamic: classifier
50
+ Dynamic: description
51
+ Dynamic: description-content-type
52
+ Dynamic: home-page
53
+ Dynamic: keywords
54
+ Dynamic: project-url
55
+ Dynamic: provides-extra
56
+ Dynamic: requires-dist
57
+ Dynamic: requires-python
58
+ Dynamic: summary
59
+
60
+ # DM-aioaiagent
61
+
62
+ ## Urls
63
+
64
+ * [PyPI](https://pypi.org/project/dm-aioaiagent)
65
+ * [GitHub](https://github.com/MykhLibs/dm-aioaiagent)
66
+
67
+ ### * Package contains both `asynchronous` and `synchronous` clients
68
+
69
+ ## Installation
70
+
71
+ By default, the package ships with **OpenAI** support. Other providers are optional extras:
72
+
73
+ ```bash
74
+ pip install dm-aioaiagent # OpenAI only
75
+ pip install dm-aioaiagent[anthropic] # + Anthropic
76
+ pip install dm-aioaiagent[anthropic,gemini] # several at once
77
+ pip install dm-aioaiagent[all] # every supported provider
78
+ ```
79
+
80
+ Available extras: `anthropic`, `gemini`, `groq`, `mistral`, `deepseek`, `ollama`, `all`.
81
+
82
+ If you call a model from a provider whose package is not installed, `init_chat_model` will raise an `ImportError` with the exact `pip install` command you need.
83
+
84
+ ## Providers
85
+
86
+ Provider resolution is delegated to LangChain's [`init_chat_model`](https://python.langchain.com/api_reference/langchain/chat_models/langchain.chat_models.base.init_chat_model.html) — the agent picks the provider automatically by model name prefix when possible. For everything else, use the `"provider:model"` mask.
87
+
88
+ ```python
89
+ # Auto-detected from model prefix (rules come from LangChain's init_chat_model)
90
+ agent = DMAioAIAgent(model="gpt-4o-mini") # → openai
91
+ agent = DMAioAIAgent(model="claude-3-5-sonnet-latest") # → anthropic
92
+ agent = DMAioAIAgent(model="gemini-2.0-flash") # → google_vertexai (see note below)
93
+
94
+ # Explicit provider via "provider:model" mask
95
+ agent = DMAioAIAgent(model="google_genai:gemini-2.0-flash")
96
+ agent = DMAioAIAgent(model="groq:llama-3.1-70b-versatile")
97
+ agent = DMAioAIAgent(model="mistralai:mistral-large-latest")
98
+ agent = DMAioAIAgent(model="deepseek:deepseek-chat")
99
+ agent = DMAioAIAgent(model="ollama:llama3.1")
100
+
101
+ # OpenAI-compatible gateway (OpenRouter, Together, vLLM, LiteLLM proxy, ...)
102
+ # Works without installing any extra — just point to the OpenAI-compatible URL.
103
+ agent = DMAioAIAgent(
104
+ model="meta-llama/llama-3.1-70b-instruct",
105
+ llm_provider_base_url="https://openrouter.ai/api/v1",
106
+ llm_provider_api_key="sk-or-...",
107
+ )
108
+ ```
109
+
110
+ > **Note about Gemini.** LangChain's auto-detect maps the `gemini*` prefix to **`google_vertexai`** (Google Cloud Vertex AI, requires a GCP service account). If you have a regular **Google AI Studio** API key (`GOOGLE_API_KEY`), use the `google_genai:` mask explicitly:
111
+ >
112
+ > ```python
113
+ > agent = DMAioAIAgent(model="google_genai:gemini-2.0-flash")
114
+ > ```
115
+
116
+ Supported provider keys for the `"provider:model"` mask (list inherited from LangChain): `openai`, `anthropic`, `azure_openai`, `azure_ai`, `google_vertexai`, `google_genai`, `bedrock`, `bedrock_converse`, `cohere`, `fireworks`, `together`, `mistralai`, `huggingface`, `groq`, `ollama`, `google_anthropic_vertex`, `deepseek`, `ibm`, `nvidia`, `xai`, `perplexity`.
117
+
118
+ ### Note about parallel tool calls
119
+
120
+ `parallel_tool_calls` is currently mapped only for **OpenAI** and **Anthropic** (their APIs use different formats). For other providers the parameter is silently ignored — extend per-provider mapping if you need it.
121
+
122
+ ## Usage
123
+
124
+ Analogue to `DMAioAIAgent` is the synchronous client `DMAIAgent`.
125
+
126
+ ### Windows Setup
127
+
128
+ ```python
129
+ import asyncio
130
+ import sys
131
+
132
+ if sys.platform == "win32":
133
+ asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
134
+ ```
135
+
136
+ ### Api Key Setup
137
+
138
+ Each provider reads its API key from a dedicated environment variable, e.g. `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, `GOOGLE_API_KEY`, `GROQ_API_KEY`, `MISTRAL_API_KEY`, etc. Alternatively, pass the key explicitly via the `llm_provider_api_key` argument — useful for multi-tenant setups, custom gateways, or runtime key rotation.
139
+
140
+ **Use load_dotenv to load the `.env` file.**
141
+
142
+ ```python
143
+ from dotenv import load_dotenv
144
+ load_dotenv()
145
+ ```
146
+
147
+ ### Use agent *with* inner memory and run *single* message
148
+
149
+ By default, agent use inner memory to store the conversation history.
150
+
151
+ (You can set *max count messages in memory* by `max_memory_messages` init argument)
152
+
153
+ ```python
154
+ import asyncio
155
+ from dm_aioaiagent import DMAioAIAgent
156
+
157
+
158
+ async def main():
159
+ # define a system message
160
+ system_message = "Your custom system message with role, backstory and goal"
161
+
162
+ # (optional) define a list of tools, if you want to use them
163
+ tools = [...]
164
+
165
+ # define a openai model, default is "gpt-4o-mini"
166
+ model_name = "gpt-4o"
167
+
168
+ # create an agent
169
+ ai_agent = DMAioAIAgent(system_message, tools, model=model_name)
170
+ # if you don't want to see the input and output messages from agent
171
+ # you can set `input_output_logging=False` init argument
172
+
173
+ # call an agent
174
+ answer = await ai_agent.run("Hello!")
175
+
176
+ # call an agent
177
+ answer = await ai_agent.run("I want to know the weather in Kyiv")
178
+
179
+ # get full conversation history
180
+ conversation_history = ai_agent.memory_messages
181
+
182
+ # clear conversation history
183
+ ai_agent.clear_memory_messages()
184
+
185
+
186
+ if __name__ == "__main__":
187
+ asyncio.run(main())
188
+ ```
189
+
190
+ ### Use agent *without* inner memory and run *multiple* messages
191
+
192
+ If you want to control the memory of the agent, you can disable it by setting `is_memory_enabled=False`
193
+
194
+ ```python
195
+ import asyncio
196
+ from dm_aioaiagent import DMAioAIAgent
197
+
198
+
199
+ async def main():
200
+ # define a system message
201
+ system_message = "Your custom system message with role, backstory and goal"
202
+
203
+ # (optional) define a list of tools, if you want to use them
204
+ tools = [...]
205
+
206
+ # define a openai model, default is "gpt-4o-mini"
207
+ model_name = "gpt-4o"
208
+
209
+ # create an agent
210
+ ai_agent = DMAioAIAgent(system_message, tools, model=model_name,
211
+ is_memory_enabled=False)
212
+ # if you don't want to see the input and output messages from agent
213
+ # you can set input_output_logging=False
214
+
215
+ # define the conversation message(s)
216
+ messages = [
217
+ {"role": "user", "content": "Hello!"}
218
+ ]
219
+
220
+ # call an agent
221
+ new_messages = await ai_agent.run_messages(messages)
222
+
223
+ # add new_messages to messages
224
+ messages.extend(new_messages)
225
+
226
+ # define the next conversation message
227
+ messages.append(
228
+ {"role": "user", "content": "I want to know the weather in Kyiv"}
229
+ )
230
+
231
+ # call an agent
232
+ new_messages = await ai_agent.run_messages(messages)
233
+
234
+
235
+ if __name__ == "__main__":
236
+ asyncio.run(main())
237
+ ```
238
+
239
+ ### Working with images — input
240
+
241
+ Use the `InputImage` helper to attach an image to a user message in a way that works **across providers** (OpenAI, Anthropic, Gemini). Each factory returns a ready-to-send `HumanMessage` whose `.content` is a list of LangChain v1 standard content blocks.
242
+
243
+ ```python
244
+ from dm_aioaiagent import DMAIAgent, InputImage
245
+
246
+ agent = DMAIAgent(agent_name="image_vision", model="gpt-4o-mini")
247
+
248
+ # from a local file (mime type inferred from extension)
249
+ msg_file = InputImage.from_file("photo.png", text="What is in the picture?")
250
+
251
+ # from a remote URL
252
+ msg_url = InputImage.from_url("https://your.domain/image.png", text="Describe it.")
253
+
254
+ # from raw bytes / base64 (mime_type required)
255
+ with open("photo.png", "rb") as f:
256
+ msg_bytes = InputImage.from_bytes(f.read(), mime_type="image/png", text="Describe.")
257
+ msg_b64 = InputImage.from_base64("aGVsbG8=", mime_type="image/png")
258
+
259
+ answer = agent.run_messages([msg_file])
260
+ print(answer[-1].content_blocks) # list of standard blocks
261
+ ```
262
+
263
+ **Multiple images per turn.** Each factory builds **one** image message. To attach several images to a single user turn, pass several messages:
264
+
265
+ ```python
266
+ messages = [
267
+ InputImage.from_file("front.png", text="Compare these two views:"),
268
+ InputImage.from_file("back.png"),
269
+ ]
270
+ agent.run_messages(messages)
271
+ ```
272
+
273
+ > **`from_url` caveats.** Some providers (notably Anthropic and Gemini) may have stricter rules about remote URLs (allowed hosts, public reachability, redirects). When in doubt — read the file yourself and use `from_file` / `from_bytes`.
274
+
275
+ ### Image generation and edit
276
+
277
+ The agent can also produce images. The mechanism differs by provider, so two flavours of model are supported:
278
+
279
+ `enable_image_generation` is the **single master switch** for image output across providers — image generation is off by default, and you opt in with one flag. The flag's effect is provider-specific (different APIs underneath), but the semantics are uniform: turn it on → the agent can draw, leave it off → it can't.
280
+
281
+ #### OpenAI — `enable_image_generation=True`
282
+
283
+ Pass the flag to a normal chat-capable OpenAI model (`gpt-5`, `gpt-5-mini`, etc.). Under the hood the agent enables the **Responses API** and binds OpenAI's built-in `image_generation` tool — the model decides on its own when to call it. Plain text turns stay text.
284
+
285
+ ```python
286
+ from dm_aioaiagent import DMAIAgent, OutputImage
287
+
288
+ agent = DMAIAgent(model="gpt-5-mini", enable_image_generation=True)
289
+
290
+ agent.run("Draw a small red square on a white background.")
291
+
292
+ # Generated images surface on agent.images
293
+ for i, img in enumerate(agent.images):
294
+ img.save(f"out_{i}.png")
295
+ ```
296
+
297
+ The same flag can be combined with regular tools — they coexist. `enable_image_generation=True` is **safe** even when the user only asks for text: the model uses `tool_choice="auto"`.
298
+
299
+ > Older OpenAI models (`gpt-4o`, `gpt-4.1`, etc.) require **organization verification** at platform.openai.com before they will accept the `image_generation` tool. The `gpt-5` family works on a fresh API key without verification.
300
+
301
+ #### Gemini — image-output models + the same flag
302
+
303
+ For Gemini you pick a model whose name contains `image` — e.g. `gemini-2.5-flash-image` (Nano Banana) — **and** turn the flag on. The agent then injects `response_modalities=["IMAGE", "TEXT"]` so the model is allowed to draw.
304
+
305
+ ```python
306
+ agent = DMAIAgent(
307
+ model="google_genai:gemini-2.5-flash-image",
308
+ enable_image_generation=True,
309
+ )
310
+
311
+ agent.run("Generate a small red square.")
312
+ agent.images[0].save("out.png")
313
+ ```
314
+
315
+ If you pick a Gemini image model but forget the flag, the agent logs a warning (`"... is image-capable but enable_image_generation=False — set the flag to True to let it draw."`) and stays in text-only mode.
316
+
317
+ > **Heads up.** A Gemini image-output model is **not** a general chat model — it tends to draw on every turn, including plain greetings. For mixed workloads use a **two-agent pattern**: a chat agent with the image agent attached as a tool. See [`agent.as_tool()`](#agentas_tool) below.
318
+
319
+ #### Anthropic — vision only
320
+
321
+ Claude **cannot generate** images. If you pass `enable_image_generation=True` to a Claude model, the flag is silently ignored and a warning is logged. Image input (vision) works as usual.
322
+
323
+ ### Working with generated images — `OutputImage`
324
+
325
+ Generated images live in `agent.images` as `OutputImage` instances:
326
+
327
+ ```python
328
+ img = agent.images[0]
329
+ img.bytes # raw image bytes
330
+ img.mime_type # e.g. "image/png"
331
+ img.save("out.png")
332
+ img.to_base64()
333
+ ```
334
+
335
+ You can also extract images directly from any `AIMessage`:
336
+
337
+ ```python
338
+ from dm_aioaiagent import OutputImage
339
+ images = OutputImage.extract_from(response_message) # list[OutputImage]
340
+ ```
341
+
342
+ ### Image memory modes
343
+
344
+ Images in `agent.memory_messages` (the conversation history sent to the LLM on each turn) and in `agent.images` (the property exposing AI-generated images) follow the `image_memory_mode` constructor argument:
345
+
346
+ | Mode | Memory (history) | `agent.images` |
347
+ |---|---|---|
348
+ | `keep_last` *(default)* | last user-image kept; last AI-image kept; older → `[image]` / `[generated image]` placeholder | last AI-image kept; replaced when a new one arrives |
349
+ | `drop` | every image (user + AI) becomes a placeholder right after the turn | only the AI-image of the **current** turn (then wiped on the next call) |
350
+ | `keep_all` | nothing is stripped — full multimodal history | every AI-image accumulates |
351
+
352
+ ```python
353
+ agent = DMAIAgent(model="gpt-4o-mini", image_memory_mode="keep_last")
354
+ agent.run_messages([InputImage.from_file("photo.png", text="Describe.")])
355
+ agent.run("What colour was dominant?") # answers based on the image
356
+ ```
357
+
358
+ `agent.clear_memory_messages()` clears both `memory_messages` and `images`.
359
+
360
+ > Only **AI-generated** images populate `agent.images`. Images you upload via `InputImage` go into history per the rules above but are not exposed on the `images` property.
361
+
362
+ ### `agent.as_tool()`
363
+
364
+ Wrap any agent as a `StructuredTool` so a *parent* agent can call it like any other tool — the basis for multi-agent composition. Default name is derived from `agent_name` (lowercased, non-alphanumerics replaced with `_`); `description` is required.
365
+
366
+ ```python
367
+ from dm_aioaiagent import DMAIAgent
368
+
369
+ # specialised image agent
370
+ image_agent = DMAIAgent(
371
+ agent_name="image_drawer",
372
+ model="google_genai:gemini-2.5-flash-image",
373
+ enable_image_generation=True,
374
+ )
375
+
376
+ # chat agent that delegates drawing to the image agent
377
+ chat_agent = DMAIAgent(
378
+ model="google_genai:gemini-2.5-flash",
379
+ tools=[image_agent.as_tool(description="Generates an image from a text prompt.")],
380
+ )
381
+
382
+ chat_agent.run("Hi! Please draw a small red square.")
383
+ # the chat agent picks the tool, the image agent draws, image lands in image_agent.images
384
+ image_agent.images[0].save("out.png")
385
+ ```
386
+
387
+ The async client (`DMAioAIAgent.as_tool`) returns a tool with both `func` and `coroutine` set, so it can be invoked from sync or async parent agents.