简介
在当今迅速发展的技术环境中,生成式人工智能(AI)成为了众多企业创新和优化业务流程的重要工具。微软的 Azure OpenAI 服务为开发生成式 AI 解决方案提供了强大的平台和工具。本文将介绍如何利用 Azure OpenAI 服务开发生成式 AI 解决方案,以帮助企业提升生产力、改进客户服务和推动创新。
1. 设置和配置 Azure OpenAI 服务
创建 Azure 账户和订阅
首先,您需要一个 Azure 账户。登录到 Azure 门户(portal.azure.com),创建一个新的订阅,并确保您的订阅具有访问 OpenAI 服务的权限。
配置 Azure OpenAI 资源
- 在 Azure 门户中,导航到“创建资源”。
- 搜索“OpenAI”并选择“Azure OpenAI 服务”。
- 按照提示配置资源,选择适当的定价层,并完成资源创建。
2. 使用 Azure OpenAI 服务
调用 OpenAI API
Azure OpenAI 服务通过 REST API 提供访问 OpenAI 模型的接口。您可以使用各种编程语言(如 Python、C#)来调用这些 API。以下是一个使用 Python 的示例:
import openai
openai.api_type = "azure"
openai.api_base = "https://YOUR_RESOURCE_NAME.openai.azure.com/"
openai.api_version = "2022-12-01"
openai.api_key = "YOUR_API_KEY"
response = openai.Completion.create(
engine="davinci",
prompt="生成一个产品描述",
max_tokens=100
)
print(response.choices[0].text.strip())
集成到业务应用中
通过 API 调用,您可以将生成式 AI 功能集成到各种业务应用中。例如,您可以创建智能客服系统,自动化文档生成,或开发个性化推荐系统。
总结
Azure OpenAI 服务为企业提供了一个强大且灵活的平台,用于开发生成式 AI 解决方案。通过正确的设置、集成和优化,企业可以显著提升业务效率,改善客户体验,并推动创新。无论是初创公司还是大型企业,都可以利用 Azure OpenAI 服务实现其业务目标,迈向智能化未来。
使用 Azure OpenAI 服务应用提示工程
请求输出组合
指定输出的结构可能会对结果产生很大影响。 这可能包括要求模型引用其来源、将响应编写为电子邮件、将响应格式设置为 SQL 查询、将情绪分类为特定结构等。 例如:
提示
code
Write a table in markdown with 6 animals in it, with their genus and species
响应
code
| Animal | Genus | Species |
| ---------------- |:--------------:|:--------------:|
| Red Panda | Ailurus | fulgens |
| African Elephant | Loxodonta | africana |
| Snow Leopard | Panthera | uncia |
| Arctic Fox | Vulpes | lagopus |
| Chimpanzee | Pan | troglodytes |
| Siberian Tiger | Panthera | tigris altaica |
此方法可用于自定义格式,例如 JSON 结构:
提示
code
Put two fictional characters into JSON of the following format
{
firstNameFictional:
jobFictional:
}
响应
code
Here's an example of how you can put two fictional characters into JSON format:
{
"firstNameFictional": "Henry",
"jobFictional": "Wizard"
},
{
"firstNameFictional": "Frank",
"jobFictional": "Hero"
}
ChatCompletion 终结点允许使用 System 聊天角色包括系统消息。
Python
response = openai.ChatCompletion.create(
model="gpt-35-turbo",
messages=[
{
"role": "system", "content": "You are a casual, helpful assistant. You will talk like an American old western film character."},
{
"role": "user", "content": "Can you direct me to the library?"}
]
)
响应
code
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Well howdy there, stranger! The library, huh?
Y'all just head down the main road till you hit the town
square. Once you're there, take a left and follow the street
for a spell. You'll see the library on your right, can’t
miss it. Happy trails!",
"role": "assistant"
}
}
],
...
}
系统消息在格式和内容方面都可能会显著更改响应。 尝试为模型定义一条明确的系统消息,以准确说明你期望的响应类型,以及你希望或不希望它包含的内容。
拆分复杂任务
改进交互的另一种方法是将复杂的提示划分为多个查询。 这使模型能够更好地了解每个单独的部分,并且可以提高整体准确性。 通过划分提示,还可以在将来的提示中包含来自上一个提示的响应,以及使用该信息以及模型功能生成相关响应。
例如,可以询问模型 Doug can ride down the zip line in 30 seconds, and takes 5 minutes to climb back up to the top. How many times can Doug ride the zip line in 17 minutes?。 结果可能是 3,如果 Doug 从 zip 线的顶部开始,则结果不正确。
通过询问多个问题可能会得到信息更丰富的答案,这些答案关于返回到 zip 线顶部的往返时间,以及如何说明 Doug 从顶部开始的事实。 拆分此问题表明,Doug 事实上可以穿越 zip 线四次。
思维链
帮助你有效地拆分任务的一个有用方法是让模型解释其思维链。
要求模型使用确定响应的分步过程做出响应是了解模型如何解释提示的有用方法。 通过执行此操作,你可以看到模型在何处进行了错误的逻辑轮次,并更好地了解如何更改提示以避免错误。 此方法可能包括要求它引用其来源,就像必应聊天(使用 GPT-4 代模型),并给出确定答案的原因。
思维链提示技术最适合用于帮助你循环访问和改进提示,以便从模型获得最高质量的答案。
例如,询问模型 What sport is easiest to learn but hardest to master? 会得到一个答案作为响应,并简要说明原因。 但是,当出现 What sport is easiest to learn but hardest to master? Give a step by step approach of your thoughts, ending in your answer 提示时,响应是它如何得出答案的完整说明。
code
Sure, here's a step-by-step approach to answering the question:
1. Identify sports that are popular and have a large following.
2. Consider the basic mechanics of each sport and how easy they are to learn.
3. Think about the various techniques and skills required to play each sport at a high level, and how difficult they are to master.
4. Compare the relative ease of learning each sport to the difficulty of mastering it.
5. Based on the above analysis, determine which sport is easiest to learn but hardest to master.
Using this approach, I would say that the sport of golf is easiest to learn but hardest to master. Here's why:
1. Golf is a popular sport with a large following.
2. The basic mechanics of hitting a golf ball are relatively simple and can be learned quickly with some practice.
3. However, achieving consistency and mastering the various shots and techniques required to play at a high level can take years of practice and dedication.
4. Compared to other sports, the difference between a beginner and an intermediate golfer is relatively small, while the difference between an intermediate and an advanced golfer is much larger.
5. Therefore, I would say that golf is the sport that is easiest to learn but hardest to master.
了解模型用于形成其响应的原因步骤后,我们可以确定需要拆分提示的位置、提供更多信息或阐明说明。
使用 Azure OpenAI 服务生成代码
编写函数
只需描述所需内容,Azure OpenAI 模型即可创建多种语言的函数和应用。 例如,假设你需要二进制文件搜索的实现,但不记得它是如何实现的了。 给定提示 write a function for binary search in python,你可能会收到包含函数的响应和对代码的说明。
code
Sure, here's an example implementation of binary search algorithm in Python:
def binary_search(array, target):
low = 0
high = len(array) - 1
while low <= high:
mid = (low + high) // 2
if array[mid] == target:
return mid
elif array[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
This function takes in two arguments - an array of integers and a target integer that we want to search
for in the array. The function initializes two pointers - low and high - to the first and last indices
of the array, respectively.
Inside the while loop, the function calculates the middle index using integer division (//). If the
middle element is equal to the target, the function returns the index of the middle element. If the
middle element is less than the target, we update the low pointer to mid + 1. Otherwise, we update the
high pointer to mid - 1. We continue these steps until we find the target or the pointers cross each
other (i.e., low > high).
If the target is not found in the array, the function returns -1.
Note that the binary search algorithm assumes that the input array is sorted in ascending order. If
the array is not sorted, the algorithm may not return the correct result.
请求可以超越函数,请求一系列函数或整个应用。 响应为尝试实现的任务提供了良好的起点。
更改编码语言
如果你的代码采用的是一种语言,但你需要它采用另一种语言,Azure OpenAI 可以转换该代码。 例如,假设在 Python 中具有此 print_squares 函数。
Python
def print_squares(n):
for i in range(1, n+1):
print(i**2)
如果提供该代码以及提示 convert this code to C#,你可能会得到类似于以下函数的内容。
C#
public void PrintSquares(int n)
{
for (int i = 1; i <= n; i++)
{
Console.WriteLine(i * i);
}
}
请注意,模型理解 for 循环和函数以不同语言(包括 C# 中的访问修饰符)定义的方式的差异,并相应地更改了代码。
理解未知代码
Azure OpenAI 模型有助于理解无法理解的代码或可能采用你不熟悉的语言的代码。 例如,假设你获得了以下函数(采用虚构的编码语言!),但不知道如何理解它。
code
fn qzplv(n: i32, m: i32) -> i32 {
if n == 0 {
return m + 1;
} else if m == 0 {
return qzplv(n - 1, 1);
} else {
return qzplv(n - 1, qzplv(n, m - 1));
}
}
将其与提示 could you explain what this code is doing? 一起提供给模型时,你可得到类似于以下内容的响应:
code
Sure, I'd be happy to explain what this code is doing!
This code is implementing the Ackermann function, which is a well-known mathematical function that
grows very quickly in terms of computation time as its inputs increase. The function takes two integer
arguments (n and m) and returns another integer.
Here's how the function works:
- If n is equal to 0, the function returns m + 1.
- If m is equal to 0, the function calls itself recursively with n - 1 and 1 as arguments.
- Otherwise, the function calls itself recursively with n - 1 and the result of calling itself with n
and m - 1 as arguments.
The function is implemented in a way that is easy to understand and follow, but it has a very high
computational complexity. In fact, it grows so quickly that it can't be computed for values of n and m
greater than about 4 or 5.
将说明和转换结合成你熟悉的语言会使此功能更加有用。
完成部分代码
Azure OpenAI 模型能够基于注释、函数名称和部分编写的代码生成代码。 为模型提供的上下文越多,响应可能就越准确。
例如,当出现以下提示时:
prompt
complete the following function
"""
# calculate the average of the numbers in an array, but only if they're even
def
该模型获取注释和函数定义的开头,并根据它具有的任何上下文完成它。
Python
# calculate the average of the even numbers in an array
def calculate_even_average(numbers):
total = 0
count = 0
for number in numbers:
if number % 2 == 0:
total += number
count += 1
if count == 0:
return 0 # avoid division by zero
else:
return total / count
在本例中,由于在函数开头之前有一个相对简单和完整的注释,它能够辨别出我们想要的内容。 对于更复杂的函数或任务,需要更多上下文以获得有用的响应。
请务必注意,对于某些语言(如 Python),语法足够清晰,使模型能够推断所需的语言。 对于语法相似的语言(如 C# 或 Java),模型使用错误语言的可能性更高。 如果是这种情况,请尝试在提示符中指定语言或为上下文提供更多代码。
可以通过向模型提供部分代码(不带任何注释)来查看类似的功能。 以下面的提示为例。
prompt
complete the following code
"""
def func1(n)
if n==0:
AI 模型使用最有可能的函数完成,尽最大努力完成函数。
Python
def func1(n):
if n == 0:
return 1
else:
return n * func1(n-1)
响应可能还包括该函数所执行的操作的自然语



被折叠的 条评论
为什么被折叠?



