-
Notifications
You must be signed in to change notification settings - Fork 3k
Prompt support for Inference SDK #37917
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 10 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d7f8a46
Prompty support within Azure AI Inference SDK
YusakuNo1 1e25075
Fix unit test
YusakuNo1 ffeaab8
Address PR feedback with copyright, merge PromptConfig to PromptTemplate
YusakuNo1 44d2f2c
Add comment and set model_name as optional
YusakuNo1 2d1d132
Bug fixes
YusakuNo1 9f7b679
Updated parameter names from PM feedbacks
YusakuNo1 b4f2d5b
Merge branch 'main' into users/daviwu/prompty
YusakuNo1 b7657e5
Merge branch 'main' into users/daviwu/prompty
YusakuNo1 38eb258
Improve sample code and unit tests
YusakuNo1 aa28df4
Update readme and comments
YusakuNo1 9a1eb79
Rename files
YusakuNo1 1252b3a
Address PR comment
YusakuNo1 b3e8616
add Pydantic as dependency
YusakuNo1 c43f88e
Fix type errors
YusakuNo1 e9cab12
Fix spelling issues
YusakuNo1 24c3ced
Address PR comments and fix linter issues
YusakuNo1 19316b8
Fix type import for "Self"
YusakuNo1 ed718cb
Change to keyword-only constructor and fix linter issues
YusakuNo1 ebfa1f8
Rename function `from_message` to `from_str`; `render` to `create_mes…
YusakuNo1 25a0365
Change from `from_str` to `from_string`
YusakuNo1 6b8ad60
Merge branch 'main' into users/daviwu/prompty
YusakuNo1 a7a0bf2
Merge latest code from `microsoft/prompty` and resolve linter issues
YusakuNo1 4b43b46
Fix PR comment
YusakuNo1 633c84f
Fix PR comments
YusakuNo1 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
23 changes: 23 additions & 0 deletions
23
sdk/ai/azure-ai-inference/azure/ai/inference/prompts/__init__.py
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,23 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
from .core import InvokerFactory | ||
from .core import Prompty | ||
|
||
from .renderers import MustacheRenderer | ||
from .parsers import PromptyChatParser | ||
from .utils import load | ||
from ._patch import patch_sdk as _patch_sdk, PromptTemplate | ||
|
||
# Register the Mustache renderer and parser | ||
InvokerFactory().register_renderer("mustache", MustacheRenderer) | ||
InvokerFactory().register_parser("prompty.chat", PromptyChatParser) | ||
|
||
__all__ = [ | ||
"load", | ||
"Prompty", | ||
"PromptTemplate", | ||
] | ||
|
||
_patch_sdk() |
85 changes: 85 additions & 0 deletions
85
sdk/ai/azure-ai-inference/azure/ai/inference/prompts/_patch.py
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,85 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
"""Customize generated code here. | ||
|
||
Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize | ||
""" | ||
|
||
import azure.ai.inference.prompts as prompts | ||
from .core import Prompty | ||
from .utils import prepare | ||
from .mustache import render | ||
|
||
|
||
class PromptTemplate: | ||
"""The helper class which takes varient of inputs, e.g. Prompty format or string, and returns the parsed prompt in an array. | ||
YusakuNo1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
:param prompty: Prompty object which contains both model config and prompt template. | ||
:type prompty: Prompty | ||
:param prompt_template: The prompt template string. | ||
:type prompt_template: str | ||
:param api: The API type, e.g. "chat" or "completion". | ||
:type api: str | ||
:param model_name: The model name, e.g. "gpt-4o-mini". | ||
:type model_name: str | ||
""" | ||
|
||
@staticmethod | ||
YusakuNo1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def from_prompty(file_path: str): | ||
if not file_path: | ||
raise ValueError("Please provide file_path") | ||
prompty = prompts.load(file_path) | ||
return PromptTemplate(prompty=prompty) | ||
YusakuNo1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@staticmethod | ||
def from_message( | ||
prompt_template: str, | ||
api: str = "chat", | ||
model_name: str | None = None | ||
): | ||
return PromptTemplate(api=api, prompt_template=prompt_template, model_name=model_name, prompty=None) | ||
YusakuNo1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __init__( | ||
YusakuNo1 marked this conversation as resolved.
Show resolved
Hide resolved
YusakuNo1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self, | ||
prompty: Prompty | None = None, | ||
api: str | None = None, | ||
prompt_template: str | None = None, | ||
model_name: str | None = None, | ||
) -> None: | ||
self.prompty = prompty | ||
if self.prompty is not None: | ||
self.model_name = prompty.model.configuration["azure_deployment"] if "azure_deployment" in prompty.model.configuration else None | ||
self.parameters = prompty.model.parameters | ||
self._parameters = {} | ||
elif prompt_template is not None: | ||
self.model_name = model_name | ||
self.parameters = {} | ||
# _parameters is a dict to hold the internal configuration | ||
self._parameters = { | ||
"api": api if api is not None else "chat", | ||
"prompt_template": prompt_template | ||
} | ||
else: | ||
raise ValueError("Please invalid arguments for PromptConfig") | ||
YusakuNo1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def render(self, data: dict[str, any] | None = None, **kwargs): | ||
YusakuNo1 marked this conversation as resolved.
Show resolved
Hide resolved
YusakuNo1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if data is None: | ||
data = kwargs | ||
|
||
if self.prompty is not None: | ||
parsed = prepare(self.prompty, data) | ||
return parsed | ||
elif "prompt_template" in self._parameters: | ||
YusakuNo1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
system_prompt = render(self._parameters["prompt_template"], data) | ||
return [{"role": "system", "content": system_prompt}] | ||
|
||
|
||
def patch_sdk(): | ||
"""Do not remove from this file. | ||
|
||
`patch_sdk` is a last resort escape hatch that allows you to do customizations | ||
you can't accomplish using the techniques described in | ||
https://aka.ms/azsdk/python/dpcodegen/python/customize | ||
""" |
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.