aimlapi-haystack 2.1.0__tar.gz → 3.1.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.1.0/CHANGELOG.md +91 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/PKG-INFO +5 -5
- aimlapi_haystack-3.1.0/examples/aimlapi_with_tools_example.py +54 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/pyproject.toml +5 -5
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/src/haystack_integrations/components/generators/aimlapi/chat/chat_generator.py +7 -60
- aimlapi_haystack-3.1.0/tests/test_aimlapi_chat_generator.py +309 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/tests/test_aimlapi_chat_generator_async.py +16 -118
- aimlapi_haystack-2.1.0/CHANGELOG.md +0 -29
- aimlapi_haystack-2.1.0/examples/aimlapi_with_tools_example.py +0 -77
- aimlapi_haystack-2.1.0/tests/test_aimlapi_chat_generator.py +0 -922
- aimlapi_haystack-2.1.0/tests/test_aimlapi_stub_async.py +0 -15
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/.gitignore +0 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/LICENSE.txt +0 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/README.md +0 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/examples/aimlapi_basic_example.py +0 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/pydoc/config_docusaurus.yml +0 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/src/haystack_integrations/components/generators/aimlapi/__init__.py +0 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/src/haystack_integrations/components/generators/aimlapi/chat/__init__.py +0 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/src/haystack_integrations/components/generators/py.typed +0 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/tests/__init__.py +0 -0
- {aimlapi_haystack-2.1.0 → aimlapi_haystack-3.1.0}/tests/conftest.py +0 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [integrations/aimlapi-v3.0.0] - 2026-10-16
|
|
4
|
+
|
|
5
|
+
### 🐛 Bug Fixes
|
|
6
|
+
|
|
7
|
+
- Fix new issues raised by ruff 0.16.0 (#3670)
|
|
8
|
+
|
|
9
|
+
### 🧪 Testing
|
|
10
|
+
|
|
11
|
+
- Fix tests for openai>=3.0.0 (#3801)
|
|
12
|
+
- Test compatible integrations with Python 3.15; update pyproject (#4104)
|
|
13
|
+
|
|
14
|
+
### ⚙️ CI
|
|
15
|
+
|
|
16
|
+
- Improve changelog generation; fix existing changelogs (#3883)
|
|
17
|
+
|
|
18
|
+
### 🧹 Chores
|
|
19
|
+
|
|
20
|
+
- Replace ToolInvoker with Agent in tests, examples and docstrings (#4108)
|
|
21
|
+
- [**breaking**] Drop Python 3.10 support (#4105)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## [integrations/aimlapi-v2.1.0] - 2026-07-13
|
|
25
|
+
|
|
26
|
+
### 🚀 Features
|
|
27
|
+
|
|
28
|
+
- Improve type annotations, fix duplicate code in aimlapi integration (#2969)
|
|
29
|
+
|
|
30
|
+
### 🐛 Bug Fixes
|
|
31
|
+
|
|
32
|
+
- Support Haystack 3.0's lazy OpenAI client initialization in OpenAI-inheriting generators (#3536)
|
|
33
|
+
|
|
34
|
+
### 📚 Documentation
|
|
35
|
+
|
|
36
|
+
- Simplify pydoc configs (#2855)
|
|
37
|
+
|
|
38
|
+
### 🧪 Testing
|
|
39
|
+
|
|
40
|
+
- Relax AIMLAPI tests (#2815)
|
|
41
|
+
- Test compatible integrations with python 3.14; update pyproject (#3001)
|
|
42
|
+
- Track test coverage for all integrations (#3065)
|
|
43
|
+
- Aimlapi - add unit tests (#3184)
|
|
44
|
+
- Trust test modules under Haystack 3.0's deserialization allowlist (#3537)
|
|
45
|
+
- Make Tool/Agent serialization assertions version-agnostic for Haystack 2.x/3.x (#3533)
|
|
46
|
+
- Guard ToolInvoker imports so chat-generator tests run under Haystack 3.0 (#3535)
|
|
47
|
+
- Fix tests for integrations that inherit from the OpenAIChatGenerator (#3583)
|
|
48
|
+
|
|
49
|
+
### 🧹 Chores
|
|
50
|
+
|
|
51
|
+
- Standardize author mentions (#2897)
|
|
52
|
+
- Enforce ruff docstring rules (D102/D103/D205/D209/D213/D417/D419) in first 10 integrations (#3008)
|
|
53
|
+
- Update aimlapi (#3290)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
## [integrations/aimlapi-v2.0.0] - 2026-01-09
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
### 🧹 Chores
|
|
60
|
+
|
|
61
|
+
- Remove Readme API CI workflow and configs (#2573)
|
|
62
|
+
- Make fmt command more forgiving (#2671)
|
|
63
|
+
- [**breaking**] Aimlapi - drop Python 3.9 and use X|Y typing (#2684)
|
|
64
|
+
|
|
65
|
+
### 🌀 Miscellaneous
|
|
66
|
+
|
|
67
|
+
- Enhancement: Adopt PEP 585 type hinting (part 6) (#2532)
|
|
68
|
+
|
|
69
|
+
## [integrations/aimlapi-v1.0.0] - 2025-11-12
|
|
70
|
+
|
|
71
|
+
### 🚀 Features
|
|
72
|
+
|
|
73
|
+
- *(aimlapi)* Add AIMLAPI provider integration (#2267)
|
|
74
|
+
|
|
75
|
+
### 📚 Documentation
|
|
76
|
+
|
|
77
|
+
- Add pydoc configurations for Docusaurus (#2411)
|
|
78
|
+
|
|
79
|
+
### ⚙️ CI
|
|
80
|
+
|
|
81
|
+
- Change pytest command (#2475)
|
|
82
|
+
|
|
83
|
+
### 🧹 Chores
|
|
84
|
+
|
|
85
|
+
- AIMLAPI - clean up README (#2356)
|
|
86
|
+
|
|
87
|
+
### 🌀 Miscellaneous
|
|
88
|
+
|
|
89
|
+
- Add missing GA workflow for `aimlapi` integration (#2504)
|
|
90
|
+
|
|
91
|
+
<!-- generated by git-cliff -->
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: aimlapi-haystack
|
|
3
|
-
Version:
|
|
3
|
+
Version: 3.1.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
|
|
@@ -10,15 +10,15 @@ 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
|
|
17
16
|
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
18
18
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
19
19
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
20
|
-
Requires-Python: >=3.
|
|
21
|
-
Requires-Dist: haystack-ai>=
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: haystack-ai>=3.5.0
|
|
22
22
|
Description-Content-Type: text/markdown
|
|
23
23
|
|
|
24
24
|
# aimlapi-haystack
|
|
@@ -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()
|
|
@@ -7,7 +7,7 @@ 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
13
|
authors = [{ name = "deepset GmbH", email = "info@deepset.ai" }, { name = "Dmitry <D1m7asis> Tumanov", email = "d1m7asis@gmail.com" }]
|
|
@@ -15,15 +15,15 @@ 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",
|
|
22
21
|
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Programming Language :: Python :: 3.15",
|
|
23
23
|
"Programming Language :: Python :: Implementation :: CPython",
|
|
24
24
|
"Programming Language :: Python :: Implementation :: PyPy",
|
|
25
25
|
]
|
|
26
|
-
dependencies = ["haystack-ai>=
|
|
26
|
+
dependencies = ["haystack-ai>=3.5.0"]
|
|
27
27
|
|
|
28
28
|
[project.urls]
|
|
29
29
|
Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/aimlapi#readme"
|
|
@@ -56,8 +56,7 @@ dependencies = [
|
|
|
56
56
|
"pytest-cov",
|
|
57
57
|
"pytest-rerunfailures",
|
|
58
58
|
"mypy",
|
|
59
|
-
"pip"
|
|
60
|
-
"pytz"
|
|
59
|
+
"pip"
|
|
61
60
|
]
|
|
62
61
|
|
|
63
62
|
[tool.hatch.envs.test.scripts]
|
|
@@ -127,6 +126,7 @@ ignore = [
|
|
|
127
126
|
"PLR0912",
|
|
128
127
|
"PLR0913",
|
|
129
128
|
"PLR0915",
|
|
129
|
+
"PLR0917",
|
|
130
130
|
# Misc
|
|
131
131
|
"B008",
|
|
132
132
|
"S101",
|
|
@@ -4,15 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
from typing import Any
|
|
6
6
|
|
|
7
|
-
from haystack import component, default_to_dict
|
|
7
|
+
from haystack import component, default_to_dict
|
|
8
8
|
from haystack.components.generators.chat import OpenAIChatGenerator
|
|
9
|
-
from haystack.dataclasses import
|
|
10
|
-
from haystack.tools import ToolsType,
|
|
9
|
+
from haystack.dataclasses import StreamingCallbackT
|
|
10
|
+
from haystack.tools import ToolsType, serialize_tools_or_toolset
|
|
11
11
|
from haystack.utils import serialize_callable
|
|
12
12
|
from haystack.utils.auth import Secret
|
|
13
13
|
|
|
14
|
-
logger = logging.getLogger(__name__)
|
|
15
|
-
|
|
16
14
|
AIMLAPI_HEADERS = {"HTTP-Referer": "https://github.com/deepset-ai/haystack-core-integrations", "X-Title": "Haystack"}
|
|
17
15
|
|
|
18
16
|
|
|
@@ -153,59 +151,8 @@ class AIMLAPIChatGenerator(OpenAIChatGenerator):
|
|
|
153
151
|
http_client_kwargs=self.http_client_kwargs,
|
|
154
152
|
)
|
|
155
153
|
|
|
156
|
-
def
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
streaming_callback: StreamingCallbackT | None = None,
|
|
161
|
-
generation_kwargs: dict[str, Any] | None = None,
|
|
162
|
-
tools: ToolsType | None = None,
|
|
163
|
-
tools_strict: bool | None = None,
|
|
164
|
-
) -> dict[str, Any]:
|
|
165
|
-
# update generation kwargs by merging with the generation kwargs passed to the run method
|
|
166
|
-
generation_kwargs = {**self.generation_kwargs, **(generation_kwargs or {})}
|
|
167
|
-
extra_headers = {**AIMLAPI_HEADERS, **(self.extra_headers or {})}
|
|
168
|
-
|
|
169
|
-
# adapt ChatMessage(s) to the format expected by the OpenAI API (AIMLAPI uses the same format)
|
|
170
|
-
aimlapi_formatted_messages: list[dict[str, Any]] = [message.to_openai_dict_format() for message in messages]
|
|
171
|
-
|
|
172
|
-
tools_strict = tools_strict if tools_strict is not None else self.tools_strict
|
|
173
|
-
flattened_tools = flatten_tools_or_toolsets(tools or self.tools)
|
|
174
|
-
_check_duplicate_tool_names(flattened_tools)
|
|
175
|
-
|
|
176
|
-
openai_tools = {}
|
|
177
|
-
if flattened_tools:
|
|
178
|
-
tool_definitions = []
|
|
179
|
-
for tool in flattened_tools:
|
|
180
|
-
function_spec = {**tool.tool_spec}
|
|
181
|
-
if tools_strict:
|
|
182
|
-
function_spec["strict"] = True
|
|
183
|
-
parameters = function_spec.get("parameters")
|
|
184
|
-
if isinstance(parameters, dict):
|
|
185
|
-
parameters["additionalProperties"] = False
|
|
186
|
-
tool_definitions.append({"type": "function", "function": function_spec})
|
|
187
|
-
openai_tools = {"tools": tool_definitions}
|
|
188
|
-
|
|
189
|
-
is_streaming = streaming_callback is not None
|
|
190
|
-
num_responses = generation_kwargs.pop("n", 1)
|
|
191
|
-
|
|
192
|
-
if is_streaming and num_responses > 1:
|
|
193
|
-
msg = "Cannot stream multiple responses, please set n=1."
|
|
194
|
-
raise ValueError(msg)
|
|
195
|
-
response_format = generation_kwargs.pop("response_format", None)
|
|
196
|
-
|
|
197
|
-
base_args = {
|
|
198
|
-
"model": self.model,
|
|
199
|
-
"messages": aimlapi_formatted_messages,
|
|
200
|
-
"n": num_responses,
|
|
201
|
-
**openai_tools,
|
|
202
|
-
"extra_body": {**generation_kwargs},
|
|
203
|
-
"extra_headers": {**extra_headers},
|
|
154
|
+
def _adapt_generation_kwargs(self, generation_kwargs: dict[str, Any]) -> dict[str, Any]:
|
|
155
|
+
return {
|
|
156
|
+
"extra_body": dict(generation_kwargs),
|
|
157
|
+
"extra_headers": {**AIMLAPI_HEADERS, **(self.extra_headers or {})},
|
|
204
158
|
}
|
|
205
|
-
if response_format and not is_streaming:
|
|
206
|
-
return {**base_args, "response_format": response_format, "openai_endpoint": "parse"}
|
|
207
|
-
|
|
208
|
-
final_args = {**base_args, "stream": is_streaming, "openai_endpoint": "create"}
|
|
209
|
-
if response_format:
|
|
210
|
-
final_args["response_format"] = response_format
|
|
211
|
-
return final_args
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from haystack.components.agents import Agent
|
|
9
|
+
from haystack.components.generators.utils import print_streaming_chunk
|
|
10
|
+
from haystack.dataclasses import ChatMessage, ChatRole, StreamingChunk, ToolCall
|
|
11
|
+
from haystack.tools import Tool
|
|
12
|
+
from haystack.utils.auth import Secret
|
|
13
|
+
|
|
14
|
+
from haystack_integrations.components.generators.aimlapi.chat.chat_generator import AIMLAPIChatGenerator
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def weather(city: str):
|
|
18
|
+
"""Get weather for a given city."""
|
|
19
|
+
return f"The weather in {city} is sunny and 32°C"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.fixture
|
|
23
|
+
def tools():
|
|
24
|
+
tool_parameters = {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}
|
|
25
|
+
tool = Tool(
|
|
26
|
+
name="weather",
|
|
27
|
+
description="useful to determine the weather in a given location",
|
|
28
|
+
parameters=tool_parameters,
|
|
29
|
+
function=weather,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
return [tool]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class TestAIMLAPIChatGenerator:
|
|
36
|
+
def test_init_default(self, monkeypatch):
|
|
37
|
+
monkeypatch.setenv("AIMLAPI_API_KEY", "test-api-key")
|
|
38
|
+
component = AIMLAPIChatGenerator()
|
|
39
|
+
assert component.api_key.resolve_value() == "test-api-key"
|
|
40
|
+
assert component.model == "openai/gpt-5-chat-latest"
|
|
41
|
+
assert component.api_base_url == "https://api.aimlapi.com/v1"
|
|
42
|
+
assert component.streaming_callback is None
|
|
43
|
+
assert not component.generation_kwargs
|
|
44
|
+
|
|
45
|
+
def test_init_with_parameters(self):
|
|
46
|
+
component = AIMLAPIChatGenerator(
|
|
47
|
+
api_key=Secret.from_token("test-api-key"),
|
|
48
|
+
model="openai/gpt-5-chat-latest",
|
|
49
|
+
streaming_callback=print_streaming_chunk,
|
|
50
|
+
api_base_url="test-base-url",
|
|
51
|
+
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
|
|
52
|
+
)
|
|
53
|
+
assert component.api_key.resolve_value() == "test-api-key"
|
|
54
|
+
assert component.model == "openai/gpt-5-chat-latest"
|
|
55
|
+
assert component.streaming_callback is print_streaming_chunk
|
|
56
|
+
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
|
|
57
|
+
|
|
58
|
+
def test_to_dict_default(self, monkeypatch):
|
|
59
|
+
monkeypatch.setenv("AIMLAPI_API_KEY", "test-api-key")
|
|
60
|
+
component = AIMLAPIChatGenerator()
|
|
61
|
+
data = component.to_dict()
|
|
62
|
+
|
|
63
|
+
assert (
|
|
64
|
+
data["type"]
|
|
65
|
+
== "haystack_integrations.components.generators.aimlapi.chat.chat_generator.AIMLAPIChatGenerator"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
expected_params = {
|
|
69
|
+
"api_key": {"env_vars": ["AIMLAPI_API_KEY"], "strict": True, "type": "env_var"},
|
|
70
|
+
"model": "openai/gpt-5-chat-latest",
|
|
71
|
+
"streaming_callback": None,
|
|
72
|
+
"api_base_url": "https://api.aimlapi.com/v1",
|
|
73
|
+
"generation_kwargs": {},
|
|
74
|
+
"extra_headers": None,
|
|
75
|
+
"timeout": None,
|
|
76
|
+
"max_retries": None,
|
|
77
|
+
"tools": None,
|
|
78
|
+
"http_client_kwargs": None,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
for key, value in expected_params.items():
|
|
82
|
+
assert data["init_parameters"][key] == value
|
|
83
|
+
|
|
84
|
+
def test_to_dict_with_parameters(self, monkeypatch):
|
|
85
|
+
monkeypatch.setenv("ENV_VAR", "test-api-key")
|
|
86
|
+
component = AIMLAPIChatGenerator(
|
|
87
|
+
api_key=Secret.from_env_var("ENV_VAR"),
|
|
88
|
+
model="openai/gpt-5-chat-latest",
|
|
89
|
+
streaming_callback=print_streaming_chunk,
|
|
90
|
+
api_base_url="test-base-url",
|
|
91
|
+
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
|
|
92
|
+
extra_headers={"test-header": "test-value"},
|
|
93
|
+
timeout=10,
|
|
94
|
+
max_retries=10,
|
|
95
|
+
tools=None,
|
|
96
|
+
http_client_kwargs={"proxy": "http://localhost:8080"},
|
|
97
|
+
)
|
|
98
|
+
data = component.to_dict()
|
|
99
|
+
|
|
100
|
+
assert (
|
|
101
|
+
data["type"]
|
|
102
|
+
== "haystack_integrations.components.generators.aimlapi.chat.chat_generator.AIMLAPIChatGenerator"
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
expected_params = {
|
|
106
|
+
"api_key": {"env_vars": ["ENV_VAR"], "strict": True, "type": "env_var"},
|
|
107
|
+
"model": "openai/gpt-5-chat-latest",
|
|
108
|
+
"api_base_url": "test-base-url",
|
|
109
|
+
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
|
|
110
|
+
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
|
|
111
|
+
"extra_headers": {"test-header": "test-value"},
|
|
112
|
+
"timeout": 10,
|
|
113
|
+
"max_retries": 10,
|
|
114
|
+
"tools": None,
|
|
115
|
+
"http_client_kwargs": {"proxy": "http://localhost:8080"},
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
for key, value in expected_params.items():
|
|
119
|
+
assert data["init_parameters"][key] == value
|
|
120
|
+
|
|
121
|
+
def test_from_dict(self, monkeypatch):
|
|
122
|
+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
|
|
123
|
+
data = {
|
|
124
|
+
"type": ("haystack_integrations.components.generators.aimlapi.chat.chat_generator.AIMLAPIChatGenerator"),
|
|
125
|
+
"init_parameters": {
|
|
126
|
+
"api_key": {"env_vars": ["AIMLAPI_API_KEY"], "strict": True, "type": "env_var"},
|
|
127
|
+
"model": "openai/gpt-5-chat-latest",
|
|
128
|
+
"api_base_url": "test-base-url",
|
|
129
|
+
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
|
|
130
|
+
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
|
|
131
|
+
"extra_headers": {"test-header": "test-value"},
|
|
132
|
+
"timeout": 10,
|
|
133
|
+
"max_retries": 10,
|
|
134
|
+
"tools": None,
|
|
135
|
+
"http_client_kwargs": {"proxy": "http://localhost:8080"},
|
|
136
|
+
},
|
|
137
|
+
}
|
|
138
|
+
component = AIMLAPIChatGenerator.from_dict(data)
|
|
139
|
+
assert component.model == "openai/gpt-5-chat-latest"
|
|
140
|
+
assert component.streaming_callback is print_streaming_chunk
|
|
141
|
+
assert component.api_base_url == "test-base-url"
|
|
142
|
+
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
|
|
143
|
+
assert component.api_key == Secret.from_env_var("AIMLAPI_API_KEY")
|
|
144
|
+
assert component.http_client_kwargs == {"proxy": "http://localhost:8080"}
|
|
145
|
+
assert component.tools is None
|
|
146
|
+
assert component.extra_headers == {"test-header": "test-value"}
|
|
147
|
+
assert component.timeout == 10
|
|
148
|
+
assert component.max_retries == 10
|
|
149
|
+
|
|
150
|
+
@pytest.mark.parametrize("extra_headers", [None, {"X-Title": "Custom", "X-Custom": "value"}])
|
|
151
|
+
def test_prepare_api_call_with_params(self, extra_headers):
|
|
152
|
+
component = AIMLAPIChatGenerator(
|
|
153
|
+
generation_kwargs={"max_tokens": 10, "temperature": 0.5}, extra_headers=extra_headers
|
|
154
|
+
)
|
|
155
|
+
args = component._prepare_api_call(
|
|
156
|
+
messages=[ChatMessage.from_user("hi")], generation_kwargs={"temperature": 0.7}
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
assert args["extra_body"] == {"max_tokens": 10, "temperature": 0.7}
|
|
160
|
+
assert args["extra_headers"] == {
|
|
161
|
+
"HTTP-Referer": "https://github.com/deepset-ai/haystack-core-integrations",
|
|
162
|
+
"X-Title": "Haystack",
|
|
163
|
+
**(extra_headers or {}),
|
|
164
|
+
}
|
|
165
|
+
assert component.generation_kwargs == {"max_tokens": 10, "temperature": 0.5}
|
|
166
|
+
|
|
167
|
+
@pytest.mark.skipif(
|
|
168
|
+
not os.environ.get("AIMLAPI_API_KEY", None),
|
|
169
|
+
reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
|
|
170
|
+
)
|
|
171
|
+
@pytest.mark.integration
|
|
172
|
+
def test_live_run(self):
|
|
173
|
+
chat_messages = [ChatMessage.from_user("What's the capital of France")]
|
|
174
|
+
component = AIMLAPIChatGenerator(model="openai/gpt-5-nano-2025-08-07")
|
|
175
|
+
results = component.run(chat_messages)
|
|
176
|
+
assert len(results["replies"]) == 1
|
|
177
|
+
message: ChatMessage = results["replies"][0]
|
|
178
|
+
assert message.text
|
|
179
|
+
assert "Paris" in message.text
|
|
180
|
+
assert "gpt-5-nano" in message.meta["model"]
|
|
181
|
+
assert message.meta["finish_reason"] == "stop"
|
|
182
|
+
|
|
183
|
+
@pytest.mark.skipif(
|
|
184
|
+
not os.environ.get("AIMLAPI_API_KEY", None),
|
|
185
|
+
reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
|
|
186
|
+
)
|
|
187
|
+
@pytest.mark.integration
|
|
188
|
+
def test_live_run_streaming(self):
|
|
189
|
+
class Callback:
|
|
190
|
+
def __init__(self):
|
|
191
|
+
self.responses = ""
|
|
192
|
+
self.counter = 0
|
|
193
|
+
|
|
194
|
+
def __call__(self, chunk: StreamingChunk) -> None:
|
|
195
|
+
self.counter += 1
|
|
196
|
+
self.responses += chunk.content if chunk.content else ""
|
|
197
|
+
|
|
198
|
+
callback = Callback()
|
|
199
|
+
component = AIMLAPIChatGenerator(streaming_callback=callback, model="openai/gpt-5-nano-2025-08-07")
|
|
200
|
+
results = component.run([ChatMessage.from_user("What's the capital of France?")])
|
|
201
|
+
|
|
202
|
+
assert len(results["replies"]) == 1
|
|
203
|
+
message: ChatMessage = results["replies"][0]
|
|
204
|
+
assert message.text
|
|
205
|
+
assert "Paris" in message.text
|
|
206
|
+
|
|
207
|
+
assert "gpt-5-nano" in message.meta["model"]
|
|
208
|
+
assert message.meta["finish_reason"] == "stop"
|
|
209
|
+
|
|
210
|
+
assert callback.counter > 1
|
|
211
|
+
assert "Paris" in callback.responses
|
|
212
|
+
|
|
213
|
+
@pytest.mark.skipif(
|
|
214
|
+
not os.environ.get("AIMLAPI_API_KEY", None),
|
|
215
|
+
reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
|
|
216
|
+
)
|
|
217
|
+
@pytest.mark.integration
|
|
218
|
+
def test_live_run_with_tools_and_response(self, tools):
|
|
219
|
+
"""
|
|
220
|
+
Integration test that the AIMLAPIChatGenerator component can run with tools and get a response.
|
|
221
|
+
"""
|
|
222
|
+
initial_messages = [ChatMessage.from_user("What's the weather like in Paris and Berlin?")]
|
|
223
|
+
component = AIMLAPIChatGenerator(tools=tools, model="openai/gpt-5-nano-2025-08-07")
|
|
224
|
+
results = component.run(messages=initial_messages, generation_kwargs={"tool_choice": "auto"})
|
|
225
|
+
|
|
226
|
+
assert len(results["replies"]) == 1
|
|
227
|
+
|
|
228
|
+
# Find the message with tool calls
|
|
229
|
+
tool_message = results["replies"][0]
|
|
230
|
+
|
|
231
|
+
assert isinstance(tool_message, ChatMessage)
|
|
232
|
+
tool_calls = tool_message.tool_calls
|
|
233
|
+
assert len(tool_calls) == 2
|
|
234
|
+
assert ChatMessage.is_from(tool_message, ChatRole.ASSISTANT)
|
|
235
|
+
|
|
236
|
+
for tool_call in tool_calls:
|
|
237
|
+
assert tool_call.id is not None
|
|
238
|
+
assert isinstance(tool_call, ToolCall)
|
|
239
|
+
assert tool_call.tool_name == "weather"
|
|
240
|
+
|
|
241
|
+
arguments = [tool_call.arguments for tool_call in tool_calls]
|
|
242
|
+
assert sorted(arguments, key=lambda x: x["city"]) == [{"city": "Berlin"}, {"city": "Paris"}]
|
|
243
|
+
assert tool_message.meta["finish_reason"] == "tool_calls"
|
|
244
|
+
|
|
245
|
+
new_messages = [
|
|
246
|
+
initial_messages[0],
|
|
247
|
+
tool_message,
|
|
248
|
+
ChatMessage.from_tool(tool_result="22° C and sunny", origin=tool_calls[0]),
|
|
249
|
+
ChatMessage.from_tool(tool_result="16° C and windy", origin=tool_calls[1]),
|
|
250
|
+
]
|
|
251
|
+
# Pass the tool result to the model to get the final response
|
|
252
|
+
results = component.run(new_messages)
|
|
253
|
+
|
|
254
|
+
assert len(results["replies"]) == 1
|
|
255
|
+
final_message = results["replies"][0]
|
|
256
|
+
assert final_message.is_from(ChatRole.ASSISTANT)
|
|
257
|
+
assert len(final_message.text) > 0
|
|
258
|
+
assert "paris" in final_message.text.lower()
|
|
259
|
+
|
|
260
|
+
@pytest.mark.skipif(
|
|
261
|
+
not os.environ.get("AIMLAPI_API_KEY", None),
|
|
262
|
+
reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
|
|
263
|
+
)
|
|
264
|
+
@pytest.mark.integration
|
|
265
|
+
def test_live_run_with_tools_streaming(self, tools):
|
|
266
|
+
"""
|
|
267
|
+
Integration test that the AIMLAPIChatGenerator component can run with tools and streaming.
|
|
268
|
+
"""
|
|
269
|
+
component = AIMLAPIChatGenerator(
|
|
270
|
+
tools=tools, streaming_callback=print_streaming_chunk, model="openai/gpt-5-nano-2025-08-07"
|
|
271
|
+
)
|
|
272
|
+
results = component.run(
|
|
273
|
+
[ChatMessage.from_user("What's the weather like in Paris and Berlin?")],
|
|
274
|
+
generation_kwargs={"tool_choice": "auto"},
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
assert len(results["replies"]) == 1
|
|
278
|
+
|
|
279
|
+
# Find the message with tool calls
|
|
280
|
+
tool_message = results["replies"][0]
|
|
281
|
+
|
|
282
|
+
assert isinstance(tool_message, ChatMessage)
|
|
283
|
+
tool_calls = tool_message.tool_calls
|
|
284
|
+
assert len(tool_calls) == 2
|
|
285
|
+
assert ChatMessage.is_from(tool_message, ChatRole.ASSISTANT)
|
|
286
|
+
|
|
287
|
+
for tool_call in tool_calls:
|
|
288
|
+
assert tool_call.id is not None
|
|
289
|
+
assert isinstance(tool_call, ToolCall)
|
|
290
|
+
assert tool_call.tool_name == "weather"
|
|
291
|
+
|
|
292
|
+
arguments = [tool_call.arguments for tool_call in tool_calls]
|
|
293
|
+
assert sorted(arguments, key=lambda x: x["city"]) == [{"city": "Berlin"}, {"city": "Paris"}]
|
|
294
|
+
assert tool_message.meta["finish_reason"] == "tool_calls"
|
|
295
|
+
|
|
296
|
+
@pytest.mark.skipif(
|
|
297
|
+
not os.environ.get("AIMLAPI_API_KEY", None),
|
|
298
|
+
reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
|
|
299
|
+
)
|
|
300
|
+
@pytest.mark.integration
|
|
301
|
+
def test_agent_with_aimlapi_chat_generator(self, tools):
|
|
302
|
+
"""
|
|
303
|
+
Test that the AIMLAPIChatGenerator component can drive tool calls in an Agent
|
|
304
|
+
"""
|
|
305
|
+
agent = Agent(chat_generator=AIMLAPIChatGenerator(model="openai/gpt-5-mini-2025-08-07"), tools=tools)
|
|
306
|
+
result = agent.run(messages=[ChatMessage.from_user("What's the weather like in Paris?")])
|
|
307
|
+
|
|
308
|
+
tool_results = [msg.tool_call_result.result for msg in result["messages"] if msg.tool_call_result]
|
|
309
|
+
assert "The weather in Paris is sunny and 32°C" in tool_results
|