10.1 ChatGPT
等一下,我在说什么呢?我们可是生活在一个人工智能创造的世界里。让我们 始终试试和它一起工作。当然,从ChatGPT开始。
使用OpenAI API(ChatGPT)的例子
要使用OpenAI API,你需要在平台上注册,获取 API密钥,并使用它进行请求时的身份验证。
然后需要安装库openai
— 这是他们的官方客户端。
pip install openai
现在向他们发送一个请求:
import openai
# 你的OpenAI API密钥
api_key = 'YOUR_OPENAI_API_KEY'
# 身份验证
openai.api_key = api_key
# 向模型ChatGPT请求
response = openai.Completion.create(
engine="text-davinci-003",
prompt="告诉我一个关于宇宙的有趣事实。",
max_tokens=500
)
# 打印响应
print(response.choices[0].text.strip())
你需要在他们的官网注册并获取密钥。如果 你是新用户(你的电话不在数据库中),他们会给你20美元的 奖励。
10.2 Google Cloud Vision API
Google Cloud Vision API 提供了图像分析功能,包括对象识别、文本识别、人脸识别和其他 元素。Google Bard API 目前尚未公开,需要通过Google Cloud Platform 进行 身份验证。
步骤1. 首先安装库 google-cloud-vision
。
pip install google-cloud-vision
步骤2. 使用服务账号密钥(Service Account Key)设置身份验证。
图像分析代码示例:
from google.cloud import vision
import io
# 初始化客户端
client = vision.ImageAnnotatorClient()
# 加载图像
file_name = 'path/to/your/image.jpg'
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
# 对象检测
response = client.object_localization(image=image)
objects = response.localized_object_annotations
# 输出检测到的对象
for object_ in objects:
print(f'Object name: {object_.name}')
print(f'Score: {object_.score}')
这个代码识别图片并给出所发现对象的列表。你可以轻松 将它添加到项目中,制作一个酷炫的web服务或应用程序。
10.3 Microsoft Text Analytics API
Azure Cognitive Services 提供文本分析API,包括 语言识别、情感分析、关键短语提取和 实体识别。
文本情感分析是确定文本情感色彩的过程(积极、消极或中立)。 这对于分析客户反馈、监控社交媒体或评估对特定事件或产品的反应非常有用。
安装库和身份验证
步骤1. 安装库 Azure
:
pip install azure-ai-textanalytics
步骤2. 使用API密钥和终端点(endpoint)
设置身份验证。
文本情感分析代码示例:
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
# 初始化客户端
endpoint = "YOUR_AZURE_ENDPOINT"
api_key = "YOUR_AZURE_API_KEY"
credential = AzureKeyCredential(api_key)
client = TextAnalyticsClient(endpoint=endpoint, credential=credential)
# 待分析的文本
documents = ["I love programming in Python!", "I'm feeling very happy today!"]
# 情感分析
response = client.analyze_sentiment(documents=documents)
# 输出结果
for doc in response:
print(f"Sentiment: {doc.sentiment}")
print(f"Confidence Scores: {doc.confidence_scores}")
10.4 DeepAI (Text Summarization API)
DeepAI 提供用于各种机器学习任务的API,包括 文本总结。
文本总结是对大量文本进行简短概述的过程, 同时保留其关键思想和主要内容。这对于快速 熟悉长篇文档、自动创建摘要或处理 大量文本信息非常有用。
安装库和身份验证
步骤1. 安装库 requests
:
pip install requests
步骤2. 使用API密钥进行身份验证。
文本总结代码示例:
import requests
# 你的DeepAI API密钥
api_key = 'YOUR_DEEPAI_API_KEY'
# 待总结的文本
text = "Artificial intelligence (AI) is intelligence demonstrated by machines, in contrast to the natural intelligence displayed by humans and animals. Leading AI textbooks define the field as the study of 'intelligent agents': any device that perceives its environment and takes actions that maximize its chance of successfully achieving its goals."
# 向API请求
response = requests.post(
"https://api.deepai.org/api/summarization",
data={'text': text},
headers={'api-key': api_key}
)
# 获取响应
data = response.json()
# 输出总结
print(data['output'])
可能的输出
如果你使用正确的API密钥运行上述代码,你会得到 类似的结果:
Artificial intelligence (AI) is the intelligence shown by machines, unlike natural intelligence in humans and
animals. AI studies 'intelligent agents': devices that perceive their environment and act to achieve their
goals.
此总结将包括原始文本的关键点, 将其缩短为更简洁的版本。请注意,确切的 结果可能会因算法和DeepAI中使用的模型版本而有所不同。
GO TO FULL VERSION