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 ) ;