Skip to content

Commit c761b1e

Browse files
authored
Re-use httpx client from assistants (#575)
To leverage connection pooling and reduce the number of connections from assistant services to the workbench service
1 parent 65322b6 commit c761b1e

File tree

17 files changed

+423
-1076
lines changed

17 files changed

+423
-1076
lines changed

assistants/project-assistant/assistant/conversation_clients.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
from typing import Any, Optional, Tuple
1010

11-
import semantic_workbench_api_model.workbench_service_client as wsc
1211
from semantic_workbench_api_model.workbench_service_client import ConversationAPIClient
13-
from semantic_workbench_assistant import settings
1412
from semantic_workbench_assistant.assistant_app import ConversationContext
1513
from semantic_workbench_assistant.storage import read_model
1614

17-
from .logging import logger
1815
from .conversation_project_link import ConversationProjectManager
16+
from .logging import logger
1917
from .project_storage import ProjectStorageManager
2018
from .project_storage_models import ConversationRole
2119

@@ -33,17 +31,7 @@ def get_conversation_client(context: ConversationContext, conversation_id: str)
3331
"""
3432
Gets a client for accessing another conversation.
3533
"""
36-
# Create a client for the target conversation
37-
builder = wsc.WorkbenchServiceClientBuilder(
38-
base_url=str(settings.workbench_service_url),
39-
assistant_service_id=context.assistant._assistant_service_id,
40-
api_key=settings.workbench_service_api_key,
41-
)
42-
client = builder.for_conversation(
43-
assistant_id=context.assistant.id,
44-
conversation_id=conversation_id,
45-
)
46-
return client
34+
return context.for_conversation(conversation_id)._conversation_client
4735

4836
@staticmethod
4937
async def get_coordinator_client_for_project(
@@ -88,17 +76,10 @@ async def create_temporary_context_for_conversation(
8876
Creates a temporary context for the target conversation ID.
8977
"""
9078
try:
91-
# We'll use the same assistant as in the source context
92-
assistant = source_context.assistant
93-
9479
# Create a temporary context with the same properties as the original
9580
# but pointing to a different conversation
9681

97-
temp_context = ConversationContext(
98-
assistant=assistant,
99-
id=target_conversation_id,
100-
title=source_context.title,
101-
)
82+
temp_context = source_context.for_conversation(target_conversation_id)
10283

10384
return temp_context
10485

examples/python/python-01-echo-bot/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
description = "Example of a python Semantic Workbench assistant."
55
authors = [{ name = "Semantic Workbench Team" }]
66
readme = "README.md"
7-
requires-python = ">=3.11"
7+
requires-python = ">=3.11,<3.13"
88
dependencies = ["openai>=1.61.0", "semantic-workbench-assistant>=0.1.0"]
99

1010
[tool.uv]

examples/python/python-01-echo-bot/uv.lock

Lines changed: 1 addition & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/python/python-02-simple-chatbot/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
description = "Exploration of a python Semantic Workbench OpenAI assistant."
55
authors = [{ name = "Semantic Workbench Team" }]
66
readme = "README.md"
7-
requires-python = ">=3.11"
7+
requires-python = ">=3.11,<3.13"
88
dependencies = [
99
"openai>=1.61.0",
1010
"tiktoken>=0.8.0",

examples/python/python-02-simple-chatbot/uv.lock

Lines changed: 2 additions & 241 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/python/python-03-multimodel-chatbot/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
description = "Exploration of a python Semantic Workbench assistant that supports multiple AI models."
55
authors = [{ name = "Semantic Workbench Team" }]
66
readme = "README.md"
7-
requires-python = ">=3.11"
7+
requires-python = ">=3.11,<3.13"
88
dependencies = [
99
"anthropic>=0.34.2",
1010
"google-genai>=1.2.0",

examples/python/python-03-multimodel-chatbot/uv.lock

Lines changed: 2 additions & 245 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libraries/python/assistant-extensions/assistant_extensions/attachments/tests/test_attachments.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Any, AsyncGenerator, AsyncIterator, Callable, Iterable
88
from unittest import mock
99

10+
import httpx
1011
import pytest
1112
from assistant_extensions.attachments import AttachmentsConfigModel, AttachmentsExtension
1213
from llm_client.model import (
@@ -104,20 +105,23 @@ async def test_get_completion_messages_for_attachments(
104105
) -> None:
105106
mock_assistant_app = mock.MagicMock(spec=AssistantAppProtocol)
106107

108+
assistant_id = uuid.uuid4()
109+
107110
mock_conversation_context = mock.MagicMock(
108111
spec=ConversationContext(
109112
id="conversation_id",
110113
title="conversation_title",
111114
assistant=AssistantContext(
112-
id="assistant_id",
115+
id=str(assistant_id),
113116
name="assistant_name",
114117
_assistant_service_id="assistant_id",
115118
_template_id="",
116119
),
120+
httpx_client=httpx.AsyncClient(),
117121
)
118122
)
119123
mock_conversation_context.id = "conversation_id"
120-
mock_conversation_context.assistant.id = "assistant_id"
124+
mock_conversation_context.assistant.id = str(assistant_id)
121125

122126
mock_conversation_context.list_files.return_value = FileList(
123127
files=[

libraries/python/assistant-extensions/assistant_extensions/workflows/runners/_user_proxy.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,7 @@ async def duplicate_conversation(
177177
conversation_id = response.conversation_ids[0]
178178

179179
# create a new conversation context
180-
workflow_context = ConversationContext(
181-
id=str(conversation_id),
182-
title=title,
183-
assistant=context.assistant,
184-
)
180+
workflow_context = context.for_conversation(str(conversation_id))
185181

186182
# send link to chat for the new conversation
187183
await context.send_messages(

libraries/python/content-safety/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
description = "Content Safety for Semantic Workbench Assistants"
55
authors = [{ name = "Semantic Workbench Team" }]
66
readme = "README.md"
7-
requires-python = ">=3.11"
7+
requires-python = ">=3.11,<3.13"
88
dependencies = [
99
"azure-ai-contentsafety>=1.0.0",
1010
"azure-core[aio]>=1.30.0",

0 commit comments

Comments
 (0)