项目文件夹

文件
wehub-resource-sync e768098d0e
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
Flake8 Lint / flake8 (push) Has been cancelled
Spell check CI / Spell_Check (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:39:52 +08:00

63 行
2.5 KiB
Markdown

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
# Generate Python docstring
This example can help you automatically generate Python code's docstring and return the modified code.
Tools used in this flow
- `load_code` tool, it can load code from a file path.
- Load content from a local file.
- Loading content from a remote URL, currently loading HTML content, not just code.
- `divide_code` tool, it can divide code into code blocks.
- To avoid files that are too long and exceed the token limit, it is necessary to split the file.
- Avoid using the same function (such as __init__(self)) to generate docstrings in the same one file, which may cause confusion when adding docstrings to the corresponding functions in the future.
- `generate_docstring` tool, it can generate docstring for a code block, and merge docstring into origin code.
## What you will learn
In this flow, you will learn
- How to compose an auto generate docstring flow.
- How to use different LLM APIs to request LLM, including synchronous/asynchronous APIs, chat/completion APIs.
- How to use asynchronous multiple coroutine approach to request LLM API.
- How to construct a prompt.
## Prerequisites
### Install promptflow sdk and other dependencies:
```bash
pip install -r requirements.txt
```
### Create connection for LLM to use
```bash
# Override keys with --set to avoid yaml file changes
pf connection create --file ../../../connections/azure_openai.yml --set api_key=<your_api_key> api_base=<your_api_base>
```
Note:
The [azure_openai.yml](../../../connections/azure_openai.yml) file is located in connections folder.
We are using connection named `open_ai_connection`in [flow.dag.yaml](flow.dag.yaml).
## Execute with Promptflow
### Execute with SDK
`python main.py --source <your_file_path>`
**Note**: the file path should be a python file path, default is `./azure_open_ai.py`.
A webpage will be generated, displaying diff:
![result](result.png)
### Execute with CLI
```bash
# run flow with default file path in flow.dag.yaml
pf flow test --flow .
# run flow with file path
pf flow test --flow . --inputs source="./azure_open_ai.py"
```
```bash
# run flow with batch data
pf run create --flow . --data ./data.jsonl --name auto_generate_docstring --column-mapping source='${data.source}'
```
Output the code after add the docstring.
You can also skip providing `column-mapping` if provided data has same column name as the flow.
Reference [here](https://aka.ms/pf/column-mapping) for default behavior when `column-mapping` not provided in CLI.