PowerAutomate使用OpenAI的ChatGPT和Whisper APIS.md

摘要

最近OpenAI发布了ChatGPT模型和Whisper模型,gpt-3.5-turbo是与ChatGPT使用的相同的模型,并且与之前的GPT-3.5模型相比,便宜了10倍。Whisper是语言转文本模型。这两个模型OpenAI都开放了对应的API请求。

GPT-3.5-Turbo模型

GPT-3.5-Turbo接受一系列的消息作为输入,返回基于模型生成的消息作为输出。

示例请求

1
2
3
4
5
6
7
curl https://api.openai.com/v1/chat/completions \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'

模型API参数

  • model 模型,可以使用gpt-3.5-turbogpt-3.5-trubo-0301
  • messages 会话过程中的消息记录,接受数组消息
    • role 代表来源, system代表ChatGPT系统消息,user代表用户消息,assistant代表助手消息
  • temperature 采样温度,介于 0 和 2 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使输出更加集中和确定。
  • top_n 一种替代温度采样的方法,称为核采样,其中模型考虑具有 top_p 概率质量的标记的结果。 所以 0.1 意味着只考虑构成前 10% 概率质量的标记。
  • n 为每个输入消息生成多少个聊天完成选项
  • stream 是否启用流消息
  • stop API停止生成消息的停止符,最多4个
  • max_tokens 生成的答案允许的最大标记数。 默认情况下,模型可以返回的标记数为(4096 - 提示标记)
  • presence_penalty -2.0 和 2.0 之间的数字。 正值会根据到目前为止是否出现在文本中来惩罚新标记,从而增加模型谈论新主题的可能性。
  • frequency_penalty -2.0 和 2.0 之间的数字。 正值会根据新标记在文本中的现有频率对其进行惩罚,从而降低模型逐字重复同一行的可能性。
  • logit_bias 修改指定标记出现在完成中的可能性。
    接受一个 json 对象,该对象将标记(由标记器中的标记 ID 指定)映射到从 -100 到 100 的关联偏差值。从数学上讲,偏差会在采样之前添加到模型生成的 logits 中。 确切的效果因模型而异,但 -1 和 1 之间的值应该会减少或增加选择的可能性; 像 -100 或 100 这样的值应该导致相关令牌的禁止或独占选择。
  • user 代表最终用户的唯一标识符,可以帮助OpenAI监控和检测滥用行为

PowerAutomate流示例

  • 创建PowerApps(V2)
  • 添加Prompt参数用于传递ChatGPT的问题
  • 添加HTTP操作,设置如下参数
    • Method 设为POST
    • URI 设为https://api.openai.com/v1/chat/completions
    • Headers 设为
1
2
3
4
{
"Content-Type": "application/json",
"Authorization": "Bearer your token for openai"
}
  • Body 设为
1
2
3
4
5
6
7
8
9
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "@{triggerBody()['text']}"
}
]
}
  • 添加Parse JSON操作,设置Content@{body('HTTP')},设置Schema
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{
"type": "object",
"properties": {
"content": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"object": {
"type": "string"
},
"created": {
"type": "integer"
},
"model": {
"type": "string"
},
"usage": {
"type": "object",
"properties": {
"prompt_tokens": {
"type": "integer"
},
"completion_tokens": {
"type": "integer"
},
"total_tokens": {
"type": "integer"
}
}
},
"choices": {
"type": "array",
"items": {
"type": "object",
"properties": {
"message": {
"type": "object",
"properties": {
"role": {
"type": "string"
},
"content": {
"type": "string"
}
}
},
"finish_reason": {},
"index": {
"type": "integer"
}
},
"required": [
"message",
"finish_reason",
"index"
]
}
}
}
},
"schema": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"properties": {
"type": "object",
"properties": {
"id": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"object": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"created": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"model": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"choices": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"items": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"properties": {
"type": "object",
"properties": {
"text": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"index": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"logprobs": {
"type": "object",
"properties": {}
},
"finish_reason": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
}
}
},
"required": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
},
"usage": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"properties": {
"type": "object",
"properties": {
"prompt_tokens": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"completion_tokens": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"total_tokens": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
}
}
  • 添加Compose操作从Parse JSON获取返回的内容,设置Inputs@{first(body('Parse_JSON')?['choices'])['message']['content']}
  • 添加Respond to a PowerApp or flow
  • 添加输出参数Result设置为@outputs('Compose')
  • 保存运行查看结果

Whisper模型

Whisper模型用于OpenAI中语音转文字API,提供两个APItranscriptionstranslations

  • 将音频转录成音频所使用的任何语言。
  • 将音频翻译并转录成英文。

最大支持25MB文件,支持以下文件mp3, mp4, mpeg, mpga, m4a, wav, 和webm.

模型示例请求

1
2
3
4
5
6
curl https://api.openai.com/v1/audio/transcriptions \
-X POST \
-H 'Authorization: Bearer TOKEN' \
-H 'Content-Type: multipart/form-data' \
-F file=@/path/to/file/audio.mp3 \
-F model=whisper-1

模型参数

  • file 要转录的音频文件,采用以下格式之一:mp3mp4mpegmpgam4awavwebm
  • model 模型,目前只支持whisper-1
  • prompt 提示, 用于指导模型的风格或继续之前的音频片段。 提示应与音频语言相匹配
  • response_format 结果输出的格式,采用以下选项之一:jsontextsrtverbose_jsonvtt
  • temperature 采样温度,介于 0 和 1 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使输出更加集中和确定。 如果设置为 0,模型将使用对数概率自动升高温度,直到达到特定阈值。
  • language 输入音频的语言。 以 ISO-639-1 格式提供输入语言将提高准确性和延迟。

PowerAutomate示例

  • 创建PowerApps(V2)
  • 添加文件类型参数Audio用于选择需要转化的语音文件
  • 添加HTTP操作,设置如下参数
    • MethodPOST
    • URIhttps://api.openai.com/v1/audio/transcriptions
    • Headers
1
2
3
4
{
"Content-Type": "multipart/form-data",
"Authorization": "Bearer your token for openai"
}
  • Body
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"$content-type": "multipart/form-data",
"$multipart": [
{
"body": "whisper-1",
"headers": {
"Content-Disposition": "form-data; name=\"model\""
}
},
{
"body": {
"$content": @{triggerBody()?['file']?['contentBytes']},
"$content-type": "audio/mpeg"
},
"headers": {
"Content-Disposition": "form-data; name=\"file\";filename=\"@{triggerBody()?['file']?['name']}\""
}
}
]
}
  • 添加Parse JSON操作
    • 设置Content@{body('HTTP')}
    • 设置Schema
1
2
3
4
5
6
7
8
{
"type": "object",
"properties": {
"text": {
"type": "string"
}
}
}
  • 添加操作Respond to a PowerApp or flow,设置Result@body('Parse_JSON')?['text']
  • 保存后运行测试