Openai Python APIをインストールする前に、APIキーを取得してローカルシステムにセットアップする必要があります。 PythonチュートリアルのOpenai APIを介してGPT-3.5およびGPT-4をフォローして、APIキーを取得してセットアップする方法を学びます。チュートリアルには、DatacampのAI対応データノートブックであるDatalabの環境変数のセットアップの例も含まれています。
さらなる支援については、datalabのワークブックを呼び出すOpenai関数のコードをチェックしてください。Openai Python APIを使用してV1にアップグレードします
その後、APIキーを使用してOpenAIクライアントを開始します。
Note
:Openaiは新しいユーザーに無料クレジットを提供しなくなったため、APIを使用するには購入する必要があります。pip install --upgrade openai -q
次の部分では、テキストから学生情報を抽出し、JSONオブジェクトとして出力を返すようにプロンプトを書きます。学生の説明に名前、メジャー、学校、成績、クラブを抽出します。
import os from openai import OpenAI client = OpenAI( api_key=os.environ['OPENAI_API_KEY'], )
OpenAI APIチャット完了モジュールにプロンプトを追加して、応答を生成します。 応答は非常に良いです。 JSONに変換して、よりよく理解しましょう
「JSON」ライブラリを使用して、テキストをJSONオブジェクトに変換します。
student_1_description = "David Nguyen is a sophomore majoring in computer science at Stanford University. He is Asian American and has a 3.8 GPA. David is known for his programming skills and is an active member of the university's Robotics Club. He hopes to pursue a career in artificial intelligence after graduating."
# A simple prompt to extract information from "student_description" in a JSON format. prompt1 = f''' Please extract the following information from the given text and return it as a JSON object: name major school grades club This is the body of text to extract the information from: {student_1_description} '''
プロンプトの学生の説明テキストを変更するだけです。
# Generating response back from gpt-3.5-turbo openai_response = client.chat.completions.create( model = 'gpt-3.5-turbo', messages = [{'role': 'user', 'content': prompt_1}] ) openai_response.choices[0].message.content
そして、2番目のプロンプトを使用してチャット完了関数を実行します。
'{\n "name": "David Nguyen",\n "major": "computer science",\n "school": "Stanford University",\n "grades": "3.8 GPA",\n "club": "Robotics Club"\n}'
openai関数呼び出し例
import json # Loading the response as a JSON object json_response = json.loads(openai_response.choices[0].message.content) json_response
:正しいパターンに従っていることを確認してください。公式ドキュメントを読んで、関数呼び出しの詳細をご覧ください。 次に、「関数」引数に追加されたカスタム関数を使用して、2つの学生の説明の応答を生成します。その後、テキスト応答をJSONオブジェクトに変換して印刷します。
pip install --upgrade openai -q
複数のカスタム関数
import os from openai import OpenAI client = OpenAI( api_key=os.environ['OPENAI_API_KEY'], )
チャット完了関数に複数のカスタム関数を追加できます。このセクションでは、Openai APIの魔法の能力と、正しい関数を自動的に選択し、正しい引数を返す方法を確認します。
辞書のPythonリストでは、「Extract_school_info」と呼ばれる別の関数を追加します。これは、テキストから大学の情報を抽出するのに役立ちます。 これを達成するには、名前、説明、パラメーターを含む関数の別の辞書を追加する必要があります。student_1_description = "David Nguyen is a sophomore majoring in computer science at Stanford University. He is Asian American and has a 3.8 GPA. David is known for his programming skills and is an active member of the university's Robotics Club. He hopes to pursue a career in artificial intelligence after graduating."
生徒と学校の説明のリストを作成し、OpenAIチャット完了関数を通過して応答を生成します。更新されたカスタム関数を提供していることを確認してください
GPT-3.5ターボモデルは、さまざまな説明タイプに対して正しい関数を自動的に選択しました。生徒と学校のための完璧なJSON出力が得られます。
「extract_school_info」関数を使用して、安価が生成されるという名前の下でも見ることができます。
# A simple prompt to extract information from "student_description" in a JSON format. prompt1 = f''' Please extract the following information from the given text and return it as a JSON object: name major school grades club This is the body of text to extract the information from: {student_1_description} '''
# Generating response back from gpt-3.5-turbo openai_response = client.chat.completions.create( model = 'gpt-3.5-turbo', messages = [{'role': 'user', 'content': prompt_1}] ) openai_response.choices[0].message.content
関数呼び出しのアプリケーション
'{\n "name": "David Nguyen",\n "major": "computer science",\n "school": "Stanford University",\n "grades": "3.8 GPA",\n "club": "Robotics Club"\n}'
最初に、extract_student_infoとextract_school_infoの2つのpython関数を作成します。
pip install --upgrade openai -q
import os from openai import OpenAI client = OpenAI( api_key=os.environ['OPENAI_API_KEY'], )
以上がOpenAI関数呼び出しチュートリアル:構造化された出力を生成しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。