将 Fabric 中的 Azure OpenAI 与 Python SDK 和 Synapse ML 结合使用(预览版)
重要
此功能目前为预览版。
本文演示了如何通过 OpenAI Python SDK 和 SynapseML 在 Fabric 中使用 Azure OpenAI 的示例。
先决条件
未在默认运行时中安装 OpenAI Python SDK,需要先进行安装。
%pip install openai==0.28.1
聊天
ChatGPT 和 GPT-4 是针对对话接口进行了优化的语言模型。 此处提供的示例展示了简单的聊天完成操作,并不旨在充当教程。
import openai
response = openai.ChatCompletion.create(
deployment_id='gpt-35-turbo-0125', # deployment_id could be one of {gpt-35-turbo-0125 or gpt-4-32k}
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Knock knock."},
{"role": "assistant", "content": "Who's there?"},
{"role": "user", "content": "Orange."},
],
temperature=0,
)
print(f"{response.choices[0].message.role}: {response.choices[0].message.content}")
输出
assistant: Orange who?
还可以流式传输响应
response = openai.ChatCompletion.create(
deployment_id='gpt-35-turbo-0125', # deployment_id could be one of {gpt-35-turbo-0125 or gpt-4-32k}
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Knock knock."},
{"role": "assistant", "content": "Who's there?"},
{"role": "user", "content": "Orange."},
],
temperature=0,
stream=True
)
for chunk in response:
delta = chunk.choices[0].delta
if "role" in delta.keys():
print(delta.role + ": ", end="", flush=True)
if "content" in delta.keys():
print(delta.content, end="", flush=True)
输出
assistant: Orange who?
嵌入
嵌入是机器学习模型和算法可以轻松使用的一种特殊数据表示格式。 它包含信息丰富的文本语义,由浮点数向量表示。 向量空间中两个嵌入之间的距离与两个原始输入之间的语义相似性有关。 例如,如果两个文本相似,则它们的向量表示形式也应该相似。
此处演示的示例展示了如何获取嵌入,并不旨在充当教程。
deployment_id = "text-embedding-ada-002" # set deployment_name as text-embedding-ada-002
embeddings = openai.Embedding.create(deployment_id=deployment_id,
input="The food was delicious and the waiter...")
print(embeddings)
输出
{
"object": "list",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": [
0.002306425478309393,
-0.009327292442321777,
0.015797346830368042,
...
0.014552861452102661,
0.010463837534189224,
-0.015327490866184235,
-0.01937841810286045,
-0.0028842221945524216
]
}
],
"model": "ada",
"usage": {
"prompt_tokens": 8,
"total_tokens": 8
}
}