AI Function Helper เป็นโมดูล Node.js ที่ทรงพลังซึ่งทำให้การรวมโมเดล GPT ของ OpenAi ง่ายขึ้นในแอปพลิเคชันของคุณ มันเป็นวิธีที่มีโครงสร้างในการโต้ตอบกับโมเดล AI ทำให้มั่นใจได้ว่าการตอบสนองที่สอดคล้องและจัดรูปแบบ
ติดตั้งตัวช่วยฟังก์ชั่น AI โดยใช้ NPM:
npm install ai-function-helperนี่คือตัวอย่างง่ายๆที่จะเริ่มต้น:
const { createAiFunctionInstance } = require ( 'ai-function-helper' ) ;
// Create an instance with your OpenAI API key
const aiFunction = createAiFunctionInstance ( 'your_api_key_here' ) ;
// Define your function
const options = {
functionName : 'generate_haiku' ,
model : 'gpt-3.5-turbo' ,
args : { topic : 'spring' } ,
description : 'Generate a haiku about the given topic.' ,
outputSchema : {
type : "object" ,
properties : {
haiku : { type : "string" }
} ,
required : [ "haiku" ]
}
} ;
// Call the function
aiFunction ( options )
. then ( result => console . log ( result . haiku ) )
. catch ( error => console . error ( error ) ) ; ในการใช้ตัวช่วยฟังก์ชั่น AI คุณต้องสร้างอินสแตนซ์ด้วยคีย์ OpenAI API ของคุณ:
const { createAiFunctionInstance } = require ( 'ai-function-helper' ) ;
const aiFunction = createAiFunctionInstance ( 'your_api_key_here' ) ;นอกจากนี้คุณยังสามารถใช้ URL ปลายทางที่กำหนดเอง:
const aiFunction = createAiFunctionInstance ( 'your_api_key_here' , 'https://api.openai.com/v1' ) ;หรือคุณสามารถใช้อินสแตนซ์ OpenAI ที่มีอยู่:
const OpenAI = require ( 'openai' ) ;
const openai = new OpenAI ( { apiKey : 'your_api_key_here' } ) ;
const aiFunction = createAiFunctionInstance ( openai ) ;เมื่อคุณมีอินสแตนซ์คุณสามารถเรียกฟังก์ชั่น AI โดยการให้ตัวเลือก:
const result = await aiFunction ( {
functionName : 'example_function' ,
model : 'gpt-4o' ,
args : { param1 : 'value1' , param2 : 'value2' } ,
description : 'This is an example function.' ,
outputSchema : {
type : "object" ,
properties : {
result : { type : "string" }
} ,
required : [ "result" ]
}
} ) ; aiFunction ใช้วัตถุตัวเลือกที่มีคุณสมบัติต่อไปนี้:
| ตัวเลือก | พิมพ์ | คำอธิบาย | ค่าเริ่มต้น |
|---|---|---|---|
functionName | สาย | ชื่อของฟังก์ชัน AI | 'custom_function' |
args | วัตถุ/สตริง | อาร์กิวเมนต์สำหรับฟังก์ชั่น | - |
description | สาย | คำอธิบายของวัตถุประสงค์ของฟังก์ชั่น | - |
outputSchema | วัตถุ | ประเภทผลตอบแทนที่คาดหวัง (JSON Schema หรือ Zod Schema) | - |
strictReturn | บูลีน | บังคับใช้การตรวจสอบประเภทผลตอบแทนที่เข้มงวด | true |
showDebug | บูลีน | พิมพ์ข้อมูลการดีบักไปยังคอนโซล | false |
debugLevel | ตัวเลข | ระดับข้อมูลการดีบัก (0-2) | 0 |
temperature | ตัวเลข | อุณหภูมิการสุ่มตัวอย่างสำหรับรุ่น AI | 0.6 |
frequency_penalty | ตัวเลข | การลงโทษความถี่สำหรับโมเดล AI | 0 |
presence_penalty | ตัวเลข | การลงโทษการปรากฏตัวสำหรับโมเดล AI | 0 |
model | สาย | โมเดล AI ที่จะใช้ | 'gpt-4o-mini' |
max_tokens | ตัวเลข | จำนวนโทเค็นสูงสุดในการสร้าง | 1000 |
top_p | ตัวเลข | ค่า P ด้านบนสำหรับโมเดล AI | null |
blockHijack | บูลีน | ป้องกันการจี้ | false |
blockHijackThrowError | บูลีน | โยนข้อผิดพลาดในความพยายามจี้ | false |
tools | อาร์เรย์ | ฟังก์ชั่นผู้ช่วยที่จะใช้ภายในฟังก์ชั่นหลัก | [] |
stream | บูลีน | เปิดใช้งานการสตรีมการตอบกลับ | false |
streamCallback | การทำงาน | โทรกลับสำหรับการตอบกลับแบบสตรีม | null |
promptVars | วัตถุ | ตัวแปรที่จะใช้ในพรอมต์ | {} |
images | สตริง/อาร์เรย์ | URL ภาพสำหรับแบบจำลองการมองเห็น | null |
imageQuality | สาย | คุณภาพของภาพสำหรับแบบจำลองการมองเห็น | 'low' |
minifyJSON | บูลีน | minify json เอาต์พุต | false |
history | อาร์เรย์ | ประวัติการสนทนาสำหรับบริบท | [] |
forceJsonMode | บูลีน | บังคับโหมด JSON สำหรับรุ่นที่ไม่ใช่ JSON | false |
timeout | ตัวเลข | หมดเวลาสำหรับการโทร API (เป็นมิลลิวินาที) | 120000 |
maxRetries | ตัวเลข | จำนวนการลองใหม่สูงสุดสำหรับการโทร API | 0 |
includeThinking | บูลีน | รวมกระบวนการคิดของ AI ในเอาท์พุทการดีบัก | false |
เอาท์พุท Schema : กำหนดโครงสร้างที่คาดหวังของเอาต์พุตฟังก์ชั่น AI โดยใช้ JSON Schema หรือ ZOD Schema สิ่งนี้ทำให้มั่นใจได้ว่าโมเดล AI จะส่งคืนข้อมูลในรูปแบบแอปพลิเคชันที่คุณคาดหวัง
เครื่องมือ : อาร์เรย์ของฟังก์ชั่นผู้ช่วยที่สามารถใช้ภายในฟังก์ชั่น AI หลัก เครื่องมือแต่ละตัวเป็นวัตถุที่มี name , function_call , description และคุณสมบัติ parameters
Blockhijack : เมื่อเปิดใช้งานคุณสมบัตินี้จะป้องกันโมเดล AI จากคำแนะนำต่อไปนี้ในข้อความผู้ใช้ที่พยายามแทนที่พฤติกรรมที่ตั้งใจไว้ของฟังก์ชั่น
พรอมต์วาร์ : ช่วยให้คุณสามารถกำหนดตัวแปรที่จะถูกแทนที่ในคำอธิบายฟังก์ชั่นซึ่งให้ความยืดหยุ่นมากขึ้นในวิศวกรรมที่รวดเร็ว
รูปภาพ : เปิดใช้งานการใช้อินพุตภาพสำหรับรุ่นที่สามารถมองเห็นได้ซึ่งขยายประเภทของงานที่ AI สามารถทำได้
รวมถึงการคิด : เมื่อตั้งค่าเป็น true ตัวเลือกนี้รวมถึงกระบวนการคิดของ AI ในผลลัพธ์การดีบักซึ่งให้ข้อมูลเชิงลึกเกี่ยวกับวิธีที่ AI มาถึงข้อสรุป สิ่งนี้มีประโยชน์อย่างยิ่งสำหรับงานการแก้ปัญหาที่ซับซ้อนและการดีบัก
ตัวเลือก includeThinking ช่วยให้คุณสามารถจับกระบวนการคิดของ AI ก่อนที่จะสร้างผลลัพธ์สุดท้าย คุณสมบัตินี้ให้ประโยชน์หลายประการ:
ปรับปรุงคุณภาพการตอบสนอง : โดย "การคิด" ก่อนที่จะตอบสนอง AI สามารถจัดระเบียบความคิดและให้คำตอบที่สอดคล้องกันและมีโครงสร้างที่ดีมากขึ้น
ความโปร่งใส : คุณสามารถเห็นเหตุผลที่อยู่เบื้องหลังการตอบสนองของ AI ซึ่งมีประโยชน์สำหรับการดีบักและทำความเข้าใจว่า AI มาถึงข้อสรุปได้อย่างไร
เครื่องช่วยแก้ไขข้อบกพร่อง : กระบวนการคิดอาจมีค่ามากเมื่อมีการปรับแต่งการปรับแต่งหรือแก้ไขปัญหาผลลัพธ์ที่ไม่คาดคิด
เมื่อเปิดใช้งานกระบวนการคิดของ AI จะรวมอยู่ในเอาท์พุทการดีบัก แต่ไม่ได้ถูกส่งคืนเป็นส่วนหนึ่งของผลลัพธ์สุดท้าย นี่คือวิธีการใช้งาน:
const options = {
functionName : 'complex_calculation' ,
args : { expression : '15*87 + ( 129/ (48*0.5) ) +12' } ,
description : 'Perform a complex mathematical calculation and show the steps.' ,
outputSchema : {
type : "object" ,
properties : {
result : { type : "number" }
} ,
required : [ "result" ]
} ,
includeThinking : true ,
showDebug : true // Set this to true to see the thinking process in the console
} ;
const result = await aiFunction ( options ) ; ในเอาท์พุทการดีบักคุณจะเห็นกระบวนการคิดที่อยู่ใน <|start_of_thinking|> และ <|end_of_thinking|> แท็ก ตัวอย่างเช่น:
--- Thinking Process ---
To solve the expression '15*87 + ( 129/ (48*0.5) ) +12', I'll break it down into steps:
1. First, let's solve the parentheses:
(48*0.5) = 24
2. Now we can simplify the division:
129 / 24 = 5.375
3. Let's calculate 15*87:
15*87 = 1305
4. Now we have simplified the expression to:
1305 + 5.375 + 12
5. Let's add these numbers:
1305 + 5.375 = 1310.375
1310.375 + 12 = 1322.375
Therefore, the final result is 1322.375.
--- Parsed JSON Output ---
{
"result": 1322.375
}
โปรดทราบว่ากระบวนการคิดจะปรากฏเฉพาะในเอาท์พุทการดีบักและไม่ส่งผลกระทบต่อโครงสร้างหรือเนื้อหาของผลลัพธ์ที่ส่งคืน คุณลักษณะนี้มีประโยชน์อย่างยิ่งสำหรับงานที่ซับซ้อนซึ่งการทำความเข้าใจการใช้เหตุผลของ AI สามารถนำไปสู่วิศวกรรมที่รวดเร็วและผลลัพธ์ที่แม่นยำยิ่งขึ้น
ในตัวอย่างนี้เราสามารถดูว่า AI แบ่งการคำนวณที่ซับซ้อนลงในขั้นตอนที่จัดการได้อย่างไรทำให้ง่ายต่อการตรวจสอบผลลัพธ์และเข้าใจวิธีการแก้ปัญหา รายละเอียดในระดับนี้ในกระบวนการคิดนี้มีค่าเป็นพิเศษสำหรับการดีบักการศึกษาหรือเมื่อขั้นตอนในการบรรลุข้อสรุปมีความสำคัญเท่ากับคำตอบสุดท้าย
เปิดใช้งานการสตรีมเพื่อประมวลผลการตอบกลับแบบเรียลไทม์:
const options = {
// ... other options ...
stream : true ,
streamCallback : ( chunk ) => {
console . log ( 'Received chunk:' , chunk ) ;
}
} ; คุณสมบัติการสตรีมช่วยให้คุณสามารถประมวลผลการตอบสนอง AI แบบเรียลไทม์ซึ่งอาจเป็นประโยชน์อย่างยิ่งสำหรับงานที่ดำเนินมายาวนานหรือเมื่อคุณต้องการให้ข้อเสนอแนะแก่ผู้ใช้ทันที นี่คือตัวอย่างของวิธีการใช้ตัวเลือก stream ด้วย streamCallback :
const { createAiFunctionInstance } = require ( 'ai-function-helper' ) ;
const aiFunction = createAiFunctionInstance ( 'your_api_key_here' ) ;
async function generateStory ( ) {
let story = '' ;
const options = {
functionName : 'generate_story' ,
model : 'gpt-4o' ,
args : {
theme : 'space exploration' ,
length : 'short'
} ,
description : 'Generate a short story about space exploration.' ,
// We don't use 'outputSchema' to return a text instead of a JSON
stream : true ,
streamCallback : ( chunk ) => {
const content = chunk . choices [ 0 ] ?. delta ?. content ;
if ( content ) {
story += content ;
console . log ( 'Received chunk:' , content ) ;
// You can update your UI here with the new content
}
}
} ;
try {
const result = await aiFunction ( options ) ;
// The result here will be the complete response
console . log ( 'Final story:' , story ) ;
return story ;
} catch ( error ) {
console . error ( 'Error generating story:' , error ) ;
}
}
generateStory ( ) ;ในตัวอย่างนี้:
stream: true ในตัวเลือกเพื่อเปิดใช้งานการสตรีมstreamCallback ที่ได้รับการตอบกลับเมื่อมาถึงวิธีการนี้ช่วยให้คุณสามารถจัดการการตอบสนองของ AI ได้แบบเรียลไทม์ซึ่งจะเป็นประโยชน์สำหรับ:
โปรดจำไว้ว่าเมื่อใช้สตรีมมิ่งผลลัพธ์สุดท้ายที่ส่งคืนโดย aiFunction จะเป็นการตอบสนองที่สมบูรณ์ดังนั้นคุณยังสามารถใช้งานได้หากจำเป็น
กำหนดฟังก์ชั่นผู้ช่วยที่จะใช้ภายในฟังก์ชั่น AI หลักของคุณ:
const options = {
// ... other options ...
tools : [
{
name : "generate_password" ,
function_call : ( { length = 5 , passwordCount = 1 } ) => {
// Password generation logic here
} ,
description : "Generate a random password" ,
parameters : {
type : "object" ,
properties : {
length : { type : "integer" } ,
passwordCount : { type : "integer" }
}
}
}
]
} ;เปิดใช้งานการป้องกันจากการจี้ที่รวดเร็ว:
const options = {
// ... other options ...
blockHijack : true ,
blockHijackThrowError : true // Optional: throw error instead of returning a message
} ;สำหรับรุ่นที่มีวิสัยทัศน์คุณสามารถรวมอินพุตภาพ:
const options = {
// ... other options ...
images : 'https://example.com/image.jpg' ,
// Or
images : [ 'https://example.com/image1.jpg' , 'https://example.com/image2.jpg' ] ,
imageQuality : 'high'
} ;ให้บริบทจากการโต้ตอบก่อนหน้านี้:
const options = {
// ... other options ...
history : [
{ role : "user" , content : "What's the weather like?" } ,
{ role : "assistant" , content : "I'm sorry, but I don't have access to real-time weather information. Is there anything else I can help you with?" }
]
} ; นี่คือตัวอย่างที่น่าสนใจบางอย่างที่แสดงให้เห็นถึงความเก่งกาจและพลังของโมดูล aiFunction :
const options = {
functionName : 'generate_quiz' ,
model : 'gpt-4o' ,
args : { topic : 'space exploration' , difficulty : 'medium' , num_questions : 2 } ,
description : 'Generate a quiz with multiple-choice questions on the given topic.' ,
outputSchema : {
type : "array" ,
items : {
type : "object" ,
properties : {
question : { type : "string" } ,
options : {
type : "array" ,
items : { type : "string" } ,
minItems : 4 ,
maxItems : 4
} ,
correct_answer : { type : "string" }
} ,
required : [ "question" , "options" , "correct_answer" ]
}
}
} ;
const quiz = await aiFunction ( options ) ;
console . log ( JSON . stringify ( quiz , null , 2 ) ) ;ผลลัพธ์ที่คาดหวัง:
[
{
"question" : " Which space agency launched the first artificial satellite, Sputnik 1? " ,
"options" : [
" NASA " ,
" Soviet Union " ,
" European Space Agency " ,
" China National Space Administration "
],
"correct_answer" : " Soviet Union "
},
{
"question" : " What year did the Apollo 11 mission successfully land humans on the Moon? " ,
"options" : [
" 1967 " ,
" 1969 " ,
" 1971 " ,
" 1973 "
],
"correct_answer" : " 1969 "
}
] const { z } = require ( 'zod' ) ;
const options = {
functionName : 'create_recipe' ,
model : 'gpt-4o' ,
args : { cuisine : 'Italian' , main_ingredient : 'pasta' , dietary_restriction : 'vegetarian' } ,
description : 'Create a recipe based on the given cuisine, main ingredient, and dietary restriction.' ,
outputSchema : z . object ( {
name : z . string ( ) ,
ingredients : z . array ( z . string ( ) ) ,
instructions : z . array ( z . string ( ) ) ,
prep_time : z . string ( ) ,
cook_time : z . string ( ) ,
servings : z . number ( ) . int ( )
} )
} ;
const recipe = await aiFunction ( options ) ;
console . log ( JSON . stringify ( recipe , null , 2 ) ) ;ผลลัพธ์ที่คาดหวัง:
{
"name" : " Vegetarian Pasta Primavera " ,
"ingredients" : [
" 12 oz penne pasta " ,
" 2 cups mixed vegetables (bell peppers, zucchini, carrots) " ,
" 1/4 cup olive oil " ,
" 3 cloves garlic, minced " ,
" 1/2 cup grated Parmesan cheese " ,
" 1/4 cup fresh basil, chopped " ,
" Salt and pepper to taste "
],
"instructions" : [
" Cook pasta according to package instructions. Reserve 1/2 cup pasta water. " ,
" In a large skillet, heat olive oil over medium heat. Add minced garlic and sauté for 1 minute. " ,
" Add mixed vegetables to the skillet and cook for 5-7 minutes until tender-crisp. " ,
" Drain pasta and add it to the skillet with vegetables. Toss to combine. " ,
" Add Parmesan cheese, basil, and pasta water as needed to create a light sauce. " ,
" Season with salt and pepper to taste. Serve hot. "
],
"prep_time" : " 15 minutes " ,
"cook_time" : " 20 minutes " ,
"servings" : 4
} const options = {
functionName : 'analyze_reviews' ,
model : 'gpt-4o' ,
args : {
reviews : [
"The product exceeded my expectations. Great value for money!" ,
"Disappointed with the quality. Wouldn't recommend." ,
"Average product, nothing special but does the job."
]
} ,
description : 'Analyze the sentiment of customer reviews and categorize them.' ,
outputSchema : {
type : "array" ,
items : {
type : "object" ,
properties : {
review : { type : "string" } ,
sentiment : { type : "string" , enum : [ "positive" , "neutral" , "negative" ] } ,
score : { type : "number" , minimum : 0 , maximum : 1 }
} ,
required : [ "review" , "sentiment" , "score" ]
}
}
} ;
const sentiment_analysis = await aiFunction ( options ) ;
console . log ( JSON . stringify ( sentiment_analysis , null , 2 ) ) ;ผลลัพธ์ที่คาดหวัง:
[
{
"review" : " The product exceeded my expectations. Great value for money! " ,
"sentiment" : " positive " ,
"score" : 0.9
},
{
"review" : " Disappointed with the quality. Wouldn't recommend. " ,
"sentiment" : " negative " ,
"score" : 0.2
},
{
"review" : " Average product, nothing special but does the job. " ,
"sentiment" : " neutral " ,
"score" : 0.5
}
] const { z } = require ( 'zod' ) ;
const options = {
functionName : 'create_travel_itinerary' ,
model : 'gpt-4o' ,
args : { destination : 'Tokyo' , duration : 3 , interests : [ 'technology' , 'culture' , 'food' ] } ,
description : 'Create a daily travel itinerary for the specified destination and duration, considering the traveler's interests.' ,
outputSchema : z . object ( {
destination : z . string ( ) ,
duration : z . number ( ) . int ( ) ,
daily_plans : z . array ( z . object ( {
day : z . number ( ) . int ( ) ,
activities : z . array ( z . object ( {
time : z . string ( ) ,
activity : z . string ( ) ,
description : z . string ( )
} ) )
} ) )
} )
} ;
const itinerary = await aiFunction ( options ) ;
console . log ( JSON . stringify ( itinerary , null , 2 ) ) ;ผลลัพธ์ที่คาดหวัง:
{
"destination" : " Tokyo " ,
"duration" : 3 ,
"daily_plans" : [
{
"day" : 1 ,
"activities" : [
{
"time" : " 09:00 " ,
"activity" : " Visit Akihabara " ,
"description" : " Explore the technology and electronics district, known for its gadgets and anime culture. "
},
{
"time" : " 13:00 " ,
"activity" : " Lunch at a Robot Restaurant " ,
"description" : " Experience a unique dining experience with robot performances. "
},
{
"time" : " 15:00 " ,
"activity" : " Tour the Miraikan Science Museum " ,
"description" : " Discover cutting-edge technology and scientific innovations at this interactive museum. "
}
]
},
{
"day" : 2 ,
"activities" : [
{
"time" : " 10:00 " ,
"activity" : " Visit Senso-ji Temple " ,
"description" : " Explore Tokyo's oldest Buddhist temple and experience traditional Japanese culture. "
},
{
"time" : " 14:00 " ,
"activity" : " Tea Ceremony in Hamarikyu Gardens " ,
"description" : " Participate in a traditional Japanese tea ceremony in a beautiful garden setting. "
},
{
"time" : " 18:00 " ,
"activity" : " Dinner at Tsukiji Outer Market " ,
"description" : " Enjoy fresh sushi and local delicacies at the world-famous fish market area. "
}
]
},
{
"day" : 3 ,
"activities" : [
{
"time" : " 09:00 " ,
"activity" : " Visit teamLab Borderless " ,
"description" : " Immerse yourself in a digital art museum that blends technology and creativity. "
},
{
"time" : " 13:00 " ,
"activity" : " Ramen Tour in Shinjuku " ,
"description" : " Sample various styles of ramen at some of Tokyo's best ramen shops. "
},
{
"time" : " 16:00 " ,
"activity" : " Shopping in Ginza " ,
"description" : " Explore high-end technology stores and experience Japanese retail innovation. "
}
]
}
]
} const options = {
functionName : 'analyze_stock' ,
model : 'gpt-4o' ,
args : { symbol : 'AAPL' , timeframe : '1 year' } ,
description : 'Analyze the stock performance and provide insights based on the given symbol and timeframe.' ,
outputSchema : {
type : "object" ,
properties : {
symbol : { type : "string" } ,
currentPrice : { type : "number" } ,
yearlyPerformance : { type : "number" } ,
technicalIndicators : {
type : "object" ,
properties : {
RSI : { type : "number" } ,
MACD : {
type : "object" ,
properties : {
value : { type : "number" } ,
signal : { type : "number" } ,
histogram : { type : "number" }
} ,
required : [ "value" , "signal" , "histogram" ]
}
} ,
required : [ "RSI" , "MACD" ]
} ,
recommendation : { type : "string" , enum : [ "Buy" , "Hold" , "Sell" ] }
} ,
required : [ "symbol" , "currentPrice" , "yearlyPerformance" , "technicalIndicators" , "recommendation" ]
}
} ;
const stockAnalysis = await aiFunction ( options ) ;
console . log ( JSON . stringify ( stockAnalysis , null , 2 ) ) ;ผลลัพธ์ที่คาดหวัง:
{
"symbol" : " AAPL " ,
"currentPrice" : 178.25 ,
"yearlyPerformance" : 0.35 ,
"technicalIndicators" : {
"RSI" : 62.5 ,
"MACD" : {
"value" : 2.1 ,
"signal" : 1.8 ,
"histogram" : 0.3
}
},
"recommendation" : " Buy "
} ตัวอย่างเหล่านี้แสดงให้เห็นถึงวิธีการใช้ตัวช่วยฟังก์ชั่น AI สำหรับงานต่าง ๆ ตั้งแต่การสร้างเนื้อหาไปจนถึงการวิเคราะห์ข้อมูล แต่ละตัวอย่างมีสคีมาที่มีรายละเอียด outputSchema (สลับกันระหว่าง JSON Schema และรูปแบบ ZOD) เพื่อให้มั่นใจว่าเอาต์พุตที่มีโครงสร้างและตรวจสอบความถูกต้องจากโมเดล AI
ใช้ชื่อฟังก์ชั่นเฉพาะ : เลือกชื่อฟังก์ชั่นที่ชัดเจนและอธิบายเพื่อช่วยให้ AI เข้าใจบริบท
ให้คำอธิบายโดยละเอียด : ยิ่งบริบทที่คุณให้ไว้ในคำอธิบายมากเท่าไหร่ AI ก็จะเข้าใจและปฏิบัติงานได้ดีขึ้น
กำหนด schemas ส่งคืนที่แม่นยำ : ใช้ schemas outputSchema โดยละเอียดเพื่อให้แน่ใจว่าคุณได้รับโครงสร้างข้อมูลที่แน่นอนที่คุณต้องการ
ใช้เครื่องมือสำหรับงานที่ซับซ้อน : สำหรับงานที่ต้องใช้การคำนวณเฉพาะหรือข้อมูลภายนอกกำหนดเครื่องมือที่กำหนดเองเพื่อจัดการด้านเหล่านี้
จัดการกับข้อผิดพลาดอย่างสง่างาม : ใช้บล็อกลองจับและพิจารณาการตั้งค่าการหมดเวลาที่เหมาะสมและลองลองใหม่สำหรับการจัดการข้อผิดพลาดที่แข็งแกร่ง
ปรับให้เหมาะสมสำหรับการใช้โทเค็น : ระวังความยาวของพรอมต์ของคุณและพิจารณาใช้ minifyJSON สำหรับผลลัพธ์ขนาดใหญ่เพื่อลดการใช้โทเค็น
ใช้สตรีมมิ่งสำหรับการตอบกลับที่ยาวนาน : สำหรับงานที่อาจสร้างการตอบสนองที่ยาวนานให้พิจารณาใช้ตัวเลือกการสตรีมมิ่งเพื่อดำเนินการตอบสนองแบบเรียลไทม์
ใช้ประโยชน์จากประวัติการสนทนา : สำหรับการโต้ตอบหลายครั้งให้ใช้ตัวเลือก history เพื่อให้บริบทจากการแลกเปลี่ยนก่อนหน้านี้
ถาม: ฉันสามารถใช้โมดูลนี้กับผู้ให้บริการ AI รายอื่นได้หรือไม่? ตอบ: ปัจจุบัน AI Function Helper ได้รับการออกแบบให้ทำงานกับโมเดลของ OpenAI การสนับสนุนสำหรับผู้ให้บริการรายอื่นอาจถูกเพิ่มในรุ่นอนาคต
ถาม: ฉันจะดีบักได้อย่างไรถ้าฉันไม่ได้รับผลลัพธ์ที่คาดหวัง? ตอบ: เปิดใช้งานการดีบักโดยการตั้งค่า showDebug: true และปรับ debugLevel สิ่งนี้จะให้ข้อมูลเพิ่มเติมเกี่ยวกับการโทรและการตอบกลับ API
ถาม: โมดูลนี้เหมาะสำหรับการใช้งานการผลิตหรือไม่? ตอบ: ใช่ แต่ตรวจสอบให้แน่ใจเสมอว่าคุณมีข้อผิดพลาดที่เหมาะสมและการ จำกัด อัตราความเคารพที่กำหนดโดย OpenAI
ถาม: ฉันสามารถใช้สิ่งนี้เพื่อสตรีมข้อมูลจำนวนมากได้หรือไม่? ตอบ: ใช่คุณสามารถใช้ตัวเลือก stream สำหรับการจัดการการตอบสนองขนาดใหญ่ได้อย่างมีประสิทธิภาพ
ถาม: โมดูลจัดการปุ่ม API อย่างปลอดภัยได้อย่างไร? ตอบ: โมดูลไม่จัดการกับที่เก็บคีย์ API หรือความปลอดภัย เป็นความรับผิดชอบของคุณในการจัดการและจัดเตรียมคีย์ API อย่างปลอดภัยเมื่อสร้างอินสแตนซ์
เราได้ทำการทดสอบอย่างกว้างขวางเกี่ยวกับโมเดล AI ต่างๆเพื่อประเมินความสามารถในการสร้างเอาต์พุต JSON ของความซับซ้อนที่แตกต่างกันในขณะที่ปฏิบัติตามรูปแบบที่ระบุ การทดสอบเหล่านี้ช่วยแสดงให้เห็นถึงความเก่งกาจและความน่าเชื่อถือของโมดูลผู้ช่วยฟังก์ชัน AI ในโมเดล AI ที่แตกต่างกัน
เพื่อให้แน่ใจว่าการทดสอบที่ครอบคลุมในรูปแบบ AI ที่หลากหลายรวมถึงแบบที่ไม่ได้รับจาก OpenAI โดยตรงเราใช้ Litellm เป็นพร็อกซี Litellm เป็นเครื่องมือที่ทรงพลังที่ให้อินเทอร์เฟซแบบครบวงจรสำหรับผู้ให้บริการ AI และรุ่นท้องถิ่น (ผ่าน Ollama) ซึ่งนำเสนอ URL ปลายทางที่เข้ากันได้ของ OpenAI วิธีการนี้ช่วยให้เราสามารถรวมและทดสอบโมเดล AI หลายรุ่นได้อย่างราบรื่นด้วยตัวช่วยฟังก์ชั่น AI ของเราแสดงให้เห็นถึงความยืดหยุ่นและความเข้ากันได้ในวงกว้าง
| แบบอย่าง | อัตราความสำเร็จ | ระยะเวลาเฉลี่ย |
|---|---|---|
| ดอกไม้ไฟ/Llama-V3P1-405B-Instruct | 100.00% | 16887.67ms |
| GROQ/LLAMA-3.1-70B-Versatile | 100.00% | 2154.89ms |
| Claude-3-Haiku-201240307 | 100.00% | 3175.72ms |
| GPT-3.5-turbo | 88.89% | 3398.67ms |
| GPT-4O-MINI | 100.00% | 5699.72ms |
| GPT-4O | 100.00% | 5673.00MS |
| Claude-3-5-Sonnet-201240620 | 100.00% | 5940.50ms |
| Gemini-1.5-flash | 88.89% | 5150.00ms |
| ราศีเมถุน | 100.00% | 10066.06ms |
| Gemma2: 9b (Ollama) | 100.00% | 13368.94ms |
การทดสอบครอบคลุมฟังก์ชันที่หลากหลายตั้งแต่การคำนวณอย่างง่ายไปจนถึงการสร้างข้อมูลและการวิเคราะห์ที่ซับซ้อน หมวดการทดสอบบางประเภทรวมถึง:
สำหรับผลลัพธ์โดยละเอียดของแต่ละกรณีทดสอบและประสิทธิภาพของโมเดลโปรดดูไฟล์ต่อไปนี้:
หากคุณต้องการเรียกใช้การทดสอบด้วยตัวเองหรือมีส่วนร่วมในการปรับปรุงพวกเขาคุณสามารถค้นหาสคริปต์ทดสอบในที่เก็บ GitHub ของเรา:
การทดสอบเหล่านี้แสดงให้เห็นถึงความสามารถของฟังก์ชั่น AI ของผู้ช่วยในการทำงานกับโมเดล AI ต่างๆและจัดการกับความซับซ้อนของงานที่หลากหลาย พวกเขายังแสดงความสามารถของโมดูลในการบังคับใช้เอาต์พุตที่มีโครงสร้างทำให้ง่ายต่อการรวมเนื้อหา Ai-Generated ลงในแอปพลิเคชันของคุณ
การทดสอบบางอย่างนั้นซับซ้อน "โง่" และได้รับการออกแบบมาเพื่อผลักดันขีด จำกัด ของโมเดล AI การทดสอบเหล่านี้ไม่ได้หมายถึงการใช้งานได้จริง แต่เพื่อแสดงให้เห็นถึงความสามารถของผู้ช่วย AI ในการจัดการกับสถานการณ์ที่ท้าทาย การทดสอบที่ล้มเหลวส่วนใหญ่สามารถทำได้สำเร็จโดยให้แบบจำลอง AI มีบริบทมากขึ้นหรือปรับปรุงการแจ้งเตือนอินพุต
ด้วยการใช้ประโยชน์จาก Litellm เราได้ขยายความเข้ากันได้ของผู้ช่วยฟังก์ชั่น AI ของเรานอกเหนือจากรุ่น OpenAI ทำให้ผู้ใช้สามารถทำงานกับผู้ให้บริการ AI ที่หลากหลายและโมเดลท้องถิ่นในขณะที่ยังคงรักษาอินเทอร์เฟซที่สอดคล้องกัน วิธีการนี้ไม่เพียง แต่ขยายการบังคับใช้เครื่องมือของเรา แต่ยังช่วยให้ผู้ใช้มีความยืดหยุ่นมากขึ้นในการเลือกโมเดล AI ที่เหมาะสมกับความต้องการและข้อ จำกัด ของพวกเขามากที่สุด
ยินดีต้อนรับ! หากคุณต้องการมีส่วนร่วมโปรดแยกที่เก็บและใช้สาขาฟีเจอร์ คำขอดึงยินดีต้อนรับอย่างอบอุ่น
AI Function Helper เป็นซอฟต์แวร์ที่ได้รับใบอนุญาตที่ได้รับอนุญาตภายใต้ใบอนุญาต MIT