-
Couldn't load subscription status.
- Fork 16
feat: initial support for conversational agents #759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GabrielVasilescu04
wants to merge
1
commit into
main
Choose a base branch
from
feature/initial-support-for-conversational-agents
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from .._config import Config | ||
| from .._execution_context import ExecutionContext | ||
| from .._utils import Endpoint, RequestSpec | ||
| from ..agent.conversation import UiPathConversationMessage | ||
| from ..tracing import traced | ||
| from ._base_service import BaseService | ||
|
|
||
|
|
||
| class ConversationsService(BaseService): | ||
| def __init__(self, config: Config, execution_context: ExecutionContext) -> None: | ||
| super().__init__(config=config, execution_context=execution_context) | ||
|
|
||
| @traced(name="retrieve_latest_exchange_message", run_type="uipath") | ||
| async def retrieve_latest_exchange_message_async( | ||
| self, conversation_id: str, exchange_id: str, message_id: str | ||
| ) -> UiPathConversationMessage: | ||
| retrieve_message_spec = self._retrieve_latest_exchange_message_spec( | ||
| conversation_id, exchange_id, message_id | ||
| ) | ||
|
|
||
| response = await self.request_async( | ||
| retrieve_message_spec.method, retrieve_message_spec.endpoint | ||
| ) | ||
|
|
||
| return UiPathConversationMessage.model_validate(response.json()) | ||
|
|
||
| def _retrieve_latest_exchange_message_spec( | ||
| self, conversation_id: str, exchange_id: str, message_id: str | ||
| ) -> RequestSpec: | ||
| return RequestSpec( | ||
| method="GET", | ||
| endpoint=Endpoint( | ||
| f"/autopilotforeveryone_/api/v1/conversation/{conversation_id}/exchange/{exchange_id}/message/{message_id}" | ||
| ), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from typing import Generic, List, Optional, TypeVar | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, Field | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
||
| class GetResponse(BaseModel, Generic[T]): | ||
| """Generic response for paginated GET requests.""" | ||
|
|
||
| model_config = ConfigDict(validate_by_name=True, validate_by_alias=True) | ||
|
|
||
| data: List[T] = Field(alias="data") | ||
| cursor: Optional[str] = Field(default=None, alias="cursor") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import pytest | ||
| from pytest_httpx import HTTPXMock | ||
|
|
||
| from uipath._config import Config | ||
| from uipath._execution_context import ExecutionContext | ||
| from uipath._services.conversations_service import ConversationsService | ||
| from uipath._utils.constants import HEADER_USER_AGENT | ||
| from uipath.agent.conversation import UiPathConversationMessage | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def service( | ||
| config: Config, execution_context: ExecutionContext | ||
| ) -> ConversationsService: | ||
| return ConversationsService(config=config, execution_context=execution_context) | ||
|
|
||
|
|
||
| class TestConversationsService: | ||
| class TestRetrieveLatestExchangeMessage: | ||
| @pytest.mark.anyio | ||
| async def test_retrieve_latest_exchange_message( | ||
| self, | ||
| httpx_mock: HTTPXMock, | ||
| service: ConversationsService, | ||
| base_url: str, | ||
| org: str, | ||
| tenant: str, | ||
| version: str, | ||
| ) -> None: | ||
| """Test retrieving a specific message from an exchange.""" | ||
| conversation_id = "123" | ||
| exchange_id = "202cf2d1-926e-422d-8cf2-4f5735fa91fa" | ||
| message_id = "08de239e-90da-4d17-b986-b7785268d8d7" | ||
|
|
||
| httpx_mock.add_response( | ||
| url=f"{base_url}{org}{tenant}/autopilotforeveryone_/api/v1/conversation/{conversation_id}/exchange/{exchange_id}/message/{message_id}", | ||
| status_code=200, | ||
| json={ | ||
| "messageId": message_id, | ||
| "role": "assistant", | ||
| "contentParts": [], | ||
| "createdAt": "2024-01-01T00:00:00Z", | ||
| "updatedAt": "2024-01-01T00:00:00Z", | ||
| }, | ||
| ) | ||
|
|
||
| result = await service.retrieve_latest_exchange_message_async( | ||
| conversation_id=conversation_id, | ||
| exchange_id=exchange_id, | ||
| message_id=message_id, | ||
| ) | ||
|
|
||
| assert isinstance(result, UiPathConversationMessage) | ||
| assert result.message_id == message_id | ||
| assert result.role == "assistant" | ||
|
|
||
| sent_request = httpx_mock.get_request() | ||
| if sent_request is None: | ||
| raise Exception("No request was sent") | ||
|
|
||
| assert sent_request.method == "GET" | ||
| assert ( | ||
| sent_request.url | ||
| == f"{base_url}{org}{tenant}/autopilotforeveryone_/api/v1/conversation/{conversation_id}/exchange/{exchange_id}/message/{message_id}" | ||
| ) | ||
|
|
||
| assert HEADER_USER_AGENT in sent_request.headers | ||
| assert ( | ||
| sent_request.headers[HEADER_USER_AGENT] | ||
| == f"UiPath.Python.Sdk/UiPath.Python.Sdk.Activities.ConversationsService.retrieve_latest_exchange_message_async/{version}" | ||
| ) | ||
|
|
||
| @pytest.mark.anyio | ||
| async def test_retrieve_latest_exchange_message_with_content_parts( | ||
| self, | ||
| httpx_mock: HTTPXMock, | ||
| service: ConversationsService, | ||
| base_url: str, | ||
| org: str, | ||
| tenant: str, | ||
| ) -> None: | ||
| """Test retrieving a message with content parts.""" | ||
| conversation_id = "123" | ||
| exchange_id = "202cf2d1-926e-422d-8cf2-4f5735fa91fa" | ||
| message_id = "08de239e-90da-4d17-b986-b7785268d8d7" | ||
|
|
||
| httpx_mock.add_response( | ||
| url=f"{base_url}{org}{tenant}/autopilotforeveryone_/api/v1/conversation/{conversation_id}/exchange/{exchange_id}/message/{message_id}", | ||
| status_code=200, | ||
| json={ | ||
| "messageId": message_id, | ||
| "role": "user", | ||
| "contentParts": [ | ||
| { | ||
| "contentPartId": "cp-1", | ||
| "mimeType": "text/plain", | ||
| "data": {"inline": "Hello, world!"}, | ||
| } | ||
| ], | ||
| "createdAt": "2024-01-01T00:00:00Z", | ||
| "updatedAt": "2024-01-01T00:00:00Z", | ||
| }, | ||
| ) | ||
|
|
||
| result = await service.retrieve_latest_exchange_message_async( | ||
| conversation_id=conversation_id, | ||
| exchange_id=exchange_id, | ||
| message_id=message_id, | ||
| ) | ||
|
|
||
| assert isinstance(result, UiPathConversationMessage) | ||
| assert result.message_id == message_id | ||
| assert result.role == "user" | ||
| assert result.content_parts is not None | ||
| assert len(result.content_parts) == 1 | ||
| assert result.content_parts[0].content_part_id == "cp-1" | ||
| assert result.content_parts[0].mime_type == "text/plain" | ||
|
|
||
| @pytest.mark.anyio | ||
| async def test_retrieve_latest_exchange_message_with_tool_calls( | ||
| self, | ||
| httpx_mock: HTTPXMock, | ||
| service: ConversationsService, | ||
| base_url: str, | ||
| org: str, | ||
| tenant: str, | ||
| ) -> None: | ||
| """Test retrieving a message with tool calls.""" | ||
| conversation_id = "123" | ||
| exchange_id = "202cf2d1-926e-422d-8cf2-4f5735fa91fa" | ||
| message_id = "08de239e-90da-4d17-b986-b7785268d8d7" | ||
|
|
||
| httpx_mock.add_response( | ||
| url=f"{base_url}{org}{tenant}/autopilotforeveryone_/api/v1/conversation/{conversation_id}/exchange/{exchange_id}/message/{message_id}", | ||
| status_code=200, | ||
| json={ | ||
| "messageId": message_id, | ||
| "role": "assistant", | ||
| "contentParts": [], | ||
| "toolCalls": [ | ||
| { | ||
| "toolCallId": "tc-1", | ||
| "name": "get_weather", | ||
| "arguments": {"inline": '{"city": "San Francisco"}'}, | ||
| } | ||
| ], | ||
| "createdAt": "2024-01-01T00:00:00Z", | ||
| "updatedAt": "2024-01-01T00:00:00Z", | ||
| }, | ||
| ) | ||
|
|
||
| result = await service.retrieve_latest_exchange_message_async( | ||
| conversation_id=conversation_id, | ||
| exchange_id=exchange_id, | ||
| message_id=message_id, | ||
| ) | ||
|
|
||
| assert isinstance(result, UiPathConversationMessage) | ||
| assert result.message_id == message_id | ||
| assert result.role == "assistant" | ||
| assert result.tool_calls is not None | ||
| assert len(result.tool_calls) == 1 | ||
| assert result.tool_calls[0].tool_call_id == "tc-1" | ||
| assert result.tool_calls[0].name == "get_weather" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.