首頁 >科技週邊 >人工智慧 >OpenAI函數調用教程:生成結構化輸出

OpenAI函數調用教程:生成結構化輸出

Lisa Kudrow
Lisa Kudrow原創
2025-03-10 12:02:12665瀏覽

使用openai而無需函數

>在本節中,我們將使用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了解其功能。

  • >名稱:寫您最近創建的python函數名稱。
  • 描述:函數的功能。
  • >
  • 參數:在“屬性”中,我們將寫入參數,類型和描述的名稱。它將幫助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的神奇功能,以及它如何自動選擇正確的函數並返回正確的參數。

。 在字典的python列表中,我們將添加另一個稱為“ extract_school_info”的功能,該功能將幫助我們從文本中提取大學信息。 為了實現這一目標,您必須添加另一個具有名稱,描述和參數的函數的字典。

>我們將使用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

>函數調用的應用

在本節中,我們將構建一個穩定的文本摘要,該摘要將以某種方式匯總學校和學生信息。 OpenAI函數調用教程:生成結構化輸出首先,我們將創建兩個python函數,即extract_student_info和extract_school_info,從函數調用中獲取參數並返回匯總的字符串。

pip install --upgrade openai -q
  1. 創建Python列表,該列表由學生描述,隨機提示和學校描述組成。添加隨機提示以驗證自動函數調用機械師。
  2. 我們將使用“描述”列表中的每個文本生成響應。
  3. >
  4. 如果使用了函數調用,我們將獲得函數的名稱,並基於它,將相關參數應用於函數。否則,返回正常響應。
  5. >
  6. 打印所有三個樣本的輸出。
import os
from openai import OpenAI

client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY'],
)
  • 示例#1 :GPT模型選擇了“ extract_student_info”,我們得到了有關學生的簡短摘要。
  • >
  • >樣本#2 :GPT模型尚未選擇任何功能,並將提示視為常規問題,結果,我們得到了亞伯拉罕·林肯的傳記。
  • >樣本#3 :GPT模型選擇了“ 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的功能調用。我們還學習瞭如何使用它來生成一致的輸出,創建多個功能並構建可靠的文本摘要。

>如果您想了解有關OpenAi API的更多信息,請考慮使用OpenAI API課程,並在Python備忘單中使用OpenAi API來創建您的第一個AI驅動項目。

>>

定義函數模式中的嵌套JSON結構。通過指定參數屬性中的層次關係,您可以確保該模型生成適當嵌套和結構化的JSON輸出,以滿足複雜的數據要求。 > 是否可以與外部API或數據庫一起使用OpenAI函數來使用?

>

>

>如果模型的功能調用與任何定義的函數不匹配,會發生什麼?

>

如果模型的函數調用與已定義的函數或所提供的架構不匹配,則該函數調用未觸發,並且該模型將輸入視為基於標准文本的提示,返回基於文本的基於典型的基於文本的響應。這確保了處理各種輸入類型的靈活性。

賺取頂級AI認證證明您可以有效,負責任地使用AI。獲得認證,被錄用

以上是OpenAI函數調用教程:生成結構化輸出的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn