Skip to content
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

New PF Tool: Open_Source_llm - Dev, Test, & Documentation #531

Merged
merged 36 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
37c20bc
[Open_Source_llm] Initial Setup
gjwoods Sep 15, 2023
64303c3
Add docs
gjwoods Sep 18, 2023
4ebe71b
Merge branch 'main' of https://github.com/gjwoods/promptflow into use…
gjwoods Sep 21, 2023
1d0eab5
Add tests and resolve
gjwoods Sep 25, 2023
6e9e03d
fix code quality
gjwoods Sep 25, 2023
b65b968
doc miss & whitespace
gjwoods Sep 25, 2023
944e19e
Merge branch 'microsoft:main' into users/gewoods/open_source_llm/initial
gjwoods Sep 25, 2023
893d7e8
Merge branch 'users/gewoods/open_source_llm/initial' of https://githu…
gjwoods Sep 25, 2023
c4c4344
Merge branch 'main' into users/gewoods/open_source_llm/initial
gjwoods Sep 26, 2023
96a0477
Ignore tests & clean set
gjwoods Sep 26, 2023
84f7a75
Merge branch 'users/gewoods/open_source_llm/initial' of https://githu…
gjwoods Sep 26, 2023
27d8089
fix test connections
gjwoods Sep 26, 2023
156b674
flake
gjwoods Sep 26, 2023
7678f6e
Merge pull request #1 from gjwoods/users/gewoods/open_source_llm/initial
gjwoods Sep 26, 2023
48c17df
Merge branch 'main' into users/gewoods/open_source_llm/initial
gjwoods Sep 26, 2023
165dc9e
Automated Pull Master Merge Tue 09/26/202311:46:35.80
gjwoods Sep 26, 2023
e9abc85
restore deployment_name values
gjwoods Sep 26, 2023
70b6ae5
Merge branch 'users/gewoods/open_source_llm/initial' of https://githu…
gjwoods Sep 26, 2023
f0a38ec
test name
gjwoods Sep 26, 2023
7331734
Merge pull request #5 from gjwoods/users/gewoods/open_source_llm/initial
gjwoods Sep 26, 2023
62ff1d9
Merge branch 'microsoft:main' into main
gjwoods Sep 27, 2023
c11fd1b
update for review comments
gjwoods Sep 27, 2023
b5920bc
code qual
gjwoods Sep 27, 2023
99349f6
Merge branch 'main' into users/gewoods/open_source_llm/initial
gjwoods Sep 27, 2023
25ef9cb
flake sucks
gjwoods Sep 27, 2023
f9eefca
Merge branch 'users/gewoods/open_source_llm/initial' of https://githu…
gjwoods Sep 27, 2023
4140569
Merge branch 'main' into users/gewoods/open_source_llm/initial
chjinche Sep 27, 2023
d468c81
Merge branch 'main' into users/gewoods/open_source_llm/initial
chjinche Sep 27, 2023
c6816d8
Review comments
gjwoods Sep 28, 2023
640b41a
Update documentation
gjwoods Sep 28, 2023
fa18abd
spelling issue
gjwoods Sep 28, 2023
5b815fd
Merge branch 'microsoft:main' into main
gjwoods Sep 28, 2023
f80b564
Merge branch 'main' of https://github.com/gjwoods/promptflow into use…
gjwoods Sep 28, 2023
6d0f143
Merge branch 'main' into users/gewoods/open_source_llm/initial
chjinche Sep 28, 2023
5e7e114
Merge branch 'main' into users/gewoods/open_source_llm/initial
chjinche Sep 28, 2023
a108fd9
Merge branch 'main' into users/gewoods/open_source_llm/initial
gjwoods Sep 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@
"pysqlite",
"AADSTS700082",
"levelno",
"LANCZOS"
"LANCZOS",
"Mobius"
],
"allowCompoundWords": true
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tools-reference/serp-api-tool
tools-reference/faiss_index_lookup_tool
tools-reference/vector_db_lookup_tool
tools-reference/embedding_tool
tools-reference/open_source_llm_tool
```
gjwoods marked this conversation as resolved.
Show resolved Hide resolved

```{toctree}
Expand Down
65 changes: 65 additions & 0 deletions docs/reference/tools-reference/open_source_llm_tool.md
gjwoods marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Open Source LLM

## Introduction

The Prompt flow Open Source LLM tool enables you to utilize a variety of Open Source and Foundational Models, such as [Falcon](https://aka.ms/AAlc25c) or [Llama 2](https://aka.ms/AAlc258) for natural language processing, in PromptFlow.

Here's how it looks in action on the Visual Studio Code Prompt flow extension. In this example, the tool is being used to call a LlaMa-2 chat endpoint and asking "What is CI?".

![Screenshot of the Open Source Llm On vsCode PromptFlow extension](../../media/reference/tools-reference/open_source_llm_on_vscode_promptflow.png)

This Prompt flow supports two different LLM API types:

- **Chat**: Shown in the example above. The chat API type facilitates interactive conversations with text-based inputs and responses.
- **Completion**: The Completion API type is used to generate single response text completions based on provided prompt input.

## Quick Overview: How do I use Open Source LLM Tool?

1. Choose a Model from the AzureML Model Catalog and deploy.
2. Setup and select the connections to the model deployment.
3. Configure the tool with the model settings.
4. Prepare the Prompt with [guidance](./prompt-tool.md#how-to-write-prompt).
5. Run the flow.

## Prerequisites: Model Deployment

1. Pick the model which matched your scenario from the [Azure Machine Learning model catalog](https://ml.azure.com/model/catalog).
2. Use the "Deploy" button to deploy the model to a AzureML Online Inference endpoint.

More detailed instructions can be found here [Deploying foundation models to endpoints for inferencing.](https://learn.microsoft.com/en-us/azure/machine-learning/how-to-use-foundation-models?view=azureml-api-2#deploying-foundation-models-to-endpoints-for-inferencing)

## Prerequisites: Prompt flow Connections

In order for Prompt flow to use your deployed model, you will need to setup a Connection. Explicitly, the Open Source LLM tool uses the CustomConnection.

1. Instructions to create a Custom Connection [can be found here.](https://microsoft.github.io/promptflow/how-to-guides/manage-connections.html#create-a-connection)

The keys to set are:

1. **endpoint_url**
- This value can be found at the previously created Inferencing endpoint.
2. **endpoint_api_key**
- Ensure to set this as a secret value.
- This value can be found at the previously created Inferencing endpoint.
3. **model_family**
- Supported values: LLAMA, DOLLY, GPT2, or FALCON
- This value is dependent on the type of deployment you are targetting.

## Running the Tool: Inputs

The Open Source LLM tool has a number of parameters, some of which are required. Please see the below table for details, you can match these to the screen shot above for visual clarity.

| Name | Type | Description | Required |
|------|------|-------------|----------|
| api | string | This is the API mode and will depend on the model used and the scenario selected. *Supported values: (Completion \| Chat)* | Yes |
| connection | CustomConnection | This is the name of the connection which points to the Online Inferencing endpoint. | Yes |
| model_kwargs | dictionary | This input is used to provide configuration specific to the model used. For example, the Llama-02 model may use {\"temperature\":0.4}. *Default: {}* | No |
| deployment_name | string | The name of the deployment to target on the Online Inferencing endpoint. If no value is passed, the Inferencing load balancer traffic settings will be used. | No |
| prompt | string | The text prompt that the language model will use to generate it's response. | Yes |

## Outputs

| API | Return Type | Description |
|------------|-------------|------------------------------------------|
| Completion | string | The text of one predicted completion |
| Chat | string | The text of one response int the conversation |
12 changes: 12 additions & 0 deletions src/promptflow-tools/connections.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
"key1"
]
},
"gpt2_connection": {
"type": "CustomConnection",
"value": {
"endpoint_url": "custom-endpoint-url",
"model_family": "GPT2",
"endpoint_api_key": "custom-endpoint-api-key"
},
"module": "promptflow.connections",
"secret_keys": [
"endpoint_api_key"
gjwoods marked this conversation as resolved.
Show resolved Hide resolved
]
},
"open_ai_connection": {
"type": "OpenAIConnection",
"value": {
Expand Down
9 changes: 6 additions & 3 deletions src/promptflow-tools/promptflow/tools/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
from promptflow.tools.exception import ChatAPIInvalidRole, WrappedOpenAIError, LLMError, JinjaTemplateError, \
ExceedMaxRetryTimes, ChatAPIInvalidFunctions, FunctionCallNotSupportedInStreamMode, \
ChatAPIFunctionRoleInvalidFormat
from typing import Set


def validate_role(role):
valid_roles = {"system", "user", "assistant", "function"}
def validate_role(role: str, valid_roles: Set[str] = None):
if not valid_roles:
valid_roles = {"system", "user", "assistant", "function"}

if role not in valid_roles:
valid_roles_str = ','.join([f'\'{role}:\\n\''for role in valid_roles])
valid_roles_str = ','.join(sorted([f'\'{role}:\\n\''for role in valid_roles]))
error_message = (
f"The Chat API requires a specific format for prompt definition, and the prompt should include separate "
f"lines as role delimiters: {valid_roles_str}. Current parsed role '{role}'"
Expand Down
21 changes: 21 additions & 0 deletions src/promptflow-tools/promptflow/tools/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,24 @@ class SerpAPIUserError(UserErrorException):

def __init__(self, **kwargs):
super().__init__(**kwargs, target=ErrorTarget.TOOL)


class OpenSourceLLMOnlineEndpointError(UserErrorException):
"""Base exception raised when the call to an online endpoint failed."""

def __init__(self, **kwargs):
super().__init__(**kwargs, target=ErrorTarget.TOOL)


class OpenSourceLLMUserError(UserErrorException):
"""Base exception raised when the call to Open Source LLM failed with a user error."""

def __init__(self, **kwargs):
super().__init__(**kwargs, target=ErrorTarget.TOOL)


class OpenSourceLLMKeyValidationError(ToolValidationError):
"""Base exception raised when failed to validate functions when call chat api."""

def __init__(self, **kwargs):
super().__init__(**kwargs)
Loading