Python から LLM を呼び出す
プロンプトを送り、応答を解析する
「Python から LLM を呼び出す」はCoddyKit上の無料NLP Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNLP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 NLP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Talk to a Model in Code
You do not run giant models on your laptop. Instead you send text to a hosted model over an API and get a reply back. 🌐
Install a Client
Most providers ship a Python client library so you can call the model with a few clean lines instead of raw HTTP.
pip install openaiKeep Your Key Secret
Calls are authenticated with an API key. Store it in an environment variable, never hard-coded in your source files.
import os
key = os.environ["OPENAI_API_KEY"]Create the Client
You start by building a client object. It reads your key and handles the network details for every request you make.
from openai import OpenAI
client = OpenAI()Messages, Not Just Text
Chat models take a list of messages, each tagged with a role like system, user, or assistant.
The System Role
A system message sets the model's behavior up front, like telling it to answer briefly or act as a helpful tutor.
Send Your Prompt
You put your question in a user message and send the whole list to the model in a single call.
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hi!"}])Parse the Response
The reply is a structured object. The text you want sits inside the first choice, ready to read or store.
text = resp.choices[0].message.content
print(text)Control With Temperature
The temperature setting controls randomness. Low values give steady answers; high values give more creative, varied ones.
Cap the Output
Setting max tokens limits how long the reply can be, which keeps responses tidy and your costs predictable.
Handle Failures
Networks fail and limits get hit, so wrap calls in try/except and retry gracefully when an error comes back.
Quick Check
Where do you find the model's text in a chat completion response?
Recap
Install a client, load your key from the environment, send role-tagged messages, then read the text from the first choice. ✅
よくある質問
「Python から LLM を呼び出す」レッスンは無料ですか?
はい。「Python から LLM を呼び出す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、NLP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 NLP Academyコースには全4レッスンが含まれています。
「Python から LLM を呼び出す」で何を学びますか?
プロンプトを送り、応答を解析する ブラウザで直接実行するハンズオンコードでNLP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
NLP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのNLP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Python から LLM を呼び出す」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このNLP Academyレッスンでコードを書いて実行できますか?
はい。すべてのNLP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- モデルを大規模にする要素
- Python から LLM を呼び出す
- Zero-Shot と Few-Shot プロンプティング
- 構造化出力とガードレール