-
Notifications
You must be signed in to change notification settings - Fork 560
feat: Add a new Orchestrator "prompt_flow" #1026
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
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
c9a9aa5
Add structure for PromptFlowOrchestrator to orchestrator strategies
tectonia ba8503e
Clean up prompt flow orchestator structure
tectonia 41ab533
change auth mechanism and invoking the endpoint for prompt flow
tectonia e701f5d
Merge branch 'main' into martyna/prompt-flow-orchestrator
tectonia 726d162
bring poetry lock up to date
tectonia e6053b8
Add temporary file for request body in PromptFlowOrchestrator
tectonia cdd6971
fix vite hosting port
tectonia 6cf6311
Clean up comments and transform the output to correct format
tectonia a4b1c3c
Merge branch 'main' into martyna/prompt-flow-orchestrator
tectonia ccf4f09
fix: Update environment variable names for Azure ML workspace
tectonia 96340a5
chore: Add package-lock.json file
tectonia 4c02b80
feat: Add deployment name and transform chat history in PromptFlowOrc…
tectonia 42c2c39
Add tests for prompt flow helpers in llmhelper
tectonia 82716b3
chore: Clean up
tectonia a28ee6b
feat: Add tests for PromptFlowOrchestrator
tectonia 6e1ddda
chore: Update environment variables for PromptFlowOrchestrator
tectonia ea3c6ab
chore: update documentation
tectonia 22e2588
Refactor get_ml_client method in LLMHelper to use lazy loading
tectonia a6479b6
fix: clean up comments
tectonia cd3e908
feat: Add role assignment to webapp for AML workspace
tectonia d6ad257
feat: Set environment variables for admin webapp in create-prompt-flo…
tectonia 35f9063
feat: Assign AzureML Data Scientist role to webapp on endpoint as well
tectonia 2b92b67
fix: Add "prompt_flow" to available orchestration strategies in test_…
tectonia fb74bb7
Refactoring prompt_flow and tests based on PR comments
tectonia 8888510
Refactor error handling in PromptFlowOrchestrator
tectonia e302922
Refactored tests
tectonia 9e93852
chore: Refactor transform_chat_history method to include situation wh…
tectonia 2852980
Refactor transform_chat_history method to handle multiple user messag…
tectonia d31f8c0
Merge branch 'main' into martyna/prompt-flow-orchestrator
tectonia b0c2a4e
semantic kernel 1.1.0 is not compatible so reverting pyproject.toml t…
tectonia 2694d48
Merge branch 'main' into martyna/prompt-flow-orchestrator
tectonia 4f3ac11
Merge branch 'main' into martyna/prompt-flow-orchestrator
adamdougal d4189ae
Resolve poetry lock file conflicts
adamdougal 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
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,93 @@ | ||
import logging | ||
from typing import List | ||
import json | ||
import tempfile | ||
|
||
from .orchestrator_base import OrchestratorBase | ||
from ..common.answer import Answer | ||
from ..helpers.llm_helper import LLMHelper | ||
from ..helpers.env_helper import EnvHelper | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class PromptFlowOrchestrator(OrchestratorBase): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.llm_helper = LLMHelper() | ||
self.env_helper = EnvHelper() | ||
|
||
# Get the ML client, endpoint and deployment names | ||
self.ml_client = self.llm_helper.get_ml_client() | ||
self.enpoint_name = self.env_helper.PROMPT_FLOW_ENDPOINT_NAME | ||
self.deployment_name = self.env_helper.PROMPT_FLOW_DEPLOYMENT_NAME | ||
|
||
async def orchestrate( | ||
tectonia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self, user_message: str, chat_history: List[dict], **kwargs: dict | ||
) -> list[dict]: | ||
# Call Content Safety tool on question | ||
if self.config.prompts.enable_content_safety: | ||
if response := self.call_content_safety_input(user_message): | ||
return response | ||
|
||
transformed_chat_history = self.transform_chat_history(chat_history) | ||
|
||
file_name = self.transform_data_into_file( | ||
user_message, transformed_chat_history | ||
) | ||
|
||
# Call the Prompt Flow service | ||
try: | ||
response = self.ml_client.online_endpoints.invoke( | ||
endpoint_name=self.enpoint_name, | ||
request_file=file_name, | ||
deployment_name=self.deployment_name, | ||
) | ||
result = json.loads(response) | ||
logger.debug(result) | ||
except Exception as error: | ||
logger.error("The request failed: %s", error) | ||
raise RuntimeError(f"The request failed: {error}") from error | ||
|
||
# Transform response into answer for further processing | ||
answer = Answer(question=user_message, answer=result["chat_output"]) | ||
|
||
# Call Content Safety tool on answer | ||
if self.config.prompts.enable_content_safety: | ||
if response := self.call_content_safety_output(user_message, answer.answer): | ||
return response | ||
|
||
# Format the output for the UI | ||
messages = self.output_parser.parse( | ||
question=answer.question, | ||
answer=answer.answer, | ||
source_documents=answer.source_documents, | ||
) | ||
return messages | ||
|
||
def transform_chat_history(self, chat_history): | ||
transformed_chat_history = [] | ||
for i, message in enumerate(chat_history): | ||
if message["role"] == "user": | ||
user_message = message["content"] | ||
assistant_message = "" | ||
if ( | ||
i + 1 < len(chat_history) | ||
and chat_history[i + 1]["role"] == "assistant" | ||
): | ||
assistant_message = chat_history[i + 1]["content"] | ||
transformed_chat_history.append( | ||
{ | ||
"inputs": {"chat_input": user_message}, | ||
"outputs": {"chat_output": assistant_message}, | ||
} | ||
) | ||
return transformed_chat_history | ||
|
||
def transform_data_into_file(self, user_message, chat_history): | ||
# Transform data input into a file for the Prompt Flow service | ||
data = {"chat_input": user_message, "chat_history": chat_history} | ||
body = str.encode(json.dumps(data)) | ||
with tempfile.NamedTemporaryFile(delete=False) as file: | ||
file.write(body) | ||
return file.name |
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
Oops, something went wrong.
Oops, something went wrong.
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.