การเรียก API ภายนอก

การพัฒนาระบบ AI ด้วย OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

ไลบรารี requests ของ Python สำหรับ API

ไดอะแกรมแสดงการส่งคำขอไปยัง API ของ Art Institute of Chicago

การพัฒนาระบบ AI ด้วย OpenAI API

ไลบรารี requests ของ Python สำหรับ API

  • นำเข้าไลบรารี
    import requests
    
  • ระบุ URL
    url = "https://api.artic.edu/api/v1/artworks/search"
    
  • ระบุพารามิเตอร์ของคิวรี
    querystring = {"q":keyword}
    
  • รับการตอบกลับ
    response = requests.request("GET", url, params=querystring)
    
1 https://api.artic.edu/docs/#quick-start
การพัฒนาระบบ AI ด้วย OpenAI API

การห่อหุ้มฟังก์ชัน

 

import requests

def get_artwork(keyword):
    url = "https://api.artic.edu/api/v1/artworks/search"
    querystring = {"q":keyword}
    response = requests.request("GET", url, params=querystring)
    return response.text
การพัฒนาระบบ AI ด้วย OpenAI API

การเพิ่ม context

response = client.chat.completions.create(
    model="gpt-4o-mini",

messages=[ {"role": "system", "content": "You are an AI assistant, a specialist in history of art. You should interpret the user prompt, and based on it extract one keyword for recommending artwork related to their preference."},
{"role": "user", "content": "I don't have much time to visit the museum and would like some recommendations. I like the seaside and quiet places."} ],
การพัฒนาระบบ AI ด้วย OpenAI API

การเพิ่มฟังก์ชันใน tools

    function_definition=[{"type": "function",
            "function" : {
                "name": "get_artwork",
                "description": "This function calls the Art Institute of Chicago API 
                                to find artwork that matches a keyword",
                "parameters": {

"type": "object", "properties": { "artwork keyword": { "type": "string", "description": "The keyword to be passed to the get_artwork function."}}}, "result": {"type": "string"} }} ] )
การพัฒนาระบบ AI ด้วย OpenAI API

รวมทุกอย่างเข้าด้วยกัน

import json

if response.choices[0].finish_reason=='tool_calls': function_call = response.choices[0].message.tool_calls[0].function
if function_call.name == "get_artwork": artwork_keyword = json.loads(function_call.arguments)["artwork keyword"] artwork = get_artwork(artwork_keyword)
if artwork: print(f"Here are some recommendations: {[i['title'] for i in json.loads(artwork)['data']]}") else: print("Apologies, I couldn't make any recommendations based on the request.")
else: print("Apologies, I couldn't find any artwork.")
else: print("I am sorry, but I could not understand your request.")
การพัฒนาระบบ AI ด้วย OpenAI API

ผลลัพธ์สุดท้าย

Here are some recommendations: ['Seaside, Port of Honfleur', 
'Little Landscape at the Seaside (Kleine Landschaft am Meer)', 
'Museum of Contemporary Art, Niterói, Rio de Janeiro, Brazil, Four Sketches',
'A seaside outing', 'Stacks of Wheat (End of Day, Autumn)', 'Stack of Wheat', 
'Stacks of Wheat (Sunset, Snow Effect)', 'Stacks of Wheat (End of Summer)', 
'Stack of Wheat (Thaw, Sunset)', 'Waitress at a Seaside Teahouse']

ภาพวาดสไตล์อิมเพรสชันนิสม์ของชายทะเลพร้อมเรือ

1 Seaside, Port of Honfleur | The Art Institute of Chicago
การพัฒนาระบบ AI ด้วย OpenAI API

มาฝึกกันเถอะ!

การพัฒนาระบบ AI ด้วย OpenAI API

Preparing Video For Download...