ai-fun เป็นไลบรารีฟังก์ชันทดลองที่ขับเคลื่อนด้วย LLM ช่วยให้คุณกำหนดวัตถุประสงค์ของฟังก์ชัน พารามิเตอร์ และสคีมาเอาต์พุต รวมถึงสร้างและรันโค้ดให้คุณในเบื้องหลัง คิดว่า Cursor/GitHub Copilot แต่เป็นไลบรารีที่เสียบได้
npm i ai-fun
ตัวอย่างที่สมบูรณ์:
example.ts
import { z } from 'zod'
import AIFunctionBuilder from 'ai-fun'
import NodeExec from 'ai-fun/src/backends/node'
import { anthropic } from '@ai-sdk/anthropic'
// Provide a LLM model
const llm = anthropic . chat ( 'claude-3-5-sonnet-20240620' )
// Create a new AI Function Builder using Node/exec backend
const backend = new NodeExec ( )
const ai = new AIFunctionBuilder ( llm , backend )
// Define the input parameters and output parameters of the function
const parameters = z . object ( { a : z . number ( ) , b : z . number ( ) } )
const output = z . number ( )
// Generate the function
const f = await ai . function ( 'add values provided' , parameters , output )
// Call the function and log the result
const result = await f ( { a : 1 , b : 2 } )
console . log ( result )
เอาท์พุท:
> bun example.ts
3
พบตัวอย่างเพิ่มเติมภายใต้ตัวอย่าง/
อ่านความคิดเห็น Reddit นี้ก่อนดำเนินการต่อ:
การแคชฟังก์ชันจะเปิดใช้งานตามค่าเริ่มต้นเพื่อมาตรการประหยัดต้นทุน ตามค่าเริ่มต้น ฟังก์ชันต่างๆ จะถูกจัดเก็บไว้ในไฟล์ชื่อ .ai-fun.json
ตัวเลือกที่คุณสามารถมอบให้กับ AIFunctionBuilder
ได้ :
{
debug ?: boolean
esModules?: boolean
cache?: boolean
cacheFile?: string
}
คุณสามารถสร้างแบ็กเอนด์ของคุณเองได้โดยใช้คลาส AIFunctionBackend
:
export abstract class AIFunctionBackend {
abstract init ( codeContent : CodeContent ) : Promise < void >
abstract exec ( params : any ) : Promise < any >
}
ดูตัวอย่าง src/backends/node
ดำเนินการฟังก์ชันที่สร้างโดย AI โดยใช้ฟังก์ชัน node:vm
exec
ตัวเลือก:
{
debug ?: boolean
packageFile?: string
installPackages?: boolean
}
ในฐานะโครงการโอเพ่นซอร์ส เรายินดีรับการสนับสนุนจากชุมชน หากคุณกำลังประสบปัญหาใดๆ หรือต้องการเพิ่มการปรับปรุง โปรดอย่าลังเลที่จะเปิดปัญหาหรือดึงคำขอ