>在本節中,我們將使用GPT-3.5-Turbo模型生成響應,而無需呼叫,以查看我們是否獲得一致的輸出。
>在安裝OpenAI Python API之前,您必須獲得一個API鍵並將其設置在本地系統上。通過Python教程中的OpenAI API遵循GPT-3.5和GPT-4,以了解如何獲取API鍵並進行設置。該教程還包括在DataCamp的DataCamp的AI啟用數據筆記本中設置環境變量的示例。以獲取進一步的幫助,請查看Datalab上的OpenAI函數撥打工作簿中的代碼。
>使用以下方式將OpenAi Python API升級到V1
之後,使用API鍵啟動OpenAI客戶端。
pip install --upgrade openai -q>
>
import os from openai import OpenAI client = OpenAI( api_key=os.environ['OPENAI_API_KEY'], )注:OpenAI不再向新用戶提供免費的積分,因此您必須購買它們才能使用API。
我們將編寫一個隨機的學生描述。您可以提出自己的文字,或者使用chatgpt為您生成一個。 >
>在下一部分中,我們將編寫一個提示,以從文本中提取學生信息並將輸出返回為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."
>將提示添加到OpenAI API聊天完成模塊中以生成響應。
# 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} '''響應非常好。讓我們將其轉換為JSON,以更好地理解它。
>
# 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我們將使用“ JSON”庫將文本轉換為JSON對象。
最終結果非常完美。那麼,為什麼我們需要調用函數?
'{\n "name": "David Nguyen",\n "major": "computer science",\n "school": "Stanford University",\n "grades": "3.8 GPA",\n "club": "Robotics Club"\n}'>
>讓我們嘗試相同的提示,但使用其他學生描述。
import json # Loading the response as a JSON object json_response = json.loads(openai_response.choices[0].message.content) json_response
>我們將在提示中更改學生描述文本。
{'name': 'David Nguyen', 'major': 'computer science', 'school': 'Stanford University', 'grades': '3.8 GPA', 'club': 'Robotics Club'}
,並使用第二個提示來運行聊天完成功能。
student_2_description="Ravi Patel is a sophomore majoring in computer science at the University of Michigan. He is South Asian Indian American and has a 3.7 GPA. Ravi is an active member of the university's Chess Club and the South Asian Student Association. He hopes to pursue a career in software engineering after graduating."如您所見,這是不一致的。它沒有返回一個俱樂部,而是返回了拉維(Ravi)加入的俱樂部名單。這也與第一個學生不同。
>
prompt2 = 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_2_description} '''openai函數調用示例
為了解決此問題,我們現在將使用最近引入的功能呼叫的功能。創建一個自定義功能以在字典列表中添加必要的信息是至關重要的,以便OpenAI API了解其功能。
note:確保您遵循正確的模式。通過閱讀官方文檔來了解有關函數調用的更多信息。 >
接下來,我們將使用添加到“函數”參數中的自定義函數為兩個學生描述生成響應。之後,我們將將文本響應轉換為JSON對象並打印它。pip install --upgrade openai -q>
如我們所見,我們獲得了統一的輸出。我們甚至在數字而不是字符串中獲得成績。一致的輸出對於創建沒有錯誤的AI應用程序至關重要。
>import os from openai import OpenAI client = OpenAI( api_key=os.environ['OPENAI_API_KEY'], )
多個自定義功能
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 API的神奇功能,以及它如何自動選擇正確的函數並返回正確的參數。
>我們將使用Chatgpt生成“ Stanford University”描述來測試我們的功能。
>創建學生和學校描述列表,並通過OpenAI聊天完成功能將其傳遞以生成響應。確保您提供了更新的自定義功能。
># 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} '''
> GPT-3.5-Turbo模型已自動為不同的描述類型選擇了正確的功能。我們為學生和學校提供了完美的JSON輸出。
># 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
我們甚至可以使用“ extract_school_info”函數生成休息的名稱。
'{\n "name": "David Nguyen",\n "major": "computer science",\n "school": "Stanford University",\n "grades": "3.8 GPA",\n "club": "Robotics Club"\n}'
import json # Loading the response as a JSON object json_response = json.loads(openai_response.choices[0].message.content) json_response
>函數調用的應用
在本節中,我們將構建一個穩定的文本摘要,該摘要將以某種方式匯總學校和學生信息。
首先,我們將創建兩個python函數,即extract_student_info和extract_school_info,從函數調用中獲取參數並返回匯總的字符串。
pip install --upgrade openai -q
import os from openai import OpenAI client = OpenAI( api_key=os.environ['OPENAI_API_KEY'], )
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的功能調用。我們還學習瞭如何使用它來生成一致的輸出,創建多個功能並構建可靠的文本摘要。
>
以上是OpenAI函數調用教程:生成結構化輸出的詳細內容。更多資訊請關注PHP中文網其他相關文章!