-
Notifications
You must be signed in to change notification settings - Fork 901
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change function call type to object (#815)
# Description **Issue:** The function_call can be of type 'str' or 'dict'. However, when function_call is set to 'str' type in the char API, it becomes unsafe to pass the parameter. Exceptions could occur when trying to convert a 'dict' input to a 'str' input in the process of passing this parameter. **Solution:** The type of function_call was changed from 'str' to 'object' to enhance safety. The 'object' type is more versatile and can handle a wider range of data types, thus reducing the risk of exceptions. **Tests:** Additional unit and end-to-end tests were added to ensure the functionality and safety of the changes. All tests were successfully passed, indicating that the solution is effective and doesn't introduce new issues. # All Promptflow Contribution checklist: - [X] **The pull request does not introduce [breaking changes].** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [X] **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 - [X] Title of the pull request is clear and informative. - [X] 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 - [x] Pull request includes test coverage for the included changes. --------- Co-authored-by: cs_lucky <[email protected]>
- Loading branch information
1 parent
6df5507
commit 4f439c8
Showing
9 changed files
with
195 additions
and
24 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
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
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
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
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
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
104 changes: 104 additions & 0 deletions
104
src/promptflow/tests/test_configs/flows/sample_flow_with_functions/flow.dag.yaml
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,104 @@ | ||
id: use_functions_with_chat_models | ||
name: Use Functions with Chat Models | ||
inputs: | ||
chat_history: | ||
type: list | ||
default: | ||
- inputs: | ||
question: What is the weather like in Boston? | ||
outputs: | ||
answer: '{"forecast":["sunny","windy"],"location":"Boston","temperature":"72","unit":"fahrenheit"}' | ||
llm_output: | ||
content: null | ||
function_call: | ||
name: get_current_weather | ||
arguments: |- | ||
{ | ||
"location": "Boston" | ||
} | ||
role: assistant | ||
is_chat_input: false | ||
question: | ||
type: string | ||
default: How about London next week? | ||
is_chat_input: true | ||
outputs: | ||
answer: | ||
type: string | ||
reference: ${run_function.output} | ||
is_chat_output: true | ||
llm_output: | ||
type: object | ||
reference: ${use_functions_with_chat_models.output} | ||
nodes: | ||
- name: run_function | ||
type: python | ||
source: | ||
type: code | ||
path: run_function.py | ||
inputs: | ||
response_message: ${use_functions_with_chat_models.output} | ||
use_variants: false | ||
- name: use_functions_with_chat_models | ||
type: llm | ||
source: | ||
type: code | ||
path: use_functions_with_chat_models.jinja2 | ||
inputs: | ||
deployment_name: gpt-35-turbo | ||
temperature: 0.7 | ||
top_p: 1 | ||
stop: "" | ||
max_tokens: 256 | ||
presence_penalty: 0 | ||
frequency_penalty: 0 | ||
logit_bias: "" | ||
functions: | ||
- name: get_current_weather | ||
description: Get the current weather in a given location | ||
parameters: | ||
type: object | ||
properties: | ||
location: | ||
type: string | ||
description: The city and state, e.g. San Francisco, CA | ||
unit: | ||
type: string | ||
enum: | ||
- celsius | ||
- fahrenheit | ||
required: | ||
- location | ||
- name: get_n_day_weather_forecast | ||
description: Get an N-day weather forecast | ||
parameters: | ||
type: object | ||
properties: | ||
location: | ||
type: string | ||
description: The city and state, e.g. San Francisco, CA | ||
format: | ||
type: string | ||
enum: | ||
- celsius | ||
- fahrenheit | ||
description: The temperature unit to use. Infer this from the users location. | ||
num_days: | ||
type: integer | ||
description: The number of days to forecast | ||
required: | ||
- location | ||
- format | ||
- num_days | ||
function_call: | ||
name: get_current_weather | ||
chat_history: ${inputs.chat_history} | ||
question: ${inputs.question} | ||
provider: AzureOpenAI | ||
connection: azure_open_ai_connection | ||
api: chat | ||
module: promptflow.tools.aoai | ||
use_variants: false | ||
node_variants: {} | ||
environment: | ||
python_requirements_txt: requirements.txt |
41 changes: 41 additions & 0 deletions
41
src/promptflow/tests/test_configs/flows/sample_flow_with_functions/run_function.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,41 @@ | ||
from promptflow import tool | ||
import json | ||
|
||
|
||
def get_current_weather(location, unit="fahrenheit"): | ||
"""Get the current weather in a given location""" | ||
weather_info = { | ||
"location": location, | ||
"temperature": "72", | ||
"unit": unit, | ||
"forecast": ["sunny", "windy"], | ||
} | ||
return weather_info | ||
|
||
|
||
def get_n_day_weather_forecast(location, format, num_days): | ||
"""Get next num_days weather in a given location""" | ||
weather_info = { | ||
"location": location, | ||
"temperature": "60", | ||
"format": format, | ||
"forecast": ["rainy"], | ||
"num_days": num_days, | ||
} | ||
return weather_info | ||
|
||
|
||
@tool | ||
def run_function(response_message: dict) -> str: | ||
if "function_call" in response_message: | ||
function_name = response_message["function_call"]["name"] | ||
function_args = json.loads(response_message["function_call"]["arguments"]) | ||
print(function_args) | ||
result = globals()[function_name](**function_args) | ||
else: | ||
print("No function call") | ||
if isinstance(response_message, dict): | ||
result = response_message["content"] | ||
else: | ||
result = response_message | ||
return result |
27 changes: 27 additions & 0 deletions
27
...tests/test_configs/flows/sample_flow_with_functions/use_functions_with_chat_models.jinja2
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,27 @@ | ||
system: | ||
Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. | ||
|
||
{% for item in chat_history %} | ||
user: | ||
{{item.inputs.question}} | ||
|
||
{% if 'function_call' in item.outputs.llm_output %} | ||
assistant: | ||
Function generation requested, function = {{item.outputs.llm_output.function_call.name}}, args = {{item.outputs.llm_output.function_call.arguments}} | ||
|
||
function: | ||
name: | ||
{{item.outputs.llm_output.function_call.name}} | ||
content: | ||
{{item.outputs.answer}} | ||
|
||
{% else %} | ||
assistant: | ||
{{item.outputs.llm_output}}}} | ||
|
||
{% endif %}} | ||
|
||
{% endfor %} | ||
|
||
user: | ||
{{question}} |