ai fns
1.0.0
모든 함수를 ChatGpt 함수로 변환하십시오.
AI-FNS는 함수가 Chatgpt에서 호출 할 수있는 함수로 변환하는 작은 라이브러리입니다.
기본 사양은 OpenAI의 새로운 기능 호출 기능을 기반으로합니다.
pnpm install ai-fns zod? ? 기능에 대한 JSON 스키마를 수동으로 만들고 chatgpt로 전달하십시오.
import openai , { OpenAI } from "openai" ;
const openai = new OpenAI ( {
apiKey : env . OPENAI_API_KEY ,
} ) ;
const weather = async ( {
latitude ,
longitude ,
} : {
latitude : number ;
longitude : number ;
} ) => {
try {
const res = await fetch (
`https://api.open-meteo.com/v1/forecast?latitude= ${ latitude } &longitude= ${ longitude } ¤t_weather=true`
) ;
return await res . json ( ) ;
} catch ( error ) {
return error ;
}
} ;
const completion = await openai . chat . completions . create ( {
model : "gpt-3.5-turbo-16k" ,
messages : [ { role : "user" , content : "What's the weather in San Francisco?" } ] ,
functions : [
{
name : "weather" ,
description : "Get the current weather in a given location" ,
parameters : {
type : "object" ,
properties : {
longitude : {
type : "number" ,
minimum : - 180 ,
maximum : 180 ,
description : "Longitude" ,
} ,
latitude : {
type : "number" ,
minimum : - 90 ,
maximum : 90 ,
description : "Latitude" ,
} ,
} ,
required : [ "longitude" , "latitude" ] ,
additionalProperties : false ,
} ,
} ,
] ,
} ) ; ai-fns 사용하여 기능에 대한 스키마를 자동으로 생성하고 Chatgpt로 전달하십시오.
import openai , { OpenAI } from "openai" ;
import { z } from "zod" ;
import { aifn } from "ai-fns" ;
const openai = new OpenAI ( {
apiKey : env . OPENAI_API_KEY ,
} ) ;
const { schema , fn } = aifn (
"weather" ,
"Get the current weather in a given location" ,
z . object ( {
longitude : z . number ( ) . min ( - 180 ) . max ( 180 ) . describe ( "Longitude" ) ,
latitude : z . number ( ) . min ( - 90 ) . max ( 90 ) . describe ( "Latitude" ) ,
} ) ,
async ( { latitude , longitude } ) => {
try {
const res = await fetch (
`https://api.open-meteo.com/v1/forecast?latitude= ${ latitude } &longitude= ${ longitude } ¤t_weather=true`
) ;
return await res . json ( ) ;
} catch ( error ) {
return error ;
}
}
) ;
// Ask the AI a question
const completion = await openai . chat . completions . create ( {
model : "gpt-3.5-turbo-16k" ,
messages : [ { role : "user" , content : "What's the weather in San Francisco?" } ] ,
functions : [ schema ] ,
} ) ; 주어진 수학적 표현의 출력을 계산하는 함수의 예는 다음과 같습니다.
import { Parser } from "expr-eval" ;
import { z } from "zod" ;
import { aifn } from "ai-fns" ;
const parser = new Parser ( ) ;
export default aifn (
"calculator" ,
"Calculate the output of a given mathematical expression" ,
z . object ( {
expression : z . string ( ) ,
} ) ,
( { expression } ) => {
try {
const result = parser . parse ( expression ) . evaluate ( ) ;
return result ;
} catch ( error ) {
return `Failed to execute script: ${ error . message } ` ;
}
}
) ;이제 chatgpt에게 수학을하도록 요청할 수 있습니다.
User: What's 45^(2.12) / 45?
Assistant: The result of 45^(2.12) / 45 is approximately 71.06.
다음은 RSS 피드에서 최신 뉴스를 가져 오는 기능의 예입니다.
import { z } from "zod" ;
import { aifn } from "ai-fns" ;
const name = "reddit" ;
const description = "Get stories from reddit" ;
const schema = z . object ( {
subreddit : z . string ( ) . optional ( ) . default ( "all" ) . describe ( "Subreddit" ) ,
limit : z . number ( ) . optional ( ) . default ( 5 ) . describe ( "Limit" ) ,
category : z
. enum ( [ "hot" , "new" , "random" , "top" , "rising" , "controversial" ] )
. default ( "hot" )
. describe ( "category" ) ,
} ) ;
const reddit = async ( {
subreddit ,
category ,
limit ,
} : z . infer < typeof schema > ) => {
try {
const params = new URLSearchParams ( {
limit : limit . toString ( ) ,
} ) ;
const url = `https://www.reddit.com/r/ ${ subreddit } / ${ category } .json? ${ params . toString ( ) } ` ;
const res = await fetch ( url ) ;
return await res . json ( ) ;
} catch ( error ) {
console . log ( error ) ;
return error ;
}
} ;
export default aifn ( name , description , schema , reddit ) ; User: What's the top story on /r/programming today?
Assistant: The top story on /r/programming is "Crumb: A New Programming Language Where There are No Keywords, and Everything is a Function". You can read more about it [here](https://github.com/liam-ilan/crumb). It has received 201 upvotes and has 25 comments.
OpenAi의 새로운 기능 호출 기능을 사용하면 Chatgpt 내에서 기능을 호출 할 수 있습니다.
그러나 기능의 입력 및 출력 유형을 포함하는 기능을 위해 JSON 스키마로 전달해야합니다. 이것은 수동으로하는 것이 약간 번거 롭습니다.
이 라이브러리는 자동으로 전환을 처리합니다
함수에 대한 아이디어가 있습니까? 풀 요청을 자유롭게 열십시오!
src/functions 디렉토리에 새 파일을 만들고 기능을 추가하십시오. 기능에 대한 설명과 스키마를 추가하십시오.
import { z } from "zod" ;
import { aifn } from "ai-fns" ;
export const name = "name" ;
export const description = "description" ;
export const schema = z . object ( {
// schema
} ) ;
export const fn = async ( { } : /* schema */ z . infer < typeof schema > ) => {
// function body
} ;
export default aifn ( name , description , schema , fn ) ;