CodeGym /課程 /Python SELF TW /AI API操作

AI API操作

Python SELF TW
等級 24 , 課堂 4
開放

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}') 

這段代碼識別圖像中發現的對象列表。你可以輕鬆地將其添加到你的項目中,製作酷炫的網頁服務或應用程式。

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 算法和模型版本而有所不同。

2
任務
Python SELF TW, 等級 24, 課堂 4
上鎖
OpenAI 的 API
OpenAI 的 API
1
問卷/小測驗
網路工作,等級 24,課堂 4
未開放
網路工作
網路工作
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION