LLM#
介绍#
Prompt flow LLM 工具使您能够利用广泛使用的大型语言模型,如 OpenAI、Azure OpenAI (AOAI) 和 Azure AI Studio 模型目录 中的模型进行自然语言处理。
[!注意] LLM工具的旧版本现在已被弃用。请升级到最新的promptflow-tools包以使用新的llm工具。
Prompt flow 提供了几种不同的 LLM API:
Completion: OpenAI的完成模型根据提供的提示生成文本。
Chat: OpenAI的聊天模型促进了基于文本输入和响应的交互式对话。
先决条件#
在Azure AI Studio模型目录中创建OpenAI资源、Azure OpenAI资源或使用LLM模型(例如:llama2、mistral、cohere等)的MaaS部署:
OpenAI
注册账户 OpenAI 网站
登录并 查找个人 API 密钥
Azure OpenAI (AOAI)
使用instruction创建Azure OpenAI资源
MaaS 部署
在 Azure AI Studio 模型目录中为模型创建 MaaS 部署,请参阅说明
您可以创建无服务器连接以使用此 MaaS 部署。
连接#
设置连接以在提示流中配置资源。
类型 |
名称 |
API 密钥 |
API 基础 |
API 类型 |
API 版本 |
|---|---|---|---|---|---|
OpenAI |
必填 |
必填 |
- |
- |
- |
AzureOpenAI |
必填 |
必填 |
必填 |
必填 |
必填 |
Serverless |
必需 |
必需 |
必需 |
- |
- |
输入#
文本补全#
名称 |
类型 |
描述 |
是否必需 |
|---|---|---|---|
prompt |
string |
语言模型将完成的文本提示 |
是 |
model, deployment_name |
string |
使用的语言模型 |
是 |
max_tokens |
integer |
在完成中生成的最大令牌数。默认值为16。 |
No |
温度 |
浮点数 |
生成文本的随机性。默认值为1。 |
否 |
stop |
list |
生成文本的停止序列。默认为null。 |
No |
suffix |
string |
附加在完成内容末尾的文本 |
No |
top_p |
float |
使用生成令牌中最高选择项的概率。默认值为1。 |
No |
logprobs |
integer |
生成的日志概率数量。默认为null。 |
否 |
echo |
boolean |
指示是否在响应中回显提示的值。默认为 false。 |
No |
presence_penalty |
float |
控制模型在重复短语方面的行为的数值。默认值为0。 |
否 |
frequency_penalty |
float |
控制模型生成罕见短语行为的数值。默认值为0。 |
否 |
best_of |
integer |
生成的最佳完成数量。默认值为1。 |
No |
logit_bias |
dictionary |
语言模型的logit偏差。默认为空字典。 |
否 |
聊天#
名称 |
类型 |
描述 |
是否必需 |
|---|---|---|---|
prompt |
string |
语言模型将回应的文本提示 |
是 |
model, deployment_name |
string |
使用的语言模型 |
是 |
max_tokens |
integer |
响应中生成的最大令牌数。默认值为无限。 |
否 |
温度 |
浮点数 |
生成文本的随机性。默认值为1。 |
否 |
stop |
list |
生成文本的停止序列。默认为null。 |
No |
top_p |
float |
使用生成令牌中最高选择项的概率。默认值为1。 |
No |
presence_penalty |
float |
控制模型在重复短语方面的行为的数值。默认值为0。 |
否 |
frequency_penalty |
float |
控制模型生成罕见短语行为的数值。默认值为0。 |
否 |
logit_bias |
dictionary |
语言模型的logit偏差。默认为空字典。 |
No |
tool_choice |
object |
控制模型调用哪个工具的值。默认值为null。 |
No |
工具 |
列表 |
模型可能生成JSON输入的工具列表。默认为null。 |
否 |
response_format |
object |
一个指定模型必须输出的格式的对象。默认为null。 |
否 |
输出#
返回类型 |
描述 |
|---|---|
string |
一个预测完成或对话响应的文本 |
如何使用LLM工具?#
设置并选择连接到OpenAI资源
配置LLM模型API及其参数
使用guidance准备提示。
如何编写聊天提示?#
要掌握创建聊天提示的基础知识,请从本节开始,以初步了解jinja。
我们提供了一种方法来区分聊天提示中的不同角色,例如“系统”、“用户”、“助手”和“工具”。“系统”、“用户”、“助手”角色可以具有“名称”和“内容”属性。然而,“工具”角色应具有“tool_call_id”和“内容”属性。有关工具聊天提示的示例,请参阅示例3。
示例 1#
# system:
You are a helpful assistant.
{% for item in chat_history %}
# user:
{{item.inputs.question}}
# assistant:
{{item.outputs.answer}}
{% endfor %}
# user:
{{question}}
在LLM工具中,提示在发送到openai聊天API之前被转换为匹配openai消息结构。
[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "<question-of-chat-history-round-1>"
},
{
"role": "assistant",
"content": "<answer-of-chat-history-round-1>"
},
...
{
"role": "user",
"content": "<question>"
}
]
示例 2#
# system:
{# For role naming customization, the following syntax is used #}
## name:
Alice
## content:
You are a bot can tell good jokes.
在LLM工具中,提示在发送到openai聊天API之前被转换为匹配openai消息结构。
[
{
"role": "system",
"name": "Alice",
"content": "You are a bot can tell good jokes."
}
]
示例 3#
此示例说明了如何编写工具聊天提示。
# system:
You are a helpful assistant.
# user:
What is the current weather like in Boston?
# assistant:
{# The assistant message with 'tool_calls' must be followed by messages with role 'tool'. #}
## tool_calls:
{{llm_output.tool_calls}}
# tool:
{#
Messages with role 'tool' must be a response to a preceding message with 'tool_calls'.
Additionally, 'tool_call_id's should match ids of assistant message 'tool_calls'.
#}
## tool_call_id:
{{llm_output.tool_calls[0].id}}
## content:
{{tool-answer-of-last-question}}
# user:
{{question}}
在LLM工具中,提示在发送到openai聊天API之前被转换为匹配openai消息结构。
[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What is the current weather like in Boston?"
},
{
"role": "assistant",
"content": null,
"function_call": null,
"tool_calls": [
{
"id": "<tool-call-id-of-last-question>",
"type": "function",
"function": "<function-to-call-of-last-question>"
}
]
},
{
"role": "tool",
"tool_call_id": "<tool-call-id-of-last-question>",
"content": "<tool-answer-of-last-question>"
}
...
{
"role": "user",
"content": "<question>"
}
]