向 OpenAI API 發出請求

Working with the OpenAI API

James Chapman

Curriculum Manager, DataCamp

API 端點

電腦透過多個存取點連到 OpenAI,這些存取點再連到不同伺服器。

Working with the OpenAI API

API 端點

電腦透過多個端點連到 OpenAI,端點再連到不同伺服器。

Working with the OpenAI API

API 端點

  • 端點(Endpoints) → 為特定互動設計的 API 存取點

醫院走廊,通往不同科別的多扇門。

Working with the OpenAI API

API 驗證

  • 驗證(Authentication) → 控制對 API 端點服務的存取(通常使用唯一的 key

醫院走廊,通往不同科別的多扇門。

Working with the OpenAI API

API 使用成本

 

  • 針對 OpenAI API:
    • 模型
    • 輸入與輸出越大 → 成本越高

 

  • 本課程不需額外費用 🎉

收費高速公路。

1 https://openai.com/pricing
Working with the OpenAI API

建立 API 金鑰

  • 本課程不需要
  1. 建立帳號 → https://platform.openai.com/signup

  2. 前往 API keys 頁面 → https://platform.openai.com/account/api-keys

  3. 建立新的 secret key 並複製

建立新 secret key 的按鈕。

Working with the OpenAI API

發出請求

from openai import OpenAI


client = OpenAI(api_key="ENTER YOUR KEY HERE")
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What is the OpenAI API?"}]
)
print(response)
Working with the OpenAI API

回應內容

ChatCompletion(id='chatcmpl-AEcQbQekIzxcxVAKYVAjgUAXokgrl', choices=[Choice(finish_reason='length', index=0,
logprobs=None, message=ChatCompletionMessage(content='The OpenAI API is a cloud-based service provided by
OpenAI that allows developers to integrate advanced AI models into their applications.', refusal=None,
role='assistant', function_call=None, tool_calls=None))], created=1728047673, model='gpt-4o-mini-2024-07-18',
object='chat.completion', service_tier=None, system_fingerprint='fp_f85bea6784',
usage=CompletionUsage(completion_tokens=30, prompt_tokens=14, total_tokens=44,
prompt_tokens_details={'cached_tokens': 0}, completion_tokens_details={'reasoning_tokens': 0}))
Working with the OpenAI API

回應內容

ChatCompletion(id='chatcmpl-AEcQbQekIzxcxVAKYVAjgUAXokgrl',
               choices=[Choice(finish_reason='length', index=0, logprobs=None,
                               message=ChatCompletionMessage(content='The OpenAI API is a cloud-based service
                               provided by OpenAI that allows developers to integrate advanced AI models
                               into their applications.',
                               refusal=None, role='assistant', function_call=None, tool_calls=None))],
               created=1728047673,
               model='gpt-4o-mini-2024-07-18',
               object='chat.completion', service_tier=None, system_fingerprint='fp_f85bea6784',
               usage=CompletionUsage(completion_tokens=25, prompt_tokens=14, total_tokens=39,
                                     prompt_tokens_details={'cached_tokens': 0},
                                     completion_tokens_details={'reasoning_tokens': 0}))
Working with the OpenAI API

解讀回應

print(response.choices)
[Choice(finish_reason='length', index=0, logprobs=None,
 message=ChatCompletionMessage(content='The OpenAI API is a cloud-based service provided by
 OpenAI that allows developers to integrate advanced AI models into their applications.',
 refusal=None, role='assistant', function_call=None, tool_calls=None))]
Working with the OpenAI API

解讀回應

print(response.choices[0])
Choice(finish_reason='length', index=0, logprobs=None,
 message=ChatCompletionMessage(content='The OpenAI API is a cloud-based service provided by
 OpenAI that allows developers to integrate advanced AI models into their applications.',
 refusal=None, role='assistant', function_call=None, tool_calls=None))
Working with the OpenAI API

解讀回應

print(response.choices[0].message)
ChatCompletionMessage(content='The OpenAI API is a cloud-based service provided by
 OpenAI that allows developers to integrate advanced AI models into their applications.',
 refusal=None, role='assistant', function_call=None, tool_calls=None)
print(response.choices[0].message.content)
The OpenAI API is a cloud-based service provided by OpenAI that allows developers to
integrate advanced AI models into their applications.
Working with the OpenAI API

一起來練習吧!

Working with the OpenAI API

Preparing Video For Download...