Files
wehub-resource-sync e768098d0e
Flake8 Lint / flake8 (push) Waiting to run
Spell check CI / Spell_Check (push) Waiting to run
tools_continuous_delivery / Private PyPI non-main branch release (push) Has been skipped
tools_continuous_delivery / Private PyPI main branch release (push) Failing after 2m42s
Publish Promptflow Doc / Build (push) Has been cancelled
Publish Promptflow Doc / Deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:39:52 +08:00
..

Chat with external functions

This flow covers how to use the LLM chat API in combination with external functions to extend the capabilities of GPT models.

tools is an optional parameter in the Chat Completion API which can be used to provide function specifications. The purpose of this is to enable models to generate function arguments which adhere to the provided specifications. Note that the API will not actually execute any function calls. It is up to developers to execute function calls using model outputs.

Within the tools parameter, if the functions parameter is provided then by default the model will decide when it is appropriate to use one of the functions. The API can be forced to use a specific function by setting the tool_choice parameter to {"type": "function", "function": {"name": "my_function"}}. The API can also be forced to not use any function by setting the tool_choice parameter to "none". If a function is used, the output will contain "finish_reason": "tool_calls" in the response, as well as a tool_calls object that has the name of the function and the generated function arguments.

You can refer to openai sample for more details.

What you will learn

In this flow, you will learn how to use external functions with LLM chat models and how to compose function role message in prompt template.

Prerequisites

Install dependencies:

pip install -r requirements.txt

Getting started

1 prepare api_key env

  • Prepare your Azure OpenAI resource follow this instruction and get your api_key if you don't have one.

  • Setup environment variables

Ensure you have put your azure OpenAI endpoint key in .env file. You can create one refer to this example file.

2 understand how chat model choose function

# see how llm will chose functions
pf flow test --flow chat_with_tools.prompty --env --inputs question="What is the weather like in Boston?"
pf flow test --flow chat_with_tools.prompty --env --inputs sample.json

3 Start chatting with flow

# run e2e chat flow with question
pf flow test --flow flow:chat --env --inputs question="What is the weather like in Boston?"

# run chat flow with question & chat_history from sample.json
pf flow test --flow flow:chat --env --inputs sample.json
# chat using ui
pf flow test --flow flow:chat --env --ui

References