-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Prompty] Support model config in prompty (#2728)
# Description - prompty with azure openai ``` --- name: Basic Prompt description: A basic prompt that uses the GPT-3 chat API to answer questions model: api: chat configuration: type: azure_openai azure_deployment: gpt-35-turbo azure_endpoint: ${env:AZURE_ENDPOINT} api_key: ${env:AZURE_API_KEY} parameters: max_tokens: 128 temperature: 0.2 inputs: firstName: type: string default: John lastName: type: string default: Doh question: type: string --- system: You are an AI assistant who helps people find information. Use their name to address them in your responses. user: {{question}} ``` - prompty with connection ``` --- name: Basic Prompt description: A basic prompt that uses the GPT-3 chat API to answer questions model: api: chat connection: azure_open_ai_connection configuration: type: azure_openai azure_deployment: gpt-35-turbo parameters: max_tokens: 128 temperature: 0.2 inputs: firstName: type: string default: John lastName: type: string default: Doh question: type: string --- system: You are an AI assistant who helps people find information. Use their name to address them in your responses. user: {{question}} ``` - prompty with openai ``` --- name: Basic Prompt description: A basic prompt that uses the GPT-3 chat API to answer questions model: api: chat connection: azure_open_ai_connection configuration: type: openai model: gpt-35-turbo api_key: ${env:API_KEY} base_url: ${env:BASE_URL} parameters: max_tokens: 128 temperature: 0.2 inputs: firstName: type: string default: John lastName: type: string default: Doh question: type: string --- system: You are an AI assistant who helps people find information. Use their name to address them in your responses. user: {{question}} ``` Please add an informative description that covers that changes made by the pull request and link all relevant issues. # All Promptflow Contribution checklist: - [ ] **The pull request does not introduce [breaking changes].** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [ ] **I have read the [contribution guidelines](../CONTRIBUTING.md).** - [ ] **Create an issue and link to the pull request to get dedicated review from promptflow team. Learn more: [suggested workflow](../CONTRIBUTING.md#suggested-workflow).** ## General Guidelines and Best Practices - [ ] Title of the pull request is clear and informative. - [ ] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md). ### Testing Guidelines - [ ] Pull request includes test coverage for the included changes.
- Loading branch information
1 parent
ac82e2e
commit 15960bf
Showing
10 changed files
with
300 additions
and
102 deletions.
There are no files selected for viewing
This file contains 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
79 changes: 79 additions & 0 deletions
79
src/promptflow-core/promptflow/core/_model_configuration.py
This file contains 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,79 @@ | ||
from dataclasses import dataclass | ||
from typing import Union | ||
|
||
from promptflow._constants import ConnectionType | ||
from promptflow.core._errors import InvalidConnectionError | ||
|
||
|
||
class ModelConfiguration: | ||
pass | ||
|
||
|
||
@dataclass | ||
class AzureOpenAIModelConfiguration(ModelConfiguration): | ||
azure_deployment: str | ||
azure_endpoint: str = None | ||
api_version: str = None | ||
api_key: str = None | ||
organization: str = None | ||
# connection and model configs are exclusive. | ||
connection: str = None | ||
|
||
def __post_init__(self): | ||
self._type = ConnectionType.AZURE_OPEN_AI | ||
if any([self.azure_endpoint, self.api_key, self.api_version, self.organization]) and self.connection: | ||
raise InvalidConnectionError("Cannot configure model config and connection at the same time.") | ||
|
||
|
||
@dataclass | ||
class OpenAIModelConfiguration(ModelConfiguration): | ||
model: str | ||
base_url: str = None | ||
api_key: str = None | ||
organization: str = None | ||
# connection and model configs are exclusive. | ||
connection: str = None | ||
|
||
def __post_init__(self): | ||
self._type = ConnectionType.OPEN_AI | ||
if any([self.base_url, self.api_key, self.api_version, self.organization]) and self.connection: | ||
raise InvalidConnectionError("Cannot configure model config and connection at the same time.") | ||
|
||
|
||
@dataclass | ||
class PromptyModelConfiguration: | ||
""" | ||
A dataclass that represents a model config of prompty. | ||
:param api: Type of the LLM request, default value is chat. | ||
:type api: str | ||
:param configuration: Prompty model connection configuration | ||
:type configuration: Union[dict, AzureOpenAIModelConfiguration, OpenAIModelConfiguration] | ||
:param parameters: Params of the LLM request. | ||
:type parameters: dict | ||
:param response: Return the complete response or the first choice, default value is first. | ||
:type response: str | ||
""" | ||
|
||
configuration: Union[dict, AzureOpenAIModelConfiguration, OpenAIModelConfiguration] | ||
parameters: dict | ||
api: str = "chat" | ||
response: str = "first" | ||
|
||
def __post_init__(self): | ||
if isinstance(self.configuration, dict): | ||
# Load connection from model configuration | ||
model_config = { | ||
k: v | ||
for k, v in self.configuration.items() | ||
if k not in ["type", "connection", "model", "azure_deployment"] | ||
} | ||
if self.configuration.get("connection", None) and any([v for v in model_config.values()]): | ||
raise InvalidConnectionError( | ||
"Cannot configure model config and connection in configuration at the same time." | ||
) | ||
self._model = self.configuration.get("azure_deployment", None) or self.configuration.get("model", None) | ||
elif isinstance(self.configuration, OpenAIModelConfiguration): | ||
self._model = self.configuration.model | ||
elif isinstance(self.configuration, AzureOpenAIModelConfiguration): | ||
self._model = self.configuration.azure_deployment |
Oops, something went wrong.