Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ซอฟต์แวร์ >> Office

สร้าง GPT-3 Content Generator อันทรงพลังภายใน Excel

สร้าง GPT-3 Content Generator อันทรงพลังภายใน Excel

OpenAI GPT-3 เพิ่มความสามารถ Excel ด้วย Content Generation Tool สำหรับ Function Definitions, Data Summaries, Product Descriptions ฯลฯ ใช้ GPT-3 หรือ GPT-3.5 Models สร้างเนื้อหา GPT-3 Models เช่น “text-davinci-003” ใช้ Text Generation Tasks 广泛 รองรับรายละเอียด แต่ Deprecated ตั้งแต่ มกราคม 2024 แต่ OpenAI เปิดตัว GPT-3.5 (เช่น gpt-3.5-turbo) ที่ Optimize สำหรับ Conversational Tasks และ Chat-based Format

บทช่วยสอนนี้นำทางผสาน GPT-3 ใน Excel ด้วย Model “gpt-3.5-turbo” สร้าง Tool สร้างเนื้อหาจาก User Prompts

ขั้นตอนที่ 1: ตั้งค่า OpenAI Account ได้ API Key

ใช้ OpenAI GPT-3 API ต้อง Copy จาก OpenAI Account

  • ยังไม่มี Account สมัครที่ OpenAI ได้ Key
  • มี Account แล้ว Login OpenAI
  • จาก Dashboard >> API Key >> Create new secret key
  • ใน Create new secret key box:
    • Name Key >> Permissions >> Create secret key

สร้าง GPT-3 Content Generator อันทรงพลังภายใน Excel

  • ใน Save your key dialog:
    • Copy API Key ที่สร้าง เก็บไว้ปลอดภัย ไม่เห็นอีกหลังสร้าง
    • คลิก Done

สร้าง GPT-3 Content Generator อันทรงพลังภายใน Excel

อย่าแชร์ API Key สาธารณะหรือ Expose ใน Client-side Code

ขั้นตอนที่ 2: ใส่ VBA Code ใน Excel

เขียน VBA Code ส่ง Prompts ไป GPT-3 และรับ Response

  • แท็บ Developer >> Visual Basic
  • ใน VBA Editor >> Insert >> Module
  • ใส่ VBA Code ต่อไปใน Module สร้าง UDF Function

สร้าง GPT-3 Content Generator อันทรงพลังภายใน Excel

VBA Code:

Private Const API_KEY As String = "Your_OpenAI_API_Key" 'Replace your OpenAI API key. ' Function to call OpenAI API Function GenerateContent(Prompt As String) As String On Error GoTo ErrorHandler Dim Http As Object Dim response As String ' Create HTTP object Set Http = CreateObject("MSXML2.XMLHTTP.6.0") ' Set up request headers and endpoint Http.Open "POST", "https://api.openai.com/v1/chat/completions", False Http.setRequestHeader "Content-Type", "application/json" Http.setRequestHeader "Authorization", "Bearer " & API_KEY ' Prepare the JSON payload for GPT-3.5-turbo Dim jsonPayload As String jsonPayload = "{""model"": ""gpt-3.5-turbo"", ""messages"": [{""role"": ""user"", ""content"": """ & Prompt & """}], ""max_tokens"": 200}" ' Send the request Http.send jsonPayload ' Get the response response = Http.responseText ' Parse the JSON response Dim jsonResponse As Object Set jsonResponse = JsonConverter.ParseJson(response) ' Return the content of the response text GenerateContent = jsonResponse("choices")(1)("message")("content") Exit Function ErrorHandler: GenerateContent = "Error: " & Err.Description End Function
  • แทนที่ “Your_OpenAI_API_Key” ด้วย API Key จริง

คำอธิบาย:

  • Function GenerateContent(prompt As String): User-defined Function รับ User Prompt เป็น Input
  • HTTP Request Setup:
    • Creates HTTP Object: ใช้ MSXML2.XMLHTTP ส่ง Request
    • Set Request Headers: กำหนด Content-Type JSON และ Authorization Header พร้อม API Key
  • JSON Payload: สร้าง JSON Object มี Model Name, User Prompt, Max Token Limit
  • Send Request: ส่ง HTTP POST ไป OpenAI API Endpoint พร้อม JSON Payload
  • Response Handling: ดึง Response Parse ด้วย JSON Parser (VBA-JSON) ดึง Generated Content จาก API Response
  • Return Output: Function คืน Generated Text แสดงใน Excel Cell

ขั้นตอนที่ 3: ติดตั้ง JSON Parsing Library

Excel ไร้ Native JSON Parsing ต้องโหลด VBA-JSON จาก GitHub Parse JSON Responses จาก GPT-3

  • Download JsonConverter.bas จาก GitHub
  • ใน VBA Editor >> File >> Import File

สร้าง GPT-3 Content Generator อันทรงพลังภายใน Excel

  • ใน Import Box >> เลือก JsonConverter.bas เพิ่มใน Project

สร้าง GPT-3 Content Generator อันทรงพลังภายใน Excel

Enable References:

ต้อง Enable Microsoft Scripting Runtime Reference จำเป็นเพราะ JsonConverter ใช้ Dictionary Objects จัดการ JSON Data Structures

  • ไป Tools >> References
  • ใน Available References >> ติ๊ก Microsoft Scripting Runtime >> OK

สร้าง GPT-3 Content Generator อันทรงพลังภายใน Excel

ขั้นตอนที่ 4: Run Script ใช้ Content Generation Tool

  • Save Code กลับไป Worksheet
  • พิมพ์ Prompt ใน Sheet เป็น Input ของ GPT-3
  • ใส่สูตรในเซลล์ B2

สูตรนี้ใช้ GPT-3.5 Turbo สร้างเนื้อหาจาก Prompt ใน A2 คืนค่าใน B2

ผลลัพธ์:

สร้าง GPT-3 Content Generator อันทรงพลังภายใน Excel

สรุป

ทำตามขั้นตอนข้างต้นสร้าง Content Generation Tool ใน Excel ด้วย GPT-3 ได้ ประหยัดเวลา Streamline Workflow ใช้ Functional Content Generation Tool ใน Excel ช่วย Generate Ideas, Draft Content, เพิ่ม Intelligent Automation ใน Content Creation แต่จำไว้ OpenAI GPT API ไม่ฟรี Usage ผ่าน API คิดค่าใช้จ่ายตาม Token Usage ติดตาม Usage ให้อยู่ใน Budget

Get FREE Advanced Excel Exercises with Solutions!