aimlapi-haystack 2.0.0__tar.gz → 3.0.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.
- aimlapi_haystack-3.0.0/CHANGELOG.md +70 -0
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/LICENSE.txt +1 -1
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/PKG-INFO +6 -5
- aimlapi_haystack-3.0.0/examples/aimlapi_with_tools_example.py +54 -0
- aimlapi_haystack-3.0.0/pydoc/config_docusaurus.yml +13 -0
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/pyproject.toml +18 -6
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/src/haystack_integrations/components/generators/aimlapi/chat/chat_generator.py +5 -5
- aimlapi_haystack-3.0.0/tests/conftest.py +11 -0
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/tests/test_aimlapi_chat_generator.py +100 -85
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/tests/test_aimlapi_chat_generator_async.py +7 -4
- aimlapi_haystack-2.0.0/CHANGELOG.md +0 -7
- aimlapi_haystack-2.0.0/examples/aimlapi_with_tools_example.py +0 -77
- aimlapi_haystack-2.0.0/pydoc/config_docusaurus.yml +0 -28
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/.gitignore +0 -0
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/README.md +0 -0
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/examples/aimlapi_basic_example.py +0 -0
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/src/haystack_integrations/components/generators/aimlapi/__init__.py +0 -0
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/src/haystack_integrations/components/generators/aimlapi/chat/__init__.py +0 -0
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/src/haystack_integrations/components/generators/py.typed +0 -0
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/tests/__init__.py +0 -0
- {aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/tests/test_aimlapi_stub_async.py +0 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [integrations/aimlapi-v2.1.0] - 2026-07-13
|
|
4
|
+
|
|
5
|
+
### 🚀 Features
|
|
6
|
+
|
|
7
|
+
- Improve type annotations, fix duplicate code in aimlapi integration (#2969)
|
|
8
|
+
|
|
9
|
+
### 🐛 Bug Fixes
|
|
10
|
+
|
|
11
|
+
- Support Haystack 3.0's lazy OpenAI client initialization in OpenAI-inheriting generators (#3536)
|
|
12
|
+
|
|
13
|
+
### 📚 Documentation
|
|
14
|
+
|
|
15
|
+
- Simplify pydoc configs (#2855)
|
|
16
|
+
|
|
17
|
+
### 🧪 Testing
|
|
18
|
+
|
|
19
|
+
- Relax AIMLAPI tests (#2815)
|
|
20
|
+
- Test compatible integrations with python 3.14; update pyproject (#3001)
|
|
21
|
+
- Track test coverage for all integrations (#3065)
|
|
22
|
+
- Aimlapi - add unit tests (#3184)
|
|
23
|
+
- Trust test modules under Haystack 3.0's deserialization allowlist (#3537)
|
|
24
|
+
- Make Tool/Agent serialization assertions version-agnostic for Haystack 2.x/3.x (#3533)
|
|
25
|
+
- Guard ToolInvoker imports so chat-generator tests run under Haystack 3.0 (#3535)
|
|
26
|
+
- Fix tests for integrations that inherit from the OpenAIChatGenerator (#3583)
|
|
27
|
+
|
|
28
|
+
### 🧹 Chores
|
|
29
|
+
|
|
30
|
+
- Standardize author mentions (#2897)
|
|
31
|
+
- Enforce ruff docstring rules (D102/D103/D205/D209/D213/D417/D419) in first 10 integrations (#3008)
|
|
32
|
+
- Update aimlapi (#3290)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## [integrations/aimlapi-v2.0.0] - 2026-01-09
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### 🧹 Chores
|
|
39
|
+
|
|
40
|
+
- Remove Readme API CI workflow and configs (#2573)
|
|
41
|
+
- Make fmt command more forgiving (#2671)
|
|
42
|
+
- [**breaking**] Aimlapi - drop Python 3.9 and use X|Y typing (#2684)
|
|
43
|
+
|
|
44
|
+
### 🌀 Miscellaneous
|
|
45
|
+
|
|
46
|
+
- Enhancement: Adopt PEP 585 type hinting (part 6) (#2532)
|
|
47
|
+
|
|
48
|
+
## [integrations/aimlapi-v1.0.0] - 2025-11-12
|
|
49
|
+
|
|
50
|
+
### 🚀 Features
|
|
51
|
+
|
|
52
|
+
- *(aimlapi)* Add AIMLAPI provider integration (#2267)
|
|
53
|
+
|
|
54
|
+
### 📚 Documentation
|
|
55
|
+
|
|
56
|
+
- Add pydoc configurations for Docusaurus (#2411)
|
|
57
|
+
|
|
58
|
+
### ⚙️ CI
|
|
59
|
+
|
|
60
|
+
- Change pytest command (#2475)
|
|
61
|
+
|
|
62
|
+
### 🧹 Chores
|
|
63
|
+
|
|
64
|
+
- AIMLAPI - clean up README (#2356)
|
|
65
|
+
|
|
66
|
+
### 🌀 Miscellaneous
|
|
67
|
+
|
|
68
|
+
- Add missing GA workflow for `aimlapi` integration (#2504)
|
|
69
|
+
|
|
70
|
+
<!-- generated by git-cliff -->
|
|
@@ -58,7 +58,7 @@ APPENDIX: How to apply the Apache License to your work.
|
|
|
58
58
|
|
|
59
59
|
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
|
60
60
|
|
|
61
|
-
Copyright 2025
|
|
61
|
+
Copyright 2025-present deepset GmbH
|
|
62
62
|
|
|
63
63
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
64
64
|
you may not use this file except in compliance with the License.
|
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: aimlapi-haystack
|
|
3
|
-
Version:
|
|
3
|
+
Version: 3.0.0
|
|
4
4
|
Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/aimlapi#readme
|
|
5
5
|
Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
|
|
6
6
|
Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/aimlapi
|
|
7
|
-
Author-email: "Dmitry <D1m7asis> Tumanov" <d1m7asis@gmail.com>
|
|
7
|
+
Author-email: deepset GmbH <info@deepset.ai>, "Dmitry <D1m7asis> Tumanov" <d1m7asis@gmail.com>
|
|
8
8
|
License-Expression: Apache-2.0
|
|
9
9
|
License-File: LICENSE.txt
|
|
10
10
|
Classifier: Development Status :: 4 - Beta
|
|
11
11
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
12
|
Classifier: Programming Language :: Python
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
14
13
|
Classifier: Programming Language :: Python :: 3.11
|
|
15
14
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
15
|
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
17
18
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
18
19
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
19
|
-
Requires-Python: >=3.
|
|
20
|
+
Requires-Python: >=3.11
|
|
20
21
|
Requires-Dist: haystack-ai>=2.22.0
|
|
21
22
|
Description-Content-Type: text/markdown
|
|
22
23
|
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
# ruff: noqa: T201
|
|
6
|
+
|
|
7
|
+
"""Run AIMLAPI chat generation with tool calling and execution."""
|
|
8
|
+
|
|
9
|
+
from haystack.components.agents import Agent
|
|
10
|
+
from haystack.dataclasses import ChatMessage
|
|
11
|
+
from haystack.tools import Tool
|
|
12
|
+
|
|
13
|
+
from haystack_integrations.components.generators.aimlapi import AIMLAPIChatGenerator
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def weather(city: str) -> str:
|
|
17
|
+
"""Return mock weather info for the given city."""
|
|
18
|
+
|
|
19
|
+
return f"The weather in {city} is sunny and 32°C"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def main() -> None:
|
|
23
|
+
"""Demonstrate an Agent calling a tool and feeding the result back to AIMLAPI."""
|
|
24
|
+
|
|
25
|
+
tool_parameters = {
|
|
26
|
+
"type": "object",
|
|
27
|
+
"properties": {"city": {"type": "string"}},
|
|
28
|
+
"required": ["city"],
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
weather_tool = Tool(
|
|
32
|
+
name="weather",
|
|
33
|
+
description="Useful for getting the weather in a specific city",
|
|
34
|
+
parameters=tool_parameters,
|
|
35
|
+
function=weather,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
agent = Agent(
|
|
39
|
+
chat_generator=AIMLAPIChatGenerator(model="openai/gpt-5-mini-2025-08-07"),
|
|
40
|
+
system_prompt="You help users by calling the provided tools when they are relevant.",
|
|
41
|
+
tools=[weather_tool],
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
result = agent.run(messages=[ChatMessage.from_user("What's the weather in Tokyo today?")])
|
|
45
|
+
|
|
46
|
+
for message in result["messages"]:
|
|
47
|
+
if message.tool_call_result:
|
|
48
|
+
print(f"tool output: {message.tool_call_result.result}")
|
|
49
|
+
|
|
50
|
+
print(f"assistant final answer: {result['last_message'].text}")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if __name__ == "__main__":
|
|
54
|
+
main()
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
loaders:
|
|
2
|
+
- modules:
|
|
3
|
+
- haystack_integrations.components.generators.aimlapi.chat.chat_generator
|
|
4
|
+
search_path: [../src]
|
|
5
|
+
processors:
|
|
6
|
+
- type: filter
|
|
7
|
+
documented_only: true
|
|
8
|
+
skip_empty_modules: true
|
|
9
|
+
renderer:
|
|
10
|
+
description: AIMLAPI integration for Haystack
|
|
11
|
+
id: integrations-aimlapi
|
|
12
|
+
filename: aimlapi.md
|
|
13
|
+
title: AIMLAPI
|
|
@@ -7,18 +7,19 @@ name = "aimlapi-haystack"
|
|
|
7
7
|
dynamic = ["version"]
|
|
8
8
|
description = ''
|
|
9
9
|
readme = "README.md"
|
|
10
|
-
requires-python = ">=3.
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
11
|
license = "Apache-2.0"
|
|
12
12
|
keywords = []
|
|
13
|
-
authors = [{ name = "Dmitry <D1m7asis> Tumanov", email = "d1m7asis@gmail.com" }]
|
|
13
|
+
authors = [{ name = "deepset GmbH", email = "info@deepset.ai" }, { name = "Dmitry <D1m7asis> Tumanov", email = "d1m7asis@gmail.com" }]
|
|
14
14
|
classifiers = [
|
|
15
15
|
"License :: OSI Approved :: Apache Software License",
|
|
16
16
|
"Development Status :: 4 - Beta",
|
|
17
17
|
"Programming Language :: Python",
|
|
18
|
-
"Programming Language :: Python :: 3.10",
|
|
19
18
|
"Programming Language :: Python :: 3.11",
|
|
20
19
|
"Programming Language :: Python :: 3.12",
|
|
21
20
|
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Programming Language :: Python :: 3.15",
|
|
22
23
|
"Programming Language :: Python :: Implementation :: CPython",
|
|
23
24
|
"Programming Language :: Python :: Implementation :: PyPy",
|
|
24
25
|
]
|
|
@@ -44,7 +45,7 @@ git_describe_command = 'git describe --tags --match="integrations/aimlapi-v[0-9]
|
|
|
44
45
|
installer = "uv"
|
|
45
46
|
dependencies = ["haystack-pydoc-tools", "ruff"]
|
|
46
47
|
[tool.hatch.envs.default.scripts]
|
|
47
|
-
docs = ["pydoc
|
|
48
|
+
docs = ["haystack-pydoc pydoc/config_docusaurus.yml"]
|
|
48
49
|
fmt = "ruff check --fix {args}; ruff format {args}"
|
|
49
50
|
fmt-check = "ruff check {args} && ruff format --check {args}"
|
|
50
51
|
|
|
@@ -63,7 +64,8 @@ dependencies = [
|
|
|
63
64
|
unit = 'pytest -m "not integration" {args:tests}'
|
|
64
65
|
integration = 'pytest -m "integration" {args:tests}'
|
|
65
66
|
all = 'pytest {args:tests}'
|
|
66
|
-
cov-retry = 'pytest --cov=haystack_integrations --reruns 3 --reruns-delay 30 -x {args:tests}'
|
|
67
|
+
unit-cov-retry = 'pytest --cov=haystack_integrations --reruns 3 --reruns-delay 30 -x -m "not integration" {args:tests}'
|
|
68
|
+
integration-cov-append-retry = 'pytest --cov=haystack_integrations --cov-append --reruns 3 --reruns-delay 30 -x -m "integration" {args:tests}'
|
|
67
69
|
|
|
68
70
|
types = "mypy -p haystack_integrations.components.generators.aimlapi {args}"
|
|
69
71
|
|
|
@@ -80,9 +82,17 @@ line-length = 120
|
|
|
80
82
|
[tool.ruff.lint]
|
|
81
83
|
select = [
|
|
82
84
|
"A",
|
|
85
|
+
"ANN",
|
|
83
86
|
"ARG",
|
|
84
87
|
"B",
|
|
85
88
|
"C",
|
|
89
|
+
"D102", # Missing docstring in public method
|
|
90
|
+
"D103", # Missing docstring in public function
|
|
91
|
+
"D205", # 1 blank line required between summary line and description
|
|
92
|
+
"D209", # Closing triple quotes go to new line
|
|
93
|
+
"D213", # summary lines must be positioned on the second physical line of the docstring
|
|
94
|
+
"D417", # Missing argument descriptions in the docstring
|
|
95
|
+
"D419", # Docstring is empty
|
|
86
96
|
"DTZ",
|
|
87
97
|
"E",
|
|
88
98
|
"EM",
|
|
@@ -117,6 +127,7 @@ ignore = [
|
|
|
117
127
|
"PLR0912",
|
|
118
128
|
"PLR0913",
|
|
119
129
|
"PLR0915",
|
|
130
|
+
"PLR0917",
|
|
120
131
|
# Misc
|
|
121
132
|
"B008",
|
|
122
133
|
"S101",
|
|
@@ -130,11 +141,12 @@ ban-relative-imports = "parents"
|
|
|
130
141
|
|
|
131
142
|
[tool.ruff.lint.per-file-ignores]
|
|
132
143
|
# Tests can use magic values, assertions, and relative imports
|
|
133
|
-
"tests/**/*" = ["PLR2004", "S101", "TID252"]
|
|
144
|
+
"tests/**/*" = ["PLR2004", "S101", "TID252", "D", "ANN"]
|
|
134
145
|
|
|
135
146
|
[tool.coverage.run]
|
|
136
147
|
source = ["haystack_integrations"]
|
|
137
148
|
branch = true
|
|
149
|
+
relative_files = true
|
|
138
150
|
parallel = false
|
|
139
151
|
|
|
140
152
|
|
|
@@ -20,6 +20,7 @@ AIMLAPI_HEADERS = {"HTTP-Referer": "https://github.com/deepset-ai/haystack-core-
|
|
|
20
20
|
class AIMLAPIChatGenerator(OpenAIChatGenerator):
|
|
21
21
|
"""
|
|
22
22
|
Enables text generation using AIMLAPI generative models.
|
|
23
|
+
|
|
23
24
|
For supported models, see AIMLAPI documentation.
|
|
24
25
|
|
|
25
26
|
Users can pass any text generation parameters valid for the AIMLAPI chat completion API
|
|
@@ -71,10 +72,11 @@ class AIMLAPIChatGenerator(OpenAIChatGenerator):
|
|
|
71
72
|
extra_headers: dict[str, Any] | None = None,
|
|
72
73
|
max_retries: int | None = None,
|
|
73
74
|
http_client_kwargs: dict[str, Any] | None = None,
|
|
74
|
-
):
|
|
75
|
+
) -> None:
|
|
75
76
|
"""
|
|
76
|
-
Creates an instance of AIMLAPIChatGenerator.
|
|
77
|
-
|
|
77
|
+
Creates an instance of AIMLAPIChatGenerator.
|
|
78
|
+
|
|
79
|
+
Unless specified otherwise, the default model is `openai/gpt-5-chat-latest`.
|
|
78
80
|
|
|
79
81
|
:param api_key:
|
|
80
82
|
The AIMLAPI API key.
|
|
@@ -192,8 +194,6 @@ class AIMLAPIChatGenerator(OpenAIChatGenerator):
|
|
|
192
194
|
raise ValueError(msg)
|
|
193
195
|
response_format = generation_kwargs.pop("response_format", None)
|
|
194
196
|
|
|
195
|
-
response_format = generation_kwargs.pop("response_format", None)
|
|
196
|
-
|
|
197
197
|
base_args = {
|
|
198
198
|
"model": self.model,
|
|
199
199
|
"messages": aimlapi_formatted_messages,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@pytest.fixture(autouse=True)
|
|
5
|
+
def allow_deserialization_of_test_modules(monkeypatch):
|
|
6
|
+
"""
|
|
7
|
+
haystack-ai >= 3.0 refuses to deserialize classes and callables from modules outside its
|
|
8
|
+
trusted-module allowlist. Tools and callbacks defined in the test modules live outside that
|
|
9
|
+
allowlist, so trust them explicitly; haystack-ai 2.x ignores this environment variable.
|
|
10
|
+
"""
|
|
11
|
+
monkeypatch.setenv("HAYSTACK_DESERIALIZATION_ALLOWLIST", "tests,test_*")
|
|
@@ -5,8 +5,8 @@ from unittest.mock import patch
|
|
|
5
5
|
import pytest
|
|
6
6
|
import pytz
|
|
7
7
|
from haystack import Pipeline
|
|
8
|
+
from haystack.components.agents import Agent
|
|
8
9
|
from haystack.components.generators.utils import print_streaming_chunk
|
|
9
|
-
from haystack.components.tools import ToolInvoker
|
|
10
10
|
from haystack.dataclasses import ChatMessage, ChatRole, StreamingChunk, ToolCall
|
|
11
11
|
from haystack.tools import Tool
|
|
12
12
|
from haystack.utils.auth import Secret
|
|
@@ -88,16 +88,24 @@ class TestAIMLAPIChatGenerator:
|
|
|
88
88
|
def test_init_default(self, monkeypatch):
|
|
89
89
|
monkeypatch.setenv("AIMLAPI_API_KEY", "test-api-key")
|
|
90
90
|
component = AIMLAPIChatGenerator()
|
|
91
|
-
assert component.
|
|
91
|
+
assert component.api_key.resolve_value() == "test-api-key"
|
|
92
92
|
assert component.model == "openai/gpt-5-chat-latest"
|
|
93
93
|
assert component.api_base_url == "https://api.aimlapi.com/v1"
|
|
94
94
|
assert component.streaming_callback is None
|
|
95
95
|
assert not component.generation_kwargs
|
|
96
96
|
|
|
97
|
+
def test_warm_up(self, monkeypatch):
|
|
98
|
+
monkeypatch.setenv("AIMLAPI_API_KEY", "test-api-key")
|
|
99
|
+
component = AIMLAPIChatGenerator()
|
|
100
|
+
component.warm_up() # with haystack-ai >= 3.0 the client is created during warm-up
|
|
101
|
+
assert component.client.api_key == "test-api-key"
|
|
102
|
+
|
|
97
103
|
def test_init_fail_wo_api_key(self, monkeypatch):
|
|
98
104
|
monkeypatch.delenv("AIMLAPI_API_KEY", raising=False)
|
|
99
105
|
with pytest.raises(ValueError, match=r"None of the .* environment variables are set"):
|
|
100
|
-
|
|
106
|
+
# haystack-ai 2.x raises at init; haystack-ai >= 3.0 raises when the client is created in warm_up
|
|
107
|
+
component = AIMLAPIChatGenerator()
|
|
108
|
+
component.warm_up()
|
|
101
109
|
|
|
102
110
|
def test_init_with_parameters(self):
|
|
103
111
|
component = AIMLAPIChatGenerator(
|
|
@@ -107,7 +115,7 @@ class TestAIMLAPIChatGenerator:
|
|
|
107
115
|
api_base_url="test-base-url",
|
|
108
116
|
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
|
|
109
117
|
)
|
|
110
|
-
assert component.
|
|
118
|
+
assert component.api_key.resolve_value() == "test-api-key"
|
|
111
119
|
assert component.model == "openai/gpt-5-chat-latest"
|
|
112
120
|
assert component.streaming_callback is print_streaming_chunk
|
|
113
121
|
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
|
|
@@ -204,24 +212,6 @@ class TestAIMLAPIChatGenerator:
|
|
|
204
212
|
assert component.timeout == 10
|
|
205
213
|
assert component.max_retries == 10
|
|
206
214
|
|
|
207
|
-
def test_from_dict_fail_wo_env_var(self, monkeypatch):
|
|
208
|
-
monkeypatch.delenv("AIMLAPI_API_KEY", raising=False)
|
|
209
|
-
data = {
|
|
210
|
-
"type": ("haystack_integrations.components.generators.aimlapi.chat.chat_generator.AIMLAPIChatGenerator"),
|
|
211
|
-
"init_parameters": {
|
|
212
|
-
"api_key": {"env_vars": ["AIMLAPI_API_KEY"], "strict": True, "type": "env_var"},
|
|
213
|
-
"model": "openai/gpt-5-chat-latest",
|
|
214
|
-
"api_base_url": "test-base-url",
|
|
215
|
-
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
|
|
216
|
-
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
|
|
217
|
-
"extra_headers": {"test-header": "test-value"},
|
|
218
|
-
"timeout": 10,
|
|
219
|
-
"max_retries": 10,
|
|
220
|
-
},
|
|
221
|
-
}
|
|
222
|
-
with pytest.raises(ValueError, match=r"None of the .* environment variables are set"):
|
|
223
|
-
AIMLAPIChatGenerator.from_dict(data)
|
|
224
|
-
|
|
225
215
|
def test_run(self, chat_messages, mock_chat_completion, monkeypatch): # noqa: ARG002
|
|
226
216
|
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
|
|
227
217
|
component = AIMLAPIChatGenerator()
|
|
@@ -264,7 +254,7 @@ class TestAIMLAPIChatGenerator:
|
|
|
264
254
|
message: ChatMessage = results["replies"][0]
|
|
265
255
|
assert message.text
|
|
266
256
|
assert "Paris" in message.text
|
|
267
|
-
assert "gpt-5-nano
|
|
257
|
+
assert "gpt-5-nano" in message.meta["model"]
|
|
268
258
|
assert message.meta["finish_reason"] == "stop"
|
|
269
259
|
|
|
270
260
|
@pytest.mark.skipif(
|
|
@@ -301,7 +291,7 @@ class TestAIMLAPIChatGenerator:
|
|
|
301
291
|
assert message.text
|
|
302
292
|
assert "Paris" in message.text
|
|
303
293
|
|
|
304
|
-
assert "gpt-5-nano
|
|
294
|
+
assert "gpt-5-nano" in message.meta["model"]
|
|
305
295
|
assert message.meta["finish_reason"] == "stop"
|
|
306
296
|
|
|
307
297
|
assert callback.counter > 1
|
|
@@ -415,29 +405,15 @@ class TestAIMLAPIChatGenerator:
|
|
|
415
405
|
reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
|
|
416
406
|
)
|
|
417
407
|
@pytest.mark.integration
|
|
418
|
-
def
|
|
408
|
+
def test_agent_with_aimlapi_chat_generator(self, tools):
|
|
419
409
|
"""
|
|
420
|
-
Test that the AIMLAPIChatGenerator component can
|
|
410
|
+
Test that the AIMLAPIChatGenerator component can drive tool calls in an Agent
|
|
421
411
|
"""
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
pipeline.add_component("tool_invoker", ToolInvoker(tools=tools))
|
|
425
|
-
|
|
426
|
-
pipeline.connect("generator", "tool_invoker")
|
|
412
|
+
agent = Agent(chat_generator=AIMLAPIChatGenerator(model="openai/gpt-5-mini-2025-08-07"), tools=tools)
|
|
413
|
+
result = agent.run(messages=[ChatMessage.from_user("What's the weather like in Paris?")])
|
|
427
414
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
"generator": {
|
|
431
|
-
"messages": [ChatMessage.from_user("What's the weather like in Paris?")],
|
|
432
|
-
"generation_kwargs": {"tool_choice": "auto"},
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
)
|
|
436
|
-
|
|
437
|
-
assert (
|
|
438
|
-
"The weather in Paris is sunny and 32°C"
|
|
439
|
-
== results["tool_invoker"]["tool_messages"][0].tool_call_result.result
|
|
440
|
-
)
|
|
415
|
+
tool_results = [msg.tool_call_result.result for msg in result["messages"] if msg.tool_call_result]
|
|
416
|
+
assert "The weather in Paris is sunny and 32°C" in tool_results
|
|
441
417
|
|
|
442
418
|
def test_serde_in_pipeline(self, monkeypatch):
|
|
443
419
|
"""
|
|
@@ -469,6 +445,11 @@ class TestAIMLAPIChatGenerator:
|
|
|
469
445
|
|
|
470
446
|
# Get pipeline dictionary and verify its structure
|
|
471
447
|
pipeline_dict = pipeline.to_dict()
|
|
448
|
+
|
|
449
|
+
# the Tool serialization format is owned by haystack-ai and varies across its versions; the
|
|
450
|
+
# dumps/loads round-trip below covers the tools, so exclude them from the pinned-dict comparison
|
|
451
|
+
tools_entries = pipeline_dict["components"]["generator"]["init_parameters"].pop("tools")
|
|
452
|
+
assert len(tools_entries) == 1
|
|
472
453
|
expected_dict = {
|
|
473
454
|
"metadata": {},
|
|
474
455
|
"max_runs_per_component": 100,
|
|
@@ -482,17 +463,6 @@ class TestAIMLAPIChatGenerator:
|
|
|
482
463
|
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
|
|
483
464
|
"api_base_url": "https://api.aimlapi.com/v1",
|
|
484
465
|
"generation_kwargs": {"temperature": 0.7},
|
|
485
|
-
"tools": [
|
|
486
|
-
{
|
|
487
|
-
"type": "haystack.tools.tool.Tool",
|
|
488
|
-
"data": {
|
|
489
|
-
"name": "weather",
|
|
490
|
-
"description": "useful to determine the weather in a given location",
|
|
491
|
-
"parameters": {"city": {"type": "string"}},
|
|
492
|
-
"function": "tests.test_aimlapi_chat_generator.weather",
|
|
493
|
-
},
|
|
494
|
-
}
|
|
495
|
-
],
|
|
496
466
|
"http_client_kwargs": None,
|
|
497
467
|
"extra_headers": None,
|
|
498
468
|
"timeout": None,
|
|
@@ -506,21 +476,6 @@ class TestAIMLAPIChatGenerator:
|
|
|
506
476
|
if not hasattr(pipeline, "_connection_type_validation"):
|
|
507
477
|
expected_dict.pop("connection_type_validation")
|
|
508
478
|
|
|
509
|
-
# add outputs_to_string, inputs_from_state and outputs_to_state tool parameters for compatibility with
|
|
510
|
-
# haystack-ai>=2.12.0
|
|
511
|
-
if hasattr(tool, "outputs_to_string"):
|
|
512
|
-
expected_dict["components"]["generator"]["init_parameters"]["tools"][0]["data"]["outputs_to_string"] = (
|
|
513
|
-
tool.outputs_to_string
|
|
514
|
-
)
|
|
515
|
-
if hasattr(tool, "inputs_from_state"):
|
|
516
|
-
expected_dict["components"]["generator"]["init_parameters"]["tools"][0]["data"]["inputs_from_state"] = (
|
|
517
|
-
tool.inputs_from_state
|
|
518
|
-
)
|
|
519
|
-
if hasattr(tool, "outputs_to_state"):
|
|
520
|
-
expected_dict["components"]["generator"]["init_parameters"]["tools"][0]["data"]["outputs_to_state"] = (
|
|
521
|
-
tool.outputs_to_state
|
|
522
|
-
)
|
|
523
|
-
|
|
524
479
|
assert pipeline_dict == expected_dict
|
|
525
480
|
|
|
526
481
|
# Test YAML serialization/deserialization
|
|
@@ -538,6 +493,74 @@ class TestAIMLAPIChatGenerator:
|
|
|
538
493
|
assert loaded_generator.tools[0].description == generator.tools[0].description
|
|
539
494
|
assert loaded_generator.tools[0].parameters == generator.tools[0].parameters
|
|
540
495
|
|
|
496
|
+
def test_prepare_api_call_with_tools(self, tools, monkeypatch):
|
|
497
|
+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
|
|
498
|
+
component = AIMLAPIChatGenerator(tools=tools)
|
|
499
|
+
args = component._prepare_api_call(messages=[ChatMessage.from_user("hi")])
|
|
500
|
+
|
|
501
|
+
assert args["tools"] == [
|
|
502
|
+
{
|
|
503
|
+
"type": "function",
|
|
504
|
+
"function": {
|
|
505
|
+
"name": "weather",
|
|
506
|
+
"description": "useful to determine the weather in a given location",
|
|
507
|
+
"parameters": {
|
|
508
|
+
"type": "object",
|
|
509
|
+
"properties": {"city": {"type": "string"}},
|
|
510
|
+
"required": ["city"],
|
|
511
|
+
},
|
|
512
|
+
},
|
|
513
|
+
}
|
|
514
|
+
]
|
|
515
|
+
assert "strict" not in args["tools"][0]["function"]
|
|
516
|
+
assert args["tools"][0]["function"]["parameters"].get("additionalProperties") is None
|
|
517
|
+
|
|
518
|
+
def test_prepare_api_call_with_tools_strict(self, tools, monkeypatch):
|
|
519
|
+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
|
|
520
|
+
component = AIMLAPIChatGenerator(tools=tools)
|
|
521
|
+
args = component._prepare_api_call(messages=[ChatMessage.from_user("hi")], tools_strict=True)
|
|
522
|
+
|
|
523
|
+
function_spec = args["tools"][0]["function"]
|
|
524
|
+
assert function_spec["strict"] is True
|
|
525
|
+
assert function_spec["parameters"]["additionalProperties"] is False
|
|
526
|
+
|
|
527
|
+
def test_prepare_api_call_streaming_multiple_responses_raises(self, monkeypatch):
|
|
528
|
+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
|
|
529
|
+
component = AIMLAPIChatGenerator()
|
|
530
|
+
with pytest.raises(ValueError, match="Cannot stream multiple responses"):
|
|
531
|
+
component._prepare_api_call(
|
|
532
|
+
messages=[ChatMessage.from_user("hi")],
|
|
533
|
+
streaming_callback=print_streaming_chunk,
|
|
534
|
+
generation_kwargs={"n": 2},
|
|
535
|
+
)
|
|
536
|
+
|
|
537
|
+
def test_prepare_api_call_with_response_format_non_streaming(self, monkeypatch):
|
|
538
|
+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
|
|
539
|
+
component = AIMLAPIChatGenerator()
|
|
540
|
+
response_format = {"type": "json_object"}
|
|
541
|
+
args = component._prepare_api_call(
|
|
542
|
+
messages=[ChatMessage.from_user("hi")],
|
|
543
|
+
generation_kwargs={"response_format": response_format},
|
|
544
|
+
)
|
|
545
|
+
|
|
546
|
+
assert args["response_format"] == response_format
|
|
547
|
+
assert args["openai_endpoint"] == "parse"
|
|
548
|
+
assert "stream" not in args
|
|
549
|
+
|
|
550
|
+
def test_prepare_api_call_with_response_format_streaming(self, monkeypatch):
|
|
551
|
+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
|
|
552
|
+
component = AIMLAPIChatGenerator()
|
|
553
|
+
response_format = {"type": "json_object"}
|
|
554
|
+
args = component._prepare_api_call(
|
|
555
|
+
messages=[ChatMessage.from_user("hi")],
|
|
556
|
+
streaming_callback=print_streaming_chunk,
|
|
557
|
+
generation_kwargs={"response_format": response_format},
|
|
558
|
+
)
|
|
559
|
+
|
|
560
|
+
assert args["response_format"] == response_format
|
|
561
|
+
assert args["openai_endpoint"] == "create"
|
|
562
|
+
assert args["stream"] is True
|
|
563
|
+
|
|
541
564
|
|
|
542
565
|
class TestChatCompletionChunkConversion:
|
|
543
566
|
def test_handle_stream_response(self):
|
|
@@ -858,18 +881,10 @@ class TestChatCompletionChunkConversion:
|
|
|
858
881
|
assert result.meta["finish_reason"] == "tool_calls"
|
|
859
882
|
assert result.meta["index"] == 0
|
|
860
883
|
assert result.meta["completion_start_time"] is not None
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
"reasoning_tokens": 0,
|
|
869
|
-
"rejected_prediction_tokens": None,
|
|
870
|
-
},
|
|
871
|
-
"prompt_tokens_details": {
|
|
872
|
-
"audio_tokens": None,
|
|
873
|
-
"cached_tokens": 0,
|
|
874
|
-
},
|
|
875
|
-
}
|
|
884
|
+
|
|
885
|
+
usage = result.meta["usage"]
|
|
886
|
+
assert usage["completion_tokens"] == 42
|
|
887
|
+
assert usage["prompt_tokens"] == 55
|
|
888
|
+
assert usage["total_tokens"] == 97
|
|
889
|
+
assert usage["completion_tokens_details"]["reasoning_tokens"] == 0
|
|
890
|
+
assert usage["prompt_tokens_details"]["cached_tokens"] == 0
|
{aimlapi_haystack-2.0.0 → aimlapi_haystack-3.0.0}/tests/test_aimlapi_chat_generator_async.py
RENAMED
|
@@ -83,14 +83,17 @@ def mock_async_chat_completion():
|
|
|
83
83
|
|
|
84
84
|
|
|
85
85
|
class TestAIMLAPIChatGeneratorAsync:
|
|
86
|
-
|
|
86
|
+
@pytest.mark.asyncio
|
|
87
|
+
async def test_warm_up_async(self, monkeypatch):
|
|
87
88
|
monkeypatch.setenv("AIMLAPI_API_KEY", "test-api-key")
|
|
88
89
|
component = AIMLAPIChatGenerator()
|
|
90
|
+
if hasattr(component, "warm_up_async"):
|
|
91
|
+
# haystack-ai >= 3.0 creates the async client during async warm-up
|
|
92
|
+
await component.warm_up_async()
|
|
89
93
|
|
|
90
94
|
assert isinstance(component.async_client, AsyncOpenAI)
|
|
91
95
|
assert component.async_client.api_key == "test-api-key"
|
|
92
96
|
assert component.async_client.base_url == "https://api.aimlapi.com/v1/"
|
|
93
|
-
assert not component.generation_kwargs
|
|
94
97
|
|
|
95
98
|
@pytest.mark.asyncio
|
|
96
99
|
async def test_run_async(self, chat_messages, mock_async_chat_completion, monkeypatch): # noqa: ARG002
|
|
@@ -138,7 +141,7 @@ class TestAIMLAPIChatGeneratorAsync:
|
|
|
138
141
|
assert len(results["replies"]) == 1
|
|
139
142
|
message: ChatMessage = results["replies"][0]
|
|
140
143
|
assert "Paris" in message.text
|
|
141
|
-
assert "gpt-5-nano
|
|
144
|
+
assert "gpt-5-nano" in message.meta["model"]
|
|
142
145
|
assert message.meta["finish_reason"] == "stop"
|
|
143
146
|
|
|
144
147
|
@pytest.mark.skipif(
|
|
@@ -164,7 +167,7 @@ class TestAIMLAPIChatGeneratorAsync:
|
|
|
164
167
|
message: ChatMessage = results["replies"][0]
|
|
165
168
|
assert "Paris" in message.text
|
|
166
169
|
|
|
167
|
-
assert "gpt-5-nano
|
|
170
|
+
assert "gpt-5-nano" in message.meta["model"]
|
|
168
171
|
assert message.meta["finish_reason"] == "stop"
|
|
169
172
|
|
|
170
173
|
assert counter > 1
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
|
|
2
|
-
#
|
|
3
|
-
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
|
|
5
|
-
# ruff: noqa: T201
|
|
6
|
-
|
|
7
|
-
"""Run AIMLAPI chat generation with tool calling and execution."""
|
|
8
|
-
|
|
9
|
-
from haystack.components.tools import ToolInvoker
|
|
10
|
-
from haystack.dataclasses import ChatMessage
|
|
11
|
-
from haystack.tools import Tool
|
|
12
|
-
|
|
13
|
-
from haystack_integrations.components.generators.aimlapi import AIMLAPIChatGenerator
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
def weather(city: str) -> str:
|
|
17
|
-
"""Return mock weather info for the given city."""
|
|
18
|
-
|
|
19
|
-
return f"The weather in {city} is sunny and 32°C"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def main() -> None:
|
|
23
|
-
"""Demonstrate calling a tool and feeding the result back to AIMLAPI."""
|
|
24
|
-
|
|
25
|
-
tool_parameters = {
|
|
26
|
-
"type": "object",
|
|
27
|
-
"properties": {"city": {"type": "string"}},
|
|
28
|
-
"required": ["city"],
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
weather_tool = Tool(
|
|
32
|
-
name="weather",
|
|
33
|
-
description="Useful for getting the weather in a specific city",
|
|
34
|
-
parameters=tool_parameters,
|
|
35
|
-
function=weather,
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
tool_invoker = ToolInvoker(tools=[weather_tool])
|
|
39
|
-
|
|
40
|
-
client = AIMLAPIChatGenerator(model="openai/gpt-5-mini-2025-08-07")
|
|
41
|
-
|
|
42
|
-
messages = [
|
|
43
|
-
ChatMessage.from_system("You help users by calling the provided tools when they are relevant."),
|
|
44
|
-
ChatMessage.from_user("What's the weather in Tokyo today?"),
|
|
45
|
-
]
|
|
46
|
-
|
|
47
|
-
print("Requesting a tool call from the model...")
|
|
48
|
-
tool_request = client.run(
|
|
49
|
-
messages=messages,
|
|
50
|
-
tools=[weather_tool],
|
|
51
|
-
generation_kwargs={"tool_choice": {"type": "function", "function": {"name": "weather"}}},
|
|
52
|
-
)["replies"][0]
|
|
53
|
-
|
|
54
|
-
print(f"assistant tool request: {tool_request}")
|
|
55
|
-
|
|
56
|
-
if not tool_request.tool_calls:
|
|
57
|
-
print("No tool call was produced by the model.")
|
|
58
|
-
return
|
|
59
|
-
|
|
60
|
-
tool_messages = tool_invoker.run(messages=[tool_request])["tool_messages"]
|
|
61
|
-
for tool_message in tool_messages:
|
|
62
|
-
for tool_result in tool_message.tool_call_results:
|
|
63
|
-
print(f"tool output: {tool_result.result}")
|
|
64
|
-
|
|
65
|
-
follow_up_messages = [*messages, tool_request, *tool_messages]
|
|
66
|
-
|
|
67
|
-
final_reply = client.run(
|
|
68
|
-
messages=follow_up_messages,
|
|
69
|
-
tools=[weather_tool],
|
|
70
|
-
generation_kwargs={"tool_choice": "none"},
|
|
71
|
-
)["replies"][0]
|
|
72
|
-
|
|
73
|
-
print(f"assistant final answer: {final_reply.text}")
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if __name__ == "__main__":
|
|
77
|
-
main()
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
loaders:
|
|
2
|
-
- ignore_when_discovered:
|
|
3
|
-
- __init__
|
|
4
|
-
modules:
|
|
5
|
-
- haystack_integrations.components.generators.aimlapi.chat.chat_generator
|
|
6
|
-
search_path:
|
|
7
|
-
- ../src
|
|
8
|
-
type: haystack_pydoc_tools.loaders.CustomPythonLoader
|
|
9
|
-
processors:
|
|
10
|
-
- do_not_filter_modules: false
|
|
11
|
-
documented_only: true
|
|
12
|
-
expression: null
|
|
13
|
-
skip_empty_modules: true
|
|
14
|
-
type: filter
|
|
15
|
-
- type: smart
|
|
16
|
-
- type: crossref
|
|
17
|
-
renderer:
|
|
18
|
-
description: AIMLAPI integration for Haystack
|
|
19
|
-
id: integrations-aimlapi
|
|
20
|
-
markdown:
|
|
21
|
-
add_member_class_prefix: false
|
|
22
|
-
add_method_class_prefix: true
|
|
23
|
-
classdef_code_block: false
|
|
24
|
-
descriptive_class_title: false
|
|
25
|
-
descriptive_module_title: true
|
|
26
|
-
filename: aimlapi.md
|
|
27
|
-
title: AIMLAPI
|
|
28
|
-
type: haystack_pydoc_tools.renderers.DocusaurusRenderer
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|