Skip to content

Commit 487704a

Browse files
committed
fix: correct OpenAPIServiceConnector and OpenAPIServiceToFunctions code snippets
- OpenAPIServiceConnector: use ToolCall and ChatMessage.from_assistant(tool_calls=[...]) instead of invalid ChatMessage.from_assistant(json.dumps(fc_payload)); fix invalid Python serper_token = <your_serper_dev_token> to use placeholder string - OpenAPIServiceToFunctions: use ByteStream.from_string() with inline spec instead of non-existent path/to/openapi_definition.yaml Fixes #10645 Made-with: Cursor
1 parent 1abd5c3 commit 487704a

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

haystack/components/connectors/openapi_service.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import Any
99

1010
from haystack import component, default_from_dict, default_to_dict
11-
from haystack.dataclasses import ChatMessage, ChatRole
11+
from haystack.dataclasses import ChatMessage, ToolCall, ChatRole
1212
from haystack.lazy_imports import LazyImport
1313

1414
with LazyImport("Run 'pip install openapi3'") as openapi_imports:
@@ -178,20 +178,22 @@ class OpenAPIServiceConnector:
178178
from haystack.dataclasses import ChatMessage
179179
180180
181-
fc_payload = [{'function': {'arguments': '{"q": "Why was Sam Altman ousted from OpenAI?"}', 'name': 'search'},
182-
'id': 'call_PmEBYvZ7mGrQP5PUASA5m9wO', 'type': 'function'}]
181+
tool_call = ToolCall(
182+
tool_name="search",
183+
arguments={"q": "Why was Sam Altman ousted from OpenAI?"},
184+
)
185+
message = ChatMessage.from_assistant(tool_calls=[tool_call])
183186
184-
serper_token = <your_serper_dev_token>
187+
serper_token = "your_serper_dev_token" # or use Secret.from_env_var("SERPERDEV_API_KEY")
185188
serperdev_openapi_spec = json.loads(requests.get("https://bit.ly/serper_dev_spec").text)
186189
service_connector = OpenAPIServiceConnector()
187-
result = service_connector.run(messages=[ChatMessage.from_assistant(json.dumps(fc_payload))],
188-
service_openapi_spec=serperdev_openapi_spec, service_credentials=serper_token)
190+
result = service_connector.run(
191+
messages=[message],
192+
service_openapi_spec=serperdev_openapi_spec,
193+
service_credentials=serper_token,
194+
)
189195
print(result)
190-
191-
>> {'service_response': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text=
192-
>> '{"searchParameters": {"q": "Why was Sam Altman ousted from OpenAI?",
193-
>> "type": "search", "engine": "google"}, "answerBox": {"snippet": "Concerns over AI safety and OpenAI\'s role
194-
>> in protecting were at the center of Altman\'s brief ouster from the company."...
196+
# {'service_response': [ChatMessage(...)]}
195197
```
196198
197199
"""

haystack/components/converters/openapi_functions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ class OpenAPIServiceToFunctions:
3737
Usage example:
3838
```python
3939
from haystack.components.converters import OpenAPIServiceToFunctions
40+
from haystack.dataclasses.byte_stream import ByteStream
4041
4142
converter = OpenAPIServiceToFunctions()
42-
result = converter.run(sources=["path/to/openapi_definition.yaml"])
43+
spec = ByteStream.from_string(
44+
'{"openapi":"3.0.0","info":{"title":"API","version":"1.0.0"},"paths":{"/search":{"get":{"operationId":"search","summary":"Search","parameters":[{"name":"q","in":"query","required":true,"schema":{"type":"string"}}]}}}}'
45+
)
46+
result = converter.run(sources=[spec])
4347
assert result["functions"]
4448
```
4549
"""

0 commit comments

Comments
 (0)