Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the acf domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/aithai/domains/aithailand.co/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the heartbeat-control domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/aithai/domains/aithailand.co/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the td-cloud-library domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/aithai/domains/aithailand.co/public_html/wp-includes/functions.php on line 6121
มาใช้ OpenAI API ใน Python กันเถอะ | AI Thailand
More

    มาใช้ OpenAI API ใน Python กันเถอะ

    A Step-by-Step Guide to Getting Started with OpenAI in Python

    ไม่นานมานี้เทรนด์ AI นั้นถือได้ว่าเป็นเทรนด์ที่กำลังมาแรงและน่าจับตามองมากที่สุดเลยก็ว่าได้ หากคุณยังไม่ได้ลองใช้มันเลย นี่อาจจะถึงเวลาที่คุณต้องคิดใหม่แล้วล่ะ ด้วยความสามารถและศักยภาพที่หลากหลายของมัน ผมรับรองได้เลยว่ามันสามารถช่วยงานคุณได้แน่นอน หากจะพูดถึง AI ก็หนีไม่พ้นเลยที่จะมองข้าม ChatGPT 3 และ 4 จากทาง OpenAI ที่ตอนนี้กำลังเป็นที่นิยมมากๆ OpenAI นั้นเป็นศูนย์วิจัยเอกชนที่มีเป้าหมายในการพัฒนาและชี้นำปัญญาประดิษฐ์ (AI) ในรูปแบบที่เป็นประโยชน์ต่อมวลมนุษยชาติ 

    โดยวันนี้เราจะมาสาธิตวิธีการใช้ API ของ OpenAI กับ Python กัน เพื่อที่จะทำให้งานของคุณง่ายขึ้นเป็นกอง ด้วย content generation, question answering, text summarization, การแปล และแม้กระทั่ง sentiment analysis ด้วย OpenAI API ไม่ว่างานไหนๆ ก็จะไม่ใช่เรื่องยากอีกต่อไป ไม่มีลิมิตชีวิตเกินร้อยกันเลยทีเดียว

    ในการที่จะใช้ OpenAI API ใน Python ขั้นตอนมีดังนี้: 

    • Sign up กับ OpenAI API Key: ลิ้งค์นี้จะนำไปคุณไปยังหน้าสมัครแอคเค้าท์ แต่ถ้าหากคุณมีแอคเค้าท์อยู่แล้วคุณก็สามารถใช้แอคเค้าท์นั้นได้เลย หลังจากนี้มันจะนำคุณไปยังอินเตอร์เฟสแบบที่คุณเห็นด้านล่าง: 

    หลังจากนั้นคลิกไปที่แอคเค้าท์โปรไฟล์ของคุณและเลือก “View API Keys” หรือคุณจะคลิกที่ลิงค์นี้ก็ได้เช่นกัน 

    ในเพจนี้ คุณจะเห็นตัวเลือกที่ให้คุณสร้าง Secret Key ใหม่ได้ คลิกเข้าไปเพื่อที่จะให้มันสร้าง secret key ให้คุณ 

    • การติดตั้ง: ในการติดตัง OpenAI Library ให้คุณเปิด command prompt หรือ terminal window และพิมพ์คำสั่งเหล่านี้ลงไปเพื่อติดตั้ง OpenAI package 

    pip install openai 

    การสาธิต: เอาล่ะ หลังจากที่คุณติดตั้ง library ลงบนคอมพิวเตอร์ของคุณเรียบร้อยแล้ว ทีนี้มาลองใช้ Question Answering task ด้วย API กัน: 

    import openai
    openai.api_key = "sk-GxJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    
    model = "davinci"
    question = "What is the capital of France?"
    
    response = openai.Completion.create(
        engine=model,
        prompt=(f"Question: {question}\n"
                "Answer:"
                ),
        max_tokens=100,
        n=1,
        stop=None,
        temperature=0.5
    )
    
    answer = response.choices[0].text.split('\n')[0]
    print(answer)

    Output

    ในโค้ดนี้ เราได้ใช้ davinci model เพื่อตอบคำถาม “เมืองหลวงของฝรั่งเศสคือที่ไหน” และสิ่งที่เราทำคือ เราไม่ได้ให้ context อะไรกับมันเลย แต่เราสามารถใส่ context ลงไป เพื่อให้ตัวโมเดลเข้าใจความหมายของคำถามที่เราถามลงไปเพื่อที่มันจะได้ generate คำตอบที่แม่นยำยิ่งขึ้นได้ 

    เรามาดูอีกตัวอย่างนึงในการใช้ Sentiment Analysis กันเถอะ

    import openai
    openai.api_key = "sk-GxJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    
    model = "text-davinci-002"
    prompt = "Please perform sentiment analysis on the following text: 'The movie was really great, I loved it!'"
    
    response = openai.Completion.create(
        engine=model,
        prompt=prompt,
        max_tokens=100,
        n=1,
        stop=None,
        temperature=0.5
    )
    
    sentiment = response.choices[0].text.strip()
    print(sentiment)

    Output

    ในตัวอย่างนี้ เราได้ใช้ text-davinci-002 model เพื่อให้มัน perform sentiment analysis อ้างอิงจาก text: “หนังเรื่องนี้มันสุดยอดมาก ฉันชอบมากๆเลย” ซึ่งทาง model ได้ประเมินว่าเป็นข้อความเชิงบวก 

    จริงๆ แล้วเราได้ลองใช้มันทำอีกหลายต่อหลายอย่างเลย มันสุดยอดมากๆ และเราอยากแนะนำให้เพื่อนๆ มาลองใช้กันดู 😀

    RIKI
    RIKI
    นักเขียนหน้าใหม่ผู้ชื่อชอบในเรื่องของเทคโนโลยี และ AI หากคุณเป็นผู้ที่รักในเทคโนโลยี และ นวัตกรรม AI ใหม่ๆ แล้วล่ะก็ฝากเนื้อฝากตัวด้วยนะครับ ;>

    Follow Us

    16,062FansLike
    338FollowersFollow
    0FollowersFollow

    Latest stories

    You might also like...