This project utilizes OpenAI's function calling
feature to build a farm trip agent. The agent can help users to find the farms near a location, get the detailed information of the available activities in the farm, and book the activity.
There is an detailed Medium article about this project. You can find it here.
If you want to know more about the function calling feature, you can refer to the following links:
- Clone the repository
- Run
yarn
to install the dependencies - Create a .env file in the root directory and add the following environment variable(s): OPENAI_API_KEY
- Run
yarn dev
to start the application - Run
yarn test
to run the tests
Please use node version 18 or above.
There is also a script to generate function files from the function description. You can run yarn function:generate
to generate the function files. Make sure you run yarn dev
before running this script.
To introduce a new function, proceed as follows:
- Extend
DescribedFunctionName
with a new enum, such asDoNewThings
.
export enum DescribedFunctionName {
GetFarm = 'get_farm',
...
DoNewThings = 'do_new_things'
}
- Define a Props type for the parameters, e.g.,
DoNewThingsProps
.
export type DoNewThingsProps = {
param_1: PropBase<"number">;
param_2: PropBase<"boolean">;
};
- Insert a new entry in the
functionDescriptionsMap
object, and use theFunctionParametersNarrowed
type to narrow down the type of the parameters.
export const functionDescriptionsMap: Record<DescribedFunctionName, FunctionDefinition> = {
...
[DescribedFunctionName.DoNewThings]: {
name: DescribedFunctionName.DoNewThings,
description: 'Do new things with the parameters.',
parameters: {
type: 'object',
properties: {
param_1: {
type: 'number',
description: 'The first parameter.',
},
param_2: {
type: 'boolean',
description: 'The third parameter.',
},
},
required: ['param_1', 'param_2'],
},
} satisfies FunctionParametersNarrowed<DoNewThingsProps>,
};
- Implement the new function in the function directory, naming it after the enum value.
export async function do_new_things(
args: ConvertedFunctionParamProps<DoNewThingsProps>
): Promise<string> {
const { param_1, param_2 } = args;
return JSON.stringify({
param_1,
param_2,
});
}
name
: The name of the function.description
: A description of the function.parameters
: An object which represents the parameters of the function. It containstype
,description
, andrequired
keys.type
: The type of the parameter(s). It is normallyobject
.properties
: An object that shows all the parameters as its keys. Each key is paired with an object that containstype
anddescription
keys, to describe the parameter.required
: An array of required parameters.
This project is licensed under the MIT License